Skip to content

Commit d546fd6

Browse files
committed
add x and y hoverformat to various cartesian traces
1 parent 623fcd1 commit d546fd6

40 files changed

+260
-35
lines changed

src/components/fx/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,11 +1608,11 @@ function cleanPoint(d, hovermode) {
16081608

16091609
// and convert the x and y label values into formatted text
16101610
if(d.xLabelVal !== undefined) {
1611-
d.xLabel = ('xLabel' in d) ? d.xLabel : Axes.hoverLabelText(d.xa, d.xLabelVal);
1611+
d.xLabel = ('xLabel' in d) ? d.xLabel : Axes.hoverLabelText(d.xa, d.xLabelVal, trace.xhoverformat);
16121612
d.xVal = d.xa.c2d(d.xLabelVal);
16131613
}
16141614
if(d.yLabelVal !== undefined) {
1615-
d.yLabel = ('yLabel' in d) ? d.yLabel : Axes.hoverLabelText(d.ya, d.yLabelVal);
1615+
d.yLabel = ('yLabel' in d) ? d.yLabel : Axes.hoverLabelText(d.ya, d.yLabelVal, trace.yhoverformat);
16161616
d.yVal = d.ya.c2d(d.yLabelVal);
16171617
}
16181618

src/plots/cartesian/axes.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,16 +1356,23 @@ axes.tickText = function(ax, x, hover, noSuffixPrefix) {
13561356
* log axes (where negative values can't be displayed but can appear in hover text)
13571357
*
13581358
* @param {object} ax: the axis to format text for
1359-
* @param {number} val: calcdata value to format
1360-
* @param {Optional(number)} val2: a second value to display
1359+
* @param {number} values: calcdata value(s) to format
1360+
* @param {Optional(string)} hoverformat: trace (x|y)hoverformat to override axis.hoverformat
13611361
*
13621362
* @returns {string} `val` formatted as a string appropriate to this axis, or
1363-
* `val` and `val2` as a range (ie '<val> - <val2>') if `val2` is provided and
1364-
* it's different from `val`.
1363+
* first value and second value as a range (ie '<val1> - <val2>') if the second value is provided and
1364+
* it's different from the first value.
13651365
*/
1366-
axes.hoverLabelText = function(ax, val, val2) {
1367-
if(val2 !== BADNUM && val2 !== val) {
1368-
return axes.hoverLabelText(ax, val) + ' - ' + axes.hoverLabelText(ax, val2);
1366+
axes.hoverLabelText = function(ax, values, hoverformat) {
1367+
if(hoverformat) ax = Lib.extendFlat({}, ax, {hoverformat: hoverformat});
1368+
1369+
var val = Array.isArray(values) ? values[0] : values;
1370+
var val2 = Array.isArray(values) ? values[1] : undefined;
1371+
if(val2 !== undefined && val2 !== val) {
1372+
return (
1373+
axes.hoverLabelText(ax, val, hoverformat) + ' - ' +
1374+
axes.hoverLabelText(ax, val2, hoverformat)
1375+
);
13691376
}
13701377

13711378
var logOffScale = (ax.type === 'log' && val <= 0);

src/plots/hoverformat_attributes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var FORMAT_LINK = require('../constants/docs').FORMAT_LINK;
4+
5+
function axisHoverFormat(axis) {
6+
return {
7+
valType: 'string',
8+
dflt: '',
9+
editType: 'none',
10+
description: [
11+
'Sets the hover text formatting rule on the ' + axis + ' axis using d3 formatting mini-languages',
12+
'which are very similar to those in Python. See:',
13+
FORMAT_LINK
14+
].join(' ')
15+
};
16+
}
17+
18+
module.exports = {
19+
xhoverformat: axisHoverFormat('x'),
20+
yhoverformat: axisHoverFormat('y'),
21+
zhoverformat: axisHoverFormat('z')
22+
};

src/traces/bar/attributes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var scatterAttrs = require('../scatter/attributes');
4+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
45
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
56
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
67
var colorScaleAttrs = require('../../components/colorscale/attributes');
@@ -104,6 +105,8 @@ module.exports = {
104105
yperiod0: scatterAttrs.yperiod0,
105106
xperiodalignment: scatterAttrs.xperiodalignment,
106107
yperiodalignment: scatterAttrs.yperiodalignment,
108+
xhoverformat: hoverformatAttrs.xhoverformat,
109+
yhoverformat: hoverformatAttrs.yhoverformat,
107110

108111
text: scatterAttrs.text,
109112
texttemplate: texttemplateAttrs({editType: 'plot'}, {

src/traces/bar/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
2424
}
2525

2626
handlePeriodDefaults(traceIn, traceOut, layout, coerce);
27+
coerce('xhoverformat');
28+
coerce('yhoverformat');
2729

2830
coerce('orientation', (traceOut.x && !traceOut.y) ? 'h' : 'v');
2931
coerce('base');

src/traces/bar/hover.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ function hoverOnBars(pointData, xval, yval, hovermode) {
170170
var hasPeriod = di.orig_p !== undefined;
171171
pointData[posLetter + 'LabelVal'] = hasPeriod ? di.orig_p : di.p;
172172

173-
pointData.labelLabel = hoverLabelText(pa, pointData[posLetter + 'LabelVal']);
174-
pointData.valueLabel = hoverLabelText(sa, pointData[sizeLetter + 'LabelVal']);
175-
pointData.baseLabel = hoverLabelText(sa, di.b);
173+
pointData.labelLabel = hoverLabelText(pa, pointData[posLetter + 'LabelVal'], trace[posLetter + 'hoverformat']);
174+
pointData.valueLabel = hoverLabelText(sa, pointData[sizeLetter + 'LabelVal'], trace[sizeLetter + 'hoverformat']);
175+
pointData.baseLabel = hoverLabelText(sa, di.b, trace[sizeLetter + 'hoverformat']);
176176

177177
// spikelines always want "closest" distance regardless of hovermode
178178
pointData.spikeDistance = (thisBarSizeFn(di) + thisBarPositionFn(di)) / 2;

src/traces/box/attributes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var scatterAttrs = require('../scatter/attributes');
44
var barAttrs = require('../bar/attributes');
55
var colorAttrs = require('../../components/color/attributes');
6+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
67
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
78
var extendFlat = require('../../lib/extend').extendFlat;
89

@@ -70,6 +71,8 @@ module.exports = {
7071
yperiod0: scatterAttrs.yperiod0,
7172
xperiodalignment: scatterAttrs.xperiodalignment,
7273
yperiodalignment: scatterAttrs.yperiodalignment,
74+
xhoverformat: hoverformatAttrs.xhoverformat,
75+
yhoverformat: hoverformatAttrs.yhoverformat,
7376

7477
name: {
7578
valType: 'string',

src/traces/box/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
1717
if(traceOut.visible === false) return;
1818

1919
handlePeriodDefaults(traceIn, traceOut, layout, coerce);
20+
coerce('xhoverformat');
21+
coerce('yhoverformat');
2022

2123
var hasPreCompStats = traceOut._hasPreCompStats;
2224

src/traces/box/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function hoverOnBoxes(pointData, xval, yval, hovermode) {
166166
pointData2.attr = attr;
167167
pointData2[vLetter + '0'] = pointData2[vLetter + '1'] = valPx;
168168
pointData2[vLetter + 'LabelVal'] = val;
169-
pointData2[vLetter + 'Label'] = (t.labels ? t.labels[attr] + ' ' : '') + Axes.hoverLabelText(vAxis, val);
169+
pointData2[vLetter + 'Label'] = (t.labels ? t.labels[attr] + ' ' : '') + Axes.hoverLabelText(vAxis, val, trace[vLetter + 'hoverformat']);
170170

171171
// Note: introduced to be able to distinguish a
172172
// clicked point from a box during click-to-select

src/traces/candlestick/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var extendFlat = require('../../lib').extendFlat;
4+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
45
var OHLCattrs = require('../ohlc/attributes');
56
var boxAttrs = require('../box/attributes');
67

@@ -21,6 +22,8 @@ module.exports = {
2122
xperiod: OHLCattrs.xperiod,
2223
xperiod0: OHLCattrs.xperiod0,
2324
xperiodalignment: OHLCattrs.xperiodalignment,
25+
xhoverformat: hoverformatAttrs.xhoverformat,
26+
yhoverformat: hoverformatAttrs.yhoverformat,
2427

2528
x: OHLCattrs.x,
2629
open: OHLCattrs.open,
@@ -46,6 +49,7 @@ module.exports = {
4649

4750
text: OHLCattrs.text,
4851
hovertext: OHLCattrs.hovertext,
52+
4953
whiskerwidth: extendFlat({}, boxAttrs.whiskerwidth, { dflt: 0 }),
5054

5155
hoverlabel: OHLCattrs.hoverlabel,

src/traces/candlestick/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
1818
}
1919

2020
handlePeriodDefaults(traceIn, traceOut, layout, coerce, {x: true});
21+
coerce('xhoverformat');
22+
coerce('yhoverformat');
2123

2224
coerce('line.width');
2325

src/traces/contour/attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var heatmapAttrs = require('../heatmap/attributes');
44
var scatterAttrs = require('../scatter/attributes');
5+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
56
var colorScaleAttrs = require('../../components/colorscale/attributes');
67
var dash = require('../../components/drawing/attributes').dash;
78
var fontAttrs = require('../../plots/font_attributes');
@@ -36,7 +37,9 @@ module.exports = extendFlat({
3637
transpose: heatmapAttrs.transpose,
3738
xtype: heatmapAttrs.xtype,
3839
ytype: heatmapAttrs.ytype,
39-
zhoverformat: heatmapAttrs.zhoverformat,
40+
xhoverformat: hoverformatAttrs.xhoverformat,
41+
yhoverformat: hoverformatAttrs.yhoverformat,
42+
zhoverformat: hoverformatAttrs.zhoverformat,
4043
hovertemplate: heatmapAttrs.hovertemplate,
4144
hoverongaps: heatmapAttrs.hoverongaps,
4245
connectgaps: extendFlat({}, heatmapAttrs.connectgaps, {

src/traces/contour/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2626
}
2727

2828
handlePeriodDefaults(traceIn, traceOut, layout, coerce);
29+
coerce('xhoverformat');
30+
coerce('yhoverformat');
2931

3032
coerce('text');
3133
coerce('hovertext');

src/traces/funnel/attributes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var barAttrs = require('../bar/attributes');
44
var lineAttrs = require('../scatter/attributes').line;
55
var baseAttrs = require('../../plots/attributes');
6+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
67
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
78
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
89
var constants = require('./constants');
@@ -23,6 +24,8 @@ module.exports = {
2324
yperiod0: barAttrs.yperiod0,
2425
xperiodalignment: barAttrs.xperiodalignment,
2526
yperiodalignment: barAttrs.yperiodalignment,
27+
xhoverformat: hoverformatAttrs.xhoverformat,
28+
yhoverformat: hoverformatAttrs.yhoverformat,
2629

2730
hovertext: barAttrs.hovertext,
2831
hovertemplate: hovertemplateAttrs({}, {

src/traces/funnel/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
2121
}
2222

2323
handlePeriodDefaults(traceIn, traceOut, layout, coerce);
24+
coerce('xhoverformat');
25+
coerce('yhoverformat');
2426

2527
coerce('orientation', (traceOut.y && !traceOut.x) ? 'v' : 'h');
2628
coerce('offset');

src/traces/heatmap/attributes.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
var scatterAttrs = require('../scatter/attributes');
44
var baseAttrs = require('../../plots/attributes');
5+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
56
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
67
var colorScaleAttrs = require('../../components/colorscale/attributes');
7-
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
88

99
var extendFlat = require('../../lib/extend').extendFlat;
1010

@@ -111,16 +111,10 @@ module.exports = extendFlat({
111111
editType: 'plot',
112112
description: 'Sets the vertical gap (in pixels) between bricks.'
113113
},
114-
zhoverformat: {
115-
valType: 'string',
116-
dflt: '',
117-
editType: 'none',
118-
description: [
119-
'Sets the hover text formatting rule using d3 formatting mini-languages',
120-
'which are very similar to those in Python. See:',
121-
FORMAT_LINK
122-
].join(' ')
123-
},
114+
xhoverformat: hoverformatAttrs.xhoverformat,
115+
yhoverformat: hoverformatAttrs.yhoverformat,
116+
zhoverformat: hoverformatAttrs.zhoverformat,
117+
124118
hovertemplate: hovertemplateAttrs(),
125119
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
126120
}, {

src/traces/heatmap/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2121
}
2222

2323
handlePeriodDefaults(traceIn, traceOut, layout, coerce);
24+
coerce('xhoverformat');
25+
coerce('yhoverformat');
2426

2527
coerce('text');
2628
coerce('hovertext');

src/traces/histogram/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var barAttrs = require('../bar/attributes');
4+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
45
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
56
var makeBinAttrs = require('./bin_attributes');
67
var constants = require('./constants');
@@ -22,6 +23,9 @@ module.exports = {
2223
].join(' ')
2324
},
2425

26+
xhoverformat: hoverformatAttrs.xhoverformat,
27+
yhoverformat: hoverformatAttrs.yhoverformat,
28+
2529
text: extendFlat({}, barAttrs.text, {
2630
description: [
2731
'Sets hover text elements associated with each bar.',

src/traces/histogram/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2424
coerce('text');
2525
coerce('hovertext');
2626
coerce('hovertemplate');
27+
coerce('xhoverformat');
28+
coerce('yhoverformat');
2729

2830
var orientation = coerce('orientation', (y && !x) ? 'h' : 'v');
2931
var sampleLetter = orientation === 'v' ? 'x' : 'y';

src/traces/histogram/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1515
if(!trace.cumulative.enabled) {
1616
var posLetter = trace.orientation === 'h' ? 'y' : 'x';
1717

18-
pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], di.ph0, di.ph1);
18+
pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], [di.ph0, di.ph1], trace[posLetter + 'hoverformat']);
1919
}
2020

2121
return pts;

src/traces/histogram2d/attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var histogramAttrs = require('../histogram/attributes');
44
var makeBinAttrs = require('../histogram/bin_attributes');
55
var heatmapAttrs = require('../heatmap/attributes');
66
var baseAttrs = require('../../plots/attributes');
7+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
78
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
89
var colorScaleAttrs = require('../../components/colorscale/attributes');
910

@@ -64,7 +65,9 @@ module.exports = extendFlat(
6465
xgap: heatmapAttrs.xgap,
6566
ygap: heatmapAttrs.ygap,
6667
zsmooth: heatmapAttrs.zsmooth,
67-
zhoverformat: heatmapAttrs.zhoverformat,
68+
xhoverformat: hoverformatAttrs.xhoverformat,
69+
yhoverformat: hoverformatAttrs.yhoverformat,
70+
zhoverformat: hoverformatAttrs.zhoverformat,
6871
hovertemplate: hovertemplateAttrs({}, {keys: 'z'}),
6972
showlegend: extendFlat({}, baseAttrs.showlegend, {dflt: false})
7073
},

src/traces/histogram2d/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
1919
handleStyleDefaults(traceIn, traceOut, coerce, layout);
2020
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'});
2121
coerce('hovertemplate');
22+
coerce('xhoverformat');
23+
coerce('yhoverformat');
2224
};

src/traces/histogram2d/hover.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, hoverLay
1313
var ny = indices[0];
1414
var nx = indices[1];
1515
var cd0 = pointData.cd[0];
16+
var trace = cd0.trace;
1617
var xRange = cd0.xRanges[nx];
1718
var yRange = cd0.yRanges[ny];
1819

19-
pointData.xLabel = hoverLabelText(pointData.xa, xRange[0], xRange[1]);
20-
pointData.yLabel = hoverLabelText(pointData.ya, yRange[0], yRange[1]);
20+
pointData.xLabel = hoverLabelText(pointData.xa, [xRange[0], xRange[1]], trace.xhoverformat);
21+
pointData.yLabel = hoverLabelText(pointData.ya, [yRange[0], yRange[1]], trace.yhoverformat);
2122

2223
return pts;
2324
};

src/traces/histogram2dcontour/attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var histogram2dAttrs = require('../histogram2d/attributes');
44
var contourAttrs = require('../contour/attributes');
55
var colorScaleAttrs = require('../../components/colorscale/attributes');
6+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
67

78
var extendFlat = require('../../lib/extend').extendFlat;
89

@@ -38,7 +39,9 @@ module.exports = extendFlat({
3839
smoothing: contourAttrs.line.smoothing,
3940
editType: 'plot'
4041
},
41-
zhoverformat: histogram2dAttrs.zhoverformat,
42+
xhoverformat: hoverformatAttrs.xhoverformat,
43+
yhoverformat: hoverformatAttrs.yhoverformat,
44+
zhoverformat: hoverformatAttrs.zhoverformat,
4245
hovertemplate: histogram2dAttrs.hovertemplate
4346
},
4447
colorScaleAttrs('', {

src/traces/histogram2dcontour/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2323
handleContoursDefaults(traceIn, traceOut, coerce, coerce2);
2424
handleStyleDefaults(traceIn, traceOut, coerce, layout);
2525
coerce('hovertemplate');
26+
coerce('xhoverformat');
27+
coerce('yhoverformat');
2628
};

src/traces/ohlc/attributes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var extendFlat = require('../../lib').extendFlat;
44
var scatterAttrs = require('../scatter/attributes');
5+
var hoverformatAttrs = require('../../plots/hoverformat_attributes');
56
var dash = require('../../components/drawing/attributes').dash;
67
var fxAttrs = require('../../components/fx/attributes');
78
var delta = require('../../constants/delta.js');
@@ -28,6 +29,8 @@ module.exports = {
2829
xperiod: scatterAttrs.xperiod,
2930
xperiod0: scatterAttrs.xperiod0,
3031
xperiodalignment: scatterAttrs.xperiodalignment,
32+
xhoverformat: hoverformatAttrs.xhoverformat,
33+
yhoverformat: hoverformatAttrs.yhoverformat,
3134

3235
x: {
3336
valType: 'data_array',

src/traces/ohlc/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
1717
}
1818

1919
handlePeriodDefaults(traceIn, traceOut, layout, coerce, {x: true});
20+
coerce('xhoverformat');
21+
coerce('yhoverformat');
2022

2123
coerce('line.width');
2224
coerce('line.dash');

0 commit comments

Comments
 (0)