Skip to content

Commit e612205

Browse files
authored
Merge pull request #1523 from plotly/hovertext-on-hover
Add 'hovertext' attribute in scatter* traces
2 parents d3bd87f + 1bf54e2 commit e612205

30 files changed

+449
-72
lines changed

src/traces/bar/arrays_to_calcdata.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var mergeArray = require('../../lib').mergeArray;
1515
// arrayOk attributes, merge them into calcdata array
1616
module.exports = function arraysToCalcdata(cd, trace) {
1717
mergeArray(trace.text, cd, 'tx');
18+
mergeArray(trace.hovertext, cd, 'htx');
1819

1920
var marker = trace.marker;
2021
if(marker) {

src/traces/bar/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = {
4949
dy: scatterAttrs.dy,
5050

5151
text: scatterAttrs.text,
52+
hovertext: scatterAttrs.hovertext,
5253

5354
textposition: {
5455
valType: 'enumerated',

src/traces/bar/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3737
coerce('width');
3838

3939
coerce('text');
40+
coerce('hovertext');
4041

4142
var textPosition = coerce('textposition');
4243

src/traces/bar/hover.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
9999
pointData.xLabelVal = di.p;
100100
}
101101

102-
if(di.tx) pointData.text = di.tx;
102+
if(di.htx) pointData.text = di.htx;
103+
else if(trace.hovertext) pointData.text = trace.hovertext;
104+
else if(di.tx) pointData.text = di.tx;
105+
else if(trace.text) pointData.text = trace.text;
103106

104107
ErrorBars.hoverInfo(di, trace, pointData);
105108

src/traces/pie/attributes.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,27 @@ module.exports = {
7979

8080
text: {
8181
valType: 'data_array',
82-
description: 'Sets text elements associated with each sector.'
82+
description: [
83+
'Sets text elements associated with each sector.',
84+
'If trace `textinfo` contains a *text* flag, these elements will seen',
85+
'on the chart.',
86+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
87+
'these elements will be seen in the hover labels.'
88+
].join(' ')
89+
},
90+
hovertext: {
91+
valType: 'string',
92+
role: 'info',
93+
dflt: '',
94+
arrayOk: true,
95+
description: [
96+
'Sets hover text elements associated with each sector.',
97+
'If a single string, the same string appears for',
98+
'all data points.',
99+
'If an array of string, the items are mapped in order of',
100+
'this trace\'s sectors.',
101+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
102+
].join(' ')
83103
},
84104

85105
// 'see eg:'

src/traces/pie/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4545

4646
var textData = coerce('text');
4747
var textInfo = coerce('textinfo', Array.isArray(textData) ? 'text+percent' : 'percent');
48+
coerce('hovertext');
4849

4950
coerce('hoverinfo', (layout._dataLength === 1) ? 'label+text+value+percent' : undefined);
5051

src/traces/pie/plot.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,16 @@ module.exports = function plot(gd, cdpie) {
110110
thisText = [];
111111

112112
if(hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
113-
if(trace2.text && trace2.text[pt.i] && hoverinfo.indexOf('text') !== -1) {
114-
thisText.push(trace2.text[pt.i]);
113+
if(hoverinfo.indexOf('text') !== -1) {
114+
if(trace2.hovertext) {
115+
thisText.push(
116+
Array.isArray(trace2.hovertext) ?
117+
trace2.hovertext[pt.i] :
118+
trace2.hovertext
119+
);
120+
} else if(trace2.text && trace2.text[pt.i]) {
121+
thisText.push(trace2.text[pt.i]);
122+
}
115123
}
116124
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
117125
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));

src/traces/scatter/arrays_to_calcdata.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var Lib = require('../../lib');
1616
module.exports = function arraysToCalcdata(cd, trace) {
1717

1818
Lib.mergeArray(trace.text, cd, 'tx');
19+
Lib.mergeArray(trace.hovertext, cd, 'htx');
1920
Lib.mergeArray(trace.customdata, cd, 'data');
2021
Lib.mergeArray(trace.textposition, cd, 'tp');
2122
if(trace.textfont) {

src/traces/scatter/attributes.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,23 @@ module.exports = {
8383
'If a single string, the same string appears over',
8484
'all the data points.',
8585
'If an array of string, the items are mapped in order to the',
86-
'this trace\'s (x,y) coordinates.'
86+
'this trace\'s (x,y) coordinates.',
87+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
88+
'these elements will be seen in the hover labels.'
89+
].join(' ')
90+
},
91+
hovertext: {
92+
valType: 'string',
93+
role: 'info',
94+
dflt: '',
95+
arrayOk: true,
96+
description: [
97+
'Sets hover text elements associated with each (x,y) pair.',
98+
'If a single string, the same string appears over',
99+
'all the data points.',
100+
'If an array of string, the items are mapped in order to the',
101+
'this trace\'s (x,y) coordinates.',
102+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
87103
].join(' ')
88104
},
89105
mode: {

src/traces/scatter/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3838

3939
coerce('customdata');
4040
coerce('text');
41+
coerce('hovertext');
4142
coerce('mode', defaultMode);
4243
coerce('ids');
4344

0 commit comments

Comments
 (0)