@aiconstructions/graficos 0.0.0-stage → 0.2.1
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/ALTERACOES.md +53 -0
- package/LICENSE-BKLIT +21 -0
- package/LICENSE-visx-pattern +21 -0
- package/README.md +188 -2
- package/dist/aic/cores.js +83 -0
- package/dist/aic/index.js +22 -0
- package/dist/aic/interacao.js +55 -0
- package/dist/charts/animation.js +55 -0
- package/dist/charts/arc-geometry.js +58 -0
- package/dist/charts/arc-path.js +15 -0
- package/dist/charts/area-chart-loading.js +89 -0
- package/dist/charts/area-chart.js +149 -0
- package/dist/charts/area-gradient-defs.js +90 -0
- package/dist/charts/area-value-label-layout.js +70 -0
- package/dist/charts/area-value-labels.js +211 -0
- package/dist/charts/area.js +272 -0
- package/dist/charts/background.js +198 -0
- package/dist/charts/band-config.js +82 -0
- package/dist/charts/band-geometry.js +186 -0
- package/dist/charts/band.js +346 -0
- package/dist/charts/bar-chart-loading.js +27 -0
- package/dist/charts/bar-chart.js +545 -0
- package/dist/charts/bar-depth-geometry.js +18 -0
- package/dist/charts/bar-depth.js +608 -0
- package/dist/charts/bar-lollipop.js +278 -0
- package/dist/charts/bar-squares-layout.js +66 -0
- package/dist/charts/bar-squares.js +467 -0
- package/dist/charts/bar-value-label-layout.js +42 -0
- package/dist/charts/bar-x-axis.js +133 -0
- package/dist/charts/bar-y-axis.js +116 -0
- package/dist/charts/bar.js +457 -0
- package/dist/charts/bollinger.js +49 -0
- package/dist/charts/candlestick-chart.js +370 -0
- package/dist/charts/candlestick.js +368 -0
- package/dist/charts/chart-brush-handle.js +91 -0
- package/dist/charts/chart-brush-layout.js +91 -0
- package/dist/charts/chart-brush-selection-overlay.js +87 -0
- package/dist/charts/chart-brush-state.js +253 -0
- package/dist/charts/chart-brush-track-overlay.js +103 -0
- package/dist/charts/chart-brush.js +517 -0
- package/dist/charts/chart-center-typography.js +8 -0
- package/dist/charts/chart-child-passthrough.js +119 -0
- package/dist/charts/chart-config-context.js +56 -0
- package/dist/charts/chart-context.js +337 -0
- package/dist/charts/chart-crosshair.js +262 -0
- package/dist/charts/chart-data-override.js +186 -0
- package/dist/charts/chart-defs.js +56 -0
- package/dist/charts/chart-formatters.js +77 -0
- package/dist/charts/chart-legend-hover.js +27 -0
- package/dist/charts/chart-legend.js +163 -0
- package/dist/charts/chart-loading-label.js +52 -0
- package/dist/charts/chart-parent-size.js +138 -0
- package/dist/charts/chart-phase.js +29 -0
- package/dist/charts/chart-reveal-clip.js +62 -0
- package/dist/charts/chart-scale.js +19 -0
- package/dist/charts/chart-stat-flow.js +72 -0
- package/dist/charts/choropleth/choropleth-background.js +29 -0
- package/dist/charts/choropleth/choropleth-camera.js +379 -0
- package/dist/charts/choropleth/choropleth-chart.js +644 -0
- package/dist/charts/choropleth/choropleth-context.js +152 -0
- package/dist/charts/choropleth/choropleth-dot-grid.js +293 -0
- package/dist/charts/choropleth/choropleth-feature.js +291 -0
- package/dist/charts/choropleth/choropleth-graticule.js +40 -0
- package/dist/charts/choropleth/choropleth-marker.js +222 -0
- package/dist/charts/choropleth/choropleth-projection.js +37 -0
- package/dist/charts/choropleth/choropleth-tooltip.js +82 -0
- package/dist/charts/choropleth/choropleth-zoom-matrix.js +150 -0
- package/dist/charts/choropleth/index.js +44 -0
- package/dist/charts/choropleth/use-choropleth-zoom-state.js +636 -0
- package/dist/charts/composed-chart.js +346 -0
- package/dist/charts/crosshair-store.js +49 -0
- package/dist/charts/dash-tail-stroke.js +58 -0
- package/dist/charts/date-pill-store.js +50 -0
- package/dist/charts/date-ranges.js +131 -0
- package/dist/charts/deadline.js +295 -0
- package/dist/charts/decimate-time-series.js +112 -0
- package/dist/charts/fade-edges.js +171 -0
- package/dist/charts/fast-path.js +78 -0
- package/dist/charts/filter-data-by-x-domain.js +45 -0
- package/dist/charts/funnel-chart.js +773 -0
- package/dist/charts/gauge-arc-layers.js +479 -0
- package/dist/charts/gauge-dial.js +329 -0
- package/dist/charts/gauge-label-layout.js +148 -0
- package/dist/charts/gauge.js +946 -0
- package/dist/charts/generate-chart-skeleton-data.js +27 -0
- package/dist/charts/gradients.js +158 -0
- package/dist/charts/grid-lines.js +114 -0
- package/dist/charts/grid.js +225 -0
- package/dist/charts/heatmap/generate-heatmap-skeleton-data.js +13 -0
- package/dist/charts/heatmap/heatmap-animation.js +128 -0
- package/dist/charts/heatmap/heatmap-cells.js +519 -0
- package/dist/charts/heatmap/heatmap-chart-loading.js +44 -0
- package/dist/charts/heatmap/heatmap-chart.js +548 -0
- package/dist/charts/heatmap/heatmap-colors.js +127 -0
- package/dist/charts/heatmap/heatmap-context.js +107 -0
- package/dist/charts/heatmap/heatmap-legend-gradient.js +97 -0
- package/dist/charts/heatmap/heatmap-legend-swatch.js +76 -0
- package/dist/charts/heatmap/heatmap-legend.js +165 -0
- package/dist/charts/heatmap/heatmap-pattern-defs.js +37 -0
- package/dist/charts/heatmap/heatmap-rect-cells.js +39 -0
- package/dist/charts/heatmap/heatmap-resolve-separator.js +84 -0
- package/dist/charts/heatmap/heatmap-separator.js +168 -0
- package/dist/charts/heatmap/heatmap-tooltip.js +73 -0
- package/dist/charts/heatmap/heatmap-utils.js +633 -0
- package/dist/charts/heatmap/heatmap-x-axis.js +79 -0
- package/dist/charts/heatmap/heatmap-y-axis.js +72 -0
- package/dist/charts/heatmap/index.js +208 -0
- package/dist/charts/highlight-segment-bounds.js +34 -0
- package/dist/charts/highlight-segment.js +119 -0
- package/dist/charts/index.js +944 -0
- package/dist/charts/indicator-fade.js +44 -0
- package/dist/charts/legend/index.js +22 -0
- package/dist/charts/legend/legend-context.js +47 -0
- package/dist/charts/legend/legend-item.js +30 -0
- package/dist/charts/legend/legend-label.js +14 -0
- package/dist/charts/legend/legend-marker.js +18 -0
- package/dist/charts/legend/legend-progress.js +39 -0
- package/dist/charts/legend/legend-value.js +31 -0
- package/dist/charts/legend/legend.js +69 -0
- package/dist/charts/line-backdrop.js +129 -0
- package/dist/charts/line-chart-loading.js +85 -0
- package/dist/charts/line-chart.js +182 -0
- package/dist/charts/line-loading-pulse.js +185 -0
- package/dist/charts/line-loading-timing.js +12 -0
- package/dist/charts/line-path.js +58 -0
- package/dist/charts/line-series-terminal-marker.js +75 -0
- package/dist/charts/line.js +499 -0
- package/dist/charts/live-line-chart.js +494 -0
- package/dist/charts/live-line.js +277 -0
- package/dist/charts/live-x-axis.js +116 -0
- package/dist/charts/live-y-axis.js +164 -0
- package/dist/charts/loading-sweep.js +344 -0
- package/dist/charts/local-point.js +56 -0
- package/dist/charts/markers/chart-markers.js +149 -0
- package/dist/charts/markers/index.js +14 -0
- package/dist/charts/markers/marker-group.js +411 -0
- package/dist/charts/motion-utils.js +29 -0
- package/dist/charts/notch-gauge-shared.js +146 -0
- package/dist/charts/particle-line.js +1340 -0
- package/dist/charts/path-stroke-utils.js +51 -0
- package/dist/charts/pattern-area.js +127 -0
- package/dist/charts/pattern-preset.js +287 -0
- package/dist/charts/pattern-tile.js +31 -0
- package/dist/charts/patterns/circles.js +62 -0
- package/dist/charts/patterns/constants.js +9 -0
- package/dist/charts/patterns/hexagons.js +38 -0
- package/dist/charts/patterns/index.js +16 -0
- package/dist/charts/patterns/lines.js +65 -0
- package/dist/charts/patterns/path.js +37 -0
- package/dist/charts/patterns/pattern.js +16 -0
- package/dist/charts/patterns/waves.js +40 -0
- package/dist/charts/pie-center-shell.js +127 -0
- package/dist/charts/pie-center.js +87 -0
- package/dist/charts/pie-chart.js +377 -0
- package/dist/charts/pie-context.js +116 -0
- package/dist/charts/pie-slice.js +394 -0
- package/dist/charts/pie-ticks.js +332 -0
- package/dist/charts/profit-loss-legend-hover.js +18 -0
- package/dist/charts/profit-loss-legend.js +53 -0
- package/dist/charts/profit-loss-line.js +162 -0
- package/dist/charts/profit-loss-segments.js +88 -0
- package/dist/charts/projection-config.js +90 -0
- package/dist/charts/projection-line-end-marker.js +50 -0
- package/dist/charts/projection-line.js +466 -0
- package/dist/charts/projection-utils.js +303 -0
- package/dist/charts/radar-area.js +192 -0
- package/dist/charts/radar-axis.js +41 -0
- package/dist/charts/radar-chart.js +184 -0
- package/dist/charts/radar-context.js +104 -0
- package/dist/charts/radar-grid.js +121 -0
- package/dist/charts/radar-labels.js +62 -0
- package/dist/charts/radial-area.js +498 -0
- package/dist/charts/radial-axis.js +652 -0
- package/dist/charts/radial-bar.js +316 -0
- package/dist/charts/radial-center.js +67 -0
- package/dist/charts/radial-chart.js +740 -0
- package/dist/charts/radial-dot-bar-layout.js +68 -0
- package/dist/charts/radial-dot-bar.js +387 -0
- package/dist/charts/radial-geometry.js +114 -0
- package/dist/charts/radial-legend.js +66 -0
- package/dist/charts/radial-line.js +280 -0
- package/dist/charts/radial-ring-bar.js +255 -0
- package/dist/charts/radial-scrubber.js +120 -0
- package/dist/charts/radial-series-color.js +10 -0
- package/dist/charts/reference-area-config.js +43 -0
- package/dist/charts/reference-area-geometry.js +76 -0
- package/dist/charts/reference-area-registration-context.js +10 -0
- package/dist/charts/reference-area.js +241 -0
- package/dist/charts/responsive-chart-label-count.js +18 -0
- package/dist/charts/ring-center.js +78 -0
- package/dist/charts/ring-chart.js +362 -0
- package/dist/charts/ring-context.js +122 -0
- package/dist/charts/ring-grid.js +175 -0
- package/dist/charts/ring-labels.js +109 -0
- package/dist/charts/ring.js +237 -0
- package/dist/charts/sankey/index.js +22 -0
- package/dist/charts/sankey/sankey-chart.js +199 -0
- package/dist/charts/sankey/sankey-context.js +32 -0
- package/dist/charts/sankey/sankey-link.js +232 -0
- package/dist/charts/sankey/sankey-node.js +348 -0
- package/dist/charts/sankey/sankey-tooltip.js +94 -0
- package/dist/charts/scatter-chart-shell.js +279 -0
- package/dist/charts/scatter-chart.js +150 -0
- package/dist/charts/scatter-jitter.js +86 -0
- package/dist/charts/scatter-svg.js +17 -0
- package/dist/charts/scatter.js +92 -0
- package/dist/charts/segment.js +107 -0
- package/dist/charts/series-bar-layout.js +42 -0
- package/dist/charts/series-bar.js +241 -0
- package/dist/charts/series-dash-tail-overlay.js +53 -0
- package/dist/charts/series-highlight-layer.js +43 -0
- package/dist/charts/series-hover-dim.js +32 -0
- package/dist/charts/series-markers.js +239 -0
- package/dist/charts/series-path-utils.js +185 -0
- package/dist/charts/series-point-marker.js +150 -0
- package/dist/charts/sparkline-geometry.js +197 -0
- package/dist/charts/sparkline.js +910 -0
- package/dist/charts/static-chart-preview-context.js +16 -0
- package/dist/charts/sunburst-breadcrumb.js +33 -0
- package/dist/charts/sunburst-center.js +37 -0
- package/dist/charts/sunburst-chart.js +408 -0
- package/dist/charts/sunburst-context.js +140 -0
- package/dist/charts/sunburst-data.js +0 -0
- package/dist/charts/sunburst-hint.js +42 -0
- package/dist/charts/sunburst-labels.js +130 -0
- package/dist/charts/sunburst-segment.js +144 -0
- package/dist/charts/sunburst.js +419 -0
- package/dist/charts/time-series-chart-shell.js +688 -0
- package/dist/charts/tooltip/chart-tooltip.js +516 -0
- package/dist/charts/tooltip/cursor-tooltip-box.js +183 -0
- package/dist/charts/tooltip/date-ticker.js +376 -0
- package/dist/charts/tooltip/index.js +32 -0
- package/dist/charts/tooltip/liquid-glass.js +142 -0
- package/dist/charts/tooltip/tooltip-box.js +502 -0
- package/dist/charts/tooltip/tooltip-content.js +59 -0
- package/dist/charts/tooltip/tooltip-dot.js +170 -0
- package/dist/charts/tooltip/tooltip-indicator.js +175 -0
- package/dist/charts/tooltip/use-delayed-tooltip-data.js +41 -0
- package/dist/charts/tooltip/use-velocity-stretch.js +52 -0
- package/dist/charts/trend-line-utils.js +64 -0
- package/dist/charts/trend-line.js +318 -0
- package/dist/charts/use-animated-band.js +175 -0
- package/dist/charts/use-animated-series-path.js +208 -0
- package/dist/charts/use-animated-y-domains.js +192 -0
- package/dist/charts/use-category-slots.js +29 -0
- package/dist/charts/use-chart-interaction.js +296 -0
- package/dist/charts/use-chart-phase-orchestrator.js +183 -0
- package/dist/charts/use-enter-complete.js +18 -0
- package/dist/charts/use-grid-shimmer.js +88 -0
- package/dist/charts/use-highlight-segment.js +65 -0
- package/dist/charts/use-mount-progress.js +23 -0
- package/dist/charts/use-scatter-chart-interaction.js +364 -0
- package/dist/charts/use-scheduled-tooltip.js +84 -0
- package/dist/charts/visx-pattern.js +30 -0
- package/dist/charts/weekends.js +174 -0
- package/dist/charts/x-axis-hover.js +46 -0
- package/dist/charts/x-axis.js +523 -0
- package/dist/charts/y-axis-scales.js +71 -0
- package/dist/charts/y-axis-ticks.js +22 -0
- package/dist/charts/y-axis.js +233 -0
- package/dist/charts/y-domain-utils.js +189 -0
- package/dist/index.d.ts +7346 -0
- package/dist/index.js +2 -0
- package/dist/lib/utils.js +8 -0
- package/dist/shimmering-text.js +54 -0
- package/package.json +78 -4
- package/tema.css +202 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import {
|
|
4
|
+
animate,
|
|
5
|
+
motionValue,
|
|
6
|
+
useMotionValue,
|
|
7
|
+
useMotionValueEvent,
|
|
8
|
+
useReducedMotion,
|
|
9
|
+
useSpring
|
|
10
|
+
} from "motion/react";
|
|
11
|
+
import { memo, useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
|
12
|
+
import { arcPath } from "./arc-path";
|
|
13
|
+
import { usePieHover, usePieStable } from "./pie-context";
|
|
14
|
+
const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
15
|
+
const clamp01 = (v) => Math.min(1, Math.max(0, v));
|
|
16
|
+
function useMotionValueApply(value, apply) {
|
|
17
|
+
const applyRef = useRef(apply);
|
|
18
|
+
applyRef.current = apply;
|
|
19
|
+
useIsoLayoutEffect(() => {
|
|
20
|
+
applyRef.current(value.get());
|
|
21
|
+
});
|
|
22
|
+
useMotionValueEvent(value, "change", (v) => applyRef.current(v));
|
|
23
|
+
}
|
|
24
|
+
const SWEEP_RAMP_TICKS = 4;
|
|
25
|
+
const DEFAULT_PIE_TICKS_SWEEP_TRANSITION = {
|
|
26
|
+
duration: 1.4,
|
|
27
|
+
ease: [0.32, 0.72, 0, 1]
|
|
28
|
+
};
|
|
29
|
+
const HOVER_SPRING = { stiffness: 420, damping: 32 };
|
|
30
|
+
const TICK_ATTACK = { type: "spring", stiffness: 700, damping: 28 };
|
|
31
|
+
const TICK_RELEASE = { duration: 0.75, ease: [0.22, 1, 0.36, 1] };
|
|
32
|
+
const TICK_FALLOFF = [1, 0.45, 0.15];
|
|
33
|
+
const TICK_WIDEN = 0.6;
|
|
34
|
+
function allocateTicks(values, count) {
|
|
35
|
+
const total = values.reduce((sum, v) => sum + Math.max(0, v), 0);
|
|
36
|
+
if (total <= 0) {
|
|
37
|
+
return values.map(() => 0);
|
|
38
|
+
}
|
|
39
|
+
const exact = values.map((v) => Math.max(0, v) / total * count);
|
|
40
|
+
const slots = exact.map(
|
|
41
|
+
(e, i) => (values[i] ?? 0) > 0 ? Math.max(1, Math.floor(e)) : 0
|
|
42
|
+
);
|
|
43
|
+
let remaining = count - slots.reduce((sum, s) => sum + s, 0);
|
|
44
|
+
const byRemainder = exact.map((e, i) => ({ i, r: e - Math.floor(e) })).filter(({ i }) => (values[i] ?? 0) > 0).sort((a, b) => b.r - a.r);
|
|
45
|
+
for (let k = 0; remaining > 0 && byRemainder.length > 0; k++) {
|
|
46
|
+
const slot = byRemainder[k % byRemainder.length];
|
|
47
|
+
if (slot) {
|
|
48
|
+
slots[slot.i] = (slots[slot.i] ?? 0) + 1;
|
|
49
|
+
}
|
|
50
|
+
remaining--;
|
|
51
|
+
}
|
|
52
|
+
return slots;
|
|
53
|
+
}
|
|
54
|
+
function polar(angle, radius) {
|
|
55
|
+
return { x: Math.sin(angle) * radius, y: -Math.cos(angle) * radius };
|
|
56
|
+
}
|
|
57
|
+
const PieTickSlice = memo(function PieTickSlice2({
|
|
58
|
+
slice,
|
|
59
|
+
color,
|
|
60
|
+
innerRadius,
|
|
61
|
+
outerRadius,
|
|
62
|
+
hoverOffset,
|
|
63
|
+
tickReach,
|
|
64
|
+
strokeWidth,
|
|
65
|
+
lineCap,
|
|
66
|
+
opacity,
|
|
67
|
+
isHovered,
|
|
68
|
+
hoverEffect,
|
|
69
|
+
sweep,
|
|
70
|
+
onHover
|
|
71
|
+
}) {
|
|
72
|
+
const refs = useRef([]);
|
|
73
|
+
const grow = useSpring(0, HOVER_SPRING);
|
|
74
|
+
const tickMode = hoverEffect === "tick";
|
|
75
|
+
const tickGrow = useRef([]);
|
|
76
|
+
const tickGrowAt = (k) => {
|
|
77
|
+
let mv = tickGrow.current[k];
|
|
78
|
+
if (!mv) {
|
|
79
|
+
mv = motionValue(0);
|
|
80
|
+
tickGrow.current[k] = mv;
|
|
81
|
+
}
|
|
82
|
+
return mv;
|
|
83
|
+
};
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
grow.set(isHovered && !tickMode ? 1 : 0);
|
|
86
|
+
}, [grow, isHovered, tickMode]);
|
|
87
|
+
const depth = outerRadius - innerRadius;
|
|
88
|
+
const applyTick = (k) => {
|
|
89
|
+
const el = refs.current[k];
|
|
90
|
+
const tick = slice.ticks[k];
|
|
91
|
+
if (!(el && tick)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const t = clamp01((sweep.get() - tick.order) / SWEEP_RAMP_TICKS);
|
|
95
|
+
const tickLift = tickGrow.current[k]?.get() ?? 0;
|
|
96
|
+
const reach = grow.get() * hoverOffset + tickLift * tickReach;
|
|
97
|
+
const end = polar(tick.angle, innerRadius + (depth + reach) * t);
|
|
98
|
+
el.setAttribute("x2", String(end.x));
|
|
99
|
+
el.setAttribute("y2", String(end.y));
|
|
100
|
+
el.setAttribute("stroke-opacity", String(t));
|
|
101
|
+
el.setAttribute(
|
|
102
|
+
"stroke-width",
|
|
103
|
+
String(strokeWidth * (1 + TICK_WIDEN * tickLift))
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
const apply = () => {
|
|
107
|
+
for (let k = 0; k < slice.ticks.length; k++) {
|
|
108
|
+
applyTick(k);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
useMotionValueApply(sweep, apply);
|
|
112
|
+
useMotionValueApply(grow, apply);
|
|
113
|
+
const applyTickRef = useRef(applyTick);
|
|
114
|
+
applyTickRef.current = applyTick;
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
if (!tickMode) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const unsubscribers = slice.ticks.map(
|
|
120
|
+
(_, k) => tickGrowAt(k).on("change", () => applyTickRef.current(k))
|
|
121
|
+
);
|
|
122
|
+
return () => {
|
|
123
|
+
for (const unsubscribe of unsubscribers) {
|
|
124
|
+
unsubscribe();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}, [tickMode, slice.ticks]);
|
|
128
|
+
const setTickHover = (k, hovered) => {
|
|
129
|
+
const reachTicks = TICK_FALLOFF.length - 1;
|
|
130
|
+
for (let d = -reachTicks; d <= reachTicks; d++) {
|
|
131
|
+
const j = k + d;
|
|
132
|
+
if (j < 0 || j >= slice.ticks.length) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const mv = tickGrowAt(j);
|
|
136
|
+
if (!hovered) {
|
|
137
|
+
animate(mv, 0, TICK_RELEASE);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const target = TICK_FALLOFF[Math.abs(d)] ?? 0;
|
|
141
|
+
if (mv.get() < target) {
|
|
142
|
+
animate(mv, target, TICK_ATTACK);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const hitOuter = outerRadius + Math.max(hoverOffset, tickMode ? tickReach : 0);
|
|
147
|
+
const hitPath = arcPath(
|
|
148
|
+
Math.max(0, innerRadius - strokeWidth),
|
|
149
|
+
hitOuter,
|
|
150
|
+
slice.startAngle,
|
|
151
|
+
slice.endAngle
|
|
152
|
+
);
|
|
153
|
+
return (
|
|
154
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: SVG hover target for the slice
|
|
155
|
+
/* @__PURE__ */ jsxs(
|
|
156
|
+
"g",
|
|
157
|
+
{
|
|
158
|
+
onMouseEnter: () => onHover(slice.index),
|
|
159
|
+
onMouseLeave: () => onHover(null),
|
|
160
|
+
style: {
|
|
161
|
+
cursor: "pointer",
|
|
162
|
+
opacity,
|
|
163
|
+
transition: "opacity 150ms ease-out"
|
|
164
|
+
},
|
|
165
|
+
children: [
|
|
166
|
+
/* @__PURE__ */ jsx("path", { d: hitPath, fill: "transparent" }),
|
|
167
|
+
tickMode ? slice.ticks.map((tick, k) => (
|
|
168
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: SVG hover hitbox for one tick
|
|
169
|
+
/* @__PURE__ */ jsx(
|
|
170
|
+
"path",
|
|
171
|
+
{
|
|
172
|
+
d: arcPath(
|
|
173
|
+
Math.max(0, innerRadius - strokeWidth),
|
|
174
|
+
hitOuter,
|
|
175
|
+
tick.angle - slice.step / 2,
|
|
176
|
+
tick.angle + slice.step / 2
|
|
177
|
+
),
|
|
178
|
+
fill: "transparent",
|
|
179
|
+
onMouseEnter: () => setTickHover(k, true),
|
|
180
|
+
onMouseLeave: () => setTickHover(k, false)
|
|
181
|
+
},
|
|
182
|
+
`hit-${tick.order}`
|
|
183
|
+
)
|
|
184
|
+
)) : null,
|
|
185
|
+
slice.ticks.map((tick, k) => {
|
|
186
|
+
const start = polar(tick.angle, innerRadius);
|
|
187
|
+
const t0 = clamp01((sweep.get() - tick.order) / SWEEP_RAMP_TICKS);
|
|
188
|
+
const end = polar(tick.angle, innerRadius + depth * t0);
|
|
189
|
+
return /* @__PURE__ */ jsx(
|
|
190
|
+
"line",
|
|
191
|
+
{
|
|
192
|
+
pointerEvents: "none",
|
|
193
|
+
ref: (el) => {
|
|
194
|
+
refs.current[k] = el;
|
|
195
|
+
},
|
|
196
|
+
stroke: color,
|
|
197
|
+
strokeLinecap: lineCap,
|
|
198
|
+
strokeOpacity: t0,
|
|
199
|
+
strokeWidth,
|
|
200
|
+
x1: start.x,
|
|
201
|
+
x2: end.x,
|
|
202
|
+
y1: start.y,
|
|
203
|
+
y2: end.y
|
|
204
|
+
},
|
|
205
|
+
tick.order
|
|
206
|
+
);
|
|
207
|
+
})
|
|
208
|
+
]
|
|
209
|
+
}
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
function PieTicks({
|
|
214
|
+
count = 96,
|
|
215
|
+
strokeWidth = 2.5,
|
|
216
|
+
lineCap = "round",
|
|
217
|
+
sliceGap = 1,
|
|
218
|
+
hoverOffset: hoverOffsetProp,
|
|
219
|
+
hoverEffect = "slice",
|
|
220
|
+
tickHoverOffset,
|
|
221
|
+
fadedOpacity = 0.3,
|
|
222
|
+
enterTransition: enterTransitionProp,
|
|
223
|
+
animate: animateOnMount = true,
|
|
224
|
+
className
|
|
225
|
+
}) {
|
|
226
|
+
const {
|
|
227
|
+
arcs,
|
|
228
|
+
data,
|
|
229
|
+
innerRadius: pieInnerRadius,
|
|
230
|
+
outerRadius: pieOuterRadius,
|
|
231
|
+
hoverOffset: pieHoverOffset,
|
|
232
|
+
enterTransition: pieEnterTransition,
|
|
233
|
+
enterStaggerScale,
|
|
234
|
+
animationKey,
|
|
235
|
+
geometryScrubbing,
|
|
236
|
+
getColor
|
|
237
|
+
} = usePieStable();
|
|
238
|
+
const { hoveredIndex, setHoveredIndex } = usePieHover();
|
|
239
|
+
const prefersReducedMotion = useReducedMotion();
|
|
240
|
+
const hoverOffset = hoverOffsetProp ?? pieHoverOffset;
|
|
241
|
+
const outerRadius = pieOuterRadius;
|
|
242
|
+
const innerRadius = pieInnerRadius > 0 ? pieInnerRadius : pieOuterRadius * 0.72;
|
|
243
|
+
const slices = useMemo(() => {
|
|
244
|
+
const first = arcs[0];
|
|
245
|
+
const last = arcs.at(-1);
|
|
246
|
+
if (!(first && last) || count < 1) {
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
const sweepStart = Math.min(first.startAngle, last.endAngle);
|
|
250
|
+
const sweepSpan = Math.abs(last.endAngle - first.startAngle);
|
|
251
|
+
const direction = last.endAngle >= first.startAngle ? 1 : -1;
|
|
252
|
+
const step = sweepSpan / count;
|
|
253
|
+
const slots = allocateTicks(
|
|
254
|
+
data.map((d) => d.value),
|
|
255
|
+
Math.round(count)
|
|
256
|
+
);
|
|
257
|
+
const base = direction === 1 ? first.startAngle : sweepStart + sweepSpan;
|
|
258
|
+
let cursor = 0;
|
|
259
|
+
return arcs.map((_, index) => {
|
|
260
|
+
const n = slots[index] ?? 0;
|
|
261
|
+
const gap = n > sliceGap ? sliceGap : 0;
|
|
262
|
+
const ticks = [];
|
|
263
|
+
for (let k = 0; k < n - gap; k++) {
|
|
264
|
+
const order = cursor + k;
|
|
265
|
+
ticks.push({ order, angle: base + direction * (order + 0.5) * step });
|
|
266
|
+
}
|
|
267
|
+
const sliceStart = base + direction * cursor * step;
|
|
268
|
+
cursor += n;
|
|
269
|
+
const sliceEnd = base + direction * cursor * step;
|
|
270
|
+
return {
|
|
271
|
+
index,
|
|
272
|
+
ticks,
|
|
273
|
+
step,
|
|
274
|
+
startAngle: Math.min(sliceStart, sliceEnd),
|
|
275
|
+
endAngle: Math.max(sliceStart, sliceEnd)
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
}, [arcs, data, count, sliceGap]);
|
|
279
|
+
const sweepEnd = count + SWEEP_RAMP_TICKS;
|
|
280
|
+
const instant = !animateOnMount || Boolean(prefersReducedMotion) || geometryScrubbing;
|
|
281
|
+
const sweep = useMotionValue(instant ? sweepEnd : 0);
|
|
282
|
+
const transition = enterTransitionProp ?? pieEnterTransition ?? DEFAULT_PIE_TICKS_SWEEP_TRANSITION;
|
|
283
|
+
const transitionKey = JSON.stringify(transition);
|
|
284
|
+
const transitionRef = useRef(transition);
|
|
285
|
+
transitionRef.current = transition;
|
|
286
|
+
useEffect(() => {
|
|
287
|
+
if (instant) {
|
|
288
|
+
sweep.jump(sweepEnd);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
sweep.jump(0);
|
|
292
|
+
const controls = animate(sweep, sweepEnd, {
|
|
293
|
+
...transitionRef.current,
|
|
294
|
+
delay: 0.1 * enterStaggerScale
|
|
295
|
+
});
|
|
296
|
+
return () => controls.stop();
|
|
297
|
+
}, [
|
|
298
|
+
instant,
|
|
299
|
+
sweep,
|
|
300
|
+
sweepEnd,
|
|
301
|
+
transitionKey,
|
|
302
|
+
enterStaggerScale,
|
|
303
|
+
animationKey
|
|
304
|
+
]);
|
|
305
|
+
const activeIndex = geometryScrubbing ? null : hoveredIndex;
|
|
306
|
+
return /* @__PURE__ */ jsx("g", { className, children: slices.map((slice) => /* @__PURE__ */ jsx(
|
|
307
|
+
PieTickSlice,
|
|
308
|
+
{
|
|
309
|
+
color: getColor(slice.index),
|
|
310
|
+
hoverEffect,
|
|
311
|
+
hoverOffset,
|
|
312
|
+
innerRadius,
|
|
313
|
+
isHovered: activeIndex === slice.index,
|
|
314
|
+
lineCap,
|
|
315
|
+
onHover: setHoveredIndex,
|
|
316
|
+
opacity: activeIndex === null || activeIndex === slice.index ? 1 : fadedOpacity,
|
|
317
|
+
outerRadius,
|
|
318
|
+
slice,
|
|
319
|
+
strokeWidth,
|
|
320
|
+
sweep,
|
|
321
|
+
tickReach: tickHoverOffset ?? hoverOffset * 2.2
|
|
322
|
+
},
|
|
323
|
+
data[slice.index]?.label ?? slice.index
|
|
324
|
+
)) });
|
|
325
|
+
}
|
|
326
|
+
PieTicks.displayName = "PieTicks";
|
|
327
|
+
var pie_ticks_default = PieTicks;
|
|
328
|
+
export {
|
|
329
|
+
DEFAULT_PIE_TICKS_SWEEP_TRANSITION,
|
|
330
|
+
PieTicks,
|
|
331
|
+
pie_ticks_default as default
|
|
332
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext } from "react";
|
|
4
|
+
const ProfitLossLegendHoverContext = createContext(null);
|
|
5
|
+
function ProfitLossLegendHoverProvider({
|
|
6
|
+
hoveredIndex,
|
|
7
|
+
children
|
|
8
|
+
}) {
|
|
9
|
+
return /* @__PURE__ */ jsx(ProfitLossLegendHoverContext.Provider, { value: { hoveredIndex }, children });
|
|
10
|
+
}
|
|
11
|
+
function useProfitLossLegendHover() {
|
|
12
|
+
const context = useContext(ProfitLossLegendHoverContext);
|
|
13
|
+
return context ?? { hoveredIndex: null };
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
ProfitLossLegendHoverProvider,
|
|
17
|
+
useProfitLossLegendHover
|
|
18
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { cn } from "../lib/utils";
|
|
4
|
+
import { ChartDataOverrideExempt } from "./chart-data-override";
|
|
5
|
+
import { Legend, LegendItem, LegendLabel, LegendMarker } from "./legend/index";
|
|
6
|
+
import {
|
|
7
|
+
PROFIT_LOSS_NEGATIVE_COLOR,
|
|
8
|
+
PROFIT_LOSS_POSITIVE_COLOR
|
|
9
|
+
} from "./profit-loss-line";
|
|
10
|
+
const PROFIT_LOSS_LEGEND_ITEMS = [
|
|
11
|
+
{ label: "Profit", value: 0, color: PROFIT_LOSS_POSITIVE_COLOR },
|
|
12
|
+
{ label: "Loss", value: 0, color: PROFIT_LOSS_NEGATIVE_COLOR }
|
|
13
|
+
];
|
|
14
|
+
const LEGEND_ALIGN_CLASSES = {
|
|
15
|
+
start: "justify-start",
|
|
16
|
+
center: "justify-center",
|
|
17
|
+
end: "justify-end"
|
|
18
|
+
};
|
|
19
|
+
function ProfitLossLegend({
|
|
20
|
+
hoveredIndex = null,
|
|
21
|
+
onHoverChange,
|
|
22
|
+
align = "start",
|
|
23
|
+
className
|
|
24
|
+
}) {
|
|
25
|
+
return /* @__PURE__ */ jsx(
|
|
26
|
+
"div",
|
|
27
|
+
{
|
|
28
|
+
className: cn(
|
|
29
|
+
"flex w-full shrink-0 px-1 py-2",
|
|
30
|
+
LEGEND_ALIGN_CLASSES[align],
|
|
31
|
+
className
|
|
32
|
+
),
|
|
33
|
+
"data-chart-legend": "",
|
|
34
|
+
children: /* @__PURE__ */ jsx(ChartDataOverrideExempt, { children: /* @__PURE__ */ jsx(
|
|
35
|
+
Legend,
|
|
36
|
+
{
|
|
37
|
+
className: "flex-row flex-wrap gap-4",
|
|
38
|
+
hoveredIndex,
|
|
39
|
+
items: [...PROFIT_LOSS_LEGEND_ITEMS],
|
|
40
|
+
onHoverChange,
|
|
41
|
+
children: /* @__PURE__ */ jsxs(LegendItem, { className: "flex items-center gap-2", children: [
|
|
42
|
+
/* @__PURE__ */ jsx(LegendMarker, { className: "h-2.5 w-2.5" }),
|
|
43
|
+
/* @__PURE__ */ jsx(LegendLabel, { className: "text-xs" })
|
|
44
|
+
] })
|
|
45
|
+
}
|
|
46
|
+
) })
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
PROFIT_LOSS_LEGEND_ITEMS,
|
|
52
|
+
ProfitLossLegend
|
|
53
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { curveLinear } from "d3-shape";
|
|
4
|
+
import { useCallback, useId, useMemo } from "react";
|
|
5
|
+
import { useChart, useChartStable } from "./chart-context";
|
|
6
|
+
import {
|
|
7
|
+
fadeGradientStops,
|
|
8
|
+
resolveFadeSides
|
|
9
|
+
} from "./fade-edges";
|
|
10
|
+
import { LinePath, linePathD } from "./line-path";
|
|
11
|
+
import { useProfitLossLegendHover } from "./profit-loss-legend-hover";
|
|
12
|
+
const PROFIT_LOSS_POSITIVE_COLOR = "var(--chart-positive, var(--chart-positive))";
|
|
13
|
+
const PROFIT_LOSS_NEGATIVE_COLOR = "var(--chart-negative, var(--chart-negative))";
|
|
14
|
+
const LEGEND_DIM_OPACITY = 0.25;
|
|
15
|
+
function profitLossColor(value) {
|
|
16
|
+
return value >= 0 ? PROFIT_LOSS_POSITIVE_COLOR : PROFIT_LOSS_NEGATIVE_COLOR;
|
|
17
|
+
}
|
|
18
|
+
const PROFIT_LOSS_TOOLTIP_LABEL_FALLBACK = "Profit/Loss";
|
|
19
|
+
function resolveProfitLossTooltipLabel(label) {
|
|
20
|
+
const trimmed = label.trim();
|
|
21
|
+
return trimmed || PROFIT_LOSS_TOOLTIP_LABEL_FALLBACK;
|
|
22
|
+
}
|
|
23
|
+
function segmentLegendIndex(isPositive) {
|
|
24
|
+
return isPositive ? 0 : 1;
|
|
25
|
+
}
|
|
26
|
+
function ProfitLossLine({
|
|
27
|
+
dataKey,
|
|
28
|
+
strokeWidth = 2.5,
|
|
29
|
+
positiveColor = PROFIT_LOSS_POSITIVE_COLOR,
|
|
30
|
+
negativeColor = PROFIT_LOSS_NEGATIVE_COLOR,
|
|
31
|
+
curve = curveLinear,
|
|
32
|
+
fadeEdges = false
|
|
33
|
+
}) {
|
|
34
|
+
const { tooltipData } = useChart();
|
|
35
|
+
const { hoveredIndex } = useProfitLossLegendHover();
|
|
36
|
+
const { renderData, xScale, yScale, xAccessor, innerWidth } = useChartStable();
|
|
37
|
+
const reactId = useId();
|
|
38
|
+
const fadeSides = resolveFadeSides(fadeEdges);
|
|
39
|
+
const fadeStops = fadeSides.any ? fadeGradientStops(fadeSides) : null;
|
|
40
|
+
const positiveGradientId = `profit-loss-gradient-pos-${dataKey}-${reactId}`;
|
|
41
|
+
const negativeGradientId = `profit-loss-gradient-neg-${dataKey}-${reactId}`;
|
|
42
|
+
const focusedLegendIndex = useMemo(() => {
|
|
43
|
+
if (hoveredIndex !== null) {
|
|
44
|
+
return hoveredIndex;
|
|
45
|
+
}
|
|
46
|
+
if (!tooltipData) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const value = tooltipData.point[dataKey];
|
|
50
|
+
if (typeof value !== "number") {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return segmentLegendIndex(value >= 0);
|
|
54
|
+
}, [dataKey, hoveredIndex, tooltipData]);
|
|
55
|
+
const getX = useCallback(
|
|
56
|
+
(d) => xScale(xAccessor(d)) ?? 0,
|
|
57
|
+
[xAccessor, xScale]
|
|
58
|
+
);
|
|
59
|
+
const getY = useCallback(
|
|
60
|
+
(d) => {
|
|
61
|
+
const value = d[dataKey];
|
|
62
|
+
return typeof value === "number" ? yScale(value) ?? 0 : 0;
|
|
63
|
+
},
|
|
64
|
+
[dataKey, yScale]
|
|
65
|
+
);
|
|
66
|
+
const pathD = useMemo(
|
|
67
|
+
() => linePathD(renderData, { curve, x: getX, y: getY }),
|
|
68
|
+
[curve, getX, getY, renderData]
|
|
69
|
+
);
|
|
70
|
+
const [rangeBottom = 0, rangeTop = 0] = yScale.range();
|
|
71
|
+
const plotTop = Math.min(rangeBottom, rangeTop);
|
|
72
|
+
const plotBottom = Math.max(rangeBottom, rangeTop);
|
|
73
|
+
const zeroY = Math.min(
|
|
74
|
+
plotBottom,
|
|
75
|
+
Math.max(plotTop, yScale(0) ?? plotBottom)
|
|
76
|
+
);
|
|
77
|
+
const bleed = strokeWidth * 4;
|
|
78
|
+
const clipX = -bleed;
|
|
79
|
+
const clipWidth = innerWidth + bleed * 2;
|
|
80
|
+
const sides = [
|
|
81
|
+
{
|
|
82
|
+
isPositive: true,
|
|
83
|
+
clipId: `profit-loss-clip-pos-${dataKey}-${reactId}`,
|
|
84
|
+
gradientId: positiveGradientId,
|
|
85
|
+
color: positiveColor,
|
|
86
|
+
y: plotTop - bleed,
|
|
87
|
+
height: zeroY - (plotTop - bleed)
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
isPositive: false,
|
|
91
|
+
clipId: `profit-loss-clip-neg-${dataKey}-${reactId}`,
|
|
92
|
+
gradientId: negativeGradientId,
|
|
93
|
+
color: negativeColor,
|
|
94
|
+
y: zeroY,
|
|
95
|
+
height: plotBottom + bleed - zeroY
|
|
96
|
+
}
|
|
97
|
+
];
|
|
98
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
99
|
+
/* @__PURE__ */ jsxs("defs", { children: [
|
|
100
|
+
sides.map((side) => /* @__PURE__ */ jsx("clipPath", { id: side.clipId, children: /* @__PURE__ */ jsx(
|
|
101
|
+
"rect",
|
|
102
|
+
{
|
|
103
|
+
height: Math.max(0, side.height),
|
|
104
|
+
width: clipWidth,
|
|
105
|
+
x: clipX,
|
|
106
|
+
y: side.y
|
|
107
|
+
}
|
|
108
|
+
) }, side.clipId)),
|
|
109
|
+
fadeStops ? sides.map((side) => /* @__PURE__ */ jsx(
|
|
110
|
+
"linearGradient",
|
|
111
|
+
{
|
|
112
|
+
gradientUnits: "userSpaceOnUse",
|
|
113
|
+
id: side.gradientId,
|
|
114
|
+
x1: 0,
|
|
115
|
+
x2: innerWidth,
|
|
116
|
+
y1: 0,
|
|
117
|
+
y2: 0,
|
|
118
|
+
children: fadeStops.map((stop) => /* @__PURE__ */ jsx(
|
|
119
|
+
"stop",
|
|
120
|
+
{
|
|
121
|
+
offset: stop.offset,
|
|
122
|
+
style: { stopColor: side.color, stopOpacity: stop.opacity }
|
|
123
|
+
},
|
|
124
|
+
stop.offset
|
|
125
|
+
))
|
|
126
|
+
},
|
|
127
|
+
side.gradientId
|
|
128
|
+
)) : null
|
|
129
|
+
] }),
|
|
130
|
+
sides.map((side) => {
|
|
131
|
+
const isDimmed = focusedLegendIndex !== null && focusedLegendIndex !== segmentLegendIndex(side.isPositive);
|
|
132
|
+
return /* @__PURE__ */ jsx(
|
|
133
|
+
"g",
|
|
134
|
+
{
|
|
135
|
+
clipPath: `url(#${side.clipId})`,
|
|
136
|
+
opacity: isDimmed ? LEGEND_DIM_OPACITY : 1,
|
|
137
|
+
style: { transition: "opacity 0.2s ease-in-out" },
|
|
138
|
+
children: /* @__PURE__ */ jsx(
|
|
139
|
+
LinePath,
|
|
140
|
+
{
|
|
141
|
+
d: pathD,
|
|
142
|
+
stroke: fadeStops ? `url(#${side.gradientId})` : side.color,
|
|
143
|
+
strokeLinecap: "round",
|
|
144
|
+
strokeLinejoin: "round",
|
|
145
|
+
strokeWidth
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
},
|
|
149
|
+
side.clipId
|
|
150
|
+
);
|
|
151
|
+
})
|
|
152
|
+
] });
|
|
153
|
+
}
|
|
154
|
+
ProfitLossLine.displayName = "ProfitLossLine";
|
|
155
|
+
export {
|
|
156
|
+
PROFIT_LOSS_NEGATIVE_COLOR,
|
|
157
|
+
PROFIT_LOSS_POSITIVE_COLOR,
|
|
158
|
+
PROFIT_LOSS_TOOLTIP_LABEL_FALLBACK,
|
|
159
|
+
ProfitLossLine,
|
|
160
|
+
profitLossColor,
|
|
161
|
+
resolveProfitLossTooltipLabel
|
|
162
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function resolveSign(value, fallback) {
|
|
2
|
+
if (value > 0) {
|
|
3
|
+
return "positive";
|
|
4
|
+
}
|
|
5
|
+
if (value < 0) {
|
|
6
|
+
return "negative";
|
|
7
|
+
}
|
|
8
|
+
return fallback;
|
|
9
|
+
}
|
|
10
|
+
function findInitialSign(data, dataKey) {
|
|
11
|
+
for (const row of data) {
|
|
12
|
+
const value = row[dataKey];
|
|
13
|
+
if (typeof value !== "number") {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (value > 0) {
|
|
17
|
+
return "positive";
|
|
18
|
+
}
|
|
19
|
+
if (value < 0) {
|
|
20
|
+
return "negative";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return "positive";
|
|
24
|
+
}
|
|
25
|
+
function interpolateZeroCrossing(a, b, dataKey, xDataKey, xAccessor) {
|
|
26
|
+
const ya = a[dataKey];
|
|
27
|
+
const yb = b[dataKey];
|
|
28
|
+
const t = ya / (ya - yb);
|
|
29
|
+
const start = xAccessor(a).getTime();
|
|
30
|
+
const end = xAccessor(b).getTime();
|
|
31
|
+
const crossDate = new Date(start + t * (end - start));
|
|
32
|
+
return {
|
|
33
|
+
...a,
|
|
34
|
+
[xDataKey]: crossDate,
|
|
35
|
+
[dataKey]: 0
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function splitProfitLossSegments({
|
|
39
|
+
data,
|
|
40
|
+
dataKey,
|
|
41
|
+
xDataKey = "date",
|
|
42
|
+
xAccessor
|
|
43
|
+
}) {
|
|
44
|
+
if (data.length === 0) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
const segments = [];
|
|
48
|
+
let currentSign = findInitialSign(data, dataKey);
|
|
49
|
+
const firstPoint = data[0];
|
|
50
|
+
if (!firstPoint) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
let currentSegment = [firstPoint];
|
|
54
|
+
for (let i = 0; i < data.length - 1; i++) {
|
|
55
|
+
const a = data[i];
|
|
56
|
+
const b = data[i + 1];
|
|
57
|
+
if (!(a && b)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const ya = a[dataKey];
|
|
61
|
+
const yb = b[dataKey];
|
|
62
|
+
if (typeof ya === "number" && typeof yb === "number" && ya !== 0 && yb !== 0 && Math.sign(ya) !== Math.sign(yb)) {
|
|
63
|
+
const cross = interpolateZeroCrossing(a, b, dataKey, xDataKey, xAccessor);
|
|
64
|
+
currentSegment.push(cross);
|
|
65
|
+
segments.push({
|
|
66
|
+
data: currentSegment,
|
|
67
|
+
isPositive: currentSign === "positive"
|
|
68
|
+
});
|
|
69
|
+
currentSegment = [cross, b];
|
|
70
|
+
currentSign = resolveSign(yb, currentSign);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
currentSegment.push(b);
|
|
74
|
+
if (typeof yb === "number" && yb !== 0) {
|
|
75
|
+
currentSign = resolveSign(yb, currentSign);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (currentSegment.length > 0) {
|
|
79
|
+
segments.push({
|
|
80
|
+
data: currentSegment,
|
|
81
|
+
isPositive: currentSign === "positive"
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return segments;
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
splitProfitLossSegments
|
|
88
|
+
};
|