Skip to content

Commit b918176

Browse files
committed
Move property out of hoverlabel, use Axis.tickText for formatting, write tests
1 parent acce9bf commit b918176

File tree

10 files changed

+112
-67
lines changed

10 files changed

+112
-67
lines changed

src/components/fx/attributes.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ module.exports = {
5050
'`namelength - 3` characters and add an ellipsis.'
5151
].join(' ')
5252
},
53-
zformat: {
54-
valType: 'string',
55-
dflt: '',
56-
role: 'style',
57-
editType: 'none',
58-
description: [
59-
'Sets the hover text formatting rule using d3 formatting mini-languages',
60-
'which are very similar to those in Python. See:',
61-
'https://github.com/d3/d3-format/blob/master/README.md#locale_format'
62-
].join(' ')
63-
},
6453
editType: 'calc'
6554
}
6655
};

src/components/fx/calc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ module.exports = function calc(gd) {
4242
fillFn(trace.hoverlabel.font.color, cd, 'htc');
4343
fillFn(trace.hoverlabel.font.family, cd, 'htf');
4444
fillFn(trace.hoverlabel.namelength, cd, 'hnl');
45-
fillFn(trace.hoverlabel.zformat, cd, 'hzf');
4645
}
4746
};
4847

src/components/fx/hover.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,6 @@ function cleanPoint(d, hovermode) {
10541054
fill('fontSize', 'hts', 'hoverlabel.font.size');
10551055
fill('fontColor', 'htc', 'hoverlabel.font.color');
10561056
fill('nameLength', 'hnl', 'hoverlabel.namelength');
1057-
fill('zformat', 'hzf', 'hoverlabel.zformat');
10581057

10591058
d.posref = hovermode === 'y' ? (d.x0 + d.x1) / 2 : (d.y0 + d.y1) / 2;
10601059

@@ -1096,12 +1095,8 @@ function cleanPoint(d, hovermode) {
10961095
d.yVal = d.ya.c2d(d.yLabelVal);
10971096
}
10981097

1099-
if(d.zLabelVal !== undefined) {
1100-
if(d.zformat !== undefined) {
1101-
d.zLabel = d3.format(d.zformat)(d.zLabelVal).replace(/-/g, constants.MINUS_SIGN);
1102-
} else {
1103-
d.zLabel = String(d.zLabelVal);
1104-
}
1098+
if(d.zLabelVal !== undefined && d.zLabel === undefined) { // Traces like heatmaps generate the zLabel in their hoverPoints function
1099+
d.zLabel = String(d.zLabelVal);
11051100
}
11061101

11071102
// for box means and error bars, add the range to the label

src/components/fx/hoverlabel_defaults.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ module.exports = function handleHoverLabelDefaults(contIn, contOut, coerce, opts
1616
coerce('hoverlabel.bgcolor', opts.bgcolor);
1717
coerce('hoverlabel.bordercolor', opts.bordercolor);
1818
coerce('hoverlabel.namelength', opts.namelength);
19-
coerce('hoverlabel.zformat', opts.zformat);
2019
Lib.coerceFont(coerce, 'hoverlabel.font', opts.font);
2120
};

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,10 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
2627
y = cd0.y,
2728
z = cd0.z,
2829
zmask = cd0.zmask,
30+
zmin = trace.zmin,
31+
zmax = trace.zmax,
32+
zhoverformat = trace.zhoverformat,
33+
_separators = trace._separators,
2934
x2 = x,
3035
y2 = y,
3136
xl,
@@ -99,6 +104,16 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
99104
text = cd0.text[ny][nx];
100105
}
101106

107+
var zLabel;
108+
var dummyAx = { // dummy axis for formatting the z value
109+
type: 'linear',
110+
range: [zmin, zmax],
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/image/mocks/heatmap_hoverlabel.json

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/jasmine/tests/heatmap_test.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,11 @@ describe('heatmap hover', function() {
616616
return hoverData;
617617
}
618618

619-
function assertLabels(hoverPoint, xLabel, yLabel, zLabel, text) {
620-
expect(hoverPoint.xLabelVal).toEqual(xLabel, 'have correct x label');
621-
expect(hoverPoint.yLabelVal).toEqual(yLabel, 'have correct y label');
622-
expect(hoverPoint.zLabelVal).toEqual(zLabel, 'have correct z label');
619+
function assertLabels(hoverPoint, xLabelVal, yLabelVal, zLabelVal, zLabel, text) {
620+
expect(hoverPoint.xLabelVal).toEqual(xLabelVal, 'have correct x label value');
621+
expect(hoverPoint.yLabelVal).toEqual(yLabelVal, 'have correct y label value');
622+
expect(hoverPoint.zLabelVal).toEqual(zLabelVal, 'have correct z label value');
623+
expect(hoverPoint.zLabel).toEqual(zLabel, 'have correct z label');
623624
expect(hoverPoint.text).toEqual(text, 'have correct text label');
624625
}
625626

@@ -640,14 +641,14 @@ describe('heatmap hover', function() {
640641
var pt = _hover(gd, 0.5, 0.5)[0];
641642

642643
expect(pt.index).toEqual([1, 0], 'have correct index');
643-
assertLabels(pt, 1, 1, 4);
644+
assertLabels(pt, 1, 1, 4, '4');
644645
});
645646

646647
it('should find closest point (case 2) and should', function() {
647648
var pt = _hover(gd, 1.5, 0.5)[0];
648649

649650
expect(pt.index).toEqual([0, 0], 'have correct index');
650-
assertLabels(pt, 2, 0.2, 6);
651+
assertLabels(pt, 2, 0.2, 6, '6');
651652
});
652653
});
653654

@@ -673,13 +674,47 @@ describe('heatmap hover', function() {
673674
var pt = _hover(gd, 0.5, 0.5)[0];
674675

675676
expect(pt.index).toEqual([0, 0], 'have correct index');
676-
assertLabels(pt, 1, 1, 10, 'a');
677+
assertLabels(pt, 1, 1, 10, '10', 'a');
677678

678679
Plotly.relayout(gd, 'xaxis.range', [1, 2]).then(function() {
679680
var pt2 = _hover(gd, 1.5, 0.5)[0];
680681

681682
expect(pt2.index).toEqual([0, 1], 'have correct index');
682-
assertLabels(pt2, 2, 1, 4, 'b');
683+
assertLabels(pt2, 2, 1, 4, '4', 'b');
684+
})
685+
.then(done);
686+
});
687+
688+
});
689+
690+
describe('for hovering with specific number format', function() {
691+
692+
beforeAll(function(done) {
693+
gd = createGraphDiv();
694+
695+
Plotly.plot(gd, [{
696+
type: 'heatmap',
697+
x: [1, 2, 3],
698+
y: [1, 1, 1],
699+
z: [0.123456789, 2.9999, 4],
700+
zhoverformat: '.2f'
701+
}])
702+
.then(done);
703+
});
704+
705+
afterAll(destroyGraphDiv);
706+
707+
it('should find closest point and should', function(done) {
708+
var pt = _hover(gd, 0.5, 0.5)[0];
709+
710+
expect(pt.index).toEqual([0, 0], 'have correct index');
711+
assertLabels(pt, 1, 1, 0.123456789, '0.12');
712+
713+
Plotly.relayout(gd, 'xaxis.range', [1, 2]).then(function() {
714+
var pt2 = _hover(gd, 1.5, 0.5)[0];
715+
716+
expect(pt2.index).toEqual([0, 1], 'have correct index');
717+
assertLabels(pt2, 2, 1, 2.9999, '3.00');
683718
})
684719
.then(done);
685720
});

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)