-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
scale.linearbase.js
307 lines (265 loc) · 9.28 KB
/
scale.linearbase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {isNullOrUndef} from '../helpers/helpers.core';
import {almostEquals, almostWhole, niceNum, _decimalPlaces, _setMinAndMaxByKey, sign, toRadians} from '../helpers/helpers.math';
import Scale from '../core/core.scale';
import {formatNumber} from '../helpers/helpers.intl';
import {_filterBetween} from '../helpers';
/**
* Generate a set of linear ticks for an axis
* 1. If generationOptions.min, generationOptions.max, and generationOptions.step are defined:
* if (max - min) / step is an integer, ticks are generated as [min, min + step, ..., max]
* Note that the generationOptions.maxCount setting is respected in this scenario
*
* 2. If generationOptions.min, generationOptions.max, and generationOptions.count is defined
* spacing = (max - min) / count
* Ticks are generated as [min, min + spacing, ..., max]
*
* 3. If generationOptions.count is defined
* spacing = (niceMax - niceMin) / count
*
* 4. Compute optimal spacing of ticks using niceNum algorithm
*
* @param generationOptions the options used to generate the ticks
* @param dataRange the range of the data
* @returns {object[]} array of tick objects
*/
function generateTicks(generationOptions, dataRange) {
const ticks = [];
// To get a "nice" value for the tick spacing, we will use the appropriately named
// "nice number" algorithm. See https://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
// for details.
const MIN_SPACING = 1e-14;
const {bounds, step, min, max, precision, count, maxTicks, maxDigits, includeBounds} = generationOptions;
const unit = step || 1;
const maxSpaces = maxTicks - 1;
const {min: rmin, max: rmax} = dataRange;
const minDefined = !isNullOrUndef(min);
const maxDefined = !isNullOrUndef(max);
const countDefined = !isNullOrUndef(count);
const minSpacing = (rmax - rmin) / (maxDigits + 1);
let spacing = niceNum((rmax - rmin) / maxSpaces / unit) * unit;
let factor, niceMin, niceMax, numSpaces;
// Beyond MIN_SPACING floating point numbers being to lose precision
// such that we can't do the math necessary to generate ticks
if (spacing < MIN_SPACING && !minDefined && !maxDefined) {
return [{value: rmin}, {value: rmax}];
}
numSpaces = Math.ceil(rmax / spacing) - Math.floor(rmin / spacing);
if (numSpaces > maxSpaces) {
// If the calculated num of spaces exceeds maxNumSpaces, recalculate it
spacing = niceNum(numSpaces * spacing / maxSpaces / unit) * unit;
}
if (!isNullOrUndef(precision)) {
// If the user specified a precision, round to that number of decimal places
factor = Math.pow(10, precision);
spacing = Math.ceil(spacing * factor) / factor;
}
if (bounds === 'ticks') {
niceMin = Math.floor(rmin / spacing) * spacing;
niceMax = Math.ceil(rmax / spacing) * spacing;
} else {
niceMin = rmin;
niceMax = rmax;
}
if (minDefined && maxDefined && step && almostWhole((max - min) / step, spacing / 1000)) {
// Case 1: If min, max and stepSize are set and they make an evenly spaced scale use it.
// spacing = step;
// numSpaces = (max - min) / spacing;
numSpaces = Math.min((max - min) / spacing, maxTicks);
spacing = (max - min) / numSpaces;
niceMin = min;
niceMax = max;
} else if (countDefined) {
// Cases 2 & 3, we have a count specified. Handle optional user defined edges to the range.
// Sometimes these are no-ops, but it makes the code a lot clearer
// and when a user defined range is specified, we want the correct ticks
niceMin = minDefined ? min : niceMin;
niceMax = maxDefined ? max : niceMax;
numSpaces = count - 1;
spacing = (niceMax - niceMin) / numSpaces;
} else {
// Case 4
numSpaces = (niceMax - niceMin) / spacing;
// If very close to our rounded value, use it.
if (almostEquals(numSpaces, Math.round(numSpaces), spacing / 1000)) {
numSpaces = Math.round(numSpaces);
} else {
numSpaces = Math.ceil(numSpaces);
}
}
// The spacing will have changed in cases 1, 2, and 3 so the factor cannot be computed
// until this point
const decimalPlaces = Math.max(
_decimalPlaces(spacing),
_decimalPlaces(niceMin),
);
factor = Math.pow(10, isNullOrUndef(precision) ? decimalPlaces : precision);
niceMin = Math.round(niceMin * factor) / factor;
niceMax = Math.round(niceMax * factor) / factor;
let j = 0;
if (minDefined) {
if (includeBounds && niceMin !== min) {
ticks.push({value: min});
if (niceMin < min) {
j++; // Skip niceMin
}
// If the next nice tick is close to min, skip it
if (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, relativeLabelSize(min, minSpacing, generationOptions))) {
j++;
}
} else if (niceMin < min) {
j++;
}
}
for (; j < numSpaces; ++j) {
ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
}
if (maxDefined && includeBounds && niceMax !== max) {
// If the previous tick is too close to max, replace it with max, else add max
if (almostEquals(ticks[ticks.length - 1].value, max, relativeLabelSize(max, minSpacing, generationOptions))) {
ticks[ticks.length - 1].value = max;
} else {
ticks.push({value: max});
}
} else if (!maxDefined || niceMax === max) {
ticks.push({value: niceMax});
}
return ticks;
}
function relativeLabelSize(value, minSpacing, {horizontal, minRotation}) {
const rad = toRadians(minRotation);
const ratio = (horizontal ? Math.sin(rad) : Math.cos(rad)) || 0.001;
const length = 0.75 * minSpacing * ('' + value).length;
return Math.min(minSpacing / ratio, length);
}
export default class LinearScaleBase extends Scale {
constructor(cfg) {
super(cfg);
/** @type {number} */
this.start = undefined;
/** @type {number} */
this.end = undefined;
/** @type {number} */
this._startValue = undefined;
/** @type {number} */
this._endValue = undefined;
this._valueRange = 0;
}
parse(raw, index) { // eslint-disable-line no-unused-vars
if (isNullOrUndef(raw)) {
return null;
}
if ((typeof raw === 'number' || raw instanceof Number) && !isFinite(+raw)) {
return null;
}
return +raw;
}
handleTickRangeOptions() {
const me = this;
const {beginAtZero} = me.options;
const {minDefined, maxDefined} = me.getUserBounds();
let {min, max} = me;
const setMin = v => (min = minDefined ? min : v);
const setMax = v => (max = maxDefined ? max : v);
if (beginAtZero) {
const minSign = sign(min);
const maxSign = sign(max);
if (minSign < 0 && maxSign < 0) {
setMax(0);
} else if (minSign > 0 && maxSign > 0) {
setMin(0);
}
}
if (min === max) {
setMax(max + 1);
if (!beginAtZero) {
setMin(min - 1);
}
}
me.min = min;
me.max = max;
}
getTickLimit() {
const me = this;
const tickOpts = me.options.ticks;
// eslint-disable-next-line prefer-const
let {maxTicksLimit, stepSize} = tickOpts;
let maxTicks;
if (stepSize) {
maxTicks = Math.ceil(me.max / stepSize) - Math.floor(me.min / stepSize) + 1;
} else {
maxTicks = me.computeTickLimit();
maxTicksLimit = maxTicksLimit || 11;
}
if (maxTicksLimit) {
maxTicks = Math.min(maxTicksLimit, maxTicks);
}
return maxTicks;
}
/**
* @protected
*/
computeTickLimit() {
return Number.POSITIVE_INFINITY;
}
buildTicks() {
const me = this;
const opts = me.options;
const tickOpts = opts.ticks;
// Figure out what the max number of ticks we can support it is based on the size of
// the axis area. For now, we say that the minimum tick spacing in pixels must be 40
// We also limit the maximum number of ticks to 11 which gives a nice 10 squares on
// the graph. Make sure we always have at least 2 ticks
let maxTicks = me.getTickLimit();
maxTicks = Math.max(2, maxTicks);
const numericGeneratorOptions = {
maxTicks,
bounds: opts.bounds,
min: opts.min,
max: opts.max,
precision: tickOpts.precision,
step: tickOpts.stepSize,
count: tickOpts.count,
maxDigits: me._maxDigits(),
horizontal: me.isHorizontal(),
minRotation: tickOpts.minRotation || 0,
includeBounds: tickOpts.includeBounds !== false
};
const dataRange = me._range || me;
const ticks = generateTicks(numericGeneratorOptions, dataRange);
// At this point, we need to update our max and min given the tick values,
// since we probably have expanded the range of the scale
if (opts.bounds === 'ticks') {
_setMinAndMaxByKey(ticks, me, 'value');
}
if (opts.reverse) {
ticks.reverse();
me.start = me.max;
me.end = me.min;
} else {
me.start = me.min;
me.end = me.max;
}
return ticks;
}
/**
* @protected
*/
configure() {
const me = this;
const ticks = me.ticks;
let start = me.min;
let end = me.max;
super.configure();
if (me.options.offset && ticks.length) {
const offset = (end - start) / Math.max(ticks.length - 1, 1) / 2;
start -= offset;
end += offset;
}
me._startValue = start;
me._endValue = end;
me._valueRange = end - start;
}
getLabelForValue(value) {
return formatNumber(value, this.chart.options.locale);
}
}