Skip to content

Commit bc5726c

Browse files
authored
Merge pull request #1822 from ckiss/hover-label-namelength
Hover label namelength
2 parents 9ab907f + e937519 commit bc5726c

File tree

8 files changed

+52
-8
lines changed

8 files changed

+52
-8
lines changed

src/components/fx/attributes.js

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ module.exports = {
3333
family: extendFlat({}, fontAttrs.family, { arrayOk: true }),
3434
size: extendFlat({}, fontAttrs.size, { arrayOk: true }),
3535
color: extendFlat({}, fontAttrs.color, { arrayOk: true })
36+
},
37+
namelength: {
38+
valType: 'integer',
39+
min: -1,
40+
arrayOk: true,
41+
role: 'style',
42+
description: [
43+
'Sets the length (in number of characters) of the trace name in',
44+
'the hover labels for this trace. -1 shows the whole name',
45+
'regardless of length. 0-3 shows the first 0-3 characters, and',
46+
'an integer >3 will show the whole name if it is less than that',
47+
'many characters, but if it is longer, will truncate to',
48+
'`namelength - 3` characters and add an ellipsis.'
49+
].join(' ')
3650
}
3751
}
3852
};

src/components/fx/calc.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports = function calc(gd) {
4141
fillFn(trace.hoverlabel.font.size, cd, 'hts');
4242
fillFn(trace.hoverlabel.font.color, cd, 'htc');
4343
fillFn(trace.hoverlabel.font.family, cd, 'htf');
44+
fillFn(trace.hoverlabel.namelength, cd, 'hnl');
4445
}
4546
};
4647

src/components/fx/hover.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,12 @@ function createHoverText(hoverData, opts, gd) {
689689
// strip out our pseudo-html elements from d.name (if it exists at all)
690690
name = svgTextUtils.plainText(d.name || '');
691691

692-
if(name.length > 15) name = name.substr(0, 12) + '...';
692+
var nameLength = Math.round(d.nameLength);
693+
694+
if(nameLength > -1 && name.length > nameLength) {
695+
if(nameLength > 3) name = name.substr(0, nameLength - 3) + '...';
696+
else name = name.substr(0, nameLength);
697+
}
693698
}
694699

695700
// used by other modules (initially just ternary) that
@@ -1061,6 +1066,7 @@ function cleanPoint(d, hovermode) {
10611066
fill('fontFamily', 'htf', 'hoverlabel.font.family');
10621067
fill('fontSize', 'hts', 'hoverlabel.font.size');
10631068
fill('fontColor', 'htc', 'hoverlabel.font.color');
1069+
fill('nameLength', 'hnl', 'hoverlabel.namelength');
10641070

10651071
d.posref = hovermode === 'y' ? (d.x0 + d.x1) / 2 : (d.y0 + d.y1) / 2;
10661072

src/components/fx/hoverlabel_defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = function handleHoverLabelDefaults(contIn, contOut, coerce, opts
1515

1616
coerce('hoverlabel.bgcolor', opts.bgcolor);
1717
coerce('hoverlabel.bordercolor', opts.bordercolor);
18+
coerce('hoverlabel.namelength', opts.namelength);
1819
Lib.coerceFont(coerce, 'hoverlabel.font', opts.font);
1920
};

src/components/fx/layout_attributes.js

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ module.exports = {
5555
dflt: constants.HOVERFONTSIZE
5656
}),
5757
color: extendFlat({}, fontAttrs.color)
58+
},
59+
namelength: {
60+
valType: 'integer',
61+
min: -1,
62+
dflt: 15,
63+
role: 'style',
64+
description: [
65+
'Sets the default length (in number of characters) of the trace name in',
66+
'the hover labels for all traces. -1 shows the whole name',
67+
'regardless of length. 0-3 shows the first 0-3 characters, and',
68+
'an integer >3 will show the whole name if it is less than that',
69+
'many characters, but if it is longer, will truncate to',
70+
'`namelength - 3` characters and add an ellipsis.'
71+
].join(' ')
5872
}
5973
}
6074
};

src/lib/coerce.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ exports.valObjects = {
9494
'are coerced to the `dflt`.'
9595
].join(' '),
9696
requiredOpts: [],
97-
otherOpts: ['dflt', 'min', 'max'],
97+
otherOpts: ['dflt', 'min', 'max', 'arrayOk'],
9898
coerceFunction: function(v, propOut, dflt, opts) {
9999
if(v % 1 || !isNumeric(v) ||
100100
(opts.min !== undefined && v < opts.min) ||

test/jasmine/tests/fx_test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ describe('Fx defaults', function() {
148148
family: 'Roboto',
149149
size: 40,
150150
color: 'pink'
151-
}
151+
},
152+
namelength: 15
152153
});
153154

154155
expect(out.data[1].hoverlabel).toEqual({
@@ -158,7 +159,8 @@ describe('Fx defaults', function() {
158159
family: 'Roboto',
159160
size: 20,
160161
color: 'red'
161-
}
162+
},
163+
namelength: 15
162164
});
163165

164166
expect(out.layout.annotations[0].hoverlabel).toEqual({

test/jasmine/tests/hover_label_test.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,13 @@ describe('hover info on stacked subplots', function() {
683683
});
684684

685685
describe('hover info on stacked subplots with shared y-axis', function() {
686-
var mock = require('@mocks/stacked_subplots_shared_yaxis.json');
686+
var mock = Lib.extendDeep(require('@mocks/stacked_subplots_shared_yaxis.json'));
687+
mock.data[0].name = 'Who put the bomp in the bomp bah bomp bah bomp';
688+
mock.data[0].hoverlabel = {namelength: -1};
689+
mock.data[1].name = 'Who put the ram in the rama lama ding dong';
690+
mock.data[1].hoverlabel = {namelength: [2, 4]};
691+
mock.data[2].name = 'Who put the bop in the bop shoo bop shoo bop';
692+
mock.layout.hoverlabel = {namelength: 10};
687693

688694
beforeEach(function(done) {
689695
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
@@ -727,11 +733,11 @@ describe('hover info on stacked subplots', function() {
727733
expect(d3.selectAll('g.hovertext').size()).toEqual(3);
728734
var textNodes = d3.selectAll('g.hovertext').selectAll('text');
729735

730-
expect(textNodes[0][0].innerHTML).toEqual('trace 0');
736+
expect(textNodes[0][0].innerHTML).toEqual('Who put the bomp in the bomp bah bomp bah bomp');
731737
expect(textNodes[0][1].innerHTML).toEqual('1');
732-
expect(textNodes[1][0].innerHTML).toEqual('trace 1');
738+
expect(textNodes[1][0].innerHTML).toEqual('Wh');
733739
expect(textNodes[1][1].innerHTML).toEqual('2.1');
734-
expect(textNodes[2][0].innerHTML).toEqual('trace 2');
740+
expect(textNodes[2][0].innerHTML).toEqual('Who put...');
735741
expect(textNodes[2][1].innerHTML).toEqual('3');
736742
});
737743
});

0 commit comments

Comments
 (0)