-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
Copy pathscale.category.js
158 lines (131 loc) · 3.82 KB
/
scale.category.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
import Scale from '../core/core.scale.js';
import {isNullOrUndef, valueOrDefault, _limitValue} from '../helpers/index.js';
const addIfString = (labels, raw, index, addedLabels) => {
if (typeof raw === 'string') {
index = labels.push(raw) - 1;
addedLabels.unshift({index, label: raw});
} else if (isNaN(raw)) {
index = null;
}
return index;
};
function findOrAddLabel(labels, raw, index, addedLabels) {
const first = labels.indexOf(raw);
if (first === -1) {
return addIfString(labels, raw, index, addedLabels);
}
const last = labels.lastIndexOf(raw);
return first !== last ? index : first;
}
const validIndex = (index, max) => index === null ? null : _limitValue(Math.round(index), 0, max);
function _getLabelForValue(value) {
const labels = this.getLabels();
if (value >= 0 && value < labels.length) {
return labels[value];
}
return value;
}
export default class CategoryScale extends Scale {
static id = 'category';
/**
* @type {any}
*/
static defaults = {
ticks: {
callback: _getLabelForValue
}
};
constructor(cfg) {
super(cfg);
/** @type {number} */
this._startValue = undefined;
this._valueRange = 0;
this._addedLabels = [];
}
init(scaleOptions) {
const added = this._addedLabels;
if (added.length) {
const labels = this.getLabels();
for (const {index, label} of added) {
if (labels[index] === label) {
labels.splice(index, 1);
}
}
this._addedLabels = [];
}
super.init(scaleOptions);
}
parse(raw, index) {
if (isNullOrUndef(raw)) {
return null;
}
const labels = this.getLabels();
index = isFinite(index) && labels[index] === raw ? index
: findOrAddLabel(labels, raw, valueOrDefault(index, raw), this._addedLabels);
return validIndex(index, labels.length - 1);
}
determineDataLimits() {
const {minDefined, maxDefined} = this.getUserBounds();
let {min, max} = this.getMinMax(true);
if (this.options.bounds === 'ticks') {
if (!minDefined) {
min = 0;
}
if (!maxDefined) {
max = this.getLabels().length - 1;
}
}
this.min = min;
this.max = max;
}
buildTicks() {
const min = this.min;
const max = this.max;
const offset = this.options.offset;
const ticks = [];
let labels = this.getLabels();
// If we are viewing some subset of labels, slice the original array
labels = (min === 0 && max === labels.length - 1) ? labels : labels.slice(min, max + 1);
this._valueRange = Math.max(labels.length - (offset ? 0 : 1), 1);
this._startValue = this.min - (offset ? 0.5 : 0);
for (let value = min; value <= max; value++) {
ticks.push({value});
}
return ticks;
}
getLabelForValue(value) {
return _getLabelForValue.call(this, value);
}
/**
* @protected
*/
configure() {
super.configure();
if (!this.isHorizontal()) {
// For backward compatibility, vertical category scale reverse is inverted.
this._reversePixels = !this._reversePixels;
}
}
// Used to get data value locations. Value can either be an index or a numerical value
getPixelForValue(value) {
if (typeof value !== 'number') {
value = this.parse(value);
}
return value === null ? NaN : this.getPixelForDecimal((value - this._startValue) / this._valueRange);
}
// Must override base implementation because it calls getPixelForValue
// and category scale can have duplicate values
getPixelForTick(index) {
const ticks = this.ticks;
if (index < 0 || index > ticks.length - 1) {
return null;
}
return this.getPixelForValue(ticks[index].value);
}
getValueForPixel(pixel) {
return Math.round(this._startValue + this.getDecimalForPixel(pixel) * this._valueRange);
}
getBasePixel() {
return this.bottom;
}
}