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,

0 commit comments

Comments
 (0)