Skip to content

Add 'hovertext' attribute in scatter* traces #1523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 7, 2017
Merged
1 change: 1 addition & 0 deletions src/traces/bar/arrays_to_calcdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var mergeArray = require('../../lib').mergeArray;
// arrayOk attributes, merge them into calcdata array
module.exports = function arraysToCalcdata(cd, trace) {
mergeArray(trace.text, cd, 'tx');
mergeArray(trace.hovertext, cd, 'htx');

var marker = trace.marker;
if(marker) {
Expand Down
1 change: 1 addition & 0 deletions src/traces/bar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = {
dy: scatterAttrs.dy,

text: scatterAttrs.text,
hovertext: scatterAttrs.hovertext,

textposition: {
valType: 'enumerated',
Expand Down
1 change: 1 addition & 0 deletions src/traces/bar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce('width');

coerce('text');
coerce('hovertext');

var textPosition = coerce('textposition');

Expand Down
5 changes: 4 additions & 1 deletion src/traces/bar/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
pointData.xLabelVal = di.p;
}

if(di.tx) pointData.text = di.tx;
if(di.htx) pointData.text = di.htx;
else if(trace.hovertext) pointData.text = trace.hovertext;
else if(di.tx) pointData.text = di.tx;
else if(trace.text) pointData.text = trace.text;

ErrorBars.hoverInfo(di, trace, pointData);

Expand Down
22 changes: 21 additions & 1 deletion src/traces/pie/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,27 @@ module.exports = {

text: {
valType: 'data_array',
Copy link
Contributor Author

@etpinard etpinard Apr 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcjohnson I just noticed that text for pie is declared as a data_array whereas other trace types have it as a string with arrayOk: true. Was that on purpose? Or should we allow single-value text input for pie traces?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was on purpose - though probably never explicitly discussed. My rationale is that text is essentially the x data for pies, and they would be meaningless without this being an array of data. Can you think of a counterexample?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree for the on-graph textinfo: 'text' part. But if someone wants to show the same text piece in all hover labels? But I guess after this PR, hovertext will cover that case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, that's label... I still don't see a good usage for a single-value text but perhaps it would be better to define it consistently with other trace types anyway.

description: 'Sets text elements associated with each sector.'
description: [
'Sets text elements associated with each sector.',
'If trace `textinfo` contains a *text* flag, these elements will seen',
'on the chart.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertext: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets hover text elements associated with each sector.',
'If a single string, the same string appears for',
'all data points.',
'If an array of string, the items are mapped in order of',
'this trace\'s sectors.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
},

// 'see eg:'
Expand Down
1 change: 1 addition & 0 deletions src/traces/pie/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout

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

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

Expand Down
12 changes: 10 additions & 2 deletions src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ module.exports = function plot(gd, cdpie) {
thisText = [];

if(hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
if(trace2.text && trace2.text[pt.i] && hoverinfo.indexOf('text') !== -1) {
thisText.push(trace2.text[pt.i]);
if(hoverinfo.indexOf('text') !== -1) {
if(trace2.hovertext) {
thisText.push(
Array.isArray(trace2.hovertext) ?
trace2.hovertext[pt.i] :
trace2.hovertext
);
} else if(trace2.text && trace2.text[pt.i]) {
thisText.push(trace2.text[pt.i]);
}
}
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter/arrays_to_calcdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Lib = require('../../lib');
module.exports = function arraysToCalcdata(cd, trace) {

Lib.mergeArray(trace.text, cd, 'tx');
Lib.mergeArray(trace.hovertext, cd, 'htx');
Lib.mergeArray(trace.customdata, cd, 'data');
Lib.mergeArray(trace.textposition, cd, 'tp');
if(trace.textfont) {
Expand Down
18 changes: 17 additions & 1 deletion src/traces/scatter/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,23 @@ module.exports = {
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y) coordinates.'
'this trace\'s (x,y) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertext: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets hover text elements associated with each (x,y) pair.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
},
mode: {
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout

coerce('customdata');
coerce('text');
coerce('hovertext');
coerce('mode', defaultMode);
coerce('ids');

Expand Down
4 changes: 3 additions & 1 deletion src/traces/scatter/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
yLabelVal: di.y
});

if(di.tx) pointData.text = di.tx;
if(di.htx) pointData.text = di.htx;
else if(trace.hovertext) pointData.text = trace.hovertext;
else if(di.tx) pointData.text = di.tx;
Copy link
Contributor Author

@etpinard etpinard Mar 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I came up with:

  • If trace hoverinfo contains a 'text' flag and hovertext is not set the text elements will be seen in the hover labels
  • If trace hoverinfo contains a 'text' flag and hovertext is set, hovertext takes precedence over text i.e. the hoverinfo elements will be seen in the hover labels

else if(trace.text) pointData.text = trace.text;

ErrorBars.hoverInfo(di, trace, pointData);
Expand Down
15 changes: 14 additions & 1 deletion src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,22 @@ module.exports = {
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y,z) coordinates.'
'this trace\'s (x,y,z) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets text elements associated with each (x,y,z) triplet.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y,z) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),

mode: extendFlat({}, scatterAttrs.mode, // shouldn't this be on-par with 2D?
{dflt: 'lines+markers'}),
surfaceaxis: {
Expand Down
10 changes: 7 additions & 3 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ proto.handlePick = function(selection) {
selection.object = this.scatterPlot;
this.scatterPlot.highlight(selection.data);
}
if(this.textLabels && this.textLabels[selection.data.index] !== undefined) {
selection.textLabel = this.textLabels[selection.data.index];
if(this.textLabels) {
if(this.textLabels[selection.data.index] !== undefined) {
selection.textLabel = this.textLabels[selection.data.index];
} else {
selection.textLabel = this.textLabels;
}
}
else selection.textLabel = '';

Expand Down Expand Up @@ -371,7 +375,7 @@ proto.update = function(data) {
opacity: data.opacity
};

this.textLabels = options.text;
this.textLabels = data.hovertext || data.text;

if(this.mode.indexOf('text') !== -1) {
if(this.textMarkers) this.textMarkers.update(textOptions);
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

coerce('text');
coerce('hovertext');
coerce('mode');

if(subTypes.hasLines(traceOut)) {
Expand Down
22 changes: 15 additions & 7 deletions src/traces/scattermapbox/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,35 @@ module.exports = {
// locations
// locationmode

mode: {
valType: 'flaglist',
flags: ['lines', 'markers', 'text'],
mode: extendFlat({}, scatterAttrs.mode, {
dflt: 'markers',
extras: ['none'],
role: 'info',
description: [
'Determines the drawing mode for this scatter trace.',
'If the provided `mode` includes *text* then the `text` elements',
'appear at the coordinates. Otherwise, the `text` elements',
'appear on hover.'
].join(' ')
},
}),

text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (lon,lat) pair',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) coordinates.'
'this trace\'s (lon,lat) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets hover text elements associated with each (lon,lat) pair',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),

Expand Down
8 changes: 7 additions & 1 deletion src/traces/scattermapbox/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = function calc(gd, trace) {
hasColorArray = (hasMarkers && Array.isArray(marker.color)),
hasSizeArray = (hasMarkers && Array.isArray(marker.size)),
hasSymbolArray = (hasMarkers && Array.isArray(marker.symbol)),
hasTextArray = Array.isArray(trace.text);
hasTextArray = Array.isArray(trace.text),
hasHoverTextArray = Array.isArray(trace.hovertext);

calcMarkerColorscale(trace);

Expand Down Expand Up @@ -94,6 +95,11 @@ module.exports = function calc(gd, trace) {
calcPt.tx = (typeof tx === 'string') ? tx : '';
}

if(hasHoverTextArray) {
var htx = trace.hovertext[i];
calcPt.htx = (typeof htx === 'string') ? htx : '';
}

calcTrace.push(calcPt);
}

Expand Down
1 change: 1 addition & 0 deletions src/traces/scattermapbox/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

coerce('text');
coerce('hovertext');
coerce('mode');

if(subTypes.hasLines(traceOut)) {
Expand Down
8 changes: 7 additions & 1 deletion src/traces/scattermapbox/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ function getExtraText(trace, di) {
else if(hasLat) text.push('lat: ' + format(lonlat[1]));

if(isAll || hoverinfo.indexOf('text') !== -1) {
var tx = di.tx || trace.text;
var tx;

if(di.htx) tx = di.htx;
else if(trace.hovertext) tx = trace.hovertext;
else if(di.tx) tx = di.tx;
else if(trace.text) tx = trace.text;

if(!Array.isArray(tx)) text.push(tx);
}

Expand Down
14 changes: 13 additions & 1 deletion src/traces/scatterternary/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,19 @@ module.exports = {
'If a single string, the same string appears over',
'all the data points.',
'If an array of strings, the items are mapped in order to the',
'the data points in (a,b,c).'
'the data points in (a,b,c).',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets hover text elements associated with each (a,b,c) point.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of strings, the items are mapped in order to the',
'the data points in (a,b,c).',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
line: {
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatterternary/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce('sum');

coerce('text');
coerce('hovertext');

var defaultMode = len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines';
coerce('mode', defaultMode);
Expand Down
15 changes: 15 additions & 0 deletions test/image/mocks/text_chart_arrays.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"top left",
"top left"
],
"hovertext": [
"Hover text\nA",
"Hover text\rB",
"Hover text\r\nC"
],
"type": "scatter",
"uid": "459c77"
},
Expand Down Expand Up @@ -83,6 +88,11 @@
"bottom left",
"bottom left"
],
"hovertext": [
"Hover text G",
"Hover text H",
"Hover text I"
],
"type": "scatter",
"uid": "f8361c"
},
Expand All @@ -103,6 +113,11 @@
"a",
"",
"b"
],
"hovertext": [
"a (hover)",
"",
"b (hover)"
]
}
],
Expand Down
Loading