Skip to content

Commit a4e748f

Browse files
authored
Merge pull request #2106 from jdugge/hoverlabel.zformat
Hoverlabel.zformat
2 parents 9bfbabf + 5c0b2f7 commit a4e748f

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

Diff for: src/components/fx/hover.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,11 @@ function cleanPoint(d, hovermode) {
10721072
d.yLabel = ('yLabel' in d) ? d.yLabel : Axes.hoverLabelText(d.ya, d.yLabelVal);
10731073
d.yVal = d.ya.c2d(d.yLabelVal);
10741074
}
1075-
if(d.zLabelVal !== undefined) d.zLabel = String(d.zLabelVal);
1075+
1076+
// Traces like heatmaps generate the zLabel in their hoverPoints function
1077+
if(d.zLabelVal !== undefined && d.zLabel === undefined) {
1078+
d.zLabel = String(d.zLabelVal);
1079+
}
10761080

10771081
// for box means and error bars, add the range to the label
10781082
if(!isNaN(d.xerr) && !(d.xa.type === 'log' && d.xerr <= 0)) {

Diff for: src/traces/contour/attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = extendFlat({
3030
transpose: heatmapAttrs.transpose,
3131
xtype: heatmapAttrs.xtype,
3232
ytype: heatmapAttrs.ytype,
33+
zhoverformat: heatmapAttrs.zhoverformat,
3334

3435
connectgaps: heatmapAttrs.connectgaps,
3536

Diff for: src/traces/contour/defaults.js

+4
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3434

3535
handleContoursDefaults(traceIn, traceOut, coerce);
3636
handleStyleDefaults(traceIn, traceOut, coerce, layout);
37+
38+
coerce('zhoverformat');
39+
// Needed for formatting of hoverlabel if format is not explicitly specified
40+
traceOut._separators = layout.separators;
3741
};

Diff for: src/traces/heatmap/attributes.js

+11
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ module.exports = extendFlat({}, {
100100
editType: 'plot',
101101
description: 'Sets the vertical gap (in pixels) between bricks.'
102102
},
103+
zhoverformat: {
104+
valType: 'string',
105+
dflt: '',
106+
role: 'style',
107+
editType: 'none',
108+
description: [
109+
'Sets the hover text formatting rule using d3 formatting mini-languages',
110+
'which are very similar to those in Python. See:',
111+
'https://github.com/d3/d3-format/blob/master/README.md#locale_format'
112+
].join(' ')
113+
},
103114
},
104115
colorscaleAttrs,
105116
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },

Diff for: src/traces/heatmap/defaults.js

+3
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4040
coerce('connectgaps', hasColumns(traceOut) && (traceOut.zsmooth !== false));
4141

4242
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'});
43+
44+
coerce('zhoverformat');
45+
traceOut._separators = layout.separators; // Needed for formatting of hoverlabel if format is not explicitly specified
4346
};

Diff for: src/traces/heatmap/hover.js

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Fx = require('../../components/fx');
1313
var Lib = require('../../lib');
14+
var Axes = require('../../plots/cartesian/axes');
1415

1516
var MAXDIST = Fx.constants.MAXDIST;
1617

@@ -26,6 +27,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
2627
y = cd0.y,
2728
z = cd0.z,
2829
zmask = cd0.zmask,
30+
range = [trace.zmin, trace.zmax],
31+
zhoverformat = trace.zhoverformat,
32+
_separators = trace._separators,
2933
x2 = x,
3034
y2 = y,
3135
xl,
@@ -99,6 +103,17 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
99103
text = cd0.text[ny][nx];
100104
}
101105

106+
var zLabel;
107+
// dummy axis for formatting the z value
108+
var dummyAx = {
109+
type: 'linear',
110+
range: range,
111+
hoverformat: zhoverformat,
112+
_separators: _separators
113+
};
114+
var zLabelObj = Axes.tickText(dummyAx, zVal, 'hover');
115+
zLabel = zLabelObj.text;
116+
102117
return [Lib.extendFlat(pointData, {
103118
index: [ny, nx],
104119
// never let a 2D override 1D type as closest point
@@ -110,6 +125,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
110125
xLabelVal: xl,
111126
yLabelVal: yl,
112127
zLabelVal: zVal,
128+
zLabel: zLabel,
113129
text: text
114130
})];
115131
};

Diff for: test/jasmine/tests/hover_label_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,43 @@ describe('hover info', function() {
509509
.catch(fail)
510510
.then(done);
511511
});
512+
513+
it('should display correct label content with specified format', function(done) {
514+
var gd = createGraphDiv();
515+
516+
Plotly.plot(gd, [{
517+
type: 'heatmap',
518+
y: [0, 1],
519+
z: [[1.11111, 2.2222, 3.33333], [4.44444, 5.55555, 6.66666]],
520+
name: 'one',
521+
zhoverformat: '.2f'
522+
}, {
523+
type: 'heatmap',
524+
y: [2, 3],
525+
z: [[1, 2, 3], [2, 2, 1]],
526+
name: 'two'
527+
}], {
528+
width: 500,
529+
height: 400,
530+
margin: {l: 0, t: 0, r: 0, b: 0}
531+
})
532+
.then(function() {
533+
_hover(gd, 250, 100);
534+
assertHoverLabelContent({
535+
nums: 'x: 1\ny: 3\nz: 2',
536+
name: 'two'
537+
});
538+
})
539+
.then(function() {
540+
_hover(gd, 250, 300);
541+
assertHoverLabelContent({
542+
nums: 'x: 1\ny: 1\nz: 5.56',
543+
name: 'one'
544+
});
545+
})
546+
.catch(fail)
547+
.then(done);
548+
});
512549
});
513550

514551
describe('hover info for negative data on a log axis', function() {

0 commit comments

Comments
 (0)