@g9tm/lynxcontrols-charts 0.0.0-stage → 2.2.0
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.
- package/README.md +13 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/charts.css +181 -0
- package/dist/charts.css.map +1 -0
- package/dist/charts.d.ts +158 -0
- package/dist/charts.d.ts.map +1 -0
- package/dist/charts.jsx +424 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +2 -0
- package/dist/svg/builders.d.ts +124 -0
- package/dist/svg/builders.d.ts.map +1 -0
- package/dist/svg/builders.jsx +354 -0
- package/dist/svg/index.d.ts +3 -0
- package/dist/svg/index.d.ts.map +1 -0
- package/dist/svg/index.jsx +2 -0
- package/dist/svg/scale.d.ts +44 -0
- package/dist/svg/scale.d.ts.map +1 -0
- package/dist/svg/scale.jsx +82 -0
- package/package.json +34 -4
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { arcPath, esc, f, g9ChartInsets, polar, svgDoc, xSlot, yScale } from "./scale.jsx";
|
|
2
|
+
function builders_seg(x1, y1, x2, y2, attrs) {
|
|
3
|
+
return `<path d="M${f(x1)} ${f(y1)} L${f(x2)} ${f(y2)}" ${attrs}/>`;
|
|
4
|
+
}
|
|
5
|
+
function gridLines(ticks, w, inset, color) {
|
|
6
|
+
if (!color) return '';
|
|
7
|
+
return ticks.map((t)=>builders_seg(inset.left, t.y, w - inset.right, t.y, `stroke="${color}" stroke-width="1"`)).join('');
|
|
8
|
+
}
|
|
9
|
+
function lineChart(w, h, series, o = {}) {
|
|
10
|
+
const inset = {
|
|
11
|
+
...g9ChartInsets,
|
|
12
|
+
...o.insets
|
|
13
|
+
};
|
|
14
|
+
const n = Math.max(0, ...series.map((s)=>s.values.length));
|
|
15
|
+
const all = series.flatMap((s)=>s.values.filter((v)=>null !== v));
|
|
16
|
+
const sc = yScale(all, h, {
|
|
17
|
+
insets: inset,
|
|
18
|
+
...o.ticks ? {
|
|
19
|
+
ticks: o.ticks
|
|
20
|
+
} : {},
|
|
21
|
+
...void 0 !== o.min ? {
|
|
22
|
+
min: o.min
|
|
23
|
+
} : {},
|
|
24
|
+
...void 0 !== o.max ? {
|
|
25
|
+
max: o.max
|
|
26
|
+
} : {},
|
|
27
|
+
...void 0 !== o.zero ? {
|
|
28
|
+
zero: o.zero
|
|
29
|
+
} : {}
|
|
30
|
+
});
|
|
31
|
+
const xs = Array.from({
|
|
32
|
+
length: n
|
|
33
|
+
}, (_, i)=>xSlot(i, n, w, inset));
|
|
34
|
+
let body = gridLines(sc.ticks, w, inset, o.grid);
|
|
35
|
+
for (const s of series){
|
|
36
|
+
var _s_dots;
|
|
37
|
+
const segs = [
|
|
38
|
+
[]
|
|
39
|
+
];
|
|
40
|
+
s.values.forEach((v, i)=>{
|
|
41
|
+
if (null === v || !Number.isFinite(v)) {
|
|
42
|
+
if (segs[segs.length - 1].length) segs.push([]);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
segs[segs.length - 1].push(`${f(xs[i])} ${f(sc.y(v))}`);
|
|
46
|
+
});
|
|
47
|
+
for (const seg of segs.filter((x)=>x.length)){
|
|
48
|
+
var _s_width;
|
|
49
|
+
const d = `M${seg.join('L')}`;
|
|
50
|
+
if (s.area && seg.length > 1) {
|
|
51
|
+
var _seg_, _seg_1;
|
|
52
|
+
const first = null == (_seg_ = seg[0]) ? void 0 : _seg_.split(' ')[0];
|
|
53
|
+
const last = null == (_seg_1 = seg[seg.length - 1]) ? void 0 : _seg_1.split(' ')[0];
|
|
54
|
+
body += `<path d="${d}L${last} ${f(sc.y(Math.max(sc.min, 0)))}L${first} ${f(sc.y(Math.max(sc.min, 0)))}Z" fill="${s.color}" fill-opacity="${s.area}" stroke="none"/>`;
|
|
55
|
+
}
|
|
56
|
+
body += `<path d="${d}" fill="none" stroke="${s.color}" stroke-width="${null != (_s_width = s.width) ? _s_width : 2}" stroke-linecap="round" stroke-linejoin="round"${s.dashed ? ' stroke-dasharray="5 4"' : ''}/>`;
|
|
57
|
+
}
|
|
58
|
+
const dots = null != (_s_dots = s.dots) ? _s_dots : 'last';
|
|
59
|
+
s.values.forEach((v, i)=>{
|
|
60
|
+
if (null === v || 'none' === dots || 'last' === dots && i !== s.values.length - 1) return;
|
|
61
|
+
body += `<circle cx="${f(xs[i])}" cy="${f(sc.y(v))}" r="3" fill="${s.color}"/>`;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
svg: svgDoc(w, h, body),
|
|
66
|
+
ticks: sc.ticks,
|
|
67
|
+
xs,
|
|
68
|
+
width: w,
|
|
69
|
+
height: h,
|
|
70
|
+
y: sc.y
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function barChart(w, h, values, o) {
|
|
74
|
+
var _o_band, _o_radius;
|
|
75
|
+
const inset = {
|
|
76
|
+
...g9ChartInsets,
|
|
77
|
+
...o.insets
|
|
78
|
+
};
|
|
79
|
+
const sc = yScale(values, h, {
|
|
80
|
+
insets: inset,
|
|
81
|
+
...o.ticks ? {
|
|
82
|
+
ticks: o.ticks
|
|
83
|
+
} : {},
|
|
84
|
+
...void 0 !== o.max ? {
|
|
85
|
+
max: o.max
|
|
86
|
+
} : {}
|
|
87
|
+
});
|
|
88
|
+
const n = values.length;
|
|
89
|
+
const xs = values.map((_, i)=>xSlot(i, n, w, inset, true));
|
|
90
|
+
const slot = (w - inset.left - inset.right) / Math.max(1, n);
|
|
91
|
+
const bw = slot * (null != (_o_band = o.band) ? _o_band : 0.62);
|
|
92
|
+
const r = Math.min(null != (_o_radius = o.radius) ? _o_radius : 4, bw / 2);
|
|
93
|
+
const zero = sc.y(0);
|
|
94
|
+
let body = gridLines(sc.ticks, w, inset, o.grid);
|
|
95
|
+
values.forEach((v, i)=>{
|
|
96
|
+
const top = Math.min(sc.y(v), zero);
|
|
97
|
+
const height = Math.max(0.5, Math.abs(sc.y(v) - zero));
|
|
98
|
+
const color = i === o.highlightIndex && o.highlight ? o.highlight : 'string' == typeof o.color ? o.color : o.color[i % o.color.length];
|
|
99
|
+
body += `<rect x="${f(xs[i] - bw / 2)}" y="${f(top)}" width="${f(bw)}" height="${f(height)}" rx="${f(r)}" fill="${color}"/>`;
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
svg: svgDoc(w, h, body),
|
|
103
|
+
ticks: sc.ticks,
|
|
104
|
+
xs,
|
|
105
|
+
width: w,
|
|
106
|
+
height: h,
|
|
107
|
+
y: sc.y
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function stackedBarChart(w, h, stacks, colors, o = {}) {
|
|
111
|
+
var _o_band;
|
|
112
|
+
const inset = {
|
|
113
|
+
...g9ChartInsets,
|
|
114
|
+
...o.insets
|
|
115
|
+
};
|
|
116
|
+
const totals = stacks.map((s)=>s.reduce((a, b)=>a + Math.max(0, b), 0));
|
|
117
|
+
const sc = yScale(totals, h, {
|
|
118
|
+
insets: inset,
|
|
119
|
+
...o.ticks ? {
|
|
120
|
+
ticks: o.ticks
|
|
121
|
+
} : {}
|
|
122
|
+
});
|
|
123
|
+
const n = stacks.length;
|
|
124
|
+
const xs = stacks.map((_, i)=>xSlot(i, n, w, inset, true));
|
|
125
|
+
const bw = (w - inset.left - inset.right) / Math.max(1, n) * (null != (_o_band = o.band) ? _o_band : 0.62);
|
|
126
|
+
let body = gridLines(sc.ticks, w, inset, o.grid);
|
|
127
|
+
stacks.forEach((parts, i)=>{
|
|
128
|
+
let acc = 0;
|
|
129
|
+
parts.forEach((p, j)=>{
|
|
130
|
+
if (p <= 0) return;
|
|
131
|
+
const y1 = sc.y(acc);
|
|
132
|
+
const y2 = sc.y(acc + p);
|
|
133
|
+
acc += p;
|
|
134
|
+
body += `<rect x="${f(xs[i] - bw / 2)}" y="${f(y2)}" width="${f(bw)}" height="${f(Math.max(0.5, y1 - y2))}" fill="${colors[j % colors.length]}"/>`;
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
svg: svgDoc(w, h, body),
|
|
139
|
+
ticks: sc.ticks,
|
|
140
|
+
xs,
|
|
141
|
+
width: w,
|
|
142
|
+
height: h,
|
|
143
|
+
y: sc.y
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function sparkline(w, h, values, color, o = {}) {
|
|
147
|
+
var _o_dim, _o_mark, _o_area;
|
|
148
|
+
if (0 === values.length) return svgDoc(w, h, '');
|
|
149
|
+
if ('bars' === o.kind) return barChart(w, h, values, {
|
|
150
|
+
color: null != (_o_dim = o.dim) ? _o_dim : color,
|
|
151
|
+
highlightIndex: null != (_o_mark = o.mark) ? _o_mark : values.length - 1,
|
|
152
|
+
highlight: color,
|
|
153
|
+
band: 0.7,
|
|
154
|
+
radius: 1.5,
|
|
155
|
+
insets: {
|
|
156
|
+
top: 2,
|
|
157
|
+
bottom: 2,
|
|
158
|
+
left: 1,
|
|
159
|
+
right: 1
|
|
160
|
+
},
|
|
161
|
+
zero: true
|
|
162
|
+
}).svg;
|
|
163
|
+
return lineChart(w, h, [
|
|
164
|
+
{
|
|
165
|
+
values,
|
|
166
|
+
color,
|
|
167
|
+
width: 1.8,
|
|
168
|
+
area: null != (_o_area = o.area) ? _o_area : 0.14,
|
|
169
|
+
dots: 'last'
|
|
170
|
+
}
|
|
171
|
+
], {
|
|
172
|
+
insets: {
|
|
173
|
+
top: 3,
|
|
174
|
+
bottom: 3,
|
|
175
|
+
left: 2,
|
|
176
|
+
right: 4
|
|
177
|
+
},
|
|
178
|
+
zero: false
|
|
179
|
+
}).svg;
|
|
180
|
+
}
|
|
181
|
+
function donut(size, thickness, slices, o = {}) {
|
|
182
|
+
var _o_gapDeg;
|
|
183
|
+
const total = slices.reduce((a, s)=>a + Math.max(0, s.value), 0);
|
|
184
|
+
const c = size / 2;
|
|
185
|
+
const r = (size - thickness) / 2;
|
|
186
|
+
const gap = slices.filter((s)=>s.value > 0).length > 1 ? null != (_o_gapDeg = o.gapDeg) ? _o_gapDeg : 2 : 0;
|
|
187
|
+
let body = o.track ? `<circle cx="${f(c)}" cy="${f(c)}" r="${f(r)}" fill="none" stroke="${o.track}" stroke-width="${f(thickness)}"/>` : '';
|
|
188
|
+
const shares = [];
|
|
189
|
+
const mids = [];
|
|
190
|
+
let a = 0;
|
|
191
|
+
for (const s of slices){
|
|
192
|
+
const share = total > 0 ? Math.max(0, s.value) / total : 0;
|
|
193
|
+
shares.push(share);
|
|
194
|
+
const sweep = 360 * share;
|
|
195
|
+
mids.push(a + sweep / 2);
|
|
196
|
+
if (share > 0) if (share >= 0.9999) body += `<circle cx="${f(c)}" cy="${f(c)}" r="${f(r)}" fill="none" stroke="${s.color}" stroke-width="${f(thickness)}"/>`;
|
|
197
|
+
else body += `<path d="${arcPath(c, c, r, a + gap / 2, a + sweep - gap / 2)}" fill="none" stroke="${s.color}" stroke-width="${f(thickness)}" stroke-linecap="butt"/>`;
|
|
198
|
+
a += sweep;
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
svg: svgDoc(size, size, body),
|
|
202
|
+
shares,
|
|
203
|
+
mids
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function heatmap(w, h, grid, color, empty, gap = 2) {
|
|
207
|
+
const rows = grid.length;
|
|
208
|
+
const cols = Math.max(0, ...grid.map((r)=>r.length));
|
|
209
|
+
const max = Math.max(0, ...grid.flat());
|
|
210
|
+
const cw = (w - gap * (cols - 1)) / Math.max(1, cols);
|
|
211
|
+
const ch = (h - gap * (rows - 1)) / Math.max(1, rows);
|
|
212
|
+
let body = '';
|
|
213
|
+
for (const [r, row] of grid.entries())for (const [c, v] of row.entries()){
|
|
214
|
+
const t = max > 0 ? v / max : 0;
|
|
215
|
+
const fill = v > 0 ? `${color}" fill-opacity="${f(0.15 + 0.85 * t)}` : empty;
|
|
216
|
+
body += `<rect x="${f(c * (cw + gap))}" y="${f(r * (ch + gap))}" width="${f(cw)}" height="${f(ch)}" rx="2" fill="${fill}"/>`;
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
svg: svgDoc(w, h, body),
|
|
220
|
+
max
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function gauge(size, thickness, value, o) {
|
|
224
|
+
var _o_segments, _o_needle;
|
|
225
|
+
const c = size / 2;
|
|
226
|
+
const r = (size - thickness) / 2 - 2;
|
|
227
|
+
const from = -120;
|
|
228
|
+
const to = 120;
|
|
229
|
+
const n = null != (_o_segments = o.segments) ? _o_segments : 48;
|
|
230
|
+
let body = `<path d="${arcPath(c, c, r, from, to)}" fill="none" stroke="${o.track}" stroke-width="${f(thickness)}" stroke-linecap="round"/>`;
|
|
231
|
+
const v = Math.min(1, Math.max(0, value));
|
|
232
|
+
for(let i = 0; i < n; i++){
|
|
233
|
+
const t0 = i / n;
|
|
234
|
+
if (t0 >= v) break;
|
|
235
|
+
const t1 = Math.min(v, (i + 1) / n);
|
|
236
|
+
body += `<path d="${arcPath(c, c, r, from + 240 * t0, from + 240 * t1 + 0.4)}" fill="none" stroke="${o.colorAt((t0 + t1) / 2)}" stroke-width="${f(thickness)}"/>`;
|
|
237
|
+
}
|
|
238
|
+
const [mx, my] = polar(c, c, r, from + 240 * v);
|
|
239
|
+
body += `<circle cx="${f(mx)}" cy="${f(my)}" r="${f(0.75 * thickness)}" fill="${null != (_o_needle = o.needle) ? _o_needle : o.colorAt(v)}" stroke="#FFFFFF" stroke-width="2"/>`;
|
|
240
|
+
return svgDoc(size, size, body);
|
|
241
|
+
}
|
|
242
|
+
function radar(size, values, color, grid, pad = 34) {
|
|
243
|
+
const c = size / 2;
|
|
244
|
+
const r = c - pad;
|
|
245
|
+
const n = values.length;
|
|
246
|
+
const ring = (k)=>Array.from({
|
|
247
|
+
length: n
|
|
248
|
+
}, (_, i)=>polar(c, c, r * k, 360 / n * i).map(f).join(' ')).join(' ');
|
|
249
|
+
let body = [
|
|
250
|
+
0.25,
|
|
251
|
+
0.5,
|
|
252
|
+
0.75,
|
|
253
|
+
1
|
|
254
|
+
].map((k)=>`<polygon points="${ring(k)}" fill="none" stroke="${grid}" stroke-width="1"/>`).join('');
|
|
255
|
+
const axes = Array.from({
|
|
256
|
+
length: n
|
|
257
|
+
}, (_, i)=>polar(c, c, r + 16, 360 / n * i));
|
|
258
|
+
for(let i = 0; i < n; i++){
|
|
259
|
+
const [x, y] = polar(c, c, r, 360 / n * i);
|
|
260
|
+
body += builders_seg(c, c, x, y, `stroke="${grid}" stroke-width="1"`);
|
|
261
|
+
}
|
|
262
|
+
const shape = values.map((v, i)=>polar(c, c, r * Math.min(1, Math.max(0, v)), 360 / n * i).map(f).join(' ')).join(' ');
|
|
263
|
+
body += `<polygon points="${shape}" fill="${color}" fill-opacity="0.22" stroke="${color}" stroke-width="2" stroke-linejoin="round"/>`;
|
|
264
|
+
return {
|
|
265
|
+
svg: svgDoc(size, size, body),
|
|
266
|
+
axes
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function fanChart(w, h, points, color, grid) {
|
|
270
|
+
const inset = {
|
|
271
|
+
...g9ChartInsets
|
|
272
|
+
};
|
|
273
|
+
const all = points.flatMap((p)=>[
|
|
274
|
+
p.p10,
|
|
275
|
+
p.p50,
|
|
276
|
+
p.p90
|
|
277
|
+
]);
|
|
278
|
+
const sc = yScale(all, h, {
|
|
279
|
+
insets: inset,
|
|
280
|
+
zero: false
|
|
281
|
+
});
|
|
282
|
+
const xs = points.map((_, i)=>xSlot(i, points.length, w, inset));
|
|
283
|
+
const up = points.map((p, i)=>`${f(xs[i])} ${f(sc.y(p.p90))}`);
|
|
284
|
+
const down = points.map((p, i)=>`${f(xs[i])} ${f(sc.y(p.p10))}`).reverse();
|
|
285
|
+
let body = gridLines(sc.ticks, w, inset, grid);
|
|
286
|
+
if (points.length > 1) body += `<path d="M${[
|
|
287
|
+
...up,
|
|
288
|
+
...down
|
|
289
|
+
].join('L')}Z" fill="${color}" fill-opacity="0.16"/>`;
|
|
290
|
+
body += `<path d="M${points.map((p, i)=>`${f(xs[i])} ${f(sc.y(p.p50))}`).join('L')}" fill="none" stroke="${color}" stroke-width="2"/>`;
|
|
291
|
+
return {
|
|
292
|
+
svg: svgDoc(w, h, body),
|
|
293
|
+
ticks: sc.ticks,
|
|
294
|
+
xs,
|
|
295
|
+
width: w,
|
|
296
|
+
height: h,
|
|
297
|
+
y: sc.y
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function candleChart(w, h, candles, o) {
|
|
301
|
+
const inset = {
|
|
302
|
+
...g9ChartInsets,
|
|
303
|
+
right: 8
|
|
304
|
+
};
|
|
305
|
+
const all = candles.flatMap((c)=>[
|
|
306
|
+
c.high,
|
|
307
|
+
c.low
|
|
308
|
+
]);
|
|
309
|
+
const sc = yScale(all, h, {
|
|
310
|
+
insets: inset,
|
|
311
|
+
zero: false
|
|
312
|
+
});
|
|
313
|
+
const n = candles.length;
|
|
314
|
+
const xs = candles.map((_, i)=>xSlot(i, n, w, inset, true));
|
|
315
|
+
const bw = Math.max(1, (w - inset.left - inset.right) / Math.max(1, n) * 0.6);
|
|
316
|
+
let body = gridLines(sc.ticks, w, inset, o.grid);
|
|
317
|
+
candles.forEach((c, i)=>{
|
|
318
|
+
const color = c.close >= c.open ? o.up : o.down;
|
|
319
|
+
const x = xs[i];
|
|
320
|
+
body += builders_seg(x, sc.y(c.high), x, sc.y(c.low), `stroke="${color}" stroke-width="1"`);
|
|
321
|
+
const top = sc.y(Math.max(c.open, c.close));
|
|
322
|
+
body += `<rect x="${f(x - bw / 2)}" y="${f(top)}" width="${f(bw)}" height="${f(Math.max(1, Math.abs(sc.y(c.open) - sc.y(c.close))))}" fill="${color}"/>`;
|
|
323
|
+
});
|
|
324
|
+
const last = n ? candles[n - 1].close : null;
|
|
325
|
+
if (null !== last && o.lastLine) body += builders_seg(inset.left, sc.y(last), w - inset.right, sc.y(last), `stroke="${o.lastLine}" stroke-width="1" stroke-dasharray="3 3"`);
|
|
326
|
+
return {
|
|
327
|
+
svg: svgDoc(w, h, body),
|
|
328
|
+
ticks: sc.ticks,
|
|
329
|
+
xs,
|
|
330
|
+
width: w,
|
|
331
|
+
height: h,
|
|
332
|
+
y: sc.y,
|
|
333
|
+
last
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function matrixChart(w, h, points, o) {
|
|
337
|
+
const xsAll = points.map((p)=>p.x);
|
|
338
|
+
const ysAll = points.map((p)=>p.y);
|
|
339
|
+
const pad = 12;
|
|
340
|
+
const xMax = Math.max(2 * o.xLine, ...xsAll, 1);
|
|
341
|
+
const yMin = Math.min(0, ...ysAll);
|
|
342
|
+
const yMax = Math.max(2 * o.yLine, ...ysAll, 1);
|
|
343
|
+
const X = (v)=>pad + v / xMax * (w - 2 * pad);
|
|
344
|
+
const Y = (v)=>h - pad - (v - yMin) / (yMax - yMin) * (h - 2 * pad);
|
|
345
|
+
let body = builders_seg(X(o.xLine), pad, X(o.xLine), h - pad, `stroke="${o.line}" stroke-dasharray="4 4"`);
|
|
346
|
+
body += builders_seg(pad, Y(o.yLine), w - pad, Y(o.yLine), `stroke="${o.line}" stroke-dasharray="4 4"`);
|
|
347
|
+
for (const p of points){
|
|
348
|
+
var _o_radius;
|
|
349
|
+
body += `<circle cx="${f(X(p.x))}" cy="${f(Y(p.y))}" r="${null != (_o_radius = o.radius) ? _o_radius : 5}" fill="${p.color}" fill-opacity="0.85"/>`;
|
|
350
|
+
if (p.label) body += `<text x="${f(X(p.x) + 7)}" y="${f(Y(p.y) + 4)}" font-size="10" fill="${p.color}">${esc(p.label)}</text>`;
|
|
351
|
+
}
|
|
352
|
+
return svgDoc(w, h, body);
|
|
353
|
+
}
|
|
354
|
+
export { barChart, candleChart, donut, fanChart, gauge, heatmap, lineChart, matrixChart, radar, sparkline, stackedBarChart };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { barChart, candleChart, donut, fanChart, type G9BarOptions, type G9BuiltChart, type G9Candle, type G9GridOptions, type G9LineSeries, type G9Slice, gauge, heatmap, lineChart, matrixChart, radar, sparkline, stackedBarChart, } from './builders.js';
|
|
2
|
+
export { arcPath, esc, f, type G9ChartInsets, type G9Tick, g9ChartInsets, nearestIndex, niceStep, polar, svgDoc, xSlot, yScale, } from './scale.js';
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svg/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,WAAW,EACX,KAAK,EACL,QAAQ,EACR,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,OAAO,EACZ,KAAK,EACL,OAAO,EACP,SAAS,EACT,WAAW,EACX,KAAK,EACL,SAAS,EACT,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,OAAO,EACP,GAAG,EACH,CAAC,EACD,KAAK,aAAa,EAClB,KAAK,MAAM,EACX,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,KAAK,EACL,MAAM,EACN,KAAK,EACL,MAAM,GACP,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Space around the plot for axes and labels. */
|
|
2
|
+
export interface G9ChartInsets {
|
|
3
|
+
readonly top: number;
|
|
4
|
+
readonly right: number;
|
|
5
|
+
readonly bottom: number;
|
|
6
|
+
readonly left: number;
|
|
7
|
+
}
|
|
8
|
+
/** Default padding (px) between a chart's edge and its plot area: 8 on every side. */
|
|
9
|
+
export declare const g9ChartInsets: G9ChartInsets;
|
|
10
|
+
/** A y tick: its value and its pixel row. */
|
|
11
|
+
export interface G9Tick {
|
|
12
|
+
readonly value: number;
|
|
13
|
+
readonly y: number;
|
|
14
|
+
}
|
|
15
|
+
/** A "nice" step (1, 2, 2.5, 5 × 10ⁿ) so ticks read as round numbers. */
|
|
16
|
+
export declare function niceStep(range: number, count: number): number;
|
|
17
|
+
/** A linear y scale over [min, max] (min ≤ 0 ≤ max is kept when the data crosses zero) with round ticks. */
|
|
18
|
+
export declare function yScale(values: readonly number[], h: number, options?: {
|
|
19
|
+
readonly insets?: Partial<G9ChartInsets>;
|
|
20
|
+
readonly ticks?: number;
|
|
21
|
+
readonly min?: number;
|
|
22
|
+
readonly max?: number;
|
|
23
|
+
readonly zero?: boolean;
|
|
24
|
+
}): {
|
|
25
|
+
readonly min: number;
|
|
26
|
+
readonly max: number;
|
|
27
|
+
readonly y: (v: number) => number;
|
|
28
|
+
readonly ticks: readonly G9Tick[];
|
|
29
|
+
};
|
|
30
|
+
/** x of slot `i` of `n` across the plot (points at slot centres when `centred`, else edge to edge). */
|
|
31
|
+
export declare function xSlot(i: number, n: number, w: number, insets?: Partial<G9ChartInsets>, centred?: boolean): number;
|
|
32
|
+
/** The point nearest to `x` (a finger's or the mouse's), or null. */
|
|
33
|
+
export declare function nearestIndex(xs: readonly number[], x: number): number | null;
|
|
34
|
+
/** Polar → cartesian, 0° up, clockwise (gauges, radars, donuts). */
|
|
35
|
+
export declare function polar(cx: number, cy: number, r: number, deg: number): [number, number];
|
|
36
|
+
/** An SVG arc path from angle `a` to `b` (degrees, clockwise). */
|
|
37
|
+
export declare function arcPath(cx: number, cy: number, r: number, a: number, b: number): string;
|
|
38
|
+
/** Two decimals, no trailing zeros: SVG strings stay short (they are cached and diffed). */
|
|
39
|
+
export declare const f: (n: number) => string;
|
|
40
|
+
/** Escapes text for an SVG <text>. */
|
|
41
|
+
export declare const esc: (s: string) => string;
|
|
42
|
+
/** Wraps SVG content in a document of `w` × `h`. */
|
|
43
|
+
export declare const svgDoc: (w: number, h: number, body: string) => string;
|
|
44
|
+
//# sourceMappingURL=scale.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scale.d.ts","sourceRoot":"","sources":["../../src/svg/scale.ts"],"names":[],"mappings":"AAGA,iDAAiD;AACjD,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,sFAAsF;AACtF,eAAO,MAAM,aAAa,EAAE,aAAuE,CAAC;AAEpG,6CAA6C;AAC7C,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,yEAAyE;AACzE,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAO7D;AAED,4GAA4G;AAC5G,wBAAgB,MAAM,CACpB,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,CAAC,EAAE,MAAM,EACT,OAAO,GAAE;IACP,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACzC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACpB,GACL;IACD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC,CAmBA;AAED,uGAAuG;AACvG,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,CAAC,aAAa,CAAM,EAAE,OAAO,UAAQ,GAAG,MAAM,CAKnH;AAED,qEAAqE;AACrE,wBAAgB,YAAY,CAAC,EAAE,EAAE,SAAS,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM5E;AAED,oEAAoE;AACpE,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAGtF;AAED,kEAAkE;AAClE,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAKvF;AAED,4FAA4F;AAC5F,eAAO,MAAM,CAAC,GAAI,GAAG,MAAM,KAAG,MAA2C,CAAC;AAE1E,sCAAsC;AACtC,eAAO,MAAM,GAAG,GAAI,GAAG,MAAM,KAAG,MAA8E,CAAC;AAE/G,oDAAoD;AACpD,eAAO,MAAM,MAAM,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,MAAM,MAAM,KAAG,MAC4D,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const g9ChartInsets = Object.freeze({
|
|
2
|
+
top: 8,
|
|
3
|
+
right: 8,
|
|
4
|
+
bottom: 8,
|
|
5
|
+
left: 8
|
|
6
|
+
});
|
|
7
|
+
function niceStep(range, count) {
|
|
8
|
+
if (!(range > 0) || count <= 0) return 1;
|
|
9
|
+
const raw = range / count;
|
|
10
|
+
const mag = 10 ** Math.floor(Math.log10(raw));
|
|
11
|
+
const norm = raw / mag;
|
|
12
|
+
const nice = norm <= 1 ? 1 : norm <= 2 ? 2 : norm <= 2.5 ? 2.5 : norm <= 5 ? 5 : 10;
|
|
13
|
+
return nice * mag;
|
|
14
|
+
}
|
|
15
|
+
function yScale(values, h, options = {}) {
|
|
16
|
+
var _options_min, _options_max, _options_ticks;
|
|
17
|
+
const inset = {
|
|
18
|
+
...g9ChartInsets,
|
|
19
|
+
...options.insets
|
|
20
|
+
};
|
|
21
|
+
const finite = values.filter((v)=>Number.isFinite(v));
|
|
22
|
+
let lo = null != (_options_min = options.min) ? _options_min : Math.min(...finite.length ? finite : [
|
|
23
|
+
0
|
|
24
|
+
]);
|
|
25
|
+
let hi = null != (_options_max = options.max) ? _options_max : Math.max(...finite.length ? finite : [
|
|
26
|
+
1
|
|
27
|
+
]);
|
|
28
|
+
if (false !== options.zero) {
|
|
29
|
+
lo = Math.min(lo, 0);
|
|
30
|
+
hi = Math.max(hi, 0);
|
|
31
|
+
}
|
|
32
|
+
if (hi === lo) hi = lo + 1;
|
|
33
|
+
const step = niceStep(hi - lo, null != (_options_ticks = options.ticks) ? _options_ticks : 4);
|
|
34
|
+
const min = Math.floor(lo / step) * step;
|
|
35
|
+
const max = Math.ceil(hi / step) * step;
|
|
36
|
+
const top = inset.top;
|
|
37
|
+
const bottom = h - inset.bottom;
|
|
38
|
+
const y = (v)=>bottom - (v - min) / (max - min) * (bottom - top);
|
|
39
|
+
const ticks = [];
|
|
40
|
+
for(let v = min; v <= max + step / 2; v += step)ticks.push({
|
|
41
|
+
value: Number(v.toFixed(10)),
|
|
42
|
+
y: y(v)
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
min,
|
|
46
|
+
max,
|
|
47
|
+
y,
|
|
48
|
+
ticks
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function xSlot(i, n, w, insets = {}, centred = false) {
|
|
52
|
+
const inset = {
|
|
53
|
+
...g9ChartInsets,
|
|
54
|
+
...insets
|
|
55
|
+
};
|
|
56
|
+
const span = w - inset.left - inset.right;
|
|
57
|
+
if (centred) return inset.left + (i + 0.5) * span / Math.max(1, n);
|
|
58
|
+
return n <= 1 ? inset.left + span / 2 : inset.left + i * span / (n - 1);
|
|
59
|
+
}
|
|
60
|
+
function nearestIndex(xs, x) {
|
|
61
|
+
if (0 === xs.length) return null;
|
|
62
|
+
let best = 0;
|
|
63
|
+
for(let i = 1; i < xs.length; i++)if (Math.abs(xs[i] - x) < Math.abs(xs[best] - x)) best = i;
|
|
64
|
+
return best;
|
|
65
|
+
}
|
|
66
|
+
function polar(cx, cy, r, deg) {
|
|
67
|
+
const rad = (deg - 90) * Math.PI / 180;
|
|
68
|
+
return [
|
|
69
|
+
cx + r * Math.cos(rad),
|
|
70
|
+
cy + r * Math.sin(rad)
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
function arcPath(cx, cy, r, a, b) {
|
|
74
|
+
const [x1, y1] = polar(cx, cy, r, a);
|
|
75
|
+
const [x2, y2] = polar(cx, cy, r, b);
|
|
76
|
+
const large = b - a > 180 ? 1 : 0;
|
|
77
|
+
return `M${f(x1)} ${f(y1)}A${f(r)} ${f(r)} 0 ${large} 1 ${f(x2)} ${f(y2)}`;
|
|
78
|
+
}
|
|
79
|
+
const f = (n)=>String(Math.round(100 * n) / 100);
|
|
80
|
+
const esc = (s)=>s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
81
|
+
const svgDoc = (w, h, body)=>`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${f(w)} ${f(h)}" width="${f(w)}" height="${f(h)}">${body}</svg>`;
|
|
82
|
+
export { arcPath, esc, f, g9ChartInsets, nearestIndex, niceStep, polar, svgDoc, xSlot, yScale };
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/lynxcontrols-charts",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Charts for G9LynxControls: pure SVG builders (line/area, bar, stacked, sparkline, donut, heatmap, gauge, radar, fan, candles, matrix) and themed ReactLynx components with RTL axes and touch tooltips.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": [
|
|
8
|
+
"**/*.css"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.jsx"
|
|
15
|
+
},
|
|
16
|
+
"./svg": {
|
|
17
|
+
"source": "./src/svg/index.ts",
|
|
18
|
+
"types": "./dist/svg/index.d.ts",
|
|
19
|
+
"default": "./dist/svg/index.jsx"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "rslib build"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@g9tm/lynxcontrols-foundation": "2.2.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@g9tm/lynxcontrols": "2.2.0",
|
|
34
|
+
"@lynx-js/react": ">=0.129.3 <0.130.0"
|
|
35
|
+
}
|
|
36
|
+
}
|