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

Skip to content

Commit bf9f915

Browse files
committed
Updated colors to handle isDark correctly for label colrs
Added support for custom legend tooltips
1 parent 8ef91ac commit bf9f915

File tree

9 files changed

+40
-5
lines changed

9 files changed

+40
-5
lines changed

src/components/color/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,14 @@ color.contrast = function(cstr, lightAmount, darkAmount) {
7474

7575
if(tc.getAlpha() !== 1) tc = tinycolor(color.combine(cstr, background));
7676

77-
var newColor = tc.isDark() ?
78-
(lightAmount ? tc.lighten(lightAmount) : background) :
79-
(darkAmount ? tc.darken(darkAmount) : defaultLine);
77+
//isDark logic updated to match with Less contrast function as tinycolor rely on brightness instead of luminance
78+
79+
var isDark = (tc.getLuminance() < 0.43);
80+
81+
// changing color to match ION specific contrast color black and white
82+
var newColor = isDark ?
83+
(lightAmount ? tc.lighten(lightAmount) : "#fff") :
84+
(darkAmount ? tc.darken(darkAmount) : "#000");
8085

8186
return newColor.toString();
8287
};

src/components/legend/draw.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,16 @@ function drawTexts(g, gd, opts) {
397397
var maxNameLength = opts._maxNameLength;
398398

399399
var name;
400+
var legendTooltip;
400401
if(!opts.entries) {
401402
name = isPieLike ? legendItem.label : trace.name;
403+
legendTooltip = (isPieLike ? legendItem.labelTooltip : trace.legendtooltip) || name;
402404
if(trace._meta) {
403405
name = Lib.templateString(name, trace._meta);
404406
}
405407
} else {
406408
name = legendItem.text;
409+
legendTooltip = legendItem.labelTooltip || name;
407410
}
408411

409412
var textEl = Lib.ensureSingle(g, 'text', 'legendtext');
@@ -417,7 +420,8 @@ function drawTexts(g, gd, opts) {
417420
.call(Drawing.font, opts.font)
418421
.text(isEditable ? ensureLength(name, maxNameLength) : legendLabel);
419422

420-
Lib.ensureSingle(g, 'title').text(name.replace("<br>", "\n"));
423+
424+
Lib.ensureSingle(g, 'title').text(legendTooltip.replace("<br>", "\n"));
421425

422426
svgTextUtils.positionText(textEl, constants.textGap, 0);
423427

src/components/legend/get_legend_data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ module.exports = function getLegendData(calcdata, opts) {
5252

5353
for(j = 0; j < cd.length; j++) {
5454
var labelj = cd[j].label;
55+
var labeltooltipj = cd[j].labelTooltip;
5556

5657
if(!slicesShown[lgroup][labelj]) {
5758
addOneItem(lgroup, {
5859
label: labelj,
60+
labelTooltip: labeltooltipj,
5961
color: cd[j].color,
6062
i: cd[j].i,
6163
trace: trace,

src/plots/attributes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ module.exports = {
5353
'when toggling legend items.'
5454
].join(' ')
5555
},
56+
legendtooltip: {
57+
valType: 'string',
58+
role: 'info',
59+
dflt: '',
60+
editType: 'style',
61+
description: [
62+
'Sets the legend tooltip to be displayed on hover'
63+
].join(' ')
64+
},
5665
opacity: {
5766
valType: 'number',
5867
role: 'style',

src/plots/plots.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
13321332
);
13331333

13341334
coerce('legendgroup');
1335+
coerce('legendtooltip');
13351336

13361337
traceOut._dfltShowLegend = true;
13371338
} else {

src/traces/pie/attributes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ module.exports = {
3636
'non-empty entry among all occurrences of the label.'
3737
].join(' ')
3838
},
39+
labeltooltips: {
40+
valType: 'data_array',
41+
editType: 'calc',
42+
description: [
43+
'Sets the legend tooltips.',
44+
45+
].join(' ')
46+
},
3947
// equivalent of x0 and dx, if label is missing
4048
label0: {
4149
valType: 'number',

src/traces/pie/calc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function calc(gd, trace) {
2222
var hiddenLabels = fullLayout.hiddenlabels || [];
2323

2424
var labels = trace.labels;
25+
var labelTooltips = trace.labeltooltips;
2526
var colors = trace.marker.colors || [];
2627
var vals = trace.values;
2728
var len = trace._length;
@@ -42,7 +43,7 @@ function calc(gd, trace) {
4243
var isAggregated = false;
4344

4445
for(i = 0; i < len; i++) {
45-
var v, label, hidden;
46+
var v, label, labelTooltip, hidden;
4647
if(hasValues) {
4748
v = vals[i];
4849
if(!isNumeric(v)) continue;
@@ -53,6 +54,8 @@ function calc(gd, trace) {
5354
label = labels[i];
5455
if(label === undefined || label === '') label = i;
5556
label = String(label);
57+
58+
labelTooltip = labelTooltips && labelTooltips[i] || null;
5659

5760
var thisLabelIndex = allThisTraceLabels[label];
5861
if(thisLabelIndex === undefined) {
@@ -65,6 +68,7 @@ function calc(gd, trace) {
6568
cd.push({
6669
v: v,
6770
label: label,
71+
labelTooltip: labelTooltip,
6872
color: pullColor(colors[i], label),
6973
i: i,
7074
pts: [i],

src/traces/pie/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
6262
coerce('label0');
6363
coerce('dlabel');
6464
}
65+
coerce('labeltooltips');
6566

6667
if(!len) {
6768
traceOut.visible = false;

src/traces/pie/plot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ function formatSliceLabel(gd, pt, cd0) {
11271127
return {
11281128
label: pt.label,
11291129
value: pt.v,
1130+
labeltooltip: pt.labeltooltip,
11301131
valueLabel: helpers.formatPieValue(pt.v, fullLayout.separators),
11311132
percent: pt.v / cd0.vTotal,
11321133
percentLabel: helpers.formatPiePercent(pt.v / cd0.vTotal, fullLayout.separators),

0 commit comments

Comments
 (0)