Skip to content

Commit 3931273

Browse files
committed
make extractOption less opinionated
- by returning falsy calcdata values - use fillHoverText in choropleth/hover, for consistency
1 parent 6a44a9a commit 3931273

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/lib/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ lib.castOption = function(trace, ptNumber, astr, fn) {
447447
* @return {any}
448448
*/
449449
lib.extractOption = function(calcPt, trace, calcKey, traceKey) {
450-
var calcVal = calcPt[calcKey];
451-
if(calcVal || calcVal === 0) return calcVal;
450+
if(calcKey in calcPt) return calcPt[calcKey];
452451

453452
// fallback to trace value,
454453
// must check if value isn't itself an array

src/traces/choropleth/hover.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
'use strict';
1111

12-
var Lib = require('../../lib');
1312
var Axes = require('../../plots/cartesian/axes');
1413
var attributes = require('./attributes');
14+
var fillHoverText = require('../scatter/fill_hover_text');
1515

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

8282
if(hasZ) text.push(formatter(pt.z));
8383
if(hasText) {
84-
var tx = Lib.extractOption(pt, trace, 'tx', 'text');
85-
if(tx && tx !== '') text.push(tx);
84+
fillHoverText(pt, trace, text);
8685
}
8786

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

src/traces/scatter/fill_hover_text.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ module.exports = function fillHoverText(calcPt, trace, contOut) {
2929
function(v) { contOut.text = v; };
3030

3131
var htx = Lib.extractOption(calcPt, trace, 'htx', 'hovertext');
32-
if(htx && htx !== '') return fill(htx);
32+
if(isValid(htx)) return fill(htx);
3333

3434
var tx = Lib.extractOption(calcPt, trace, 'tx', 'text');
35-
if(tx && tx !== '') return fill(tx);
35+
if(isValid(tx)) return fill(tx);
3636
};
37+
38+
// accept all truthy values and 0 (which gets cast to '0' in the hover labels)
39+
function isValid(v) {
40+
return v || v === 0;
41+
}

0 commit comments

Comments
 (0)