/
LogarithmicAxis.ts
306 lines (256 loc) · 7.29 KB
/
LogarithmicAxis.ts
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
/* *
*
* (c) 2010-2024 Torstein Honsi
*
* License: www.highcharts.com/license
*
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
*
* */
'use strict';
/* *
*
* Imports
*
* */
import type Axis from './Axis';
import U from '../Utilities.js';
const {
addEvent,
normalizeTickInterval,
pick
} = U;
/* *
*
* Declarations
*
* */
declare module './AxisComposition' {
interface AxisComposition {
logarithmic?: LogarithmicAxis.Additions;
}
}
declare module './AxisType' {
interface AxisTypeRegistry {
LogarithmicAxis: LogarithmicAxis.Composition;
}
}
/* *
*
* Class
*
* */
/**
* @private
*/
namespace LogarithmicAxis {
/* *
*
* Declarations
*
* */
export declare class Composition extends Axis {
logarithmic: Additions;
}
/* *
*
* Functions
*
* */
/**
* Provides logarithmic support for axes.
* @private
*/
export function compose<T extends typeof Axis>(
AxisClass: T
): (T&typeof Composition) {
if (!AxisClass.keepProps.includes('logarithmic')) {
AxisClass.keepProps.push('logarithmic');
addEvent(AxisClass, 'init', onInit);
addEvent(AxisClass, 'afterInit', onAfterInit);
}
return AxisClass as (T&typeof Composition);
}
/**
* @private
*/
function onInit(
this: Axis,
e: { userOptions: Axis['options'] }
): void {
const axis = this;
const options = e.userOptions;
let logarithmic = axis.logarithmic;
if (options.type !== 'logarithmic') {
axis.logarithmic = void 0;
} else {
if (!logarithmic) {
logarithmic = axis.logarithmic = new Additions(
axis as Composition
);
}
}
}
/**
* @private
*/
function onAfterInit(
this: Axis
): void {
const axis = this as Composition;
const log = axis.logarithmic;
// Extend logarithmic axis
if (log) {
axis.lin2val = function (num: number): number {
return log.lin2log(num);
};
axis.val2lin = function (num: number): number {
return log.log2lin(num);
};
}
}
/* *
*
* Class
*
* */
/**
* Provides logarithmic support for axes.
* @private
* @class
*/
export class Additions {
/* *
*
* Constructors
*
* */
public constructor(axis: Composition) {
this.axis = axis;
}
/* *
*
* Properties
*
* */
public axis: Composition;
public minorAutoInterval?: number;
/* *
*
* Functions
*
* */
/**
* Set the tick positions of a logarithmic axis.
*/
public getLogTickPositions(
interval: number,
min: number,
max: number,
minor?: boolean
): Array<number> {
const log = this;
const axis = log.axis;
const axisLength = axis.len;
const options = axis.options;
// Since we use this method for both major and minor ticks,
// use a local variable and return the result
let positions = [];
// Reset
if (!minor) {
log.minorAutoInterval = void 0;
}
// First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
if (interval >= 0.5) {
interval = Math.round(interval);
positions = axis.getLinearTickPositions(interval, min, max);
// Second case: We need intermediary ticks. For example
// 1, 2, 4, 6, 8, 10, 20, 40 etc.
} else if (interval >= 0.08) {
const roundedMin = Math.floor(min);
let intermediate,
i,
j,
len,
pos,
lastPos,
break2;
if (interval > 0.3) {
intermediate = [1, 2, 4];
// 0.2 equals five minor ticks per 1, 10, 100 etc
} else if (interval > 0.15) {
intermediate = [1, 2, 4, 6, 8];
} else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
for (i = roundedMin; i < max + 1 && !break2; i++) {
len = intermediate.length;
for (j = 0; j < len && !break2; j++) {
pos = log.log2lin(log.lin2log(i) * intermediate[j]);
// #1670, lastPos is #3113
if (
pos > min &&
(!minor || (lastPos as any) <= max) &&
typeof lastPos !== 'undefined'
) {
positions.push(lastPos);
}
if ((lastPos as any) > max) {
break2 = true;
}
lastPos = pos;
}
}
// Third case: We are so deep in between whole logarithmic values,
// that we might as well handle the tick positions like a linear
// axis. For example 1.01, 1.02, 1.03, 1.04.
} else {
const realMin = log.lin2log(min),
realMax = log.lin2log(max),
tickIntervalOption = minor ?
axis.getMinorTickInterval() :
options.tickInterval,
filteredTickIntervalOption = tickIntervalOption === 'auto' ?
null :
tickIntervalOption,
tickPixelIntervalOption =
(options.tickPixelInterval as any) / (minor ? 5 : 1),
totalPixelLength = minor ?
axisLength / axis.tickPositions.length :
axisLength;
interval = pick(
filteredTickIntervalOption,
log.minorAutoInterval,
(realMax - realMin) *
tickPixelIntervalOption / (totalPixelLength || 1)
);
interval = normalizeTickInterval(interval);
positions = axis.getLinearTickPositions(
interval,
realMin,
realMax
).map(log.log2lin);
if (!minor) {
log.minorAutoInterval = interval / 5;
}
}
// Set the axis-level tickInterval variable
if (!minor) {
axis.tickInterval = interval;
}
return positions;
}
public lin2log(num: number): number {
return Math.pow(10, num);
}
public log2lin(num: number): number {
return Math.log(num) / Math.LN10;
}
}
}
/* *
*
* Default Export
*
* */
export default LogarithmicAxis;