diff --git a/draftlogs/7723_add.md b/draftlogs/7723_add.md new file mode 100644 index 00000000000..fbf504c14eb --- /dev/null +++ b/draftlogs/7723_add.md @@ -0,0 +1 @@ +- Add support for arrays for the pie property legendrank, so that it can be configured per slice. [[#7723](https://github.com/plotly/plotly.js/pull/7723)] diff --git a/src/components/legend/get_legend_data.js b/src/components/legend/get_legend_data.js index ba32065c802..c080785729d 100644 --- a/src/components/legend/get_legend_data.js +++ b/src/components/legend/get_legend_data.js @@ -100,7 +100,8 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) { // find minimum rank within group var groupMinRank = Infinity; for(j = 0; j < legendData[i].length; j++) { - var rank = legendData[i][j].trace.legendrank; + var legendrank = legendData[i][j].trace.legendrank; + var rank = Array.isArray(legendrank) ? Math.min(legendrank) : legendrank; if(groupMinRank > rank) groupMinRank = rank; } @@ -117,8 +118,10 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) { }; var orderFn2 = function(a, b) { + var a_rank = Array.isArray(a.trace.legendrank) ? a.trace.legendrank[a.i] : a.trace.legendrank; + var b_rank = Array.isArray(b.trace.legendrank) ? b.trace.legendrank[b.i] : b.trace.legendrank; return ( - (a.trace.legendrank - b.trace.legendrank) || + (a_rank - b_rank) || (a._preSort - b._preSort) // fallback for old Chrome < 70 https://bugs.chromium.org/p/v8/issues/detail?id=90 ); }; diff --git a/src/plots/plots.js b/src/plots/plots.js index ffbeb132f28..b3ef101ef33 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -1261,8 +1261,10 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac coerce('legendwidth'); coerce('legendgroup'); coerce('legendgrouptitle.text'); - coerce('legendrank'); - + Lib.coerce(traceIn, traceOut, + _module.attributes.legend ? _module.attributes : plots.attributes, + 'legendrank' + ); traceOut._dfltShowLegend = true; } else { traceOut._dfltShowLegend = false; diff --git a/src/traces/pie/attributes.js b/src/traces/pie/attributes.js index 7816082dce3..eef04a3a468 100644 --- a/src/traces/pie/attributes.js +++ b/src/traces/pie/attributes.js @@ -198,6 +198,20 @@ module.exports = { '`layout.legend`, `layout.legend2`, etc.' ].join(' ') }), + legendrank: extendFlat({}, baseAttrs.legendrank, { + arrayOk: true, + description: [ + 'Sets the legend rank for this pie. If passed as an array, this will set the legend', + 'rank of the individual pie slices.', + 'Items and groups with smaller ranks are presented on top/left side while', + 'with *reversed* `legend.traceorder` they are on bottom/right side.', + 'The default legendrank is 1000,', + 'so that you can use ranks less than 1000 to place certain items before all unranked items,', + 'and ranks greater than 1000 to go after all unranked items.', + 'When having unranked or equal rank items shapes would be displayed after traces', + 'i.e. according to their order in data and layout.' + ].join(' ') + }), title: { text: { valType: 'string', diff --git a/test/image/baselines/zz-pie-slice-legendrank.png b/test/image/baselines/zz-pie-slice-legendrank.png new file mode 100644 index 00000000000..25f58cfc7e8 Binary files /dev/null and b/test/image/baselines/zz-pie-slice-legendrank.png differ diff --git a/test/image/mocks/zz-pie-slice-legendrank.json b/test/image/mocks/zz-pie-slice-legendrank.json new file mode 100644 index 00000000000..02974e748f8 --- /dev/null +++ b/test/image/mocks/zz-pie-slice-legendrank.json @@ -0,0 +1,54 @@ +{ + "data": [ + { + "labels": [ + "slice in legend1: rank 3", + "slice in legend1: rank 2", + "slice in legend1: rank 1", + "slice in legend2: rank 4", + "slice in legend2: rank 3", + "slice in legend2: rank 2", + "slice in legend2: rank 1" + ], + "values": [3, 4, 2, 7, 1, 5, 8], + "type": "pie", + "legendrank": [3, 2, 1, 4, 3, 2, 1], + "legend": ["legend", "legend", "legend", "legend2", "legend2", "legend2", "legend2"], + "domain": { + "y": [0.5, 1] + } + }, + { + "type": "bar", + "x": [0, 1], + "y": [1, 1.5], + "legendrank": 1, + "name": "bar in legend1: rank 1", + "marker": { + "color": "brown" + } + } + ], + "layout": { + "title": { + "text": "legendrank test" + }, + "yaxis": { + "range": [0, 4] + }, + "width": 500, + "height": 400, + "legend": { + "title": { + "text": "legend1" + }, + "y": 1 + }, + "legend2": { + "title": { + "text": "legend2" + }, + "y": 0 + } + } +} diff --git a/test/plot-schema.json b/test/plot-schema.json index 9e6c6299dad..cd0ca2e8798 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -56758,11 +56758,17 @@ } }, "legendrank": { - "description": "Sets the legend rank for this trace. Items and groups with smaller ranks are presented on top/left side while with *reversed* `legend.traceorder` they are on bottom/right side. The default legendrank is 1000, so that you can use ranks less than 1000 to place certain items before all unranked items, and ranks greater than 1000 to go after all unranked items. When having unranked or equal rank items shapes would be displayed after traces i.e. according to their order in data and layout.", + "arrayOk": true, + "description": "Sets the legend rank for this pie. If passed as an array, this will set the legend rank of the individual pie slices. Items and groups with smaller ranks are presented on top/left side while with *reversed* `legend.traceorder` they are on bottom/right side. The default legendrank is 1000, so that you can use ranks less than 1000 to place certain items before all unranked items, and ranks greater than 1000 to go after all unranked items. When having unranked or equal rank items shapes would be displayed after traces i.e. according to their order in data and layout.", "dflt": 1000, "editType": "style", "valType": "number" }, + "legendranksrc": { + "description": "Sets the source reference on Chart Studio Cloud for `legendrank`.", + "editType": "none", + "valType": "string" + }, "legendsrc": { "description": "Sets the source reference on Chart Studio Cloud for `legend`.", "editType": "none",