Skip to content

Commit afa0cd6

Browse files
add pattern to sunburst
1st version, setting the attributes & defaults in the extended schema, and rework styleOne, in case a marker point is set
1 parent 384e492 commit afa0cd6

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

src/traces/sunburst/attributes.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var domainAttrs = require('../../plots/domain').attributes;
99
var pieAttrs = require('../pie/attributes');
1010
var constants = require('./constants');
1111
var extendFlat = require('../../lib/extend').extendFlat;
12+
var pattern = require('../../components/drawing/attributes').pattern;
1213

1314
module.exports = {
1415
labels: {
@@ -113,6 +114,7 @@ module.exports = {
113114
width: extendFlat({}, pieAttrs.marker.line.width, {dflt: 1}),
114115
editType: 'calc'
115116
},
117+
pattern: pattern,
116118
editType: 'calc'
117119
},
118120
colorScaleAttrs('marker', {

src/traces/sunburst/defaults.js

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

89
var Colorscale = require('../../components/colorscale');
910
var hasColorscale = Colorscale.hasColorscale;
@@ -35,7 +36,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3536
var lineWidth = coerce('marker.line.width');
3637
if(lineWidth) coerce('marker.line.color', layout.paper_bgcolor);
3738

38-
coerce('marker.colors');
39+
var markerColors = coerce('marker.colors');
40+
coercePattern(coerce, 'marker.pattern', markerColors);
41+
// 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)
42+
if(traceIn.marker && !traceOut.marker.pattern.fgcolor) traceOut.marker.pattern.fgcolor = traceIn.marker.colors;
43+
if(!traceOut.marker.pattern.bgcolor) traceOut.marker.pattern.bgcolor = layout.paper_bgcolor;
44+
3945
var withColorscale = traceOut._hasColorscale = (
4046
hasColorscale(traceIn, 'marker', 'colors') ||
4147
(traceIn.marker || {}).coloraxis // N.B. special logic to consider "values" colorscales

src/traces/sunburst/style.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var d3 = require('@plotly/d3');
44
var Color = require('../../components/color');
55
var Lib = require('../../lib');
66
var resizeText = require('../bar/uniform_text').resizeText;
7+
var Drawing = require('../../components/drawing');
78

89
function style(gd) {
910
var s = gd._fullLayout._sunburstlayer.selectAll('.trace');
@@ -17,20 +18,34 @@ function style(gd) {
1718
gTrace.style('opacity', trace.opacity);
1819

1920
gTrace.selectAll('path.surface').each(function(pt) {
20-
d3.select(this).call(styleOne, pt, trace);
21+
d3.select(this).call(styleOne, pt, trace, gd);
2122
});
2223
});
2324
}
2425

25-
function styleOne(s, pt, trace) {
26+
function styleOne(s, pt, trace, gd) {
2627
var cdi = pt.data.data;
2728
var isLeaf = !pt.children;
2829
var ptNumber = cdi.i;
2930
var lineColor = Lib.castOption(trace, ptNumber, 'marker.line.color') || Color.defaultLine;
3031
var lineWidth = Lib.castOption(trace, ptNumber, 'marker.line.width') || 0;
3132

33+
if(gd && ptNumber >= 0) {
34+
pt.i = cdi.i;
35+
36+
var marker = trace.marker;
37+
if(marker.pattern) {
38+
if(!marker.colors || !marker.pattern.shape) marker.color = cdi.color;
39+
} else {
40+
marker.color = cdi.color;
41+
}
42+
43+
Drawing.pointStyle(s, trace, gd, pt);
44+
} else {
45+
Color.fill(s, cdi.color);
46+
}
47+
3248
s.style('stroke-width', lineWidth)
33-
.call(Color.fill, cdi.color)
3449
.call(Color.stroke, lineColor)
3550
.style('opacity', isLeaf ? trace.leaf.opacity : null);
3651
}

test/plot-schema.json

+93
Original file line numberDiff line numberDiff line change
@@ -67493,6 +67493,99 @@
6749367493
"valType": "string"
6749467494
}
6749567495
},
67496+
"pattern": {
67497+
"bgcolor": {
67498+
"arrayOk": true,
67499+
"description": "When there is no colorscale sets the color of background pattern fill. Defaults to a `marker.color` background when `fillmode` is *overlay*. Otherwise, defaults to a transparent background.",
67500+
"editType": "style",
67501+
"valType": "color"
67502+
},
67503+
"bgcolorsrc": {
67504+
"description": "Sets the source reference on Chart Studio Cloud for `bgcolor`.",
67505+
"editType": "none",
67506+
"valType": "string"
67507+
},
67508+
"description": "Sets the pattern within the marker.",
67509+
"editType": "style",
67510+
"fgcolor": {
67511+
"arrayOk": true,
67512+
"description": "When there is no colorscale sets the color of foreground pattern fill. Defaults to a `marker.color` background when `fillmode` is *replace*. Otherwise, defaults to dark grey or white to increase contrast with the `bgcolor`.",
67513+
"editType": "style",
67514+
"valType": "color"
67515+
},
67516+
"fgcolorsrc": {
67517+
"description": "Sets the source reference on Chart Studio Cloud for `fgcolor`.",
67518+
"editType": "none",
67519+
"valType": "string"
67520+
},
67521+
"fgopacity": {
67522+
"description": "Sets the opacity of the foreground pattern fill. Defaults to a 0.5 when `fillmode` is *overlay*. Otherwise, defaults to 1.",
67523+
"editType": "style",
67524+
"max": 1,
67525+
"min": 0,
67526+
"valType": "number"
67527+
},
67528+
"fillmode": {
67529+
"description": "Determines whether `marker.color` should be used as a default to `bgcolor` or a `fgcolor`.",
67530+
"dflt": "replace",
67531+
"editType": "style",
67532+
"valType": "enumerated",
67533+
"values": [
67534+
"replace",
67535+
"overlay"
67536+
]
67537+
},
67538+
"role": "object",
67539+
"shape": {
67540+
"arrayOk": true,
67541+
"description": "Sets the shape of the pattern fill. By default, no pattern is used for filling the area.",
67542+
"dflt": "",
67543+
"editType": "style",
67544+
"valType": "enumerated",
67545+
"values": [
67546+
"",
67547+
"/",
67548+
"\\",
67549+
"x",
67550+
"-",
67551+
"|",
67552+
"+",
67553+
"."
67554+
]
67555+
},
67556+
"shapesrc": {
67557+
"description": "Sets the source reference on Chart Studio Cloud for `shape`.",
67558+
"editType": "none",
67559+
"valType": "string"
67560+
},
67561+
"size": {
67562+
"arrayOk": true,
67563+
"description": "Sets the size of unit squares of the pattern fill in pixels, which corresponds to the interval of repetition of the pattern.",
67564+
"dflt": 8,
67565+
"editType": "style",
67566+
"min": 0,
67567+
"valType": "number"
67568+
},
67569+
"sizesrc": {
67570+
"description": "Sets the source reference on Chart Studio Cloud for `size`.",
67571+
"editType": "none",
67572+
"valType": "string"
67573+
},
67574+
"solidity": {
67575+
"arrayOk": true,
67576+
"description": "Sets the solidity of the pattern fill. Solidity is roughly the fraction of the area filled by the pattern. Solidity of 0 shows only the background color without pattern and solidty of 1 shows only the foreground color without pattern.",
67577+
"dflt": 0.3,
67578+
"editType": "style",
67579+
"max": 1,
67580+
"min": 0,
67581+
"valType": "number"
67582+
},
67583+
"soliditysrc": {
67584+
"description": "Sets the source reference on Chart Studio Cloud for `solidity`.",
67585+
"editType": "none",
67586+
"valType": "string"
67587+
}
67588+
},
6749667589
"reversescale": {
6749767590
"description": "Reverses the color mapping if true. Has an effect only if colors is set to a numerical array. If true, `marker.cmin` will correspond to the last color in the array and `marker.cmax` will correspond to the first color.",
6749867591
"dflt": false,

0 commit comments

Comments
 (0)