Skip to content

Commit 94f660a

Browse files
committed
finish and test hoverlabel.namelength
- -1 for no constraint - more complete description - complete arrayOk support - tests
1 parent 8a308a8 commit 94f660a

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

src/components/fx/attributes.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ module.exports = {
3535
color: extendFlat({}, fontAttrs.color, { arrayOk: true })
3636
},
3737
namelength: {
38-
valType: 'number',
39-
min: 0,
40-
dflt: 15,
38+
valType: 'integer',
39+
min: -1,
4140
arrayOk: true,
4241
role: 'style',
43-
description: 'Sets the length (in number of characters) of the hover labels for this trace'
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(' ')
4450
}
4551
}
4652
};

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,11 @@ 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(commonLabelOpts.namelength > 0 && name.length > commonLabelOpts.namelength) {
693-
name = name.substr(0, commonLabelOpts.namelength - 3) + '...';
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);
694697
}
695698
}
696699

@@ -1063,6 +1066,7 @@ function cleanPoint(d, hovermode) {
10631066
fill('fontFamily', 'htf', 'hoverlabel.font.family');
10641067
fill('fontSize', 'hts', 'hoverlabel.font.size');
10651068
fill('fontColor', 'htc', 'hoverlabel.font.color');
1069+
fill('nameLength', 'hnl', 'hoverlabel.namelength');
10661070

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

src/components/fx/layout_attributes.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ module.exports = {
5757
color: extendFlat({}, fontAttrs.color)
5858
},
5959
namelength: {
60-
valType: 'number',
61-
min: 0,
60+
valType: 'integer',
61+
min: -1,
6262
dflt: 15,
6363
role: 'style',
64-
description: 'Sets the length (in number of characters) of the hover labels for this trace'
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(' ')
6572
}
6673
}
6774
};

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)