@entropix/data 0.0.0 → 1.0.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/dist/index.cjs +645 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +144 -0
- package/dist/index.css.map +1 -1
- package/dist/index.d.cts +58 -3
- package/dist/index.d.ts +58 -3
- package/dist/index.js +644 -3
- package/dist/index.js.map +1 -1
- package/dist/styles/chart.css +193 -0
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -267,9 +267,654 @@ function DataTable(props) {
|
|
|
267
267
|
)
|
|
268
268
|
] }) });
|
|
269
269
|
}
|
|
270
|
+
function ChartContainer({
|
|
271
|
+
height = 300,
|
|
272
|
+
className,
|
|
273
|
+
children
|
|
274
|
+
}) {
|
|
275
|
+
const containerRef = react.useRef(null);
|
|
276
|
+
const [width, setWidth] = react.useState(0);
|
|
277
|
+
const handleResize = react.useCallback(() => {
|
|
278
|
+
if (containerRef.current) {
|
|
279
|
+
setWidth(containerRef.current.clientWidth);
|
|
280
|
+
}
|
|
281
|
+
}, []);
|
|
282
|
+
react.useEffect(() => {
|
|
283
|
+
const el = containerRef.current;
|
|
284
|
+
if (!el) return;
|
|
285
|
+
handleResize();
|
|
286
|
+
const observer = new ResizeObserver(() => {
|
|
287
|
+
handleResize();
|
|
288
|
+
});
|
|
289
|
+
observer.observe(el);
|
|
290
|
+
return () => observer.disconnect();
|
|
291
|
+
}, [handleResize]);
|
|
292
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
293
|
+
"div",
|
|
294
|
+
{
|
|
295
|
+
ref: containerRef,
|
|
296
|
+
className: className ? `entropix-chart ${className}` : "entropix-chart",
|
|
297
|
+
style: { position: "relative", width: "100%" },
|
|
298
|
+
children: width > 0 && /* @__PURE__ */ jsxRuntime.jsx(
|
|
299
|
+
"svg",
|
|
300
|
+
{
|
|
301
|
+
className: "entropix-chart__svg",
|
|
302
|
+
width,
|
|
303
|
+
height,
|
|
304
|
+
viewBox: `0 0 ${width} ${height}`,
|
|
305
|
+
children: children(width, height)
|
|
306
|
+
}
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
function XAxis({ scale, y, height: _height, formatter }) {
|
|
312
|
+
const domain = scale.domain();
|
|
313
|
+
const bandwidth = scale.bandwidth();
|
|
314
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { children: [
|
|
315
|
+
domain.map((label) => {
|
|
316
|
+
const x = scale(label) + bandwidth / 2;
|
|
317
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { transform: `translate(${x}, ${y})`, children: [
|
|
318
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { y2: 6, stroke: "currentColor", strokeWidth: 1 }),
|
|
319
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
320
|
+
"text",
|
|
321
|
+
{
|
|
322
|
+
className: "entropix-chart__axis-text",
|
|
323
|
+
y: 18,
|
|
324
|
+
textAnchor: "middle",
|
|
325
|
+
dominantBaseline: "auto",
|
|
326
|
+
children: formatter ? formatter(label) : label
|
|
327
|
+
}
|
|
328
|
+
)
|
|
329
|
+
] }, label);
|
|
330
|
+
}),
|
|
331
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
332
|
+
"line",
|
|
333
|
+
{
|
|
334
|
+
x1: scale.range()[0],
|
|
335
|
+
x2: scale.range()[1],
|
|
336
|
+
y1: y,
|
|
337
|
+
y2: y,
|
|
338
|
+
stroke: "currentColor",
|
|
339
|
+
strokeWidth: 1
|
|
340
|
+
}
|
|
341
|
+
)
|
|
342
|
+
] });
|
|
343
|
+
}
|
|
344
|
+
function YAxis({
|
|
345
|
+
scale,
|
|
346
|
+
x,
|
|
347
|
+
width,
|
|
348
|
+
formatter,
|
|
349
|
+
showGrid = true
|
|
350
|
+
}) {
|
|
351
|
+
const ticks = scale.ticks();
|
|
352
|
+
return /* @__PURE__ */ jsxRuntime.jsx("g", { children: ticks.map((tick) => {
|
|
353
|
+
const y = scale(tick);
|
|
354
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { transform: `translate(${x}, ${y})`, children: [
|
|
355
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
356
|
+
"text",
|
|
357
|
+
{
|
|
358
|
+
className: "entropix-chart__axis-text",
|
|
359
|
+
x: -8,
|
|
360
|
+
textAnchor: "end",
|
|
361
|
+
dominantBaseline: "middle",
|
|
362
|
+
children: formatter ? formatter(tick) : String(tick)
|
|
363
|
+
}
|
|
364
|
+
),
|
|
365
|
+
showGrid && /* @__PURE__ */ jsxRuntime.jsx(
|
|
366
|
+
"line",
|
|
367
|
+
{
|
|
368
|
+
className: "entropix-chart__grid-line",
|
|
369
|
+
x1: 0,
|
|
370
|
+
x2: width,
|
|
371
|
+
y1: 0,
|
|
372
|
+
y2: 0
|
|
373
|
+
}
|
|
374
|
+
)
|
|
375
|
+
] }, tick);
|
|
376
|
+
}) });
|
|
377
|
+
}
|
|
378
|
+
function ChartTooltip({ data, containerRef }) {
|
|
379
|
+
const tooltipRef = react.useRef(null);
|
|
380
|
+
const [position, setPosition] = react.useState({
|
|
381
|
+
left: 0,
|
|
382
|
+
top: 0
|
|
383
|
+
});
|
|
384
|
+
react.useEffect(() => {
|
|
385
|
+
if (!data || !tooltipRef.current || !containerRef.current) return;
|
|
386
|
+
const container = containerRef.current;
|
|
387
|
+
const tooltip = tooltipRef.current;
|
|
388
|
+
const containerRect = container.getBoundingClientRect();
|
|
389
|
+
const tooltipWidth = tooltip.offsetWidth;
|
|
390
|
+
const tooltipHeight = tooltip.offsetHeight;
|
|
391
|
+
let left = data.x + 12;
|
|
392
|
+
let top = data.y - tooltipHeight / 2;
|
|
393
|
+
if (left + tooltipWidth > containerRect.width) {
|
|
394
|
+
left = data.x - tooltipWidth - 12;
|
|
395
|
+
}
|
|
396
|
+
if (top < 0) {
|
|
397
|
+
top = 0;
|
|
398
|
+
}
|
|
399
|
+
if (top + tooltipHeight > containerRect.height) {
|
|
400
|
+
top = containerRect.height - tooltipHeight;
|
|
401
|
+
}
|
|
402
|
+
setPosition({ left, top });
|
|
403
|
+
}, [data, containerRef]);
|
|
404
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
405
|
+
"div",
|
|
406
|
+
{
|
|
407
|
+
ref: tooltipRef,
|
|
408
|
+
className: `entropix-chart__tooltip${data ? " entropix-chart__tooltip--visible" : ""}`,
|
|
409
|
+
style: {
|
|
410
|
+
left: position.left,
|
|
411
|
+
top: position.top
|
|
412
|
+
},
|
|
413
|
+
children: data && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "entropix-chart__tooltip-row", children: [
|
|
414
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
415
|
+
"span",
|
|
416
|
+
{
|
|
417
|
+
className: "entropix-chart__tooltip-swatch",
|
|
418
|
+
style: { backgroundColor: data.color }
|
|
419
|
+
}
|
|
420
|
+
),
|
|
421
|
+
/* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
|
|
422
|
+
data.series,
|
|
423
|
+
": ",
|
|
424
|
+
data.label,
|
|
425
|
+
" \u2014 ",
|
|
426
|
+
data.value
|
|
427
|
+
] })
|
|
428
|
+
] })
|
|
429
|
+
}
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
function ChartLegend({ items, onToggle }) {
|
|
433
|
+
if (items.length === 0) return null;
|
|
434
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "entropix-chart__legend", children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
435
|
+
"button",
|
|
436
|
+
{
|
|
437
|
+
type: "button",
|
|
438
|
+
className: `entropix-chart__legend-item${!item.active ? " entropix-chart__legend-item--inactive" : ""}`,
|
|
439
|
+
onClick: () => onToggle(item.name),
|
|
440
|
+
children: [
|
|
441
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
442
|
+
"span",
|
|
443
|
+
{
|
|
444
|
+
className: "entropix-chart__legend-swatch",
|
|
445
|
+
style: { backgroundColor: item.color }
|
|
446
|
+
}
|
|
447
|
+
),
|
|
448
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: item.name })
|
|
449
|
+
]
|
|
450
|
+
},
|
|
451
|
+
item.name
|
|
452
|
+
)) });
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/utils/chart-colors.ts
|
|
456
|
+
var CSS_CHART_COLORS = [
|
|
457
|
+
"var(--chart-series-1)",
|
|
458
|
+
"var(--chart-series-2)",
|
|
459
|
+
"var(--chart-series-3)",
|
|
460
|
+
"var(--chart-series-4)",
|
|
461
|
+
"var(--chart-series-5)",
|
|
462
|
+
"var(--chart-series-6)",
|
|
463
|
+
"var(--chart-series-7)",
|
|
464
|
+
"var(--chart-series-8)"
|
|
465
|
+
];
|
|
466
|
+
var MARGINS = { top: 20, right: 20, bottom: 40, left: 50 };
|
|
467
|
+
function BarChart({
|
|
468
|
+
data,
|
|
469
|
+
height = 300,
|
|
470
|
+
colors,
|
|
471
|
+
stacked = false,
|
|
472
|
+
showGrid = true,
|
|
473
|
+
showTooltip = true,
|
|
474
|
+
showLegend = true,
|
|
475
|
+
xAxis,
|
|
476
|
+
yAxis,
|
|
477
|
+
className
|
|
478
|
+
}) {
|
|
479
|
+
const [hiddenSeries, setHiddenSeries] = react.useState(/* @__PURE__ */ new Set());
|
|
480
|
+
const [tooltip, setTooltip] = react.useState(null);
|
|
481
|
+
const containerRef = react.useRef(null);
|
|
482
|
+
const toggleSeries = react.useCallback((name) => {
|
|
483
|
+
setHiddenSeries((prev) => {
|
|
484
|
+
const next = new Set(prev);
|
|
485
|
+
if (next.has(name)) {
|
|
486
|
+
next.delete(name);
|
|
487
|
+
} else {
|
|
488
|
+
next.add(name);
|
|
489
|
+
}
|
|
490
|
+
return next;
|
|
491
|
+
});
|
|
492
|
+
}, []);
|
|
493
|
+
const allSeries = core.normalizeChartData(data, colors ?? CSS_CHART_COLORS);
|
|
494
|
+
const visibleSeries = allSeries.filter((s) => !hiddenSeries.has(s.name));
|
|
495
|
+
const { categories, yMin, yMax } = core.getDataExtent(visibleSeries);
|
|
496
|
+
const legendItems = allSeries.map((s) => ({
|
|
497
|
+
name: s.name,
|
|
498
|
+
color: s.color,
|
|
499
|
+
active: !hiddenSeries.has(s.name)
|
|
500
|
+
}));
|
|
501
|
+
const handleBarEnter = react.useCallback(
|
|
502
|
+
(rect, seriesName, event) => {
|
|
503
|
+
if (!showTooltip) return;
|
|
504
|
+
const svg = event.target.closest("svg");
|
|
505
|
+
if (!svg) return;
|
|
506
|
+
svg.getBoundingClientRect();
|
|
507
|
+
const container = containerRef.current;
|
|
508
|
+
if (!container) return;
|
|
509
|
+
container.getBoundingClientRect();
|
|
510
|
+
setTooltip({
|
|
511
|
+
x: rect.x + rect.width / 2 + MARGINS.left,
|
|
512
|
+
y: rect.y + MARGINS.top,
|
|
513
|
+
series: seriesName,
|
|
514
|
+
label: rect.label,
|
|
515
|
+
value: rect.value,
|
|
516
|
+
color: rect.color
|
|
517
|
+
});
|
|
518
|
+
},
|
|
519
|
+
[showTooltip]
|
|
520
|
+
);
|
|
521
|
+
const handleBarLeave = react.useCallback(() => {
|
|
522
|
+
setTooltip(null);
|
|
523
|
+
}, []);
|
|
524
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: className ? `entropix-chart ${className}` : "entropix-chart", style: { position: "relative" }, children: [
|
|
525
|
+
/* @__PURE__ */ jsxRuntime.jsx(ChartContainer, { height, children: (width, h) => {
|
|
526
|
+
const innerWidth = width - MARGINS.left - MARGINS.right;
|
|
527
|
+
const innerHeight = h - MARGINS.top - MARGINS.bottom;
|
|
528
|
+
if (innerWidth <= 0 || innerHeight <= 0) return null;
|
|
529
|
+
const bounds = core.niceBounds(yMin, yMax, yAxis?.tickCount ?? 5);
|
|
530
|
+
const yScale = core.createLinearScale(
|
|
531
|
+
[bounds.min, bounds.max],
|
|
532
|
+
[innerHeight, 0]
|
|
533
|
+
);
|
|
534
|
+
const xScale = core.createBandScale(categories, [0, innerWidth]);
|
|
535
|
+
const bars = core.computeBarGeometry(
|
|
536
|
+
visibleSeries,
|
|
537
|
+
xScale,
|
|
538
|
+
yScale,
|
|
539
|
+
innerHeight,
|
|
540
|
+
stacked
|
|
541
|
+
);
|
|
542
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { transform: `translate(${MARGINS.left}, ${MARGINS.top})`, children: [
|
|
543
|
+
yAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
544
|
+
YAxis,
|
|
545
|
+
{
|
|
546
|
+
scale: yScale,
|
|
547
|
+
x: 0,
|
|
548
|
+
width: innerWidth,
|
|
549
|
+
showGrid,
|
|
550
|
+
formatter: yAxis?.formatter
|
|
551
|
+
}
|
|
552
|
+
),
|
|
553
|
+
xAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
554
|
+
XAxis,
|
|
555
|
+
{
|
|
556
|
+
scale: xScale,
|
|
557
|
+
y: innerHeight,
|
|
558
|
+
height: innerHeight,
|
|
559
|
+
formatter: xAxis?.formatter
|
|
560
|
+
}
|
|
561
|
+
),
|
|
562
|
+
bars.map((rect, i) => {
|
|
563
|
+
const series = visibleSeries[rect.seriesIndex];
|
|
564
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
565
|
+
"rect",
|
|
566
|
+
{
|
|
567
|
+
className: "entropix-chart__bar",
|
|
568
|
+
x: rect.x,
|
|
569
|
+
y: rect.y,
|
|
570
|
+
width: rect.width,
|
|
571
|
+
height: rect.height,
|
|
572
|
+
fill: rect.color,
|
|
573
|
+
onMouseEnter: (e) => handleBarEnter(rect, series?.name ?? "", e),
|
|
574
|
+
onMouseLeave: handleBarLeave
|
|
575
|
+
},
|
|
576
|
+
i
|
|
577
|
+
);
|
|
578
|
+
})
|
|
579
|
+
] });
|
|
580
|
+
} }),
|
|
581
|
+
showTooltip && /* @__PURE__ */ jsxRuntime.jsx(ChartTooltip, { data: tooltip, containerRef }),
|
|
582
|
+
showLegend && allSeries.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(ChartLegend, { items: legendItems, onToggle: toggleSeries })
|
|
583
|
+
] });
|
|
584
|
+
}
|
|
585
|
+
var MARGINS2 = { top: 20, right: 20, bottom: 40, left: 50 };
|
|
586
|
+
function LineChart({
|
|
587
|
+
data,
|
|
588
|
+
height = 300,
|
|
589
|
+
colors,
|
|
590
|
+
curved = false,
|
|
591
|
+
showPoints = true,
|
|
592
|
+
showGrid = true,
|
|
593
|
+
showTooltip = true,
|
|
594
|
+
showLegend = true,
|
|
595
|
+
xAxis,
|
|
596
|
+
yAxis,
|
|
597
|
+
className
|
|
598
|
+
}) {
|
|
599
|
+
const [hiddenSeries, setHiddenSeries] = react.useState(/* @__PURE__ */ new Set());
|
|
600
|
+
const [tooltip, setTooltip] = react.useState(null);
|
|
601
|
+
const containerRef = react.useRef(null);
|
|
602
|
+
const toggleSeries = react.useCallback((name) => {
|
|
603
|
+
setHiddenSeries((prev) => {
|
|
604
|
+
const next = new Set(prev);
|
|
605
|
+
if (next.has(name)) next.delete(name);
|
|
606
|
+
else next.add(name);
|
|
607
|
+
return next;
|
|
608
|
+
});
|
|
609
|
+
}, []);
|
|
610
|
+
const allSeries = core.normalizeChartData(data, colors ?? CSS_CHART_COLORS);
|
|
611
|
+
const visibleSeries = allSeries.filter((s) => !hiddenSeries.has(s.name));
|
|
612
|
+
const { categories, yMin, yMax } = core.getDataExtent(visibleSeries);
|
|
613
|
+
const legendItems = allSeries.map((s) => ({
|
|
614
|
+
name: s.name,
|
|
615
|
+
color: s.color,
|
|
616
|
+
active: !hiddenSeries.has(s.name)
|
|
617
|
+
}));
|
|
618
|
+
const handlePointEnter = react.useCallback(
|
|
619
|
+
(point, seriesName, color) => {
|
|
620
|
+
if (!showTooltip) return;
|
|
621
|
+
setTooltip({
|
|
622
|
+
x: point.x + MARGINS2.left,
|
|
623
|
+
y: point.y + MARGINS2.top,
|
|
624
|
+
series: seriesName,
|
|
625
|
+
label: point.label,
|
|
626
|
+
value: point.value,
|
|
627
|
+
color
|
|
628
|
+
});
|
|
629
|
+
},
|
|
630
|
+
[showTooltip]
|
|
631
|
+
);
|
|
632
|
+
const handlePointLeave = react.useCallback(() => {
|
|
633
|
+
setTooltip(null);
|
|
634
|
+
}, []);
|
|
635
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: className ? `entropix-chart ${className}` : "entropix-chart", style: { position: "relative" }, children: [
|
|
636
|
+
/* @__PURE__ */ jsxRuntime.jsx(ChartContainer, { height, children: (width, h) => {
|
|
637
|
+
const innerWidth = width - MARGINS2.left - MARGINS2.right;
|
|
638
|
+
const innerHeight = h - MARGINS2.top - MARGINS2.bottom;
|
|
639
|
+
if (innerWidth <= 0 || innerHeight <= 0) return null;
|
|
640
|
+
const bounds = core.niceBounds(yMin, yMax, yAxis?.tickCount ?? 5);
|
|
641
|
+
const yScale = core.createLinearScale(
|
|
642
|
+
[bounds.min, bounds.max],
|
|
643
|
+
[innerHeight, 0]
|
|
644
|
+
);
|
|
645
|
+
const xScale = core.createBandScale(categories, [0, innerWidth]);
|
|
646
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { transform: `translate(${MARGINS2.left}, ${MARGINS2.top})`, children: [
|
|
647
|
+
yAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
648
|
+
YAxis,
|
|
649
|
+
{
|
|
650
|
+
scale: yScale,
|
|
651
|
+
x: 0,
|
|
652
|
+
width: innerWidth,
|
|
653
|
+
showGrid,
|
|
654
|
+
formatter: yAxis?.formatter
|
|
655
|
+
}
|
|
656
|
+
),
|
|
657
|
+
xAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
658
|
+
XAxis,
|
|
659
|
+
{
|
|
660
|
+
scale: xScale,
|
|
661
|
+
y: innerHeight,
|
|
662
|
+
height: innerHeight,
|
|
663
|
+
formatter: xAxis?.formatter
|
|
664
|
+
}
|
|
665
|
+
),
|
|
666
|
+
visibleSeries.map((series) => {
|
|
667
|
+
const points = core.computeLinePoints(series, xScale, yScale);
|
|
668
|
+
const pathD = core.describeLinePath(points, curved);
|
|
669
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { children: [
|
|
670
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
671
|
+
"path",
|
|
672
|
+
{
|
|
673
|
+
className: "entropix-chart__line",
|
|
674
|
+
d: pathD,
|
|
675
|
+
stroke: series.color
|
|
676
|
+
}
|
|
677
|
+
),
|
|
678
|
+
showPoints && points.map((pt, i) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
679
|
+
"circle",
|
|
680
|
+
{
|
|
681
|
+
className: "entropix-chart__point",
|
|
682
|
+
cx: pt.x,
|
|
683
|
+
cy: pt.y,
|
|
684
|
+
r: 3.5,
|
|
685
|
+
fill: series.color,
|
|
686
|
+
onMouseEnter: () => handlePointEnter(pt, series.name, series.color),
|
|
687
|
+
onMouseLeave: handlePointLeave
|
|
688
|
+
},
|
|
689
|
+
i
|
|
690
|
+
))
|
|
691
|
+
] }, series.name);
|
|
692
|
+
})
|
|
693
|
+
] });
|
|
694
|
+
} }),
|
|
695
|
+
showTooltip && /* @__PURE__ */ jsxRuntime.jsx(ChartTooltip, { data: tooltip, containerRef }),
|
|
696
|
+
showLegend && allSeries.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(ChartLegend, { items: legendItems, onToggle: toggleSeries })
|
|
697
|
+
] });
|
|
698
|
+
}
|
|
699
|
+
var MARGINS3 = { top: 20, right: 20, bottom: 40, left: 50 };
|
|
700
|
+
function AreaChart({
|
|
701
|
+
data,
|
|
702
|
+
height = 300,
|
|
703
|
+
colors,
|
|
704
|
+
curved = false,
|
|
705
|
+
opacity = 0.3,
|
|
706
|
+
showGrid = true,
|
|
707
|
+
showTooltip = true,
|
|
708
|
+
showLegend = true,
|
|
709
|
+
xAxis,
|
|
710
|
+
yAxis,
|
|
711
|
+
className
|
|
712
|
+
}) {
|
|
713
|
+
const [hiddenSeries, setHiddenSeries] = react.useState(/* @__PURE__ */ new Set());
|
|
714
|
+
const [tooltip, setTooltip] = react.useState(null);
|
|
715
|
+
const containerRef = react.useRef(null);
|
|
716
|
+
const toggleSeries = react.useCallback((name) => {
|
|
717
|
+
setHiddenSeries((prev) => {
|
|
718
|
+
const next = new Set(prev);
|
|
719
|
+
if (next.has(name)) next.delete(name);
|
|
720
|
+
else next.add(name);
|
|
721
|
+
return next;
|
|
722
|
+
});
|
|
723
|
+
}, []);
|
|
724
|
+
const allSeries = core.normalizeChartData(data, colors ?? CSS_CHART_COLORS);
|
|
725
|
+
const visibleSeries = allSeries.filter((s) => !hiddenSeries.has(s.name));
|
|
726
|
+
const { categories, yMin, yMax } = core.getDataExtent(visibleSeries);
|
|
727
|
+
const legendItems = allSeries.map((s) => ({
|
|
728
|
+
name: s.name,
|
|
729
|
+
color: s.color,
|
|
730
|
+
active: !hiddenSeries.has(s.name)
|
|
731
|
+
}));
|
|
732
|
+
const handlePointEnter = react.useCallback(
|
|
733
|
+
(point, seriesName, color) => {
|
|
734
|
+
if (!showTooltip) return;
|
|
735
|
+
setTooltip({
|
|
736
|
+
x: point.x + MARGINS3.left,
|
|
737
|
+
y: point.y + MARGINS3.top,
|
|
738
|
+
series: seriesName,
|
|
739
|
+
label: point.label,
|
|
740
|
+
value: point.value,
|
|
741
|
+
color
|
|
742
|
+
});
|
|
743
|
+
},
|
|
744
|
+
[showTooltip]
|
|
745
|
+
);
|
|
746
|
+
const handlePointLeave = react.useCallback(() => {
|
|
747
|
+
setTooltip(null);
|
|
748
|
+
}, []);
|
|
749
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: className ? `entropix-chart ${className}` : "entropix-chart", style: { position: "relative" }, children: [
|
|
750
|
+
/* @__PURE__ */ jsxRuntime.jsx(ChartContainer, { height, children: (width, h) => {
|
|
751
|
+
const innerWidth = width - MARGINS3.left - MARGINS3.right;
|
|
752
|
+
const innerHeight = h - MARGINS3.top - MARGINS3.bottom;
|
|
753
|
+
if (innerWidth <= 0 || innerHeight <= 0) return null;
|
|
754
|
+
const bounds = core.niceBounds(yMin, yMax, yAxis?.tickCount ?? 5);
|
|
755
|
+
const yScale = core.createLinearScale(
|
|
756
|
+
[bounds.min, bounds.max],
|
|
757
|
+
[innerHeight, 0]
|
|
758
|
+
);
|
|
759
|
+
const xScale = core.createBandScale(categories, [0, innerWidth]);
|
|
760
|
+
const baselineY = yScale(bounds.min);
|
|
761
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { transform: `translate(${MARGINS3.left}, ${MARGINS3.top})`, children: [
|
|
762
|
+
yAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
763
|
+
YAxis,
|
|
764
|
+
{
|
|
765
|
+
scale: yScale,
|
|
766
|
+
x: 0,
|
|
767
|
+
width: innerWidth,
|
|
768
|
+
showGrid,
|
|
769
|
+
formatter: yAxis?.formatter
|
|
770
|
+
}
|
|
771
|
+
),
|
|
772
|
+
xAxis?.show !== false && /* @__PURE__ */ jsxRuntime.jsx(
|
|
773
|
+
XAxis,
|
|
774
|
+
{
|
|
775
|
+
scale: xScale,
|
|
776
|
+
y: innerHeight,
|
|
777
|
+
height: innerHeight,
|
|
778
|
+
formatter: xAxis?.formatter
|
|
779
|
+
}
|
|
780
|
+
),
|
|
781
|
+
visibleSeries.map((series) => {
|
|
782
|
+
const points = core.computeLinePoints(series, xScale, yScale);
|
|
783
|
+
const areaD = core.describeAreaPath(points, baselineY, curved);
|
|
784
|
+
const lineD = core.describeLinePath(points, curved);
|
|
785
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("g", { children: [
|
|
786
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
787
|
+
"path",
|
|
788
|
+
{
|
|
789
|
+
className: "entropix-chart__area",
|
|
790
|
+
d: areaD,
|
|
791
|
+
fill: series.color,
|
|
792
|
+
fillOpacity: opacity
|
|
793
|
+
}
|
|
794
|
+
),
|
|
795
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
796
|
+
"path",
|
|
797
|
+
{
|
|
798
|
+
className: "entropix-chart__line",
|
|
799
|
+
d: lineD,
|
|
800
|
+
stroke: series.color
|
|
801
|
+
}
|
|
802
|
+
),
|
|
803
|
+
points.map((pt, i) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
804
|
+
"circle",
|
|
805
|
+
{
|
|
806
|
+
className: "entropix-chart__point",
|
|
807
|
+
cx: pt.x,
|
|
808
|
+
cy: pt.y,
|
|
809
|
+
r: 3,
|
|
810
|
+
fill: series.color,
|
|
811
|
+
onMouseEnter: () => handlePointEnter(pt, series.name, series.color),
|
|
812
|
+
onMouseLeave: handlePointLeave
|
|
813
|
+
},
|
|
814
|
+
i
|
|
815
|
+
))
|
|
816
|
+
] }, series.name);
|
|
817
|
+
})
|
|
818
|
+
] });
|
|
819
|
+
} }),
|
|
820
|
+
showTooltip && /* @__PURE__ */ jsxRuntime.jsx(ChartTooltip, { data: tooltip, containerRef }),
|
|
821
|
+
showLegend && allSeries.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(ChartLegend, { items: legendItems, onToggle: toggleSeries })
|
|
822
|
+
] });
|
|
823
|
+
}
|
|
824
|
+
function PieChart({
|
|
825
|
+
data,
|
|
826
|
+
height = 300,
|
|
827
|
+
colors,
|
|
828
|
+
innerRadius = 0,
|
|
829
|
+
showTooltip = true,
|
|
830
|
+
showLegend = true,
|
|
831
|
+
className
|
|
832
|
+
}) {
|
|
833
|
+
const [hiddenSlices, setHiddenSlices] = react.useState(/* @__PURE__ */ new Set());
|
|
834
|
+
const [tooltip, setTooltip] = react.useState(null);
|
|
835
|
+
const containerRef = react.useRef(null);
|
|
836
|
+
const toggleSlice = react.useCallback((name) => {
|
|
837
|
+
setHiddenSlices((prev) => {
|
|
838
|
+
const next = new Set(prev);
|
|
839
|
+
if (next.has(name)) next.delete(name);
|
|
840
|
+
else next.add(name);
|
|
841
|
+
return next;
|
|
842
|
+
});
|
|
843
|
+
}, []);
|
|
844
|
+
const palette = colors ?? CSS_CHART_COLORS;
|
|
845
|
+
const coloredData = data.map((d, i) => ({
|
|
846
|
+
...d,
|
|
847
|
+
color: core.getSeriesColor(i, palette)
|
|
848
|
+
}));
|
|
849
|
+
const visibleData = coloredData.filter((d) => !hiddenSlices.has(d.label));
|
|
850
|
+
const legendItems = coloredData.map((d) => ({
|
|
851
|
+
name: d.label,
|
|
852
|
+
color: d.color,
|
|
853
|
+
active: !hiddenSlices.has(d.label)
|
|
854
|
+
}));
|
|
855
|
+
const handleArcEnter = react.useCallback(
|
|
856
|
+
(label, value, percentage, color, cx, cy) => {
|
|
857
|
+
if (!showTooltip) return;
|
|
858
|
+
setTooltip({
|
|
859
|
+
x: cx,
|
|
860
|
+
y: cy,
|
|
861
|
+
series: `${(percentage * 100).toFixed(1)}%`,
|
|
862
|
+
label,
|
|
863
|
+
value,
|
|
864
|
+
color
|
|
865
|
+
});
|
|
866
|
+
},
|
|
867
|
+
[showTooltip]
|
|
868
|
+
);
|
|
869
|
+
const handleArcLeave = react.useCallback(() => {
|
|
870
|
+
setTooltip(null);
|
|
871
|
+
}, []);
|
|
872
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: className ? `entropix-chart ${className}` : "entropix-chart", style: { position: "relative" }, children: [
|
|
873
|
+
/* @__PURE__ */ jsxRuntime.jsx(ChartContainer, { height, children: (width, h) => {
|
|
874
|
+
const cx = width / 2;
|
|
875
|
+
const cy = h / 2;
|
|
876
|
+
const outerRadius = Math.min(cx, cy) - 10;
|
|
877
|
+
if (outerRadius <= 0) return null;
|
|
878
|
+
const slices = core.computeArcGeometry(
|
|
879
|
+
visibleData,
|
|
880
|
+
outerRadius,
|
|
881
|
+
innerRadius
|
|
882
|
+
);
|
|
883
|
+
return /* @__PURE__ */ jsxRuntime.jsx("g", { transform: `translate(${cx}, ${cy})`, children: slices.map((slice, i) => {
|
|
884
|
+
const midAngle = (slice.startAngle + slice.endAngle) / 2;
|
|
885
|
+
const tooltipX = cx + outerRadius * 0.6 * Math.cos(midAngle);
|
|
886
|
+
const tooltipY = cy + outerRadius * 0.6 * Math.sin(midAngle);
|
|
887
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
888
|
+
"path",
|
|
889
|
+
{
|
|
890
|
+
className: "entropix-chart__arc",
|
|
891
|
+
d: slice.path,
|
|
892
|
+
fill: slice.color,
|
|
893
|
+
onMouseEnter: () => handleArcEnter(
|
|
894
|
+
slice.label,
|
|
895
|
+
slice.value,
|
|
896
|
+
slice.percentage,
|
|
897
|
+
slice.color,
|
|
898
|
+
tooltipX,
|
|
899
|
+
tooltipY
|
|
900
|
+
),
|
|
901
|
+
onMouseLeave: handleArcLeave
|
|
902
|
+
},
|
|
903
|
+
i
|
|
904
|
+
);
|
|
905
|
+
}) });
|
|
906
|
+
} }),
|
|
907
|
+
showTooltip && /* @__PURE__ */ jsxRuntime.jsx(ChartTooltip, { data: tooltip, containerRef }),
|
|
908
|
+
showLegend && /* @__PURE__ */ jsxRuntime.jsx(ChartLegend, { items: legendItems, onToggle: toggleSlice })
|
|
909
|
+
] });
|
|
910
|
+
}
|
|
270
911
|
|
|
912
|
+
exports.AreaChart = AreaChart;
|
|
913
|
+
exports.BarChart = BarChart;
|
|
271
914
|
exports.DataTable = DataTable;
|
|
272
915
|
exports.DataTableContext = DataTableContext;
|
|
916
|
+
exports.LineChart = LineChart;
|
|
917
|
+
exports.PieChart = PieChart;
|
|
273
918
|
exports.useDataTableContext = useDataTableContext;
|
|
274
919
|
//# sourceMappingURL=index.cjs.map
|
|
275
920
|
//# sourceMappingURL=index.cjs.map
|