Skip to content

Commit 1baa14a

Browse files
yauhen-kavaliouapalchys
authored andcommitted
add 'tickformatstops'
1 parent ff8ecb1 commit 1baa14a

File tree

8 files changed

+84
-3
lines changed

8 files changed

+84
-3
lines changed

src/components/colorbar/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ module.exports = {
163163
tickfont: axesAttrs.tickfont,
164164
tickangle: axesAttrs.tickangle,
165165
tickformat: axesAttrs.tickformat,
166+
tickformatstops: axesAttrs.tickformatstops,
166167
tickprefix: axesAttrs.tickprefix,
167168
showtickprefix: axesAttrs.showtickprefix,
168169
ticksuffix: axesAttrs.ticksuffix,

src/plots/cartesian/axes.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ function tickTextObj(ax, x, text) {
12411241

12421242
function formatDate(ax, out, hover, extraPrecision) {
12431243
var tr = ax._tickround,
1244-
fmt = (hover && ax.hoverformat) || ax.tickformat;
1244+
fmt = (hover && ax.hoverformat) || axes.getTickFormat(ax);
12451245

12461246
if(extraPrecision) {
12471247
// second or sub-second precision: extra always shows max digits.
@@ -1406,7 +1406,7 @@ function numFormat(v, ax, fmtoverride, hover) {
14061406
tickRound = ax._tickround,
14071407
exponentFormat = fmtoverride || ax.exponentformat || 'B',
14081408
exponent = ax._tickexponent,
1409-
tickformat = ax.tickformat,
1409+
tickformat = axes.getTickFormat(ax),
14101410
separatethousands = ax.separatethousands;
14111411

14121412
// special case for hover: set exponent just for this value, and
@@ -1507,6 +1507,59 @@ function numFormat(v, ax, fmtoverride, hover) {
15071507
return v;
15081508
}
15091509

1510+
axes.getTickFormat = function(ax) {
1511+
function convertToMs(dtick) {
1512+
return typeof dtick !== 'string' ? dtick : Number(dtick.replace('M', '') * ONEAVGMONTH);
1513+
}
1514+
function isProperStop(dtick, range, convert) {
1515+
var convertFn = convert || function(x) { return x;};
1516+
var leftDtick = range[0];
1517+
var rightDtick = range[1];
1518+
return (!leftDtick || convertFn(leftDtick) <= convertFn(dtick)) &&
1519+
(!rightDtick || convertFn(rightDtick) >= convertFn(dtick));
1520+
}
1521+
function getRangeWidth(range, convert) {
1522+
var convertFn = convert || function(x) { return x;};
1523+
var left = range[0] || 0;
1524+
var right = range[1] || 0;
1525+
return Math.abs(convertFn(right) - convertFn(left));
1526+
}
1527+
1528+
var tickstop;
1529+
if(ax.tickformatstops && ax.tickformatstops.length > 0) {
1530+
switch(ax.type) {
1531+
case 'date': {
1532+
tickstop = ax.tickformatstops.reduce(function(acc, stop) {
1533+
if(!isProperStop(ax.dtick, stop.dtickrange, convertToMs)) {
1534+
return acc;
1535+
}
1536+
if(!acc) {
1537+
return stop;
1538+
} else {
1539+
return getRangeWidth(stop.dtickrange, convertToMs) > getRangeWidth(acc.dtickrange, convertToMs) ? stop : acc;
1540+
}
1541+
}, null);
1542+
break;
1543+
}
1544+
case 'linear': {
1545+
tickstop = ax.tickformatstops.reduce(function(acc, stop) {
1546+
if(!isProperStop(ax.dtick, stop.dtickrange)) {
1547+
return acc;
1548+
}
1549+
if(!acc) {
1550+
return stop;
1551+
} else {
1552+
return getRangeWidth(stop.dtickrange) > getRangeWidth(acc.dtickrange) ? stop : acc;
1553+
}
1554+
}, null);
1555+
break;
1556+
}
1557+
default:
1558+
}
1559+
}
1560+
return tickstop ? tickstop.value : ax.tickformat;
1561+
};
1562+
15101563
axes.subplotMatch = /^x([0-9]*)y([0-9]*)$/;
15111564

15121565
// getSubplots - extract all combinations of axes we need to make plots for

src/plots/cartesian/layout_attributes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,18 @@ module.exports = {
448448
'*%H~%M~%S.%2f* would display *09~15~23.46*'
449449
].join(' ')
450450
},
451+
tickformatstops: {
452+
valType: 'any',
453+
arrayOk: true,
454+
role: 'style',
455+
description: [
456+
'Set rules for customizing tickformat on different zoom levels for *date* and',
457+
'*linear axis types. You can specify these rules in following way',
458+
'[{dtickrange: [*min*, *max*], value: *format*}]. Where *min*, *max* - dtick values',
459+
'which describe some zoom level, it is possible to omit *min* or *max* value by passing',
460+
'*null*. *format* - string, exactly as *tickformat*'
461+
].join(' ')
462+
},
451463
hoverformat: {
452464
valType: 'string',
453465
dflt: '',

src/plots/cartesian/tick_label_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe
4040

4141
if(axType !== 'category') {
4242
var tickFormat = coerce('tickformat');
43+
coerce('tickformatstops');
4344
if(!tickFormat && axType !== 'date') {
4445
coerce('showexponent', showAttrDflt);
4546
coerce('exponentformat');

src/plots/gl3d/layout/axis_attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = {
100100
exponentformat: axesAttrs.exponentformat,
101101
separatethousands: axesAttrs.separatethousands,
102102
tickformat: axesAttrs.tickformat,
103+
tickformatstops: axesAttrs.tickformatstops,
103104
hoverformat: axesAttrs.hoverformat,
104105
// lines and grids
105106
showline: axesAttrs.showline,

src/plots/ternary/layout/axis_attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
tickfont: axesAttrs.tickfont,
4040
tickangle: axesAttrs.tickangle,
4141
tickformat: axesAttrs.tickformat,
42+
tickformatstops: axesAttrs.tickformatstops,
4243
hoverformat: axesAttrs.hoverformat,
4344
// lines and grids
4445
showline: extendFlat({}, axesAttrs.showline, {dflt: true}),

src/traces/carpet/axis_attributes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ module.exports = {
265265
'*%H~%M~%S.%2f* would display *09~15~23.46*'
266266
].join(' ')
267267
},
268+
tickformatstops: {
269+
valType: 'any',
270+
arrayOk: true,
271+
role: 'style',
272+
description: [
273+
'Set rules for customizing tickformat on different zoom levels for *date* and',
274+
'*linear axis types. You can specify these rules in following way',
275+
'[{dtickrange: [*min*, *max*], value: *format*}]. Where *min*, *max* - dtick values',
276+
'which describe some zoom level, it is possible to omit *min* or *max* value by passing',
277+
'*null*. *format* - string, exactly as *tickformat*'
278+
].join(' ')
279+
},
268280
categoryorder: {
269281
valType: 'enumerated',
270282
values: [

tasks/util/strict_d3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = transformTools.makeRequireTransform('requireTransform',
1818
var pathOut;
1919

2020
if(pathIn === 'd3' && opts.file !== pathToStrictD3Module) {
21-
pathOut = 'require(\'' + pathToStrictD3Module + '\')';
21+
pathOut = 'require(\'' + pathToStrictD3Module.replace(/\\/g, '/') + '\')';
2222
}
2323

2424
if(pathOut) return cb(null, pathOut);

0 commit comments

Comments
 (0)