Skip to content

Commit 342bd09

Browse files
pie : add Pattern
Patterns were already added to a few trace types in Plotly, like bar & scatter. Hereby a first version to do so for pie charts, cf. Feature request: ability to add pattern fills to pie chart plotly#6134
1 parent a4ce2b7 commit 342bd09

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

src/components/drawing/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ drawing.singlePointStyle = function(d, sel, trace, fns, gd) {
699699
if('mc' in d) {
700700
fillColor = d.mcc = fns.markerScale(d.mc);
701701
} else {
702-
fillColor = marker.color || 'rgba(0,0,0,0)';
702+
fillColor = marker.color || marker.colors || 'rgba(0,0,0,0)';
703703
}
704704

705705
if(fns.selectedColorFn) {
@@ -766,7 +766,7 @@ drawing.singlePointStyle = function(d, sel, trace, fns, gd) {
766766
patternBGColor, patternFGColor, patternFGOpacity
767767
);
768768
} else {
769-
Color.fill(sel, fillColor);
769+
Lib.isArrayOrTypedArray(fillColor) ? Color.fill(sel, fillColor[d.i]) : Color.fill(sel, fillColor);
770770
}
771771

772772
if(lineWidth) {

src/components/legend/style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ module.exports = function style(s, gd, legend) {
512512

513513
var d0Mod = Lib.minExtend(d0, {trace: tMod});
514514

515-
stylePie(pts, d0Mod, tMod);
515+
stylePie(pts, d0Mod, tMod, gd);
516516
}
517517
}
518518

src/traces/pie/attributes.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplat
88
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
99

1010
var extendFlat = require('../../lib/extend').extendFlat;
11+
var pattern = require('../../components/drawing/attributes').pattern;
1112

1213
var textFontAttrs = fontAttrs({
1314
editType: 'plot',
@@ -89,6 +90,7 @@ module.exports = {
8990
},
9091
editType: 'calc'
9192
},
93+
pattern: pattern,
9294
editType: 'calc'
9395
},
9496

src/traces/pie/defaults.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Lib = require('../../lib');
55
var attributes = require('./attributes');
66
var handleDomainDefaults = require('../../plots/domain').defaults;
77
var handleText = require('../bar/defaults').handleText;
8+
var coercePattern = require('../../lib').coercePattern;
89

910
function handleLabelsAndValues(labels, values) {
1011
var hasLabels = Array.isArray(labels);
@@ -64,7 +65,11 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
6465
var lineWidth = coerce('marker.line.width');
6566
if(lineWidth) coerce('marker.line.color');
6667

67-
coerce('marker.colors');
68+
var markerColors = coerce('marker.colors');
69+
coercePattern(coerce, 'marker.pattern', markerColors);
70+
// push the marker colors (with s) to the foreground colors, to work around logic in the drawing pattern code on marker.color (without s, which is okay for a bar trace)
71+
if(!traceOut.marker.pattern.fgcolor) traceOut.marker.pattern.fgcolor = traceIn.marker.colors;
72+
if(!traceOut.marker.pattern.bgcolor) traceOut.marker.pattern.bgcolor = layout.paper_bgcolor;
6873

6974
coerce('scalegroup');
7075
// TODO: hole needs to be coerced to the same value within a scaleegroup

src/traces/pie/style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function style(gd) {
1717
traceSelection.style({opacity: trace.opacity});
1818

1919
traceSelection.selectAll('path.surface').each(function(pt) {
20-
d3.select(this).call(styleOne, pt, trace);
20+
d3.select(this).call(styleOne, pt, trace, gd);
2121
});
2222
});
2323
};

src/traces/pie/style_one.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22

33
var Color = require('../../components/color');
44
var castOption = require('./helpers').castOption;
5+
var Drawing = require('../../components/drawing');
56

6-
module.exports = function styleOne(s, pt, trace) {
7+
module.exports = function styleOne(s, pt, trace, gd) {
78
var line = trace.marker.line;
89
var lineColor = castOption(line.color, pt.pts) || Color.defaultLine;
910
var lineWidth = castOption(line.width, pt.pts) || 0;
1011

12+
// some temporary console statements, for later debugging, needs to be removed once the assembler below gets clean
13+
// console.log('styleOne - s0', s[0], s[0][0], '__data__', s[0][0].__data__);
14+
// console.log( 's0 iiiiiiiii pie : ', (s[0][0].__data__.i !== undefined ? s[0][0].__data__.i : 'nope'));
15+
// console.log( 's0 iiiiiiiii legend : ', (s[0][0].__data__[0] !== undefined ? s[0][0].__data__[0].i : 'nope'));
16+
// console.log( 'pie style_one: s', s, 'trace', trace, 'gd', gd);
17+
18+
// to do: rework this assembler code in a next iteration.
19+
if(s[0][0].__data__.i === undefined) {
20+
// coming from a legend
21+
s[0][0].__data__.i = s[0][0].__data__[0].i;
22+
}
23+
// console.log( 's0 - i : ', s[0][0].__data__['i']);
24+
25+
Drawing.pointStyle(s, trace, gd);
26+
// to do : push into s.style d3 logic
27+
1128
s.style('stroke-width', lineWidth)
12-
.call(Color.fill, pt.color)
29+
// .call(Color.fill, pt.color)
1330
.call(Color.stroke, lineColor);
1431
};

0 commit comments

Comments
 (0)