Skip to content

Commit e26f8f4

Browse files
authored
Merge pull request #3847 from plotly/fix3843-display-falsy-zero-number
Display zero number in pie and sunburst text and hover
2 parents d6da48e + 92ffc2b commit e26f8f4

File tree

20 files changed

+190
-74
lines changed

20 files changed

+190
-74
lines changed

src/lib/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,34 @@ lib.pseudoRandom = function() {
11271127
if(Math.abs(randSeed - lastVal) < 429496729) return lib.pseudoRandom();
11281128
return randSeed / 4294967296;
11291129
};
1130+
1131+
1132+
/** Fill hover 'pointData' container with 'correct' hover text value
1133+
*
1134+
* - If trace hoverinfo contains a 'text' flag and hovertext is not set,
1135+
* the text elements will be seen in the hover labels.
1136+
*
1137+
* - If trace hoverinfo contains a 'text' flag and hovertext is set,
1138+
* hovertext takes precedence over text
1139+
* i.e. the hoverinfo elements will be seen in the hover labels
1140+
*
1141+
* @param {object} calcPt
1142+
* @param {object} trace
1143+
* @param {object || array} contOut (mutated here)
1144+
*/
1145+
lib.fillText = function(calcPt, trace, contOut) {
1146+
var fill = Array.isArray(contOut) ?
1147+
function(v) { contOut.push(v); } :
1148+
function(v) { contOut.text = v; };
1149+
1150+
var htx = lib.extractOption(calcPt, trace, 'htx', 'hovertext');
1151+
if(lib.isValidTextValue(htx)) return fill(htx);
1152+
1153+
var tx = lib.extractOption(calcPt, trace, 'tx', 'text');
1154+
if(lib.isValidTextValue(tx)) return fill(tx);
1155+
};
1156+
1157+
// accept all truthy values and 0 (which gets cast to '0' in the hover labels)
1158+
lib.isValidTextValue = function(v) {
1159+
return v || v === 0;
1160+
};

src/plots/gl3d/camera.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/traces/bar/hover.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
var Fx = require('../../components/fx');
1313
var Registry = require('../../registry');
1414
var Color = require('../../components/color');
15-
var fillHoverText = require('../scatter/fill_hover_text');
15+
16+
var fillText = require('../../lib').fillText;
1617

1718
function hoverPoints(pointData, xval, yval, hovermode) {
1819
var barPointData = hoverOnBars(pointData, xval, yval, hovermode);
@@ -155,7 +156,7 @@ function hoverOnBars(pointData, xval, yval, hovermode) {
155156
// in case of bars shifted within groups
156157
pointData[posLetter + 'Spike'] = pa.c2p(di.p, true);
157158

158-
fillHoverText(di, trace, pointData);
159+
fillText(di, trace, pointData);
159160
pointData.hovertemplate = trace.hovertemplate;
160161

161162
return pointData;

src/traces/barpolar/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
var Fx = require('../../components/fx');
1212
var Lib = require('../../lib');
1313
var getTraceColor = require('../bar/hover').getTraceColor;
14-
var fillHoverText = require('../scatter/fill_hover_text');
14+
var fillText = Lib.fillText;
1515
var makeHoverPointText = require('../scatterpolar/hover').makeHoverPointText;
1616
var isPtInsidePolygon = require('../../plots/polar/helpers').isPtInsidePolygon;
1717

@@ -59,7 +59,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
5959
pointData.y0 = pointData.y1 = cdi.ct[1];
6060

6161
var _cdi = Lib.extendFlat({}, cdi, {r: cdi.s, theta: cdi.p});
62-
fillHoverText(cdi, trace, pointData);
62+
fillText(cdi, trace, pointData);
6363
makeHoverPointText(_cdi, trace, subplot, pointData);
6464
pointData.hovertemplate = trace.hovertemplate;
6565
pointData.color = getTraceColor(trace, cdi);

src/traces/box/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Axes = require('../../plots/cartesian/axes');
1212
var Lib = require('../../lib');
1313
var Fx = require('../../components/fx');
1414
var Color = require('../../components/color');
15-
var fillHoverText = require('../scatter/fill_hover_text');
15+
var fillText = Lib.fillText;
1616

1717
function hoverPoints(pointData, xval, yval, hovermode) {
1818
var cd = pointData.cd;
@@ -270,7 +270,7 @@ function hoverOnPoints(pointData, xval, yval) {
270270
var pLetter = pa._id.charAt(0);
271271
closePtData[pLetter + 'Spike'] = pa.c2p(di.pos, true);
272272

273-
fillHoverText(pt, trace, closePtData);
273+
fillText(pt, trace, closePtData);
274274

275275
return closePtData;
276276
}

src/traces/choropleth/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
var Axes = require('../../plots/cartesian/axes');
1313
var attributes = require('./attributes');
14-
var fillHoverText = require('../scatter/fill_hover_text');
14+
var fillText = require('../../lib').fillText;
1515

1616
module.exports = function hoverPoints(pointData, xval, yval) {
1717
var cd = pointData.cd;
@@ -86,7 +86,7 @@ function makeHoverInfo(pointData, trace, pt, axis) {
8686

8787
if(hasZ) text.push(formatter(pt.z));
8888
if(hasText) {
89-
fillHoverText(pt, trace, text);
89+
fillText(pt, trace, text);
9090
}
9191

9292
pointData.extraText = text.join('<br>');

src/traces/ohlc/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Axes = require('../../plots/cartesian/axes');
1212
var Lib = require('../../lib');
1313
var Fx = require('../../components/fx');
1414
var Color = require('../../components/color');
15-
var fillHoverText = require('../scatter/fill_hover_text');
15+
var fillText = require('../../lib').fillText;
1616

1717
var DIRSYMBOL = {
1818
increasing: '▲',
@@ -184,7 +184,7 @@ function hoverOnPoints(pointData, xval, yval, hovermode) {
184184
getLabelLine('low'),
185185
getLabelLine('close') + ' ' + DIRSYMBOL[dir]
186186
] : [];
187-
if(hasText) fillHoverText(di, trace, textParts);
187+
if(hasText) fillText(di, trace, textParts);
188188

189189
// don't make .yLabelVal or .text, since we're managing hoverinfo
190190
// put it all in .extraText

src/traces/pie/calc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var tinycolor = require('tinycolor2');
1414

1515
var Color = require('../../components/color');
1616
var helpers = require('./helpers');
17+
var isValidTextValue = require('../../lib').isValidTextValue;
1718

1819
var pieExtendedColorWays = {};
1920

@@ -99,8 +100,8 @@ function calc(gd, trace) {
99100
pt = cd[i];
100101
thisText = hasLabel ? [pt.label] : [];
101102
if(hasText) {
102-
var texti = helpers.getFirstFilled(trace.text, pt.pts);
103-
if(texti) thisText.push(texti);
103+
var tx = helpers.getFirstFilled(trace.text, pt.pts);
104+
if(isValidTextValue(tx)) thisText.push(tx);
104105
}
105106
if(hasValue) thisText.push(helpers.formatPieValue(pt.v, separators));
106107
if(hasPercent) thisText.push(helpers.formatPiePercent(pt.v / vTotal, separators));

src/traces/pie/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ function attachFxHandlers(sliceTop, gd, cd) {
352352
if(hoverinfo && hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
353353
pt.text = helpers.castOption(trace2.hovertext || trace2.text, pt.pts);
354354
if(hoverinfo && hoverinfo.indexOf('text') !== -1) {
355-
var texti = pt.text;
356-
if(texti) thisText.push(texti);
355+
var tx = pt.text;
356+
if(Lib.isValidTextValue(tx)) thisText.push(tx);
357357
}
358358
pt.value = pt.v;
359359
pt.valueLabel = helpers.formatPieValue(pt.v, separators);

src/traces/scatter/fill_hover_text.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)