Skip to content

Commit 48509f3

Browse files
committed
Add zhoverformat to heatmap and contour traces
1 parent f991039 commit 48509f3

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

src/components/fx/hover.js

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

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

src/traces/contour/attributes.js

Lines changed: 1 addition & 0 deletions
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

src/traces/contour/defaults.js

Lines changed: 4 additions & 0 deletions
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
};

src/traces/heatmap/attributes.js

Lines changed: 11 additions & 0 deletions
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}) },

src/traces/heatmap/defaults.js

Lines changed: 3 additions & 0 deletions
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
};

src/traces/heatmap/hover.js

Lines changed: 16 additions & 0 deletions
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
};

test/jasmine/tests/hover_label_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,43 @@ describe('hover info', function() {
499499
.catch(fail)
500500
.then(done);
501501
});
502+
503+
it('should display correct label content with specified format', function(done) {
504+
var gd = createGraphDiv();
505+
506+
Plotly.plot(gd, [{
507+
type: 'heatmap',
508+
y: [0, 1],
509+
z: [[1.11111, 2.2222, 3.33333], [4.44444, 5.55555, 6.66666]],
510+
name: 'one',
511+
zhoverformat: '.2f'
512+
}, {
513+
type: 'heatmap',
514+
y: [2, 3],
515+
z: [[1, 2, 3], [2, 2, 1]],
516+
name: 'two'
517+
}], {
518+
width: 500,
519+
height: 400,
520+
margin: {l: 0, t: 0, r: 0, b: 0}
521+
})
522+
.then(function() {
523+
_hover(gd, 250, 100);
524+
assertHoverLabelContent({
525+
nums: 'x: 1\ny: 3\nz: 2',
526+
name: 'two'
527+
});
528+
})
529+
.then(function() {
530+
_hover(gd, 250, 300);
531+
assertHoverLabelContent({
532+
nums: 'x: 1\ny: 1\nz: 5.56',
533+
name: 'one'
534+
});
535+
})
536+
.catch(fail)
537+
.then(done);
538+
});
502539
});
503540

504541
describe('hoverformat', function() {

0 commit comments

Comments
 (0)