cx 24.4.5 → 24.4.7

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,252 +1,254 @@
1
- import { BoundedObject } from "../../svg/BoundedObject";
2
- import { VDOM } from "../../ui/Widget";
3
- import { isUndefined } from "../../util/isUndefined";
4
- import { parseStyle } from "../../util/parseStyle";
5
-
6
- export class Axis extends BoundedObject {
7
- init() {
8
- if (this.labelAnchor == "auto") this.labelAnchor = this.vertical ? (this.secondary ? "start" : "end") : "middle";
9
-
10
- if (this.labelDx == "auto") this.labelDx = 0;
11
-
12
- if (this.labelDy == "auto") this.labelDy = this.vertical ? "0.4em" : this.secondary ? 0 : "0.8em";
13
-
14
- if (isUndefined(this.minLabelDistance))
15
- this.minLabelDistance = this.vertical ? this.minLabelDistanceVertical : this.minLabelDistanceHorizontal;
16
-
17
- if (this.labelLineCountDyFactor == "auto")
18
- this.labelLineCountDyFactor = this.vertical ? -this.labelLineHeight / 2 : this.secondary ? -1 : 0;
19
-
20
- this.lineStyle = parseStyle(this.lineStyle);
21
- this.tickStyle = parseStyle(this.tickStyle);
22
- this.labelStyle = parseStyle(this.labelStyle);
23
-
24
- super.init();
25
- }
26
-
27
- declareData() {
28
- super.declareData(
29
- {
30
- anchors: undefined,
31
- hideLabels: undefined,
32
- hideLine: undefined,
33
- hideTicks: undefined,
34
- labelRotation: undefined,
35
- labelAnchor: undefined,
36
- lineStyle: undefined,
37
- lineClass: undefined,
38
- labelStyle: undefined,
39
- labelClass: undefined,
40
- tickStyle: undefined,
41
- tickClass: undefined,
42
- },
43
- ...arguments
44
- );
45
- }
46
-
47
- prepareData(context, instance) {
48
- super.prepareData(context, instance);
49
- if (this.onCreateLabelFormatter)
50
- instance.labelFormatter = instance.invoke("onCreateLabelFormatter", context, instance);
51
- }
52
-
53
- report(context, instance) {
54
- return instance.calculator;
55
- }
56
-
57
- renderTicksAndLabels(context, instance, valueFormatter) {
58
- if (this.hidden) return false;
59
-
60
- var { data, calculator, labelFormatter } = instance;
61
- var { bounds } = data;
62
- let { CSS, baseClass } = this;
63
- var size = calculator.findTickSize(this.minLabelDistance);
64
-
65
- var labelClass = CSS.expand(CSS.element(baseClass, "label"), data.labelClass);
66
- var offsetClass = CSS.element(baseClass, "label-offset");
67
-
68
- var x1,
69
- y1,
70
- x2,
71
- y2,
72
- tickSize = this.tickSize;
73
-
74
- if (this.vertical) {
75
- x1 = x2 = this.secondary ? bounds.r : bounds.l;
76
- y1 = bounds.b;
77
- y2 = bounds.t;
78
- } else {
79
- x1 = bounds.l;
80
- x2 = bounds.r;
81
- y1 = y2 = this.secondary ? bounds.t : bounds.b;
82
- }
83
-
84
- var res = [null, null];
85
-
86
- if (!data.hideLine) {
87
- res[0] = (
88
- <line
89
- key="line"
90
- className={CSS.expand(CSS.element(baseClass, "line"), data.lineClass)}
91
- style={data.lineStyle}
92
- x1={x1}
93
- y1={y1}
94
- x2={x2}
95
- y2={y2}
96
- />
97
- );
98
- }
99
-
100
- var t = [];
101
- if (size > 0 && !data.hideLabels) {
102
- var ticks = calculator.getTicks([size]);
103
- ticks.forEach((serie, si) => {
104
- serie.forEach((v, i) => {
105
- var s = calculator.map(v);
106
-
107
- if (this.secondary) {
108
- x1 = this.vertical ? bounds.r : s;
109
- y1 = this.vertical ? s : bounds.t;
110
- x2 = this.vertical ? bounds.r + tickSize : s;
111
- y2 = this.vertical ? s : bounds.t - tickSize;
112
- } else {
113
- x1 = this.vertical ? bounds.l : s;
114
- y1 = this.vertical ? s : bounds.b;
115
- x2 = this.vertical ? bounds.l - tickSize : s;
116
- y2 = this.vertical ? s : bounds.b + tickSize;
117
- }
118
-
119
- t.push(`M ${x1} ${y1} L ${x2} ${y2}`);
120
-
121
- var x, y;
122
- if (this.secondary) {
123
- x = this.vertical ? bounds.r + this.labelOffset : s;
124
- y = this.vertical ? s : bounds.t - this.labelOffset;
125
- } else {
126
- x = this.vertical ? bounds.l - this.labelOffset : s;
127
- y = this.vertical ? s : bounds.b + this.labelOffset;
128
- }
129
-
130
- var transform = data.labelRotation ? `rotate(${data.labelRotation} ${x} ${y})` : null;
131
- var formattedValue = valueFormatter(v);
132
- var lines = labelFormatter ? labelFormatter(formattedValue, v) : this.wrapLines(formattedValue);
133
- res.push(
134
- <text
135
- key={`label-${si}-${i}`}
136
- className={labelClass}
137
- style={data.labelStyle}
138
- x={x}
139
- y={y}
140
- textAnchor={data.labelAnchor}
141
- transform={transform}
142
- >
143
- {this.renderLabels(lines, x, this.labelDy, this.labelDx, offsetClass)}
144
- </text>
145
- );
146
- });
147
- });
148
- }
149
-
150
- if (!data.hideTicks) {
151
- res[1] = (
152
- <path
153
- key="ticks"
154
- className={CSS.expand(CSS.element(baseClass, "ticks"), data.tickClass)}
155
- style={data.tickStyle}
156
- d={t.join(" ")}
157
- />
158
- );
159
- }
160
-
161
- return res;
162
- }
163
-
164
- wrapLines(str) {
165
- if (!this.labelWrap || typeof str != "string") return [{ text: str }];
166
-
167
- let parts = str.split(" ");
168
- if (parts.length == 0) return null;
169
-
170
- let lines = [];
171
- let line = null;
172
- for (let i = 0; i < parts.length; i++) {
173
- if (!line) line = parts[i];
174
- else if (parts[i].length + line.length < this.labelMaxLineLength) line += " " + parts[i];
175
- else {
176
- lines.push({ text: line });
177
- line = parts[i];
178
- }
179
- }
180
- lines.push({ text: line });
181
- return lines;
182
- }
183
-
184
- renderLabels(lines, x, dy, dx, offsetClass) {
185
- let offset = this.labelLineCountDyFactor * (lines.length - 1);
186
- let result = [];
187
-
188
- if (lines.length > 1 && dy != null) {
189
- result.push(
190
- <tspan key={-2} className={offsetClass} dy={dy}>
191
- _
192
- </tspan>
193
- );
194
- }
195
-
196
- lines.forEach((p, i) => {
197
- result.push(
198
- <tspan
199
- key={i}
200
- dy={lines.length > 1 ? `${i == 0 ? offset : this.labelLineHeight}em` : dy}
201
- x={x}
202
- style={p.style}
203
- className={p.className}
204
- dx={dx}
205
- >
206
- {p.text}
207
- </tspan>
208
- );
209
- });
210
- return result;
211
- }
212
-
213
- prepare(context, instance) {
214
- super.prepare(context, instance);
215
- var { bounds } = instance.data;
216
- var [a, b] = !this.vertical ? [bounds.l, bounds.r] : [bounds.b, bounds.t];
217
- instance.calculator.measure(a, b);
218
- if (this.onMeasured) instance.invoke("onMeasured", instance.calculator.hash(), instance);
219
- if (!instance.calculator.isSame(instance.cached.axis)) instance.markShouldUpdate(context);
220
- }
221
-
222
- cleanup(context, instance) {
223
- var { cached, calculator } = instance;
224
- cached.axis = calculator.hash();
225
- }
226
- }
227
-
228
- Axis.prototype.anchors = "0 1 1 0";
229
- Axis.prototype.styled = true;
230
- Axis.prototype.vertical = false;
231
- Axis.prototype.secondary = false;
232
- Axis.prototype.inverted = false;
233
- Axis.prototype.hidden = false;
234
- Axis.prototype.hideLabels = false;
235
- Axis.prototype.hideTicks = false;
236
- Axis.prototype.hideLine = false;
237
-
238
- Axis.prototype.tickSize = 3;
239
- Axis.prototype.minTickDistance = 25;
240
- Axis.prototype.minLabelDistanceVertical = 40;
241
- Axis.prototype.minLabelDistanceHorizontal = 50;
242
- Axis.prototype.labelOffset = 10;
243
- Axis.prototype.labelRotation = 0;
244
- Axis.prototype.labelAnchor = "auto";
245
- Axis.prototype.labelDx = "auto";
246
- Axis.prototype.labelDy = "auto";
247
- Axis.prototype.labelWrap = false;
248
- Axis.prototype.labelLineCountDyFactor = "auto";
249
- Axis.prototype.labelLineHeight = 1;
250
- Axis.prototype.labelMaxLineLength = 10;
251
-
252
- Axis.namespace = "ui.svg.chart.axis";
1
+ import { BoundedObject } from "../../svg/BoundedObject";
2
+ import { VDOM } from "../../ui/Widget";
3
+ import { isUndefined } from "../../util/isUndefined";
4
+ import { parseStyle } from "../../util/parseStyle";
5
+
6
+ export class Axis extends BoundedObject {
7
+ init() {
8
+ if (this.labelAnchor == "auto") this.labelAnchor = this.vertical ? (this.secondary ? "start" : "end") : "middle";
9
+
10
+ if (this.labelDx == "auto") this.labelDx = 0;
11
+
12
+ if (this.labelDy == "auto") this.labelDy = this.vertical ? "0.4em" : this.secondary ? 0 : "0.8em";
13
+
14
+ if (isUndefined(this.minLabelDistance))
15
+ this.minLabelDistance = this.vertical ? this.minLabelDistanceVertical : this.minLabelDistanceHorizontal;
16
+
17
+ if (this.labelLineCountDyFactor == "auto")
18
+ this.labelLineCountDyFactor = this.vertical ? -this.labelLineHeight / 2 : this.secondary ? -1 : 0;
19
+
20
+ this.lineStyle = parseStyle(this.lineStyle);
21
+ this.tickStyle = parseStyle(this.tickStyle);
22
+ this.labelStyle = parseStyle(this.labelStyle);
23
+
24
+ super.init();
25
+ }
26
+
27
+ declareData() {
28
+ super.declareData(
29
+ {
30
+ anchors: undefined,
31
+ hideLabels: undefined,
32
+ hideLine: undefined,
33
+ hideTicks: undefined,
34
+ labelRotation: undefined,
35
+ labelAnchor: undefined,
36
+ lineStyle: undefined,
37
+ lineClass: undefined,
38
+ labelStyle: undefined,
39
+ labelClass: undefined,
40
+ tickStyle: undefined,
41
+ tickClass: undefined,
42
+ },
43
+ ...arguments,
44
+ );
45
+ }
46
+
47
+ prepareData(context, instance) {
48
+ super.prepareData(context, instance);
49
+ if (this.onCreateLabelFormatter)
50
+ instance.labelFormatter = instance.invoke("onCreateLabelFormatter", context, instance);
51
+ }
52
+
53
+ report(context, instance) {
54
+ return instance.calculator;
55
+ }
56
+
57
+ reportData(context, instance) {}
58
+
59
+ renderTicksAndLabels(context, instance, valueFormatter) {
60
+ if (this.hidden) return false;
61
+
62
+ var { data, calculator, labelFormatter } = instance;
63
+ var { bounds } = data;
64
+ let { CSS, baseClass } = this;
65
+ var size = calculator.findTickSize(this.minLabelDistance);
66
+
67
+ var labelClass = CSS.expand(CSS.element(baseClass, "label"), data.labelClass);
68
+ var offsetClass = CSS.element(baseClass, "label-offset");
69
+
70
+ var x1,
71
+ y1,
72
+ x2,
73
+ y2,
74
+ tickSize = this.tickSize;
75
+
76
+ if (this.vertical) {
77
+ x1 = x2 = this.secondary ? bounds.r : bounds.l;
78
+ y1 = bounds.b;
79
+ y2 = bounds.t;
80
+ } else {
81
+ x1 = bounds.l;
82
+ x2 = bounds.r;
83
+ y1 = y2 = this.secondary ? bounds.t : bounds.b;
84
+ }
85
+
86
+ var res = [null, null];
87
+
88
+ if (!data.hideLine) {
89
+ res[0] = (
90
+ <line
91
+ key="line"
92
+ className={CSS.expand(CSS.element(baseClass, "line"), data.lineClass)}
93
+ style={data.lineStyle}
94
+ x1={x1}
95
+ y1={y1}
96
+ x2={x2}
97
+ y2={y2}
98
+ />
99
+ );
100
+ }
101
+
102
+ var t = [];
103
+ if (size > 0 && !data.hideLabels) {
104
+ var ticks = calculator.getTicks([size]);
105
+ ticks.forEach((serie, si) => {
106
+ serie.forEach((v, i) => {
107
+ var s = calculator.map(v);
108
+
109
+ if (this.secondary) {
110
+ x1 = this.vertical ? bounds.r : s;
111
+ y1 = this.vertical ? s : bounds.t;
112
+ x2 = this.vertical ? bounds.r + tickSize : s;
113
+ y2 = this.vertical ? s : bounds.t - tickSize;
114
+ } else {
115
+ x1 = this.vertical ? bounds.l : s;
116
+ y1 = this.vertical ? s : bounds.b;
117
+ x2 = this.vertical ? bounds.l - tickSize : s;
118
+ y2 = this.vertical ? s : bounds.b + tickSize;
119
+ }
120
+
121
+ t.push(`M ${x1} ${y1} L ${x2} ${y2}`);
122
+
123
+ var x, y;
124
+ if (this.secondary) {
125
+ x = this.vertical ? bounds.r + this.labelOffset : s;
126
+ y = this.vertical ? s : bounds.t - this.labelOffset;
127
+ } else {
128
+ x = this.vertical ? bounds.l - this.labelOffset : s;
129
+ y = this.vertical ? s : bounds.b + this.labelOffset;
130
+ }
131
+
132
+ var transform = data.labelRotation ? `rotate(${data.labelRotation} ${x} ${y})` : null;
133
+ var formattedValue = valueFormatter(v);
134
+ var lines = labelFormatter ? labelFormatter(formattedValue, v) : this.wrapLines(formattedValue);
135
+ res.push(
136
+ <text
137
+ key={`label-${si}-${i}`}
138
+ className={labelClass}
139
+ style={data.labelStyle}
140
+ x={x}
141
+ y={y}
142
+ textAnchor={data.labelAnchor}
143
+ transform={transform}
144
+ >
145
+ {this.renderLabels(lines, x, this.labelDy, this.labelDx, offsetClass)}
146
+ </text>,
147
+ );
148
+ });
149
+ });
150
+ }
151
+
152
+ if (!data.hideTicks) {
153
+ res[1] = (
154
+ <path
155
+ key="ticks"
156
+ className={CSS.expand(CSS.element(baseClass, "ticks"), data.tickClass)}
157
+ style={data.tickStyle}
158
+ d={t.join(" ")}
159
+ />
160
+ );
161
+ }
162
+
163
+ return res;
164
+ }
165
+
166
+ wrapLines(str) {
167
+ if (!this.labelWrap || typeof str != "string") return [{ text: str }];
168
+
169
+ let parts = str.split(" ");
170
+ if (parts.length == 0) return null;
171
+
172
+ let lines = [];
173
+ let line = null;
174
+ for (let i = 0; i < parts.length; i++) {
175
+ if (!line) line = parts[i];
176
+ else if (parts[i].length + line.length < this.labelMaxLineLength) line += " " + parts[i];
177
+ else {
178
+ lines.push({ text: line });
179
+ line = parts[i];
180
+ }
181
+ }
182
+ lines.push({ text: line });
183
+ return lines;
184
+ }
185
+
186
+ renderLabels(lines, x, dy, dx, offsetClass) {
187
+ let offset = this.labelLineCountDyFactor * (lines.length - 1);
188
+ let result = [];
189
+
190
+ if (lines.length > 1 && dy != null) {
191
+ result.push(
192
+ <tspan key={-2} className={offsetClass} dy={dy}>
193
+ _
194
+ </tspan>,
195
+ );
196
+ }
197
+
198
+ lines.forEach((p, i) => {
199
+ result.push(
200
+ <tspan
201
+ key={i}
202
+ dy={lines.length > 1 ? `${i == 0 ? offset : this.labelLineHeight}em` : dy}
203
+ x={x}
204
+ style={p.style}
205
+ className={p.className}
206
+ dx={dx}
207
+ >
208
+ {p.text}
209
+ </tspan>,
210
+ );
211
+ });
212
+ return result;
213
+ }
214
+
215
+ prepare(context, instance) {
216
+ super.prepare(context, instance);
217
+ var { bounds } = instance.data;
218
+ var [a, b] = !this.vertical ? [bounds.l, bounds.r] : [bounds.b, bounds.t];
219
+ instance.calculator.measure(a, b);
220
+ if (this.onMeasured) instance.invoke("onMeasured", instance.calculator.hash(), instance);
221
+ if (!instance.calculator.isSame(instance.cached.axis)) instance.markShouldUpdate(context);
222
+ }
223
+
224
+ cleanup(context, instance) {
225
+ var { cached, calculator } = instance;
226
+ cached.axis = calculator.hash();
227
+ }
228
+ }
229
+
230
+ Axis.prototype.anchors = "0 1 1 0";
231
+ Axis.prototype.styled = true;
232
+ Axis.prototype.vertical = false;
233
+ Axis.prototype.secondary = false;
234
+ Axis.prototype.inverted = false;
235
+ Axis.prototype.hidden = false;
236
+ Axis.prototype.hideLabels = false;
237
+ Axis.prototype.hideTicks = false;
238
+ Axis.prototype.hideLine = false;
239
+
240
+ Axis.prototype.tickSize = 3;
241
+ Axis.prototype.minTickDistance = 25;
242
+ Axis.prototype.minLabelDistanceVertical = 40;
243
+ Axis.prototype.minLabelDistanceHorizontal = 50;
244
+ Axis.prototype.labelOffset = 10;
245
+ Axis.prototype.labelRotation = 0;
246
+ Axis.prototype.labelAnchor = "auto";
247
+ Axis.prototype.labelDx = "auto";
248
+ Axis.prototype.labelDy = "auto";
249
+ Axis.prototype.labelWrap = false;
250
+ Axis.prototype.labelLineCountDyFactor = "auto";
251
+ Axis.prototype.labelLineHeight = 1;
252
+ Axis.prototype.labelMaxLineLength = 10;
253
+
254
+ Axis.namespace = "ui.svg.chart.axis";
@@ -1,22 +1,24 @@
1
- import * as Cx from '../../core';
2
- import {AxisProps} from './Axis';
1
+ import * as Cx from "../../core";
2
+ import { AxisProps } from "./Axis";
3
3
 
4
4
  interface CategoryAxisProps extends AxisProps {
5
-
6
5
  /** Uniform axes provide exact size and offset for all entries, while non-uniform axes adapt their size and offset to the number of entries under each category. */
7
- uniform?: Cx.BooleanProp,
6
+ uniform?: Cx.BooleanProp;
8
7
 
9
8
  /** Names corresponding the given `values`. For example, `values` may be 0 .. 11 and `names` could be Jan .. Dec. */
10
- names?: Cx.Prop<any[] | Cx.Config>,
9
+ names?: Cx.Prop<any[] | Cx.Config>;
11
10
 
12
11
  /** Values used to initialize the axis. If an object is provided, keys are used for values and values are used for names. */
13
- values?: Cx.Prop<any[] | Cx.Config>,
12
+ values?: Cx.Prop<any[] | Cx.Config>;
14
13
 
15
14
  /** Sometimes, there is not enough data and each category takes a lot of space. `minSize` can be used to add fake entries up to the specified number, so everything looks normal. */
16
- minSize?: Cx.NumberProp,
15
+ minSize?: Cx.NumberProp;
17
16
 
18
17
  /** Base CSS class to be applied to the element. Defaults to `categoryaxis`. */
19
- baseClass?: string
18
+ baseClass?: string;
19
+
20
+ /** Output value that can be used to calculate chart dimensions based on discovered category values. */
21
+ categoryCount?: Binding | Cx.AccessorChain<number> | Cx.GetSet<number>;
20
22
  }
21
23
 
22
- export class CategoryAxis extends Cx.Widget<CategoryAxisProps> {}
24
+ export class CategoryAxis extends Cx.Widget<CategoryAxisProps> {}