forked from chartjs/Chart.js
/
scale.logarithmic.js
200 lines (168 loc) · 4.97 KB
/
scale.logarithmic.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
import {finiteOrDefault, isFinite} from '../helpers/helpers.core';
import {formatNumber} from '../helpers/helpers.intl';
import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math';
import Scale from '../core/core.scale';
import LinearScaleBase from './scale.linearbase';
import Ticks from '../core/core.ticks';
function isMajor(tickVal) {
const remain = tickVal / (Math.pow(10, Math.floor(log10(tickVal))));
return remain === 1;
}
/**
* Generate a set of logarithmic ticks
* @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 endExp = Math.floor(log10(dataRange.max));
const endSignificand = Math.ceil(dataRange.max / Math.pow(10, endExp));
const ticks = [];
let tickVal = finiteOrDefault(generationOptions.min, Math.pow(10, Math.floor(log10(dataRange.min))));
let exp = Math.floor(log10(tickVal));
let significand = Math.floor(tickVal / Math.pow(10, exp));
let precision = exp < 0 ? Math.pow(10, Math.abs(exp)) : 1;
do {
ticks.push({value: tickVal, major: isMajor(tickVal)});
++significand;
if (significand === 10) {
significand = 1;
++exp;
precision = exp >= 0 ? 1 : precision;
}
tickVal = Math.round(significand * Math.pow(10, exp) * precision) / precision;
} while (exp < endExp || (exp === endExp && significand < endSignificand));
const lastTick = finiteOrDefault(generationOptions.max, tickVal);
ticks.push({value: lastTick, major: isMajor(tickVal)});
return ticks;
}
export default class LogarithmicScale extends Scale {
constructor(cfg) {
super(cfg);
/** @type {number} */
this.start = undefined;
/** @type {number} */
this.end = undefined;
/** @type {number} */
this._startValue = undefined;
this._valueRange = 0;
}
parse(raw, index) {
const value = LinearScaleBase.prototype.parse.apply(this, [raw, index]);
if (value === 0) {
this._zero = true;
return undefined;
}
return isFinite(value) && value > 0 ? value : null;
}
determineDataLimits() {
const me = this;
const {min, max} = me.getMinMax(true);
me.min = isFinite(min) ? Math.max(0, min) : null;
me.max = isFinite(max) ? Math.max(0, max) : null;
if (me.options.beginAtZero) {
me._zero = true;
}
me.handleTickRangeOptions();
}
handleTickRangeOptions() {
const me = this;
const {minDefined, maxDefined} = me.getUserBounds();
let min = me.min;
let max = me.max;
const setMin = v => (min = minDefined ? min : v);
const setMax = v => (max = maxDefined ? max : v);
const exp = (v, m) => Math.pow(10, Math.floor(log10(v)) + m);
if (min === max) {
if (min <= 0) { // includes null
setMin(1);
setMax(10);
} else {
setMin(exp(min, -1));
setMax(exp(max, +1));
}
}
if (min <= 0) {
setMin(exp(max, -1));
}
if (max <= 0) {
setMax(exp(min, +1));
}
// if data has `0` in it or `beginAtZero` is true, min (non zero) value is at bottom
// of scale, and it does not equal suggestedMin, lower the min bound by one exp.
if (me._zero && me.min !== me._suggestedMin && min === exp(me.min, 0)) {
setMin(exp(min, -1));
}
me.min = min;
me.max = max;
}
buildTicks() {
const me = this;
const opts = me.options;
const generationOptions = {
min: me._userMin,
max: me._userMax
};
const ticks = generateTicks(generationOptions, me);
// 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;
}
/**
* @param {number} value
* @return {string}
*/
getLabelForValue(value) {
return value === undefined ? '0' : formatNumber(value, this.chart.options.locale);
}
/**
* @protected
*/
configure() {
const me = this;
const start = me.min;
super.configure();
me._startValue = log10(start);
me._valueRange = log10(me.max) - log10(start);
}
getPixelForValue(value) {
const me = this;
if (value === undefined || value === 0) {
value = me.min;
}
if (value === null || isNaN(value)) {
return NaN;
}
return me.getPixelForDecimal(value === me.min
? 0
: (log10(value) - me._startValue) / me._valueRange);
}
getValueForPixel(pixel) {
const me = this;
const decimal = me.getDecimalForPixel(pixel);
return Math.pow(10, me._startValue + decimal * me._valueRange);
}
}
LogarithmicScale.id = 'logarithmic';
/**
* @type {any}
*/
LogarithmicScale.defaults = {
ticks: {
callback: Ticks.formatters.logarithmic,
major: {
enabled: true
}
}
};