Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b57760f

Browse files
authored
Merge pull request plotly#5114 from plotly/improve-axis-tick-increment
Improve axis tick increment
2 parents 512ff14 + cb005b9 commit b57760f

File tree

10 files changed

+1072
-4
lines changed

10 files changed

+1072
-4
lines changed

src/lib/increment.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2012-2020, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
module.exports = function incrementNumeric(x, delta) {
13+
if(!delta) return x;
14+
15+
// Note 1:
16+
// 0.3 != 0.1 + 0.2 == 0.30000000000000004
17+
// but 0.3 == (10 * 0.1 + 10 * 0.2) / 10
18+
// Attempt to use integer steps to increment
19+
var scale = 1 / Math.abs(delta);
20+
var newX = (scale > 1) ? (
21+
scale * x +
22+
scale * delta
23+
) / scale : x + delta;
24+
25+
// Note 2:
26+
// now we may also consider rounding to cover few more edge cases
27+
// e.g. 0.3 * 3 = 0.8999999999999999
28+
var lenX1 = String(newX).length;
29+
if(lenX1 > 16) {
30+
var lenDt = String(delta).length;
31+
var lenX0 = String(x).length;
32+
33+
if(lenX1 >= lenX0 + lenDt) { // likely a rounding error!
34+
var s = parseFloat(newX).toPrecision(12);
35+
if(s.indexOf('e+') === -1) newX = +s;
36+
}
37+
}
38+
39+
return newX;
40+
};

src/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ lib.filterUnique = require('./filter_unique');
158158
lib.filterVisible = require('./filter_visible');
159159
lib.pushUnique = require('./push_unique');
160160

161+
lib.increment = require('./increment');
162+
161163
lib.cleanNumber = require('./clean_number');
162164

163165
lib.ensureNumber = function ensureNumber(v) {

src/plots/cartesian/axes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ axes.tickIncrement = function(x, dtick, axrev, calendar) {
10631063
var axSign = axrev ? -1 : 1;
10641064

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

10681068
// everything else is a string, one character plus a number
10691069
var tType = dtick.charAt(0);
85.3 KB
Loading

test/image/baselines/tick-percent.png

71.4 KB
Loading

test/image/compare_pixels_test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ if(allMock || argv.filter) {
102102

103103
var FLAKY_LIST = [
104104
'treemap_coffee',
105+
'treemap_textposition',
105106
'treemap_sunburst_marker_colors',
106-
'trace_metatext',
107-
'gl3d_directions-streamtube1',
108-
'gl3d_traces-with-opacity'
107+
'treemap_with-without_values',
109108
];
110109

111110
console.log('');

test/image/mocks/tick-increment.json

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
"data": [
3+
{
4+
"name": "s & positive",
5+
"x": [
6+
"2019-12-31 23:59:59.998",
7+
"2020-01-01 00:00:00",
8+
"2020-01-01 00:00:00.002"
9+
],
10+
"y": [
11+
"1e-1",
12+
"1e-2",
13+
"1e-0"
14+
],
15+
"type": "scatter"
16+
},
17+
{
18+
"name": "s & negative",
19+
"xaxis": "x2",
20+
"yaxis": "y2",
21+
"x": [
22+
"2019-12-31 23:59:59.998",
23+
"2020-01-01 00:00:00",
24+
"2020-01-01 00:00:00.002"
25+
],
26+
"y": [
27+
"-1e-1",
28+
"-1e-2",
29+
"-1e-0"
30+
],
31+
"type": "scatter"
32+
},
33+
{
34+
"name": "p & negative",
35+
"xaxis": "x3",
36+
"yaxis": "y3",
37+
"x": [
38+
"2019-12-31 23:59:59.998",
39+
"2020-01-01 00:00:00",
40+
"2020-01-01 00:00:00.002"
41+
],
42+
"y": [
43+
"-1e-1",
44+
"-1e-2",
45+
"-1e-0"
46+
],
47+
"type": "scatter"
48+
},
49+
{
50+
"name": "p & positive",
51+
"xaxis": "x4",
52+
"yaxis": "y4",
53+
"x": [
54+
"2019-12-31 23:59:59.998",
55+
"2020-01-01 00:00:00",
56+
"2020-01-01 00:00:00.002"
57+
],
58+
"y": [
59+
"1e-1",
60+
"1e-2",
61+
"1e-0"
62+
],
63+
"type": "scatter"
64+
}
65+
],
66+
"layout": {
67+
"width": 800,
68+
"height": 800,
69+
"xaxis": {
70+
"type": "date",
71+
"domain": [
72+
0,
73+
0.45
74+
]
75+
},
76+
"xaxis2": {
77+
"type": "date",
78+
"anchor": "y2",
79+
"domain": [
80+
0.6,
81+
1
82+
]
83+
},
84+
"xaxis3": {
85+
"type": "date",
86+
"anchor": "y3",
87+
"domain": [
88+
0,
89+
0.45
90+
]
91+
},
92+
"xaxis4": {
93+
"type": "date",
94+
"anchor": "y4",
95+
"domain": [
96+
0.6,
97+
1
98+
]
99+
},
100+
"yaxis": {
101+
"nticks": 10,
102+
"tickformat": "s",
103+
"domain": [
104+
0,
105+
0.45
106+
]
107+
},
108+
"yaxis2": {
109+
"nticks": 10,
110+
"tickformat": "s",
111+
"anchor": "x2",
112+
"domain": [
113+
0,
114+
0.45
115+
]
116+
},
117+
"yaxis3": {
118+
"nticks": 10,
119+
"tickformat": "p",
120+
"anchor": "x3",
121+
"domain": [
122+
0.6,
123+
1
124+
]
125+
},
126+
"yaxis4": {
127+
"nticks": 10,
128+
"tickformat": "p",
129+
"anchor": "x4",
130+
"domain": [
131+
0.6,
132+
1
133+
]
134+
}
135+
}
136+
}

test/image/mocks/tick-percent.json

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"data": [
3+
{
4+
"type": "scatter",
5+
"y": [
6+
1,
7+
0,
8+
0.5
9+
]
10+
},
11+
{
12+
"xaxis": "x2",
13+
"yaxis": "y2",
14+
"type": "scatter",
15+
"y": [
16+
1,
17+
0,
18+
0.5
19+
]
20+
},
21+
{
22+
"xaxis": "x3",
23+
"yaxis": "y3",
24+
"type": "scatter",
25+
"y": [
26+
1,
27+
0,
28+
0.5
29+
]
30+
},
31+
{
32+
"xaxis": "x4",
33+
"yaxis": "y4",
34+
"type": "scatter",
35+
"y": [
36+
1,
37+
0,
38+
0.5
39+
]
40+
}
41+
],
42+
"layout": {
43+
"width": 800,
44+
"height": 800,
45+
"xaxis": {
46+
"domain": [
47+
0,
48+
0.48
49+
]
50+
},
51+
"xaxis2": {
52+
"anchor": "y2",
53+
"domain": [
54+
0.52,
55+
1
56+
]
57+
},
58+
"xaxis3": {
59+
"anchor": "y3",
60+
"domain": [
61+
0,
62+
0.48
63+
]
64+
},
65+
"xaxis4": {
66+
"autorange": "reversed",
67+
"anchor": "y4",
68+
"domain": [
69+
0.52,
70+
1
71+
]
72+
},
73+
"yaxis": {
74+
"tickformat": "p",
75+
"domain": [
76+
0,
77+
0.48
78+
]
79+
},
80+
"yaxis2": {
81+
"tickformat": "p",
82+
"dtick": 0.1,
83+
"anchor": "x2",
84+
"domain": [
85+
0.52,
86+
1
87+
]
88+
},
89+
"yaxis3": {
90+
"tickformat": "p",
91+
"dtick": 0.3,
92+
"anchor": "x3",
93+
"domain": [
94+
0.52,
95+
1
96+
]
97+
},
98+
"yaxis4": {
99+
"tickformat": "p",
100+
"dtick": 0.05,
101+
"anchor": "x4",
102+
"domain": [
103+
0,
104+
0.48
105+
]
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)