Skip to content

Commit

Permalink
make extractOption less opinionated
Browse files Browse the repository at this point in the history
- by returning falsy calcdata values
- use fillHoverText in choropleth/hover,
  for consistency
  • Loading branch information
etpinard committed Oct 5, 2017
1 parent 6a44a9a commit 3931273
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,7 @@ lib.castOption = function(trace, ptNumber, astr, fn) {
* @return {any}
*/
lib.extractOption = function(calcPt, trace, calcKey, traceKey) {
var calcVal = calcPt[calcKey];
if(calcVal || calcVal === 0) return calcVal;
if(calcKey in calcPt) return calcPt[calcKey];

// fallback to trace value,
// must check if value isn't itself an array
Expand Down
5 changes: 2 additions & 3 deletions src/traces/choropleth/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

'use strict';

var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var attributes = require('./attributes');
var fillHoverText = require('../scatter/fill_hover_text');

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

if(hasZ) text.push(formatter(pt.z));
if(hasText) {
var tx = Lib.extractOption(pt, trace, 'tx', 'text');
if(tx && tx !== '') text.push(tx);
fillHoverText(pt, trace, text);
}

pointData.extraText = text.join('<br>');
Expand Down
9 changes: 7 additions & 2 deletions src/traces/scatter/fill_hover_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ module.exports = function fillHoverText(calcPt, trace, contOut) {
function(v) { contOut.text = v; };

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

var tx = Lib.extractOption(calcPt, trace, 'tx', 'text');
if(tx && tx !== '') return fill(tx);
if(isValid(tx)) return fill(tx);
};

// accept all truthy values and 0 (which gets cast to '0' in the hover labels)
function isValid(v) {
return v || v === 0;
}

0 comments on commit 3931273

Please sign in to comment.