Skip to content

Commit f958c3b

Browse files
yauhen-kavaliouapalchys
authored andcommitted
change tickformatstops definition and code review fixes
1 parent 1baa14a commit f958c3b

File tree

5 files changed

+97
-28
lines changed

5 files changed

+97
-28
lines changed

src/plots/cartesian/axes.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,8 @@ function formatDate(ax, out, hover, extraPrecision) {
12971297

12981298
function formatLog(ax, out, hover, extraPrecision, hideexp) {
12991299
var dtick = ax.dtick,
1300-
x = out.x;
1300+
x = out.x,
1301+
tickformat = ax.tickformat;
13011302

13021303
if(hideexp === 'never') {
13031304
// If this is a hover label, then we must *never* hide the exponent
@@ -1311,7 +1312,7 @@ function formatLog(ax, out, hover, extraPrecision, hideexp) {
13111312

13121313
if(extraPrecision && ((typeof dtick !== 'string') || dtick.charAt(0) !== 'L')) dtick = 'L3';
13131314

1314-
if(ax.tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
1315+
if(tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
13151316
out.text = numFormat(Math.pow(10, x), ax, hideexp, extraPrecision);
13161317
}
13171318
else if(isNumeric(dtick) || ((dtick.charAt(0) === 'D') && (Lib.mod(x + 0.01, 1) < 0.1))) {
@@ -1515,15 +1516,33 @@ axes.getTickFormat = function(ax) {
15151516
var convertFn = convert || function(x) { return x;};
15161517
var leftDtick = range[0];
15171518
var rightDtick = range[1];
1518-
return (!leftDtick || convertFn(leftDtick) <= convertFn(dtick)) &&
1519-
(!rightDtick || convertFn(rightDtick) >= convertFn(dtick));
1519+
return (leftDtick === null || convertFn(leftDtick) <= convertFn(dtick)) &&
1520+
(rightDtick === null || convertFn(rightDtick) >= convertFn(dtick));
15201521
}
15211522
function getRangeWidth(range, convert) {
15221523
var convertFn = convert || function(x) { return x;};
15231524
var left = range[0] || 0;
15241525
var right = range[1] || 0;
15251526
return Math.abs(convertFn(right) - convertFn(left));
15261527
}
1528+
function compareLogTicks(left, right) {
1529+
var priority = ['L', 'D'];
1530+
if(typeof left === typeof right) {
1531+
if(typeof left === 'number') {
1532+
return left - right;
1533+
} else {
1534+
var leftPriority = priority.indexOf(left.charAt(0));
1535+
var rightPriority = priority.indexOf(right.charAt(0));
1536+
if(leftPriority === rightPriority) {
1537+
return Number(left.replace(/(L|D)/g, '')) - Number(right.replace(/(L|D)/g, ''));
1538+
} else {
1539+
return leftPriority - rightPriority;
1540+
}
1541+
}
1542+
} else {
1543+
return typeof left === 'number' ? 1 : -1;
1544+
}
1545+
}
15271546

15281547
var tickstop;
15291548
if(ax.tickformatstops && ax.tickformatstops.length > 0) {
@@ -1554,6 +1573,17 @@ axes.getTickFormat = function(ax) {
15541573
}, null);
15551574
break;
15561575
}
1576+
case 'log': {
1577+
tickstop = ax.tickformatstops.filter(function(stop) {
1578+
var left = stop.dtickrange[0], right = stop.dtickrange[1];
1579+
var isLeftDtickNull = left === null;
1580+
var isRightDtickNull = right === null;
1581+
var isDtickInRangeLeft = compareLogTicks(ax.dtick, left) >= 0;
1582+
var isDtickInRangeRight = compareLogTicks(ax.dtick, right) <= 0;
1583+
return (isLeftDtickNull || isDtickInRangeLeft) && (isRightDtickNull || isDtickInRangeRight);
1584+
})[0];
1585+
break;
1586+
}
15571587
default:
15581588
}
15591589
}

src/plots/cartesian/layout_attributes.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,24 @@ module.exports = {
449449
].join(' ')
450450
},
451451
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(' ')
452+
_isLinkedToArray: 'tickformatstop',
453+
454+
dtickrange: {
455+
valType: 'data_array',
456+
description: [
457+
'range [*min*, *max*], where *min*, *max* - dtick values',
458+
'which describe some zoom level, it is possible to omit *min*',
459+
'or *max* value by passing *null*'
460+
].join(' ')
461+
},
462+
value: {
463+
valType: 'string',
464+
dflt: '',
465+
role: 'style',
466+
description: [
467+
'string - dtickformat for described zoom level, the same as *tickformat*'
468+
].join(' ')
469+
}
462470
},
463471
hoverformat: {
464472
valType: 'string',

src/plots/cartesian/tick_label_defaults.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13-
13+
var layoutAttributes = require('./layout_attributes');
1414

1515
/**
1616
* options: inherits font, outerTicks, noHover from axes.handleAxisDefaults
@@ -40,7 +40,7 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe
4040

4141
if(axType !== 'category') {
4242
var tickFormat = coerce('tickformat');
43-
coerce('tickformatstops');
43+
tickformatstopsDefaults(containerIn, containerOut);
4444
if(!tickFormat && axType !== 'date') {
4545
coerce('showexponent', showAttrDflt);
4646
coerce('exponentformat');
@@ -81,3 +81,26 @@ function getShowAttrDflt(containerIn) {
8181
return containerIn[showAttrs[0]];
8282
}
8383
}
84+
85+
function tickformatstopsDefaults(tickformatIn, tickformatOut) {
86+
var valuesIn = tickformatIn.tickformatstops || [],
87+
valuesOut = tickformatOut.tickformatstops = [];
88+
89+
var valueIn, valueOut;
90+
91+
function coerce(attr, dflt) {
92+
return Lib.coerce(valueIn, valueOut, layoutAttributes.tickformatstops, attr, dflt);
93+
}
94+
95+
for(var i = 0; i < valuesIn.length; i++) {
96+
valueIn = valuesIn[i];
97+
valueOut = {};
98+
99+
coerce('dtickrange');
100+
coerce('value');
101+
102+
valuesOut.push(valueOut);
103+
}
104+
105+
return valuesOut;
106+
}

src/traces/carpet/axis_attributes.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,24 @@ module.exports = {
266266
].join(' ')
267267
},
268268
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(' ')
269+
_isLinkedToArray: 'tickformatstop',
270+
271+
dtickrange: {
272+
valType: 'data_array',
273+
description: [
274+
'range [*min*, *max*], where *min*, *max* - dtick values',
275+
'which describe some zoom level, it is possible to omit *min*',
276+
'or *max* value by passing *null*'
277+
].join(' ')
278+
},
279+
value: {
280+
valType: 'string',
281+
dflt: '',
282+
role: 'style',
283+
description: [
284+
'string - dtickformat for described zoom level, the same as *tickformat*'
285+
].join(' ')
286+
}
279287
},
280288
categoryorder: {
281289
valType: 'enumerated',

tasks/util/strict_d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var pathToStrictD3Module = path.join(
66
constants.pathToImageTest,
77
'strict-d3.js'
88
);
9-
9+
var normalizedpathToStrictD3Module = pathToStrictD3Module.replace(/\\/g, '/'); // fix npm-sripts for windows users
1010
/**
1111
* Transform `require('d3')` expressions to `require(/path/to/strict-d3.js)`
1212
*/
@@ -18,7 +18,7 @@ module.exports = transformTools.makeRequireTransform('requireTransform',
1818
var pathOut;
1919

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

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

0 commit comments

Comments
 (0)