Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 706a883

Browse files
committed
add option to enable legends for
- choropleth - choroplethmapbox - cone - densitymapbox - heatmap - histogram2d - isosurface - mesh3d - streamtube - surface - volume
1 parent 9174470 commit 706a883

File tree

26 files changed

+173
-28
lines changed

26 files changed

+173
-28
lines changed

src/components/legend/defaults.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
3333
// *would* be shown by default, toward the two traces you need to
3434
// ensure the legend is shown by default, because this can still help
3535
// disambiguate.
36-
if(trace.showlegend || trace._dfltShowLegend) {
36+
if(trace.showlegend || (
37+
trace._dfltShowLegend && !(
38+
trace._module &&
39+
trace._module.attributes &&
40+
trace._module.attributes.showlegend &&
41+
trace._module.attributes.showlegend.dflt === false
42+
)
43+
)) {
3744
legendTraceCount++;
3845
if(trace.showlegend) {
3946
legendReallyHasATrace = true;

src/components/legend/style.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module.exports = function style(s, gd) {
8484
.enter().append('g')
8585
.classed('legendpoints', true);
8686
})
87+
.each(styleSpatial)
8788
.each(styleWaterfalls)
8889
.each(styleFunnels)
8990
.each(styleBars)
@@ -481,6 +482,124 @@ module.exports = function style(s, gd) {
481482
stylePie(pts, d0Mod, tMod);
482483
}
483484
}
485+
486+
function styleSpatial(d) { // i.e. maninly traces having z and colorscale
487+
var trace = d[0].trace;
488+
489+
var useGradient;
490+
var ptsData = [];
491+
if(trace.visible) {
492+
switch(trace.type) {
493+
case 'histogram2d' :
494+
case 'heatmap' :
495+
ptsData = [
496+
['M-15,-2V4H15V-2Z'] // similar to contour
497+
];
498+
useGradient = true;
499+
break;
500+
case 'choropleth' :
501+
case 'choroplethmapbox' :
502+
case 'densitymapbox' :
503+
ptsData = [
504+
['M-6,-6V6H6V-6Z']
505+
];
506+
useGradient = true;
507+
break;
508+
case 'cone' :
509+
ptsData = [
510+
['M-6,2 A2,2 0 0,0 -6,6 V6L6,4Z'],
511+
['M-6,-6 A2,2 0 0,0 -6,-2 L6,-4Z'],
512+
['M-6,-2 A2,2 0 0,0 -6,2 L6,0Z']
513+
];
514+
useGradient = false;
515+
break;
516+
case 'streamtube' :
517+
ptsData = [
518+
['M-6,2 A2,2 0 0,0 -6,6 H6 A2,2 0 0,1 6,2 Z'],
519+
['M-6,-6 A2,2 0 0,0 -6,-2 H6 A2,2 0 0,1 6,-6 Z'],
520+
['M-6,-2 A2,2 0 0,0 -6,2 H6 A2,2 0 0,1 6,-2 Z']
521+
];
522+
useGradient = false;
523+
break;
524+
case 'surface' :
525+
ptsData = [
526+
['M-6,-6 A2,3 0 0,0 -6,0 H6 A2,3 0 0,1 6,-6 Z'],
527+
['M-6,1 A2,3 0 0,1 -6,6 H6 A2,3 0 0,0 6,0 Z']
528+
];
529+
useGradient = true;
530+
break;
531+
case 'mesh3d' :
532+
ptsData = [
533+
['M-6,6H0L-6,-6Z'],
534+
['M6,6H0L6,-6Z'],
535+
['M-6,-6H6L0,6Z']
536+
];
537+
useGradient = false;
538+
break;
539+
case 'volume' :
540+
ptsData = [
541+
['M-6,6H0L-6,-6Z'],
542+
['M6,6H0L6,-6Z'],
543+
['M-6,-6H6L0,6Z']
544+
];
545+
useGradient = true;
546+
break;
547+
case 'isosurface':
548+
ptsData = [
549+
['M-6,6H0L-6,-6Z'],
550+
['M6,6H0L6,-6Z'],
551+
['M-6,-6 A12,24 0 0,0 6,-6 L0,6Z']
552+
];
553+
useGradient = false;
554+
break;
555+
}
556+
}
557+
558+
var pts = d3.select(this).select('g.legendpoints')
559+
.selectAll('path.legend3dandfriends')
560+
.data(ptsData);
561+
pts.enter().append('path').classed('legend3dandfriends', true)
562+
.attr('transform', 'translate(20,0)')
563+
.style('stroke-miterlimit', 1);
564+
pts.exit().remove();
565+
566+
pts.each(function(dd, i) {
567+
var pt = d3.select(this);
568+
569+
var cOpts = extractOpts(trace);
570+
var colorscale = cOpts.colorscale;
571+
var reversescale = cOpts.reversescale;
572+
var fillGradient = function(s) {
573+
if(s.size()) {
574+
var gradientID = 'legendfill-' + trace.uid;
575+
Drawing.gradient(s, gd, gradientID,
576+
reversescale ? 'horizontal' : 'horizontalreversed',
577+
colorscale, 'fill');
578+
}
579+
};
580+
581+
var fillColor;
582+
if(!colorscale) {
583+
var color = trace.vertexcolor || trace.facecolor || trace.color;
584+
fillColor = Lib.isArrayOrTypedArray(color) ? (color[i] || color[0]) : color;
585+
} else {
586+
if(!useGradient) {
587+
var len = colorscale.length;
588+
fillColor =
589+
i === 0 ? colorscale[reversescale ? len - 1 : 0][1] : // minimum
590+
i === 1 ? colorscale[reversescale ? 0 : len - 1][1] : // maximum
591+
colorscale[Math.floor((len - 1) / 2)][1]; // middle
592+
}
593+
}
594+
595+
pt.attr('d', dd[0]);
596+
if(fillColor) {
597+
pt.call(Color.fill, fillColor);
598+
} else {
599+
pt.call(fillGradient);
600+
}
601+
});
602+
}
484603
};
485604

486605
function getGradientDirection(reversescale) {

src/plots/plots.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,9 +1291,16 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
12911291
coerce('meta');
12921292

12931293
if(Registry.traceIs(traceOut, 'showLegend')) {
1294-
traceOut._dfltShowLegend = true;
1295-
coerce('showlegend');
1294+
Lib.coerce(traceIn, traceOut,
1295+
_module.attributes.showlegend ?
1296+
_module.attributes :
1297+
plots.attributes,
1298+
'showlegend'
1299+
);
1300+
12961301
coerce('legendgroup');
1302+
1303+
traceOut._dfltShowLegend = true;
12971304
} else {
12981305
traceOut._dfltShowLegend = false;
12991306
}

src/traces/choropleth/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ module.exports = extendFlat({
7777
editType: 'calc',
7878
flags: ['location', 'z', 'text', 'name']
7979
}),
80-
hovertemplate: hovertemplateAttrs()
80+
hovertemplate: hovertemplateAttrs(),
81+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
8182
},
8283

8384
colorScaleAttrs('', {

src/traces/choropleth/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
moduleType: 'trace',
2424
name: 'choropleth',
2525
basePlotModule: require('../../plots/geo'),
26-
categories: ['geo', 'noOpacity'],
26+
categories: ['geo', 'noOpacity', 'showLegend'],
2727
meta: {
2828
description: [
2929
'The data that describes the choropleth value-to-color mapping',

src/traces/choroplethmapbox/attributes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
var choroplethAttrs = require('../choropleth/attributes');
1212
var colorScaleAttrs = require('../../components/colorscale/attributes');
1313
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
14-
14+
var baseAttrs = require('../../plots/attributes');
1515
var extendFlat = require('../../lib/extend').extendFlat;
1616

1717
module.exports = extendFlat({
@@ -103,7 +103,8 @@ module.exports = extendFlat({
103103
},
104104

105105
hoverinfo: choroplethAttrs.hoverinfo,
106-
hovertemplate: hovertemplateAttrs({}, {keys: ['properties']})
106+
hovertemplate: hovertemplateAttrs({}, {keys: ['properties']}),
107+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
107108
},
108109

109110
colorScaleAttrs('', {

src/traces/choroplethmapbox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {
5252
moduleType: 'trace',
5353
name: 'choroplethmapbox',
5454
basePlotModule: require('../../plots/mapbox'),
55-
categories: ['mapbox', 'gl', 'noOpacity'],
55+
categories: ['mapbox', 'gl', 'noOpacity', 'showLegend'],
5656
meta: {
5757
hr_name: 'choropleth_mapbox',
5858
description: [

src/traces/cone/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ var attrs = {
166166
editType: 'calc',
167167
description: 'Same as `text`.'
168168
},
169-
hovertemplate: hovertemplateAttrs({editType: 'calc'}, {keys: ['norm']})
169+
170+
hovertemplate: hovertemplateAttrs({editType: 'calc'}, {keys: ['norm']}),
171+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
170172
};
171173

172174
extendFlat(attrs, colorScaleAttrs('', {

src/traces/cone/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
moduleType: 'trace',
1313
name: 'cone',
1414
basePlotModule: require('../../plots/gl3d'),
15-
categories: ['gl3d'],
15+
categories: ['gl3d', 'showLegend'],
1616

1717
attributes: require('./attributes'),
1818
supplyDefaults: require('./defaults'),

src/traces/densitymapbox/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ module.exports = extendFlat({
8484
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
8585
flags: ['lon', 'lat', 'z', 'text', 'name']
8686
}),
87-
hovertemplate: hovertemplateAttrs()
87+
hovertemplate: hovertemplateAttrs(),
88+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
8889
},
8990
colorScaleAttrs('', {
9091
cLetter: 'z',

src/traces/densitymapbox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = {
3737
moduleType: 'trace',
3838
name: 'densitymapbox',
3939
basePlotModule: require('../../plots/mapbox'),
40-
categories: ['mapbox', 'gl'],
40+
categories: ['mapbox', 'gl', 'showLegend'],
4141
meta: {
4242
hr_name: 'density_mapbox',
4343
description: [

src/traces/heatmap/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var scatterAttrs = require('../scatter/attributes');
12+
var baseAttrs = require('../../plots/attributes');
1213
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
1314
var colorScaleAttrs = require('../../components/colorscale/attributes');
1415
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
@@ -130,7 +131,8 @@ module.exports = extendFlat({
130131
FORMAT_LINK
131132
].join(' ')
132133
},
133-
hovertemplate: hovertemplateAttrs()
134+
hovertemplate: hovertemplateAttrs(),
135+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
134136
}, {
135137
transforms: undefined
136138
},

src/traces/heatmap/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
moduleType: 'trace',
2121
name: 'heatmap',
2222
basePlotModule: require('../../plots/cartesian'),
23-
categories: ['cartesian', 'svg', '2dMap'],
23+
categories: ['cartesian', 'svg', '2dMap', 'showLegend'],
2424
meta: {
2525
description: [
2626
'The data that describes the heatmap value-to-color mapping',

src/traces/histogram2d/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var histogramAttrs = require('../histogram/attributes');
1212
var makeBinAttrs = require('../histogram/bin_attributes');
1313
var heatmapAttrs = require('../heatmap/attributes');
14+
var baseAttrs = require('../../plots/attributes');
1415
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
1516
var colorScaleAttrs = require('../../components/colorscale/attributes');
1617

@@ -72,7 +73,8 @@ module.exports = extendFlat(
7273
ygap: heatmapAttrs.ygap,
7374
zsmooth: heatmapAttrs.zsmooth,
7475
zhoverformat: heatmapAttrs.zhoverformat,
75-
hovertemplate: hovertemplateAttrs({}, {keys: 'z'})
76+
hovertemplate: hovertemplateAttrs({}, {keys: 'z'}),
77+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
7678
},
7779
colorScaleAttrs('', {cLetter: 'z', autoColorDflt: false})
7880
);

src/traces/histogram2d/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
'use strict';
1010

1111
module.exports = {
12-
1312
attributes: require('./attributes'),
1413
supplyDefaults: require('./defaults'),
1514
crossTraceDefaults: require('../histogram/cross_trace_defaults'),
@@ -24,7 +23,7 @@ module.exports = {
2423
moduleType: 'trace',
2524
name: 'histogram2d',
2625
basePlotModule: require('../../plots/cartesian'),
27-
categories: ['cartesian', 'svg', '2dMap', 'histogram'],
26+
categories: ['cartesian', 'svg', '2dMap', 'histogram', 'showLegend'],
2827
meta: {
2928
hrName: 'histogram_2d',
3029
description: [

src/traces/isosurface/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ var attrs = module.exports = overrideAll(extendFlat({
232232
arrayOk: true,
233233
description: 'Same as `text`.'
234234
},
235-
hovertemplate: hovertemplateAttrs()
235+
hovertemplate: hovertemplateAttrs(),
236+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
236237
},
237238

238239
colorScaleAttrs('', {

src/traces/isosurface/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
moduleType: 'trace',
2222
name: 'isosurface',
2323
basePlotModule: require('../../plots/gl3d'),
24-
categories: ['gl3d'],
24+
categories: ['gl3d', 'showLegend'],
2525
meta: {
2626
description: [
2727
'Draws isosurfaces between iso-min and iso-max values with coordinates given by',

src/traces/mesh3d/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,6 @@ colorScaleAttrs('', {
238238
editType: 'calc'
239239
}, surfaceAttrs.lighting),
240240

241-
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'})
241+
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'}),
242+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
242243
});

src/traces/mesh3d/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
moduleType: 'trace',
2222
name: 'mesh3d',
2323
basePlotModule: require('../../plots/gl3d'),
24-
categories: ['gl3d'],
24+
categories: ['gl3d', 'showLegend'],
2525
meta: {
2626
description: [
2727
'Draws sets of triangles with coordinates given by',

src/traces/streamtube/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ var attrs = {
144144
'tubeu', 'tubev', 'tubew',
145145
'norm', 'divergence'
146146
]
147-
})
147+
}),
148+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
148149
};
149150

150151
extendFlat(attrs, colorScaleAttrs('', {

src/traces/streamtube/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
moduleType: 'trace',
1313
name: 'streamtube',
1414
basePlotModule: require('../../plots/gl3d'),
15-
categories: ['gl3d'],
15+
categories: ['gl3d', 'showLegend'],
1616

1717
attributes: require('./attributes'),
1818
supplyDefaults: require('./defaults'),

src/traces/surface/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ colorScaleAttrs('', {
310310
})
311311
},
312312

313-
hoverinfo: extendFlat({}, baseAttrs.hoverinfo)
313+
hoverinfo: extendFlat({}, baseAttrs.hoverinfo),
314+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false}),
314315
}), 'calc', 'nested');
315316

316317
attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';

src/traces/surface/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
moduleType: 'trace',
2222
name: 'surface',
2323
basePlotModule: require('../../plots/gl3d'),
24-
categories: ['gl3d', '2dMap'],
24+
categories: ['gl3d', '2dMap', 'showLegend'],
2525
meta: {
2626
description: [
2727
'The data the describes the coordinates of the surface is set in `z`.',

src/traces/volume/attributes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ colorScaleAttrs('', {
8686
flatshading: isosurfaceAttrs.flatshading,
8787
contour: isosurfaceAttrs.contour,
8888

89-
hoverinfo: extendFlat({}, baseAttrs.hoverinfo)
89+
hoverinfo: extendFlat({}, baseAttrs.hoverinfo),
90+
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
9091
}), 'calc', 'nested');
9192

9293
attrs.x.editType = attrs.y.editType = attrs.z.editType = attrs.value.editType = 'calc+clearAxisTypes';

0 commit comments

Comments
 (0)