Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'tickformatstops' #1965

Merged
merged 17 commits into from Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/colorbar/attributes.js
Expand Up @@ -166,6 +166,7 @@ module.exports = overrideAll({
}),
tickangle: axesAttrs.tickangle,
tickformat: axesAttrs.tickformat,
tickformatstops: axesAttrs.tickformatstops,
tickprefix: axesAttrs.tickprefix,
showtickprefix: axesAttrs.showtickprefix,
ticksuffix: axesAttrs.ticksuffix,
Expand Down
79 changes: 75 additions & 4 deletions src/plots/cartesian/axes.js
Expand Up @@ -1232,7 +1232,7 @@ function tickTextObj(ax, x, text) {

function formatDate(ax, out, hover, extraPrecision) {
var tr = ax._tickround,
fmt = (hover && ax.hoverformat) || ax.tickformat;
fmt = (hover && ax.hoverformat) || axes.getTickFormat(ax);

if(extraPrecision) {
// second or sub-second precision: extra always shows max digits.
Expand Down Expand Up @@ -1288,7 +1288,8 @@ function formatDate(ax, out, hover, extraPrecision) {

function formatLog(ax, out, hover, extraPrecision, hideexp) {
var dtick = ax.dtick,
x = out.x;
x = out.x,
tickformat = ax.tickformat;

if(hideexp === 'never') {
// If this is a hover label, then we must *never* hide the exponent
Expand All @@ -1302,7 +1303,7 @@ function formatLog(ax, out, hover, extraPrecision, hideexp) {

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

if(ax.tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
if(tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
out.text = numFormat(Math.pow(10, x), ax, hideexp, extraPrecision);
}
else if(isNumeric(dtick) || ((dtick.charAt(0) === 'D') && (Lib.mod(x + 0.01, 1) < 0.1))) {
Expand Down Expand Up @@ -1397,7 +1398,7 @@ function numFormat(v, ax, fmtoverride, hover) {
tickRound = ax._tickround,
exponentFormat = fmtoverride || ax.exponentformat || 'B',
exponent = ax._tickexponent,
tickformat = ax.tickformat,
tickformat = axes.getTickFormat(ax),
separatethousands = ax.separatethousands;

// special case for hover: set exponent just for this value, and
Expand Down Expand Up @@ -1498,6 +1499,76 @@ function numFormat(v, ax, fmtoverride, hover) {
return v;
}

axes.getTickFormat = function(ax) {
var i;

function convertToMs(dtick) {
return typeof dtick !== 'string' ? dtick : Number(dtick.replace('M', '')) * ONEAVGMONTH;
}

function compareLogTicks(left, right) {
var priority = ['L', 'D'];
if(typeof left === typeof right) {
if(typeof left === 'number') {
return left - right;
} else {
var leftPriority = priority.indexOf(left.charAt(0));
var rightPriority = priority.indexOf(right.charAt(0));
if(leftPriority === rightPriority) {
return Number(left.replace(/(L|D)/g, '')) - Number(right.replace(/(L|D)/g, ''));
} else {
return leftPriority - rightPriority;
}
}
} else {
return typeof left === 'number' ? 1 : -1;
}
}

function isProperStop(dtick, range, convert) {
var convertFn = convert || function(x) { return x;};
var leftDtick = range[0];
var rightDtick = range[1];
return ((!leftDtick && typeof leftDtick !== 'number') || convertFn(leftDtick) <= convertFn(dtick)) &&
((!rightDtick && typeof rightDtick !== 'number') || convertFn(rightDtick) >= convertFn(dtick));
}

function isProperLogStop(dtick, range) {
var isLeftDtickNull = range[0] === null;
var isRightDtickNull = range[1] === null;
var isDtickInRangeLeft = compareLogTicks(dtick, range[0]) >= 0;
var isDtickInRangeRight = compareLogTicks(dtick, range[1]) <= 0;
return (isLeftDtickNull || isDtickInRangeLeft) && (isRightDtickNull || isDtickInRangeRight);
}

var tickstop;
if(ax.tickformatstops && ax.tickformatstops.length > 0) {
switch(ax.type) {
case 'date':
case 'linear': {
for(i = 0; i < ax.tickformatstops.length; i++) {
if(isProperStop(ax.dtick, ax.tickformatstops[i].dtickrange, convertToMs)) {
tickstop = ax.tickformatstops[i];
break;
}
}
break;
}
case 'log': {
for(i = 0; i < ax.tickformatstops.length; i++) {
if(isProperLogStop(ax.dtick, ax.tickformatstops[i].dtickrange)) {
tickstop = ax.tickformatstops[i];
break;
}
}
break;
}
default:
}
}
return tickstop ? tickstop.value : ax.tickformat;
};

axes.subplotMatch = /^x([0-9]*)y([0-9]*)$/;

// getSubplots - extract all combinations of axes we need to make plots for
Expand Down
28 changes: 28 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Expand Up @@ -492,6 +492,34 @@ module.exports = {
'*%H~%M~%S.%2f* would display *09~15~23.46*'
].join(' ')
},
tickformatstops: {
_isLinkedToArray: 'tickformatstop',

dtickrange: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'any', editType: 'ticks'},
{valType: 'any', editType: 'ticks'}
],
editType: 'ticks',
description: [
'range [*min*, *max*], where *min*, *max* - dtick values',
'which describe some zoom level, it is possible to omit *min*',
'or *max* value by passing *null*'
].join(' ')
},
value: {
valType: 'string',
dflt: '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is axis.tickformat: '' a valid format? More precisely, could

tickformatstop: [{
  dtickrange: [/* */],
  value: ''
}]

be used to hide tick labels for some dtick range?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, tickformat: '' gives our default formatting. I suppose we could imagine making our own "hide" tickformat (for date axes you can already hack this with tickformat: ' ' but that doesn't work for numbers) but it seems a bit of a kludgy way to handle a very esoteric edge case.

role: 'style',
editType: 'ticks',
description: [
'string - dtickformat for described zoom level, the same as *tickformat*'
].join(' ')
},
editType: 'ticks'
},
hoverformat: {
valType: 'string',
dflt: '',
Expand Down
26 changes: 25 additions & 1 deletion src/plots/cartesian/tick_label_defaults.js
Expand Up @@ -10,7 +10,7 @@
'use strict';

var Lib = require('../../lib');

var layoutAttributes = require('./layout_attributes');

/**
* options: inherits font, outerTicks, noHover from axes.handleAxisDefaults
Expand Down Expand Up @@ -40,6 +40,7 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe

if(axType !== 'category') {
var tickFormat = coerce('tickformat');
tickformatstopsDefaults(containerIn, containerOut);
if(!tickFormat && axType !== 'date') {
coerce('showexponent', showAttrDflt);
coerce('exponentformat');
Expand Down Expand Up @@ -80,3 +81,26 @@ function getShowAttrDflt(containerIn) {
return containerIn[showAttrs[0]];
}
}

function tickformatstopsDefaults(tickformatIn, tickformatOut) {
var valuesIn = tickformatIn.tickformatstops;
var valuesOut = tickformatOut.tickformatstops = [];

if(!Array.isArray(valuesIn)) return;

var valueIn, valueOut;

function coerce(attr, dflt) {
return Lib.coerce(valueIn, valueOut, layoutAttributes.tickformatstops, attr, dflt);
}

for(var i = 0; i < valuesIn.length; i++) {
valueIn = valuesIn[i];
valueOut = {};

coerce('dtickrange');
coerce('value');

valuesOut.push(valueOut);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. Thanks!

🎉

}
}
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_attributes.js
Expand Up @@ -101,6 +101,7 @@ module.exports = overrideAll({
exponentformat: axesAttrs.exponentformat,
separatethousands: axesAttrs.separatethousands,
tickformat: axesAttrs.tickformat,
tickformatstops: axesAttrs.tickformatstops,
hoverformat: axesAttrs.hoverformat,
// lines and grids
showline: axesAttrs.showline,
Expand Down
1 change: 1 addition & 0 deletions src/plots/ternary/layout/axis_attributes.js
Expand Up @@ -39,6 +39,7 @@ module.exports = {
tickfont: axesAttrs.tickfont,
tickangle: axesAttrs.tickangle,
tickformat: axesAttrs.tickformat,
tickformatstops: axesAttrs.tickformatstops,
hoverformat: axesAttrs.hoverformat,
// lines and grids
showline: extendFlat({}, axesAttrs.showline, {dflt: true}),
Expand Down
3 changes: 3 additions & 0 deletions src/traces/carpet/axis_attributes.js
Expand Up @@ -10,6 +10,8 @@

var fontAttrs = require('../../plots/font_attributes');
var colorAttrs = require('../../components/color/attributes');
var axesAttrs = require('../../plots/cartesian/layout_attributes');
var overrideAll = require('../../plot_api/edit_types').overrideAll;

module.exports = {
color: {
Expand Down Expand Up @@ -290,6 +292,7 @@ module.exports = {
'*%H~%M~%S.%2f* would display *09~15~23.46*'
].join(' ')
},
tickformatstops: overrideAll(axesAttrs.tickformatstops, 'calc', 'from-root'),
categoryorder: {
valType: 'enumerated',
values: [
Expand Down
3 changes: 2 additions & 1 deletion tasks/util/strict_d3.js
Expand Up @@ -6,7 +6,6 @@ var pathToStrictD3Module = path.join(
constants.pathToImageTest,
'strict-d3.js'
);

/**
* Transform `require('d3')` expressions to `require(/path/to/strict-d3.js)`
*/
Expand All @@ -18,6 +17,8 @@ module.exports = transformTools.makeRequireTransform('requireTransform',
var pathOut;

if(pathIn === 'd3' && opts.file !== pathToStrictD3Module) {
// JSON.stringify: fix npm-scripts for windows users, for whom
// path has \ in it, without stringify that turns into control chars.
pathOut = 'require(' + JSON.stringify(pathToStrictD3Module) + ')';
}

Expand Down
Binary file added test/image/baselines/tickformatstops.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions test/image/mocks/tickformatstops.json
@@ -0,0 +1,46 @@
{
"data": [
{
"x": ["2005-01","2005-02","2005-03","2005-04","2005-05","2005-06","2005-07"],
"y": [-20,10,-5,0,5,-10,20]
}
],
"layout": {
"xaxis": {
"tickformatstops": [
{
"dtickrange": [null, 1000],
"value": "%H:%M:%S.%L ms"
},
{
"dtickrange": [1000, 60000],
"value": "%H:%M:%S s"
},
{
"dtickrange": [60000, 3600000],
"value": "%H:%M m"
},
{
"dtickrange": [3600000, 86400000],
"value": "%H:%M h"
},
{
"dtickrange": [86400000, 604800000],
"value": "%e. %b d"
},
{
"dtickrange": [604800000, "M1"],
"value": "%e. %b w"
},
{
"dtickrange": ["M1", "M12"],
"value": "%b '%y M"
},
{
"dtickrange": ["M12", null],
"value": "%Y Y"
}
]
}
}
}