cx 24.10.4 → 24.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,212 +1,235 @@
1
- import { Axis } from "./Axis";
2
- import { VDOM } from "../../ui/Widget";
3
- import { isUndefined } from "../../util/isUndefined";
4
- import { isArray } from "../../util/isArray";
5
-
6
- export class CategoryAxis extends Axis {
7
- declareData() {
8
- super.declareData(...arguments, {
9
- inverted: undefined,
10
- uniform: undefined,
11
- names: undefined,
12
- values: undefined,
13
- minSize: undefined,
14
- categoryCount: undefined,
15
- });
16
- }
17
-
18
- initInstance(context, instance) {
19
- instance.calculator = new CategoryScale();
20
- }
21
-
22
- explore(context, instance) {
23
- super.explore(context, instance);
24
- var { values, names, inverted, uniform, minSize } = instance.data;
25
- instance.calculator.reset(inverted, uniform, values, names, minSize);
26
- }
27
-
28
- reportData(context, instance) {
29
- instance.set("categoryCount", instance.calculator.valueList.length);
30
- }
31
-
32
- render(context, instance, key) {
33
- var { data, calculator } = instance;
34
-
35
- if (!data.bounds.valid()) return null;
36
-
37
- var formatter = (v) => calculator.names[v] || v;
38
-
39
- return (
40
- <g key={key} className={data.classNames} style={data.style}>
41
- {this.renderTicksAndLabels(context, instance, formatter)}
42
- </g>
43
- );
44
- }
45
- }
46
-
47
- CategoryAxis.prototype.baseClass = "categoryaxis";
48
- CategoryAxis.prototype.anchors = "0 1 1 0";
49
- CategoryAxis.prototype.vertical = false;
50
- CategoryAxis.prototype.inverted = false;
51
- CategoryAxis.prototype.uniform = false;
52
- CategoryAxis.prototype.labelOffset = 10;
53
- CategoryAxis.prototype.labelRotation = 0;
54
- CategoryAxis.prototype.labelAnchor = "auto";
55
- CategoryAxis.prototype.labelDx = "auto";
56
- CategoryAxis.prototype.labelDy = "auto";
57
- CategoryAxis.prototype.minSize = 1;
58
-
59
- Axis.alias("category", CategoryAxis);
60
-
61
- class CategoryScale {
62
- reset(inverted, uniform, values, names, minSize) {
63
- this.padding = 0.5;
64
- delete this.min;
65
- delete this.max;
66
- delete this.minValue;
67
- delete this.maxValue;
68
- this.minSize = minSize;
69
- this.valuesMap = {};
70
- this.valueList = [];
71
- this.inverted = inverted;
72
- this.uniform = uniform;
73
- this.valueStacks = {};
74
- this.names = {};
75
-
76
- if (values) {
77
- if (isArray(values)) values.forEach((v) => this.acknowledge(v));
78
- else if (typeof values == "object")
79
- for (var k in values) {
80
- this.acknowledge(k);
81
- this.names[k] = values[k];
82
- }
83
- }
84
-
85
- if (names) {
86
- if (isArray(names)) {
87
- values = values || [];
88
- names.forEach((name, index) => {
89
- var value = values[index];
90
- this.names[value != null ? value : index] = name;
91
- });
92
- } else this.names = names;
93
- }
94
- }
95
-
96
- decodeValue(n) {
97
- return n;
98
- }
99
-
100
- encodeValue(v) {
101
- return v;
102
- }
103
-
104
- map(v, offset = 0) {
105
- var index = this.valuesMap[v] || 0;
106
-
107
- return this.origin + (index + offset - this.min + this.padding) * this.factor;
108
- }
109
-
110
- measure(a, b) {
111
- this.a = a;
112
- this.b = b;
113
-
114
- if (this.min == null) this.min = this.minValue || 0;
115
-
116
- if (this.max == null) this.max = !isNaN(this.maxValue) ? this.maxValue : 100;
117
-
118
- var sign = this.inverted ? -1 : 1;
119
-
120
- if (this.max - this.min + 1 < this.minSize) {
121
- this.factor = (sign * (this.b - this.a)) / this.minSize;
122
- this.origin = (this.b + this.a) * 0.5 - (this.factor * (this.max - this.min + 1)) / 2;
123
- } else {
124
- this.factor = (sign * (this.b - this.a)) / (this.max - this.min + 2 * this.padding);
125
- this.origin = (this.a * (1 + sign)) / 2 + (this.b * (1 - sign)) / 2; //a || b
126
- }
127
- }
128
-
129
- hash() {
130
- return {
131
- origin: this.origin,
132
- factor: this.factor,
133
- min: this.min,
134
- minSize: this.minSize,
135
- padding: this.padding,
136
- values: this.valueList.join(":"),
137
- names: JSON.stringify(this.names),
138
- };
139
- }
140
-
141
- isSame(x) {
142
- var h = this.hash();
143
- var same = x && !Object.keys(h).some((k) => x[k] !== h[k]);
144
- this.shouldUpdate = !same;
145
- return same;
146
- }
147
-
148
- acknowledge(value, width = 0, offset = 0) {
149
- var index = this.valuesMap[value];
150
- if (isUndefined(index)) {
151
- index = this.valueList.length;
152
- this.valueList.push(value);
153
- this.valuesMap[value] = index;
154
- }
155
-
156
- if (this.minValue == null || index < this.minValue) {
157
- this.minValue = index;
158
- this.padding = Math.max(this.padding, Math.abs(offset - width / 2));
159
- }
160
-
161
- if (this.maxValue == null || index > this.maxValue) {
162
- this.maxValue = index;
163
- this.padding = Math.max(this.padding, Math.abs(offset + width / 2));
164
- }
165
- }
166
-
167
- book(value, name) {
168
- if (this.uniform) value = 0;
169
-
170
- var stack = this.valueStacks[value];
171
- if (!stack)
172
- stack = this.valueStacks[value] = {
173
- index: {},
174
- count: 0,
175
- };
176
- if (!stack.index.hasOwnProperty(name)) stack.index[name] = stack.count++;
177
- }
178
-
179
- locate(value, name) {
180
- if (this.uniform) value = 0;
181
-
182
- var stack = this.valueStacks[value];
183
- if (!stack) return [0, 1];
184
-
185
- return [stack.index[name], stack.count];
186
- }
187
-
188
- trackValue(v, offset = 0, constrain = false) {
189
- let index = Math.round((v - this.origin) / this.factor - offset + this.min - this.padding);
190
- if (index < this.min) index = this.min;
191
- if (index > this.max) index = this.max;
192
- return this.valueList[index];
193
- }
194
-
195
- findTickSize(minPxDist) {
196
- return 1;
197
- }
198
-
199
- getTickSizes() {
200
- return [1];
201
- }
202
-
203
- getTicks(tickSizes) {
204
- return tickSizes.map((size) => this.valueList);
205
- }
206
-
207
- mapGridlines() {
208
- return Array.from({ length: this.valueList.length + 1 }).map(
209
- (_, index) => this.origin + (index - 0.5 - this.min + this.padding) * this.factor,
210
- );
211
- }
212
- }
1
+ import { Axis } from "./Axis";
2
+ import { VDOM } from "../../ui/Widget";
3
+ import { isUndefined } from "../../util/isUndefined";
4
+ import { isArray } from "../../util/isArray";
5
+
6
+ export class CategoryAxis extends Axis {
7
+ declareData() {
8
+ super.declareData(...arguments, {
9
+ inverted: undefined,
10
+ uniform: undefined,
11
+ names: undefined,
12
+ values: undefined,
13
+ minSize: undefined,
14
+ categoryCount: undefined,
15
+ });
16
+ }
17
+
18
+ initInstance(context, instance) {
19
+ instance.calculator = new CategoryScale();
20
+ }
21
+
22
+ explore(context, instance) {
23
+ super.explore(context, instance);
24
+ var { values, names, inverted, uniform, minSize } = instance.data;
25
+ instance.calculator.reset(inverted, uniform, values, names, minSize, this.minTickDistance, this.minLabelDistance);
26
+ }
27
+
28
+ reportData(context, instance) {
29
+ instance.set("categoryCount", instance.calculator.valueList.length);
30
+ }
31
+
32
+ render(context, instance, key) {
33
+ var { data, calculator } = instance;
34
+
35
+ if (!data.bounds.valid()) return null;
36
+
37
+ var formatter = (v) => calculator.names[v] || v;
38
+
39
+ return (
40
+ <g key={key} className={data.classNames} style={data.style}>
41
+ {this.renderTicksAndLabels(context, instance, formatter)}
42
+ </g>
43
+ );
44
+ }
45
+ }
46
+
47
+ CategoryAxis.prototype.baseClass = "categoryaxis";
48
+ CategoryAxis.prototype.anchors = "0 1 1 0";
49
+ CategoryAxis.prototype.vertical = false;
50
+ CategoryAxis.prototype.inverted = false;
51
+ CategoryAxis.prototype.uniform = false;
52
+ CategoryAxis.prototype.labelOffset = 10;
53
+ CategoryAxis.prototype.labelRotation = 0;
54
+ CategoryAxis.prototype.labelAnchor = "auto";
55
+ CategoryAxis.prototype.labelDx = "auto";
56
+ CategoryAxis.prototype.labelDy = "auto";
57
+ CategoryAxis.prototype.minSize = 1;
58
+ CategoryAxis.prototype.minLabelDistanceHorizontal = 0;
59
+ CategoryAxis.prototype.minLabelDistanceVertical = 0;
60
+ CategoryAxis.prototype.minTickDistance = 0;
61
+
62
+ Axis.alias("category", CategoryAxis);
63
+
64
+ class CategoryScale {
65
+ reset(inverted, uniform, values, names, minSize, minTickDistance, minLabelDistance) {
66
+ this.padding = 0.5;
67
+ delete this.min;
68
+ delete this.max;
69
+ delete this.minValue;
70
+ delete this.maxValue;
71
+ this.minSize = minSize;
72
+ this.valuesMap = {};
73
+ this.valueList = [];
74
+ this.inverted = inverted;
75
+ this.uniform = uniform;
76
+ this.valueStacks = {};
77
+ this.names = {};
78
+ this.minTickDistance = minTickDistance;
79
+ this.minLabelDistance = minLabelDistance;
80
+
81
+ if (values) {
82
+ if (isArray(values)) values.forEach((v) => this.acknowledge(v));
83
+ else if (typeof values == "object")
84
+ for (var k in values) {
85
+ this.acknowledge(k);
86
+ this.names[k] = values[k];
87
+ }
88
+ }
89
+
90
+ if (names) {
91
+ if (isArray(names)) {
92
+ values = values || [];
93
+ names.forEach((name, index) => {
94
+ var value = values[index];
95
+ this.names[value != null ? value : index] = name;
96
+ });
97
+ } else this.names = names;
98
+ }
99
+ }
100
+
101
+ decodeValue(n) {
102
+ return n;
103
+ }
104
+
105
+ encodeValue(v) {
106
+ return v;
107
+ }
108
+
109
+ map(v, offset = 0) {
110
+ var index = this.valuesMap[v] || 0;
111
+
112
+ return this.origin + (index + offset - this.min + this.padding) * this.factor;
113
+ }
114
+
115
+ measure(a, b) {
116
+ this.a = a;
117
+ this.b = b;
118
+
119
+ if (this.min == null) this.min = this.minValue || 0;
120
+
121
+ if (this.max == null) this.max = !isNaN(this.maxValue) ? this.maxValue : 100;
122
+
123
+ var sign = this.inverted ? -1 : 1;
124
+
125
+ if (this.max - this.min + 1 < this.minSize) {
126
+ this.factor = (sign * (this.b - this.a)) / this.minSize;
127
+ this.origin = (this.b + this.a) * 0.5 - (this.factor * (this.max - this.min + 1)) / 2;
128
+ } else {
129
+ this.factor = (sign * (this.b - this.a)) / (this.max - this.min + 2 * this.padding);
130
+ this.origin = (this.a * (1 + sign)) / 2 + (this.b * (1 - sign)) / 2; //a || b
131
+ }
132
+
133
+ this.tickSizes = [];
134
+ let tickMultiplier = [1, 2, 5];
135
+ let absFactor = Math.abs(this.factor);
136
+ for (let base = 1; base < 10000 && this.tickSizes.length < 2; base *= 10) {
137
+ for (let m of tickMultiplier) {
138
+ if (base * m * absFactor >= this.minTickDistance && this.tickSizes.length == 0)
139
+ this.tickSizes.push(base * m);
140
+ if (base * m * absFactor >= this.minLabelDistance) {
141
+ this.tickSizes.push(base * m);
142
+ break;
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ hash() {
149
+ return {
150
+ origin: this.origin,
151
+ factor: this.factor,
152
+ min: this.min,
153
+ minSize: this.minSize,
154
+ padding: this.padding,
155
+ values: this.valueList.join(":"),
156
+ names: JSON.stringify(this.names),
157
+ };
158
+ }
159
+
160
+ isSame(x) {
161
+ var h = this.hash();
162
+ var same = x && !Object.keys(h).some((k) => x[k] !== h[k]);
163
+ this.shouldUpdate = !same;
164
+ return same;
165
+ }
166
+
167
+ acknowledge(value, width = 0, offset = 0) {
168
+ var index = this.valuesMap[value];
169
+ if (isUndefined(index)) {
170
+ index = this.valueList.length;
171
+ this.valueList.push(value);
172
+ this.valuesMap[value] = index;
173
+ }
174
+
175
+ if (this.minValue == null || index < this.minValue) {
176
+ this.minValue = index;
177
+ this.padding = Math.max(this.padding, Math.abs(offset - width / 2));
178
+ }
179
+
180
+ if (this.maxValue == null || index > this.maxValue) {
181
+ this.maxValue = index;
182
+ this.padding = Math.max(this.padding, Math.abs(offset + width / 2));
183
+ }
184
+ }
185
+
186
+ book(value, name) {
187
+ if (this.uniform) value = 0;
188
+
189
+ var stack = this.valueStacks[value];
190
+ if (!stack)
191
+ stack = this.valueStacks[value] = {
192
+ index: {},
193
+ count: 0,
194
+ };
195
+ if (!stack.index.hasOwnProperty(name)) stack.index[name] = stack.count++;
196
+ }
197
+
198
+ locate(value, name) {
199
+ if (this.uniform) value = 0;
200
+
201
+ var stack = this.valueStacks[value];
202
+ if (!stack) return [0, 1];
203
+
204
+ return [stack.index[name], stack.count];
205
+ }
206
+
207
+ trackValue(v, offset = 0, constrain = false) {
208
+ let index = Math.round((v - this.origin) / this.factor - offset + this.min - this.padding);
209
+ if (index < this.min) index = this.min;
210
+ if (index > this.max) index = this.max;
211
+ return this.valueList[index];
212
+ }
213
+
214
+ findTickSize(minPxDist) {
215
+ for (let tickSize of this.tickSizes) if (tickSize * Math.abs(this.factor) >= minPxDist) return tickSize;
216
+ return 1;
217
+ }
218
+
219
+ getTickSizes() {
220
+ return this.tickSizes;
221
+ }
222
+
223
+ getTicks(tickSizes) {
224
+ return tickSizes.map((size) => this.valueList.filter((_, i) => i % size == 0));
225
+ }
226
+
227
+ mapGridlines() {
228
+ let result = [];
229
+ if (this.tickSizes.length == 0) return result;
230
+ let step = this.tickSizes[0];
231
+ for (let index = this.min; index <= this.max + 1; index += step)
232
+ result.push(this.origin + (index - 0.5 - this.min + this.padding) * this.factor);
233
+ return result;
234
+ }
235
+ }