p1-charts 0.1.0 → 0.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.
Files changed (51) hide show
  1. package/README.md +15 -1
  2. package/dist/CategoryBarChart-YEJ9VB8h.js +453 -0
  3. package/dist/CategoryBarChart-YEJ9VB8h.js.map +1 -0
  4. package/dist/{PolarChart-D_ULWctY.js → PolarChart-BUEhn3CI.js} +135 -83
  5. package/dist/PolarChart-BUEhn3CI.js.map +1 -0
  6. package/dist/{TimeSeriesChart-tYmzRNzV.js → TimeSeriesChart-BIwJxgnD.js} +483 -418
  7. package/dist/TimeSeriesChart-BIwJxgnD.js.map +1 -0
  8. package/dist/category.d.ts +2 -2
  9. package/dist/category.d.ts.map +1 -1
  10. package/dist/category.js +1 -1
  11. package/dist/charts/CategoryBarChart/index.d.ts +1 -1
  12. package/dist/charts/CategoryBarChart/index.d.ts.map +1 -1
  13. package/dist/charts/PolarChart/index.d.ts.map +1 -1
  14. package/dist/charts/ScatterChart/index.d.ts +1 -1
  15. package/dist/charts/ScatterChart/index.d.ts.map +1 -1
  16. package/dist/charts/TimeSeriesChart/index.d.ts +1 -1
  17. package/dist/charts/TimeSeriesChart/index.d.ts.map +1 -1
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +3 -3
  21. package/dist/scatter.js +1 -1
  22. package/dist/series.d.ts +1 -1
  23. package/dist/series.d.ts.map +1 -1
  24. package/dist/series.js +1 -1
  25. package/dist/types/category.d.ts +41 -2
  26. package/dist/types/category.d.ts.map +1 -1
  27. package/dist/types/polar.d.ts +7 -1
  28. package/dist/types/polar.d.ts.map +1 -1
  29. package/dist/types/scatter.d.ts +13 -0
  30. package/dist/types/scatter.d.ts.map +1 -1
  31. package/dist/types/timeSeries.d.ts +31 -0
  32. package/dist/types/timeSeries.d.ts.map +1 -1
  33. package/dist/utils/category.utils.d.ts +46 -7
  34. package/dist/utils/category.utils.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/category.ts +13 -2
  37. package/src/charts/CategoryBarChart/index.tsx +301 -50
  38. package/src/charts/PolarChart/index.tsx +63 -18
  39. package/src/charts/ScatterChart/index.tsx +51 -7
  40. package/src/charts/TimeSeriesChart/index.tsx +117 -2
  41. package/src/index.ts +13 -1
  42. package/src/series.ts +4 -0
  43. package/src/types/category.ts +47 -2
  44. package/src/types/polar.ts +11 -1
  45. package/src/types/scatter.ts +13 -0
  46. package/src/types/timeSeries.ts +35 -0
  47. package/src/utils/category.utils.ts +321 -43
  48. package/dist/CategoryBarChart-CV58kvIr.js +0 -182
  49. package/dist/CategoryBarChart-CV58kvIr.js.map +0 -1
  50. package/dist/PolarChart-D_ULWctY.js.map +0 -1
  51. package/dist/TimeSeriesChart-tYmzRNzV.js.map +0 -1
@@ -1,8 +1,11 @@
1
1
  export type CategoryBarMode = "group" | "stack" | "overlay";
2
+ export type CategoryOrientation = "vertical" | "horizontal";
3
+ export type CategoryScale = "left" | "right";
2
4
 
3
5
  export interface CategorySeriesInput {
4
6
  id: string;
5
7
  data: readonly number[];
8
+ scale?: CategoryScale;
6
9
  }
7
10
 
8
11
  export interface CategoryRect {
@@ -15,22 +18,79 @@ export interface CategoryRect {
15
18
  value: number;
16
19
  }
17
20
 
21
+ export interface CategoryPad {
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ left: number;
26
+ }
27
+
28
+ export interface CategoryExtent {
29
+ min: number;
30
+ max: number;
31
+ }
32
+
33
+ export interface CategoryDomain {
34
+ min?: number;
35
+ max?: number;
36
+ }
37
+
18
38
  export interface CategoryLayoutOptions {
19
39
  mode: CategoryBarMode;
20
40
  series: readonly CategorySeriesInput[];
21
41
  categories: number;
22
42
  width: number;
23
43
  height: number;
24
- pad?: { top: number; right: number; bottom: number; left: number };
44
+ pad?: CategoryPad;
45
+ orientation?: CategoryOrientation;
46
+ domain?: CategoryDomain;
47
+ rightDomain?: CategoryDomain;
48
+ /** Values the axis must contain, such as a threshold the bars have not reached. */
49
+ pins?: readonly { value: number; scale?: CategoryScale }[];
25
50
  }
26
51
 
27
- const PAD = { top: 16, right: 12, bottom: 28, left: 36 };
52
+ export interface CategoryLine {
53
+ x1: number;
54
+ y1: number;
55
+ x2: number;
56
+ y2: number;
57
+ }
28
58
 
29
- function extent(
59
+ export interface CategoryFrame {
60
+ rects: CategoryRect[];
61
+ orientation: CategoryOrientation;
62
+ pad: CategoryPad;
63
+ left: CategoryExtent;
64
+ right: CategoryExtent | null;
65
+ /** A line across the plot at one value. Horizontal charts draw it upright. */
66
+ across: (value: number, scale?: CategoryScale) => CategoryLine;
67
+ }
68
+
69
+ const VERTICAL_PAD: CategoryPad = { top: 16, right: 12, bottom: 28, left: 36 };
70
+ const HORIZONTAL_PAD: CategoryPad = { top: 12, right: 56, bottom: 28, left: 96 };
71
+
72
+ interface PresentBar {
73
+ id: string;
74
+ value: number;
75
+ scale: CategoryScale;
76
+ }
77
+
78
+ function scaleOf(series: CategorySeriesInput): CategoryScale {
79
+ return series.scale ?? "left";
80
+ }
81
+
82
+ function extentOf(
30
83
  mode: CategoryBarMode,
31
84
  series: readonly CategorySeriesInput[],
32
85
  categories: number,
33
- ): { min: number; max: number } {
86
+ scale: CategoryScale,
87
+ domain?: CategoryDomain,
88
+ pins?: readonly number[],
89
+ ): CategoryExtent | null {
90
+ const mine = series.filter((item) => scaleOf(item) === scale);
91
+
92
+ if (mine.length === 0) return null;
93
+
34
94
  let min = 0;
35
95
  let max = 0;
36
96
 
@@ -38,7 +98,7 @@ function extent(
38
98
  let positive = 0;
39
99
  let negative = 0;
40
100
 
41
- for (const item of series) {
101
+ for (const item of mine) {
42
102
  const value = item.data[category];
43
103
 
44
104
  if (typeof value !== "number" || !Number.isFinite(value)) continue;
@@ -58,36 +118,54 @@ function extent(
58
118
  }
59
119
  }
60
120
 
61
- return { min, max: max === min ? max + 1 : max };
121
+ for (const pin of pins ?? []) {
122
+ if (!Number.isFinite(pin)) continue;
123
+
124
+ min = Math.min(min, pin);
125
+ max = Math.max(max, pin);
126
+ }
127
+
128
+ const next = {
129
+ min: domain?.min ?? min,
130
+ max: domain?.max ?? max,
131
+ };
132
+
133
+ return { min: next.min, max: next.max === next.min ? next.max + 1 : next.max };
62
134
  }
63
135
 
64
- /** Bar rectangles for grouped, stacked, or overlaid category columns. */
65
- export function categoryLayout({
66
- mode,
67
- series,
68
- categories,
69
- width,
70
- height,
71
- pad = PAD,
72
- }: CategoryLayoutOptions): CategoryRect[] {
73
- if (categories <= 0 || series.length === 0) return [];
136
+ function presentAt(
137
+ series: readonly CategorySeriesInput[],
138
+ category: number,
139
+ ): PresentBar[] {
140
+ return series.flatMap((item) => {
141
+ const value = item.data[category];
74
142
 
75
- const plotW = Math.max(0, width - pad.left - pad.right);
143
+ return typeof value === "number" && Number.isFinite(value)
144
+ ? [{ id: item.id, value, scale: scaleOf(item) }]
145
+ : [];
146
+ });
147
+ }
148
+
149
+ function placeVertical(
150
+ options: CategoryLayoutOptions,
151
+ pad: CategoryPad,
152
+ left: CategoryExtent,
153
+ right: CategoryExtent | null,
154
+ ): CategoryRect[] {
155
+ const { mode, series, categories, width, height } = options;
76
156
  const plotH = Math.max(0, height - pad.top - pad.bottom);
77
- const { min, max } = extent(mode, series, categories);
78
- const span = max - min || 1;
79
- const yOf = (value: number) => pad.top + ((max - value) / span) * plotH;
157
+ const plotW = Math.max(0, width - pad.left - pad.right);
158
+ const yFor = (scale: CategoryScale) => {
159
+ const extent = scale === "right" && right ? right : left;
160
+ const span = extent.max - extent.min || 1;
161
+
162
+ return (value: number) => pad.top + ((extent.max - value) / span) * plotH;
163
+ };
80
164
  const slot = plotW / categories;
81
165
  const rects: CategoryRect[] = [];
82
166
 
83
167
  for (let category = 0; category < categories; category += 1) {
84
- const present = series.flatMap((item) => {
85
- const value = item.data[category];
86
-
87
- return typeof value === "number" && Number.isFinite(value)
88
- ? [{ id: item.id, value }]
89
- : [];
90
- });
168
+ const present = presentAt(series, category);
91
169
 
92
170
  if (present.length === 0) continue;
93
171
 
@@ -99,6 +177,7 @@ export function categoryLayout({
99
177
  const barW = (inner - gap * (present.length - 1)) / present.length;
100
178
 
101
179
  present.forEach((item, index) => {
180
+ const yOf = yFor(item.scale);
102
181
  const y = Math.min(yOf(item.value), yOf(0));
103
182
 
104
183
  rects.push({
@@ -116,6 +195,7 @@ export function categoryLayout({
116
195
 
117
196
  if (mode === "overlay") {
118
197
  present.forEach((item) => {
198
+ const yOf = yFor(item.scale);
119
199
  const y = Math.min(yOf(item.value), yOf(0));
120
200
 
121
201
  rects.push({
@@ -131,29 +211,227 @@ export function categoryLayout({
131
211
  continue;
132
212
  }
133
213
 
134
- let positive = 0;
135
- let negative = 0;
214
+ for (const scale of ["left", "right"] as const) {
215
+ let positive = 0;
216
+ let negative = 0;
217
+ const yOf = yFor(scale);
218
+
219
+ for (const item of present) {
220
+ if (item.scale !== scale) continue;
221
+
222
+ const start = item.value >= 0 ? positive : negative;
223
+ const end = start + item.value;
224
+
225
+ if (item.value >= 0) positive = end;
226
+ else negative = end;
227
+
228
+ const y = Math.min(yOf(start), yOf(end));
229
+
230
+ rects.push({
231
+ series: item.id,
232
+ category,
233
+ x: origin,
234
+ y,
235
+ width: inner,
236
+ height: Math.abs(yOf(end) - yOf(start)),
237
+ value: item.value,
238
+ });
239
+ }
240
+ }
241
+ }
242
+
243
+ return rects;
244
+ }
245
+
246
+ function placeHorizontal(
247
+ options: CategoryLayoutOptions,
248
+ pad: CategoryPad,
249
+ left: CategoryExtent,
250
+ right: CategoryExtent | null,
251
+ ): CategoryRect[] {
252
+ const { mode, series, categories, width, height } = options;
253
+ const plotW = Math.max(0, width - pad.left - pad.right);
254
+ const plotH = Math.max(0, height - pad.top - pad.bottom);
255
+ const xFor = (scale: CategoryScale) => {
256
+ const extent = scale === "right" && right ? right : left;
257
+ const span = extent.max - extent.min || 1;
258
+
259
+ return (value: number) => pad.left + ((value - extent.min) / span) * plotW;
260
+ };
261
+ const slot = plotH / categories;
262
+ const rects: CategoryRect[] = [];
136
263
 
137
- for (const item of present) {
138
- const start = item.value >= 0 ? positive : negative;
139
- const end = start + item.value;
264
+ for (let category = 0; category < categories; category += 1) {
265
+ const present = presentAt(series, category);
266
+
267
+ if (present.length === 0) continue;
268
+
269
+ const inner = slot * 0.72;
270
+ const origin = pad.top + category * slot + (slot - inner) / 2;
140
271
 
141
- if (item.value >= 0) positive = end;
142
- else negative = end;
272
+ if (mode === "group") {
273
+ const gap = present.length > 1 ? 2 : 0;
274
+ const barH = (inner - gap * (present.length - 1)) / present.length;
143
275
 
144
- const y = Math.min(yOf(start), yOf(end));
276
+ present.forEach((item, index) => {
277
+ const xOf = xFor(item.scale);
278
+ const x = Math.min(xOf(item.value), xOf(0));
145
279
 
146
- rects.push({
147
- series: item.id,
148
- category,
149
- x: origin,
150
- y,
151
- width: inner,
152
- height: Math.abs(yOf(end) - yOf(start)),
153
- value: item.value,
280
+ rects.push({
281
+ series: item.id,
282
+ category,
283
+ x,
284
+ y: origin + index * (barH + gap),
285
+ width: Math.abs(xOf(item.value) - xOf(0)),
286
+ height: barH,
287
+ value: item.value,
288
+ });
154
289
  });
290
+ continue;
291
+ }
292
+
293
+ if (mode === "overlay") {
294
+ present.forEach((item) => {
295
+ const xOf = xFor(item.scale);
296
+ const x = Math.min(xOf(item.value), xOf(0));
297
+
298
+ rects.push({
299
+ series: item.id,
300
+ category,
301
+ x,
302
+ y: origin,
303
+ width: Math.abs(xOf(item.value) - xOf(0)),
304
+ height: inner,
305
+ value: item.value,
306
+ });
307
+ });
308
+ continue;
309
+ }
310
+
311
+ for (const scale of ["left", "right"] as const) {
312
+ let positive = 0;
313
+ let negative = 0;
314
+ const xOf = xFor(scale);
315
+
316
+ for (const item of present) {
317
+ if (item.scale !== scale) continue;
318
+
319
+ const start = item.value >= 0 ? positive : negative;
320
+ const end = start + item.value;
321
+
322
+ if (item.value >= 0) positive = end;
323
+ else negative = end;
324
+
325
+ const x = Math.min(xOf(start), xOf(end));
326
+
327
+ rects.push({
328
+ series: item.id,
329
+ category,
330
+ x,
331
+ y: origin,
332
+ width: Math.abs(xOf(end) - xOf(start)),
333
+ height: inner,
334
+ value: item.value,
335
+ });
336
+ }
155
337
  }
156
338
  }
157
339
 
158
340
  return rects;
159
341
  }
342
+
343
+ function acrossLine(
344
+ orientation: CategoryOrientation,
345
+ pad: CategoryPad,
346
+ width: number,
347
+ height: number,
348
+ left: CategoryExtent,
349
+ right: CategoryExtent | null,
350
+ value: number,
351
+ scale: CategoryScale,
352
+ ): CategoryLine {
353
+ const extent = scale === "right" && right ? right : left;
354
+ const span = extent.max - extent.min || 1;
355
+
356
+ if (orientation === "horizontal") {
357
+ const plotW = Math.max(0, width - pad.left - pad.right);
358
+ const x = pad.left + ((value - extent.min) / span) * plotW;
359
+
360
+ return { x1: x, y1: pad.top, x2: x, y2: height - pad.bottom };
361
+ }
362
+
363
+ const plotH = Math.max(0, height - pad.top - pad.bottom);
364
+ const y = pad.top + ((extent.max - value) / span) * plotH;
365
+
366
+ return { x1: pad.left, y1: y, x2: width - pad.right, y2: y };
367
+ }
368
+
369
+ /** Bar rectangles, the value extents, and a helper for a reference line. */
370
+ export function categoryFrame(options: CategoryLayoutOptions): CategoryFrame {
371
+ const orientation = options.orientation ?? "vertical";
372
+ const pad = options.pad ?? (orientation === "horizontal" ? HORIZONTAL_PAD : VERTICAL_PAD);
373
+ const pinsFor = (scale: CategoryScale) =>
374
+ (options.pins ?? []).flatMap((pin) =>
375
+ (pin.scale ?? "left") === scale && Number.isFinite(pin.value) ? [pin.value] : [],
376
+ );
377
+ const left = extentOf(
378
+ options.mode,
379
+ options.series,
380
+ options.categories,
381
+ "left",
382
+ options.domain,
383
+ pinsFor("left"),
384
+ ) ?? {
385
+ min: 0,
386
+ max: 1,
387
+ };
388
+ const right = extentOf(
389
+ options.mode,
390
+ options.series,
391
+ options.categories,
392
+ "right",
393
+ options.rightDomain,
394
+ pinsFor("right"),
395
+ );
396
+ const rects =
397
+ options.categories <= 0 || options.series.length === 0
398
+ ? []
399
+ : orientation === "horizontal"
400
+ ? placeHorizontal(options, pad, left, right)
401
+ : placeVertical(options, pad, left, right);
402
+
403
+ return {
404
+ rects,
405
+ orientation,
406
+ pad,
407
+ left,
408
+ right,
409
+ across: (value, scale = "left") =>
410
+ acrossLine(
411
+ orientation,
412
+ pad,
413
+ options.width,
414
+ options.height,
415
+ left,
416
+ right,
417
+ value,
418
+ scale,
419
+ ),
420
+ };
421
+ }
422
+
423
+ /** Bar rectangles for grouped, stacked, or overlaid category columns. */
424
+ export function categoryLayout(options: CategoryLayoutOptions): CategoryRect[] {
425
+ return categoryFrame(options).rects;
426
+ }
427
+
428
+ /** Even ticks from `min` to `max`, including both ends. */
429
+ export function axisTicks(min: number, max: number, count = 4): number[] {
430
+ if (!(max > min) || count < 1) return [min];
431
+
432
+ const step = (max - min) / count;
433
+
434
+ return Array.from({ length: count + 1 }, (_, index) =>
435
+ Number((min + step * index).toPrecision(6)),
436
+ );
437
+ }
@@ -1,182 +0,0 @@
1
- "use client";
2
- import { n as e } from "./heatmap-CIBukkjS.js";
3
- import { t } from "./dom.utils-CKMWLto-.js";
4
- import { t as n } from "./useChartTheme-B06p_jvu.js";
5
- import { useState as r } from "react";
6
- import { jsx as i, jsxs as a } from "react/jsx-runtime";
7
- //#region src/utils/category.utils.ts
8
- var o = {
9
- top: 16,
10
- right: 12,
11
- bottom: 28,
12
- left: 36
13
- };
14
- function s(e, t, n) {
15
- let r = 0, i = 0;
16
- for (let a = 0; a < n; a += 1) {
17
- let n = 0, o = 0;
18
- for (let s of t) {
19
- let t = s.data[a];
20
- typeof t == "number" && Number.isFinite(t) && (e === "stack" ? t >= 0 ? n += t : o += t : (r = Math.min(r, t), i = Math.max(i, t)));
21
- }
22
- e === "stack" && (r = Math.min(r, o), i = Math.max(i, n));
23
- }
24
- return {
25
- min: r,
26
- max: i === r ? i + 1 : i
27
- };
28
- }
29
- function c({ mode: e, series: t, categories: n, width: r, height: i, pad: a = o }) {
30
- if (n <= 0 || t.length === 0) return [];
31
- let c = Math.max(0, r - a.left - a.right), l = Math.max(0, i - a.top - a.bottom), { min: u, max: d } = s(e, t, n), f = d - u || 1, p = (e) => a.top + (d - e) / f * l, m = c / n, h = [];
32
- for (let r = 0; r < n; r += 1) {
33
- let n = t.flatMap((e) => {
34
- let t = e.data[r];
35
- return typeof t == "number" && Number.isFinite(t) ? [{
36
- id: e.id,
37
- value: t
38
- }] : [];
39
- });
40
- if (n.length === 0) continue;
41
- let i = m * .72, o = a.left + r * m + (m - i) / 2;
42
- if (e === "group") {
43
- let e = n.length > 1 ? 2 : 0, t = (i - e * (n.length - 1)) / n.length;
44
- n.forEach((n, i) => {
45
- let a = Math.min(p(n.value), p(0));
46
- h.push({
47
- series: n.id,
48
- category: r,
49
- x: o + i * (t + e),
50
- y: a,
51
- width: t,
52
- height: Math.abs(p(n.value) - p(0)),
53
- value: n.value
54
- });
55
- });
56
- continue;
57
- }
58
- if (e === "overlay") {
59
- n.forEach((e) => {
60
- let t = Math.min(p(e.value), p(0));
61
- h.push({
62
- series: e.id,
63
- category: r,
64
- x: o,
65
- y: t,
66
- width: i,
67
- height: Math.abs(p(e.value) - p(0)),
68
- value: e.value
69
- });
70
- });
71
- continue;
72
- }
73
- let s = 0, c = 0;
74
- for (let e of n) {
75
- let t = e.value >= 0 ? s : c, n = t + e.value;
76
- e.value >= 0 ? s = n : c = n;
77
- let a = Math.min(p(t), p(n));
78
- h.push({
79
- series: e.id,
80
- category: r,
81
- x: o,
82
- y: a,
83
- width: i,
84
- height: Math.abs(p(n) - p(t)),
85
- value: e.value
86
- });
87
- }
88
- }
89
- return h;
90
- }
91
- //#endregion
92
- //#region src/charts/CategoryBarChart/index.tsx
93
- var l = 640, u = 320;
94
- function d(e, t, n) {
95
- return n ?? e[t % Math.max(1, e.length)] ?? "currentColor";
96
- }
97
- function f({ categories: o, series: s, mode: f = "group", className: p, ariaLabel: m = "Bar chart", formatValue: h, renderTooltip: g, theme: _, unstyled: v }) {
98
- let y = n({
99
- theme: _,
100
- unstyled: v
101
- }), b = y?.palette.categorical ?? [], x = y?.palette.text, S = y?.palette.grid, C = c({
102
- mode: f,
103
- series: s,
104
- categories: o.length,
105
- width: l,
106
- height: u
107
- }), [w, T] = r(0), E = o[w] ?? o[0], D = s.flatMap((e, t) => {
108
- let n = E === void 0 ? void 0 : e.data[w];
109
- return typeof n == "number" && Number.isFinite(n) ? [{
110
- label: e.name,
111
- value: h?.(e.id, n) ?? String(n),
112
- color: d(b, t, e.color)
113
- }] : [];
114
- });
115
- return /* @__PURE__ */ a("div", {
116
- className: t(p),
117
- "data-mode": f,
118
- style: { position: "relative" },
119
- children: [/* @__PURE__ */ a("svg", {
120
- role: "img",
121
- "aria-label": m,
122
- tabIndex: 0,
123
- viewBox: `0 0 ${l} ${u}`,
124
- style: e,
125
- onKeyDown: (e) => {
126
- (e.key === "ArrowLeft" || e.key === "ArrowRight") && (e.preventDefault(), T((t) => {
127
- let n = e.key === "ArrowRight" ? t + 1 : t - 1;
128
- return Math.max(0, Math.min(o.length - 1, n));
129
- }));
130
- },
131
- children: [
132
- /* @__PURE__ */ i("line", {
133
- "data-part": "axis",
134
- x1: 36,
135
- y1: 292,
136
- x2: 628,
137
- y2: 292,
138
- stroke: S
139
- }),
140
- o.map((e, t) => /* @__PURE__ */ i("text", {
141
- "data-part": "tick",
142
- x: 36 + 592 / o.length * (t + .5),
143
- y: 310,
144
- textAnchor: "middle",
145
- fill: x,
146
- fontSize: 11,
147
- children: e
148
- }, e)),
149
- C.map((e) => /* @__PURE__ */ i("rect", {
150
- "data-part": "bar",
151
- "data-series": e.series,
152
- "data-category": e.category,
153
- "data-active": e.category === w ? "" : void 0,
154
- x: e.x,
155
- y: e.y,
156
- width: e.width,
157
- height: Math.max(e.height, e.value === 0 ? 0 : 1),
158
- fill: d(b, s.findIndex((t) => t.id === e.series), s.find((t) => t.id === e.series)?.color),
159
- opacity: f === "overlay" ? .72 : 1,
160
- onPointerEnter: () => T(e.category)
161
- }, `${e.series}:${e.category}`))
162
- ]
163
- }), E !== void 0 && (g?.(E, D) ?? /* @__PURE__ */ a("div", {
164
- "data-part": "tooltip",
165
- children: [/* @__PURE__ */ i("div", {
166
- "data-part": "tooltip-time",
167
- children: E
168
- }), D.map((e) => /* @__PURE__ */ a("div", {
169
- "data-part": "tooltip-row",
170
- children: [
171
- e.label,
172
- " ",
173
- e.value
174
- ]
175
- }, e.label))]
176
- }))]
177
- });
178
- }
179
- //#endregion
180
- export { c as n, f as t };
181
-
182
- //# sourceMappingURL=CategoryBarChart-CV58kvIr.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CategoryBarChart-CV58kvIr.js","names":["HEATMAP_SVG","useChartTheme","PAD","extent","min","max","category","categories","positive","negative","item","series","value","mode","categoryLayout","plotW","width","pad","plotH","height","span","yOf","slot","rects","present","inner","origin","gap","barW","y","index","start","end","WIDTH","HEIGHT","ink","color","categorical","CategoryBarChart","resolved","text","grid","active","setActive","useState","rows","formatValue","classes","className","ariaLabel","event","next","current","label","rect","renderTooltip","row"],"sources":["../src/utils/category.utils.ts","../src/charts/CategoryBarChart/index.tsx"],"sourcesContent":["export type CategoryBarMode = \"group\" | \"stack\" | \"overlay\";\n\nexport interface CategorySeriesInput {\n id: string;\n data: readonly number[];\n}\n\nexport interface CategoryRect {\n series: string;\n category: number;\n x: number;\n y: number;\n width: number;\n height: number;\n value: number;\n}\n\nexport interface CategoryLayoutOptions {\n mode: CategoryBarMode;\n series: readonly CategorySeriesInput[];\n categories: number;\n width: number;\n height: number;\n pad?: { top: number; right: number; bottom: number; left: number };\n}\n\nconst PAD = { top: 16, right: 12, bottom: 28, left: 36 };\n\nfunction extent(\n mode: CategoryBarMode,\n series: readonly CategorySeriesInput[],\n categories: number,\n): { min: number; max: number } {\n let min = 0;\n let max = 0;\n\n for (let category = 0; category < categories; category += 1) {\n let positive = 0;\n let negative = 0;\n\n for (const item of series) {\n const value = item.data[category];\n\n if (typeof value !== \"number\" || !Number.isFinite(value)) continue;\n\n if (mode === \"stack\") {\n if (value >= 0) positive += value;\n else negative += value;\n } else {\n min = Math.min(min, value);\n max = Math.max(max, value);\n }\n }\n\n if (mode === \"stack\") {\n min = Math.min(min, negative);\n max = Math.max(max, positive);\n }\n }\n\n return { min, max: max === min ? max + 1 : max };\n}\n\n/** Bar rectangles for grouped, stacked, or overlaid category columns. */\nexport function categoryLayout({\n mode,\n series,\n categories,\n width,\n height,\n pad = PAD,\n}: CategoryLayoutOptions): CategoryRect[] {\n if (categories <= 0 || series.length === 0) return [];\n\n const plotW = Math.max(0, width - pad.left - pad.right);\n const plotH = Math.max(0, height - pad.top - pad.bottom);\n const { min, max } = extent(mode, series, categories);\n const span = max - min || 1;\n const yOf = (value: number) => pad.top + ((max - value) / span) * plotH;\n const slot = plotW / categories;\n const rects: CategoryRect[] = [];\n\n for (let category = 0; category < categories; category += 1) {\n const present = series.flatMap((item) => {\n const value = item.data[category];\n\n return typeof value === \"number\" && Number.isFinite(value)\n ? [{ id: item.id, value }]\n : [];\n });\n\n if (present.length === 0) continue;\n\n const inner = slot * 0.72;\n const origin = pad.left + category * slot + (slot - inner) / 2;\n\n if (mode === \"group\") {\n const gap = present.length > 1 ? 2 : 0;\n const barW = (inner - gap * (present.length - 1)) / present.length;\n\n present.forEach((item, index) => {\n const y = Math.min(yOf(item.value), yOf(0));\n\n rects.push({\n series: item.id,\n category,\n x: origin + index * (barW + gap),\n y,\n width: barW,\n height: Math.abs(yOf(item.value) - yOf(0)),\n value: item.value,\n });\n });\n continue;\n }\n\n if (mode === \"overlay\") {\n present.forEach((item) => {\n const y = Math.min(yOf(item.value), yOf(0));\n\n rects.push({\n series: item.id,\n category,\n x: origin,\n y,\n width: inner,\n height: Math.abs(yOf(item.value) - yOf(0)),\n value: item.value,\n });\n });\n continue;\n }\n\n let positive = 0;\n let negative = 0;\n\n for (const item of present) {\n const start = item.value >= 0 ? positive : negative;\n const end = start + item.value;\n\n if (item.value >= 0) positive = end;\n else negative = end;\n\n const y = Math.min(yOf(start), yOf(end));\n\n rects.push({\n series: item.id,\n category,\n x: origin,\n y,\n width: inner,\n height: Math.abs(yOf(end) - yOf(start)),\n value: item.value,\n });\n }\n }\n\n return rects;\n}\n","import { useState, type KeyboardEvent } from \"react\";\nimport type { CategoryBarChartProps } from \"../../types/category.js\";\nimport { useChartTheme } from \"../../hooks/useChartTheme.js\";\nimport { categoryLayout } from \"../../utils/category.utils.js\";\nimport { classes } from \"../../utils/dom.utils.js\";\nimport { HEATMAP_SVG } from \"../../styles/heatmap.js\";\n\nconst WIDTH = 640;\nconst HEIGHT = 320;\n\nfunction ink(categorical: readonly string[], index: number, color?: string) {\n return color ?? categorical[index % Math.max(1, categorical.length)] ?? \"currentColor\";\n}\n\nexport function CategoryBarChart({\n categories,\n series,\n mode = \"group\",\n className,\n ariaLabel = \"Bar chart\",\n formatValue,\n renderTooltip,\n theme,\n unstyled,\n}: CategoryBarChartProps) {\n const resolved = useChartTheme({ theme, unstyled });\n const categorical = resolved?.palette.categorical ?? [];\n const text = resolved?.palette.text;\n const grid = resolved?.palette.grid;\n const rects = categoryLayout({\n mode,\n series,\n categories: categories.length,\n width: WIDTH,\n height: HEIGHT,\n });\n const [active, setActive] = useState(0);\n const category = categories[active] ?? categories[0];\n const rows = series.flatMap((item, index) => {\n const value = category === undefined ? undefined : item.data[active];\n\n return typeof value === \"number\" && Number.isFinite(value)\n ? [\n {\n label: item.name,\n value: formatValue?.(item.id, value) ?? String(value),\n color: ink(categorical, index, item.color),\n },\n ]\n : [];\n });\n\n const onKey = (event: KeyboardEvent<SVGSVGElement>) => {\n if (event.key !== \"ArrowLeft\" && event.key !== \"ArrowRight\") return;\n\n event.preventDefault();\n setActive((current) => {\n const next = event.key === \"ArrowRight\" ? current + 1 : current - 1;\n\n return Math.max(0, Math.min(categories.length - 1, next));\n });\n };\n\n return (\n <div className={classes(className)} data-mode={mode} style={{ position: \"relative\" }}>\n <svg\n role=\"img\"\n aria-label={ariaLabel}\n tabIndex={0}\n viewBox={`0 0 ${WIDTH} ${HEIGHT}`}\n style={HEATMAP_SVG}\n onKeyDown={onKey}\n >\n <line data-part=\"axis\" x1={36} y1={HEIGHT - 28} x2={WIDTH - 12} y2={HEIGHT - 28} stroke={grid} />\n {categories.map((label, index) => (\n <text\n key={label}\n data-part=\"tick\"\n x={36 + ((WIDTH - 48) / categories.length) * (index + 0.5)}\n y={HEIGHT - 10}\n textAnchor=\"middle\"\n fill={text}\n fontSize={11}\n >\n {label}\n </text>\n ))}\n {rects.map((rect) => (\n <rect\n key={`${rect.series}:${rect.category}`}\n data-part=\"bar\"\n data-series={rect.series}\n data-category={rect.category}\n data-active={rect.category === active ? \"\" : undefined}\n x={rect.x}\n y={rect.y}\n width={rect.width}\n height={Math.max(rect.height, rect.value === 0 ? 0 : 1)}\n fill={ink(\n categorical,\n series.findIndex((item) => item.id === rect.series),\n series.find((item) => item.id === rect.series)?.color,\n )}\n opacity={mode === \"overlay\" ? 0.72 : 1}\n onPointerEnter={() => setActive(rect.category)}\n />\n ))}\n </svg>\n {category !== undefined &&\n (renderTooltip?.(category, rows) ?? (\n <div data-part=\"tooltip\">\n <div data-part=\"tooltip-time\">{category}</div>\n {rows.map((row) => (\n <div key={row.label} data-part=\"tooltip-row\">\n {row.label} {row.value}\n </div>\n ))}\n </div>\n ))}\n </div>\n );\n}\n"],"mappings":";;;;;;;AA0BA,IAAME,IAAM;CAAE,KAAK;CAAI,OAAO;CAAI,QAAQ;CAAI,MAAM;AAAG;AAEvD,SAASC,EACP,GACA,GACA,GAC8B;CAC9B,IAAIC,IAAM,GACNC,IAAM;CAEV,KAAK,IAAIC,IAAW,GAAGA,IAAWC,GAAY,KAAY,GAAG;EAC3D,IAAIC,IAAW,GACXC,IAAW;EAEf,KAAK,IAAMC,KAAQC,GAAQ;GACzB,IAAMC,IAAQF,EAAK,KAAKJ;GAExB,AAAI,OAAOM,KAAU,YAAa,OAAO,SAASA,CAAK,MAEnDC,MAAS,UACPD,KAAS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