Skip to content

Commit 984a691

Browse files
authored
Merge pull request #6351 from plotly/treemap-round-corners
Add `marker.cornerradius` to `treemap`
2 parents da4e322 + a814408 commit 984a691

File tree

10 files changed

+99
-19
lines changed

10 files changed

+99
-19
lines changed

draftlogs/6351_add.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add `marker.cornerradius` attribute to `treemap` trace [[#6351](https://github.com/plotly/plotly.js/pull/6351)]

src/traces/icicle/attributes.js

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ module.exports = {
6161

6262
line: sunburstAttrs.marker.line,
6363

64+
cornerradius: treemapAttrs.marker.cornerradius,
65+
6466
editType: 'calc'
6567
},
6668
colorScaleAttrs('marker', {

src/traces/icicle/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6262
var lineWidth = coerce('marker.line.width');
6363
if(lineWidth) coerce('marker.line.color', layout.paper_bgcolor);
6464

65+
coerce('marker.cornerradius');
66+
6567
coerce('marker.colors');
6668
var withColorscale = traceOut._hasColorscale = (
6769
hasColorscale(traceIn, 'marker', 'colors') ||

src/traces/treemap/attributes.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,17 @@ module.exports = {
141141

142142
line: sunburstAttrs.marker.line,
143143

144-
editType: 'calc'
144+
cornerradius: {
145+
valType: 'number',
146+
min: 0,
147+
dflt: 0,
148+
editType: 'plot',
149+
description: [
150+
'Sets the maximum rounding of corners (in px).'
151+
].join(' ')
152+
},
153+
154+
editType: 'calc',
145155
},
146156
colorScaleAttrs('marker', {
147157
colorAttr: 'colors',

src/traces/treemap/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
8585
coerce('marker.pad.r', headerSize / 4);
8686
coerce('marker.pad.b', bottomText ? headerSize : headerSize / 4);
8787

88+
coerce('marker.cornerradius');
89+
8890
traceOut._hovered = {
8991
marker: {
9092
line: {

src/traces/treemap/plot_one.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ module.exports = function plotOne(gd, cd, element, transitionOpts, drawDescendan
206206
);
207207
};
208208

209+
// Note that `pad` is just an integer for `icicle`` traces where
210+
// `pad` is a hashmap for treemap: pad.t, pad.b, pad.l, and pad.r
211+
var pad = trace[isIcicle ? 'tiling' : 'marker'].pad;
212+
213+
var hasFlag = function(f) { return trace.textposition.indexOf(f) !== -1; };
214+
215+
var hasTop = hasFlag('top');
216+
var hasLeft = hasFlag('left');
217+
var hasRight = hasFlag('right');
218+
var hasBottom = hasFlag('bottom');
219+
209220
// slice path generation fn
210221
var pathDescendant = function(d) {
211222
var _x0 = viewMapX(d.x0);
@@ -217,12 +228,19 @@ module.exports = function plotOne(gd, cd, element, transitionOpts, drawDescendan
217228
var dy = _y1 - _y0;
218229
if(!dx || !dy) return '';
219230

220-
var FILLET = 0; // TODO: may expose this constant
221-
222-
var r = (
223-
dx > 2 * FILLET &&
224-
dy > 2 * FILLET
225-
) ? FILLET : 0;
231+
var cornerradius = trace.marker.cornerradius || 0;
232+
var r = Math.min(cornerradius, dx / 2, dy / 2);
233+
if(
234+
r &&
235+
d.data &&
236+
d.data.data &&
237+
d.data.data.label
238+
) {
239+
if(hasTop) r = Math.min(r, pad.t);
240+
if(hasLeft) r = Math.min(r, pad.l);
241+
if(hasRight) r = Math.min(r, pad.r);
242+
if(hasBottom) r = Math.min(r, pad.b);
243+
}
226244

227245
var arc = function(rx, ry) { return r ? 'a' + pos(r, r) + ' 0 0 1 ' + pos(rx, ry) : ''; };
228246

@@ -245,25 +263,19 @@ module.exports = function plotOne(gd, cd, element, transitionOpts, drawDescendan
245263
var y1 = pt.y1;
246264
var textBB = pt.textBB;
247265

248-
var hasFlag = function(f) { return trace.textposition.indexOf(f) !== -1; };
249-
250-
var hasBottom = hasFlag('bottom');
251-
var hasTop = hasFlag('top') || (opts.isHeader && !hasBottom);
266+
var _hasTop = hasTop || (opts.isHeader && !hasBottom);
252267

253268
var anchor =
254-
hasTop ? 'start' :
269+
_hasTop ? 'start' :
255270
hasBottom ? 'end' : 'middle';
256271

257-
var hasRight = hasFlag('right');
258-
var hasLeft = hasFlag('left') || opts.onPathbar;
272+
var _hasRight = hasFlag('right');
273+
var _hasLeft = hasFlag('left') || opts.onPathbar;
259274

260275
var leftToRight =
261-
hasLeft ? -1 :
262-
hasRight ? 1 : 0;
276+
_hasLeft ? -1 :
277+
_hasRight ? 1 : 0;
263278

264-
// Note that `pad` is just an integer for `icicle`` traces where
265-
// `pad` is a hashmap for treemap: pad.t, pad.b, pad.l, and pad.r
266-
var pad = trace[isIcicle ? 'tiling' : 'marker'].pad;
267279
if(opts.isHeader) {
268280
x0 += (isIcicle ? pad : pad.l) - TEXTPAD;
269281
x1 -= (isIcicle ? pad : pad.r) - TEXTPAD;
Loading

test/image/mocks/treemap_packages_colorscale_novalue.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"line": {
1010
"color": "#777"
1111
},
12+
"cornerradius": 4,
1213
"colorscale": [[0, "#FFF"], [0.01, "#FF0"], [0.1, "#F00"], [1, "#000"]],
1314
"showscale": true
1415
},

test/jasmine/tests/treemap_test.js

+36
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,42 @@ describe('Test treemap restyle:', function() {
12941294
.then(_assert('back to dflt', ['Root', 'B', 'A\nnode1', 'b\nnode3']))
12951295
.then(done, done.fail);
12961296
});
1297+
1298+
it('should be able to restyle *marker.cornerradius*', function(done) {
1299+
var mock = {
1300+
data: [{
1301+
type: 'treemap',
1302+
labels: ['Root', 'A', 'B', 'b'],
1303+
parents: ['', 'Root', 'Root', 'B'],
1304+
text: ['node0', 'node1', 'node2', 'node3'],
1305+
values: [0, 1, 2, 3],
1306+
pathbar: { visible: false }
1307+
}]
1308+
};
1309+
1310+
function _assert(msg, exp) {
1311+
return function() {
1312+
var layer = d3Select(gd).select('.treemaplayer');
1313+
layer.selectAll('.surface').each(function() {
1314+
var surfaces = d3Select(this);
1315+
var path = surfaces[0][0].getAttribute('d');
1316+
expect(path.indexOf('a') !== -1).toEqual(exp);
1317+
});
1318+
};
1319+
}
1320+
1321+
Plotly.newPlot(gd, mock)
1322+
.then(_assert('no arcs', false))
1323+
.then(function() {
1324+
return Plotly.restyle(gd, 'marker.cornerradius', 10);
1325+
})
1326+
.then(_assert('has arcs', true))
1327+
.then(function() {
1328+
return Plotly.restyle(gd, 'marker.cornerradius', 0);
1329+
})
1330+
.then(_assert('no arcs', false))
1331+
.then(done, done.fail);
1332+
});
12971333
});
12981334

12991335
describe('Test treemap tweening:', function() {

test/plot-schema.json

+14
Original file line numberDiff line numberDiff line change
@@ -34112,6 +34112,13 @@
3411234112
"editType": "none",
3411334113
"valType": "string"
3411434114
},
34115+
"cornerradius": {
34116+
"description": "Sets the maximum rounding of corners (in px).",
34117+
"dflt": 0,
34118+
"editType": "plot",
34119+
"min": 0,
34120+
"valType": "number"
34121+
},
3411534122
"editType": "calc",
3411634123
"line": {
3411734124
"color": {
@@ -69171,6 +69178,13 @@
6917169178
"editType": "none",
6917269179
"valType": "string"
6917369180
},
69181+
"cornerradius": {
69182+
"description": "Sets the maximum rounding of corners (in px).",
69183+
"dflt": 0,
69184+
"editType": "plot",
69185+
"min": 0,
69186+
"valType": "number"
69187+
},
6917469188
"depthfade": {
6917569189
"description": "Determines if the sector colors are faded towards the background from the leaves up to the headers. This option is unavailable when a `colorscale` is present, defaults to false when `marker.colors` is set, but otherwise defaults to true. When set to *reversed*, the fading direction is inverted, that is the top elements within hierarchy are drawn with fully saturated colors while the leaves are faded towards the background color.",
6917669190
"editType": "style",

0 commit comments

Comments
 (0)