Skip to content

Commit bc8294d

Browse files
committed
add Lib.extractOption
- abstracting out calcdata-to-global-trace-fallback - use it in Drawing! - use it in hover label style logic - not super happy with the name - not super happy about it being on Lib
1 parent 85d7061 commit bc8294d

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

src/components/drawing/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,10 @@ drawing.tryColorscale = function(marker, prefix) {
443443
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1};
444444
drawing.textPointStyle = function(s, trace, gd) {
445445
s.each(function(d) {
446-
var p = d3.select(this),
447-
text = d.tx || trace.text;
446+
var p = d3.select(this);
447+
var text = Lib.extractOption(d, trace, 'tx', 'text');
448448

449-
if(!text || Array.isArray(text)) {
450-
// isArray test handles the case of (intentionally) missing
451-
// or empty text within a text array
449+
if(!text) {
452450
p.remove();
453451
return;
454452
}

src/components/fx/hover.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,24 +1028,22 @@ function alignHoverText(hoverLabels, rotateLabels) {
10281028
}
10291029

10301030
function cleanPoint(d, hovermode) {
1031+
var index = d.index;
10311032
var trace = d.trace || {};
10321033
var cd0 = d.cd[0];
1033-
var cd = d.cd[d.index] || {};
1034+
var cd = d.cd[index] || {};
1035+
1036+
var getVal = Array.isArray(index) ?
1037+
function(calcKey, traceKey) {
1038+
return Lib.castOption(cd0, index, calcKey) ||
1039+
Lib.extractOption({}, trace, '', traceKey);
1040+
} :
1041+
function(calcKey, traceKey) {
1042+
return Lib.extractOption(cd, trace, calcKey, traceKey);
1043+
};
10341044

10351045
function fill(key, calcKey, traceKey) {
1036-
var val;
1037-
1038-
if(cd[calcKey]) {
1039-
val = cd[calcKey];
1040-
} else if(cd0[calcKey]) {
1041-
var arr = cd0[calcKey];
1042-
if(Array.isArray(arr) && Array.isArray(arr[d.index[0]])) {
1043-
val = arr[d.index[0]][d.index[1]];
1044-
}
1045-
} else {
1046-
val = Lib.nestedProperty(trace, traceKey).get();
1047-
}
1048-
1046+
var val = getVal(calcKey, traceKey);
10491047
if(val) d[key] = val;
10501048
}
10511049

src/lib/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,27 @@ lib.castOption = function(trace, ptNumber, astr, fn) {
437437
}
438438
};
439439

440+
/** Extract option from calcdata item, correctly falling back to
441+
* trace value if not found.
442+
*
443+
* @param {object} calcPt : calcdata[i][j] item
444+
* @param {object} trace : (full) trace object
445+
* @param {string} calcKey : calcdata key
446+
* @param {string} traceKey : aka trace attribute string
447+
* @return {any}
448+
*/
449+
lib.extractOption = function(calcPt, trace, calcKey, traceKey) {
450+
var calcVal = calcPt[calcKey];
451+
if(calcVal || calcVal === 0) return calcVal;
452+
453+
// fallback to trace value,
454+
// must check if value isn't itself an array
455+
// which means the trace attribute has a corresponding
456+
// calcdata key, but its value is falsy
457+
var traceVal = lib.nestedProperty(trace, traceKey).get();
458+
if(!Array.isArray(traceVal)) return traceVal;
459+
};
460+
440461
/** Returns target as set by 'target' transform attribute
441462
*
442463
* @param {object} trace : full trace object

0 commit comments

Comments
 (0)