Skip to content

Improve axis tick increment #5114

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

Merged
merged 10 commits into from
Sep 2, 2020
40 changes: 40 additions & 0 deletions src/lib/increment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2012-2020, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

module.exports = function incrementNumeric(x, delta) {
if(!delta) return x;

// Note 1:
// 0.3 != 0.1 + 0.2 == 0.30000000000000004
// but 0.3 == (10 * 0.1 + 10 * 0.2) / 10
// Attempt to use integer steps to increment
var scale = 1 / Math.abs(delta);
var newX = (scale > 1) ? (
scale * x +
scale * delta
) / scale : x + delta;

// Note 2:
// now we may also consider rounding to cover few more edge cases
// e.g. 0.3 * 3 = 0.8999999999999999
var lenX1 = String(newX).length;
if(lenX1 > 16) {
var lenDt = String(delta).length;
var lenX0 = String(x).length;

if(lenX1 >= lenX0 + lenDt) { // likely a rounding error!
var s = parseFloat(newX).toPrecision(12);
if(s.indexOf('e+') === -1) newX = +s;
}
}

return newX;
};
2 changes: 2 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ lib.filterUnique = require('./filter_unique');
lib.filterVisible = require('./filter_visible');
lib.pushUnique = require('./push_unique');

lib.increment = require('./increment');

lib.cleanNumber = require('./clean_number');

lib.ensureNumber = function ensureNumber(v) {
Expand Down
2 changes: 1 addition & 1 deletion src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ axes.tickIncrement = function(x, dtick, axrev, calendar) {
var axSign = axrev ? -1 : 1;

// includes linear, all dates smaller than month, and pure 10^n in log
if(isNumeric(dtick)) return x + axSign * dtick;
if(isNumeric(dtick)) return Lib.increment(x, axSign * dtick);

// everything else is a string, one character plus a number
var tType = dtick.charAt(0);
Expand Down
Binary file added test/image/baselines/tick-increment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/tick-percent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions test/image/compare_pixels_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ if(allMock || argv.filter) {

var FLAKY_LIST = [
'treemap_coffee',
'treemap_textposition',
'treemap_sunburst_marker_colors',
'trace_metatext',
'gl3d_directions-streamtube1',
'gl3d_traces-with-opacity'
'treemap_with-without_values',
];

console.log('');
Expand Down
136 changes: 136 additions & 0 deletions test/image/mocks/tick-increment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"data": [
{
"name": "s & positive",
"x": [
"2019-12-31 23:59:59.998",
"2020-01-01 00:00:00",
"2020-01-01 00:00:00.002"
],
"y": [
"1e-1",
"1e-2",
"1e-0"
],
"type": "scatter"
},
{
"name": "s & negative",
"xaxis": "x2",
"yaxis": "y2",
"x": [
"2019-12-31 23:59:59.998",
"2020-01-01 00:00:00",
"2020-01-01 00:00:00.002"
],
"y": [
"-1e-1",
"-1e-2",
"-1e-0"
],
"type": "scatter"
},
{
"name": "p & negative",
"xaxis": "x3",
"yaxis": "y3",
"x": [
"2019-12-31 23:59:59.998",
"2020-01-01 00:00:00",
"2020-01-01 00:00:00.002"
],
"y": [
"-1e-1",
"-1e-2",
"-1e-0"
],
"type": "scatter"
},
{
"name": "p & positive",
"xaxis": "x4",
"yaxis": "y4",
"x": [
"2019-12-31 23:59:59.998",
"2020-01-01 00:00:00",
"2020-01-01 00:00:00.002"
],
"y": [
"1e-1",
"1e-2",
"1e-0"
],
"type": "scatter"
}
],
"layout": {
"width": 800,
"height": 800,
"xaxis": {
"type": "date",
"domain": [
0,
0.45
]
},
"xaxis2": {
"type": "date",
"anchor": "y2",
"domain": [
0.6,
1
]
},
"xaxis3": {
"type": "date",
"anchor": "y3",
"domain": [
0,
0.45
]
},
"xaxis4": {
"type": "date",
"anchor": "y4",
"domain": [
0.6,
1
]
},
"yaxis": {
"nticks": 10,
"tickformat": "s",
"domain": [
0,
0.45
]
},
"yaxis2": {
"nticks": 10,
"tickformat": "s",
"anchor": "x2",
"domain": [
0,
0.45
]
},
"yaxis3": {
"nticks": 10,
"tickformat": "p",
"anchor": "x3",
"domain": [
0.6,
1
]
},
"yaxis4": {
"nticks": 10,
"tickformat": "p",
"anchor": "x4",
"domain": [
0.6,
1
]
}
}
}
108 changes: 108 additions & 0 deletions test/image/mocks/tick-percent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"data": [
{
"type": "scatter",
"y": [
1,
0,
0.5
]
},
{
"xaxis": "x2",
"yaxis": "y2",
"type": "scatter",
"y": [
1,
0,
0.5
]
},
{
"xaxis": "x3",
"yaxis": "y3",
"type": "scatter",
"y": [
1,
0,
0.5
]
},
{
"xaxis": "x4",
"yaxis": "y4",
"type": "scatter",
"y": [
1,
0,
0.5
]
}
],
"layout": {
"width": 800,
"height": 800,
"xaxis": {
"domain": [
0,
0.48
]
},
"xaxis2": {
"anchor": "y2",
"domain": [
0.52,
1
]
},
"xaxis3": {
"anchor": "y3",
"domain": [
0,
0.48
]
},
"xaxis4": {
"autorange": "reversed",
"anchor": "y4",
"domain": [
0.52,
1
]
},
"yaxis": {
"tickformat": "p",
"domain": [
0,
0.48
]
},
"yaxis2": {
"tickformat": "p",
"dtick": 0.1,
"anchor": "x2",
"domain": [
0.52,
1
]
},
"yaxis3": {
"tickformat": "p",
"dtick": 0.3,
"anchor": "x3",
"domain": [
0.52,
1
]
},
"yaxis4": {
"tickformat": "p",
"dtick": 0.05,
"anchor": "x4",
"domain": [
0,
0.48
]
}
}
}
Loading