@aiconstructions/graficos 0.0.0-stage → 0.2.2

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 (267) hide show
  1. package/ALTERACOES.md +57 -0
  2. package/LICENSE-BKLIT +21 -0
  3. package/LICENSE-visx-pattern +21 -0
  4. package/README.md +189 -2
  5. package/dist/aic/cores.js +83 -0
  6. package/dist/aic/index.js +22 -0
  7. package/dist/aic/interacao.js +55 -0
  8. package/dist/charts/animation.js +55 -0
  9. package/dist/charts/arc-geometry.js +58 -0
  10. package/dist/charts/arc-path.js +15 -0
  11. package/dist/charts/area-chart-loading.js +89 -0
  12. package/dist/charts/area-chart.js +149 -0
  13. package/dist/charts/area-gradient-defs.js +90 -0
  14. package/dist/charts/area-value-label-layout.js +70 -0
  15. package/dist/charts/area-value-labels.js +211 -0
  16. package/dist/charts/area.js +272 -0
  17. package/dist/charts/background.js +198 -0
  18. package/dist/charts/band-config.js +82 -0
  19. package/dist/charts/band-geometry.js +186 -0
  20. package/dist/charts/band.js +346 -0
  21. package/dist/charts/bar-chart-loading.js +27 -0
  22. package/dist/charts/bar-chart.js +545 -0
  23. package/dist/charts/bar-depth-geometry.js +18 -0
  24. package/dist/charts/bar-depth.js +608 -0
  25. package/dist/charts/bar-lollipop.js +278 -0
  26. package/dist/charts/bar-squares-layout.js +66 -0
  27. package/dist/charts/bar-squares.js +467 -0
  28. package/dist/charts/bar-value-label-layout.js +42 -0
  29. package/dist/charts/bar-x-axis.js +133 -0
  30. package/dist/charts/bar-y-axis.js +116 -0
  31. package/dist/charts/bar.js +457 -0
  32. package/dist/charts/bollinger.js +49 -0
  33. package/dist/charts/candlestick-chart.js +370 -0
  34. package/dist/charts/candlestick.js +368 -0
  35. package/dist/charts/chart-brush-handle.js +91 -0
  36. package/dist/charts/chart-brush-layout.js +91 -0
  37. package/dist/charts/chart-brush-selection-overlay.js +87 -0
  38. package/dist/charts/chart-brush-state.js +253 -0
  39. package/dist/charts/chart-brush-track-overlay.js +103 -0
  40. package/dist/charts/chart-brush.js +517 -0
  41. package/dist/charts/chart-center-typography.js +8 -0
  42. package/dist/charts/chart-child-passthrough.js +119 -0
  43. package/dist/charts/chart-config-context.js +56 -0
  44. package/dist/charts/chart-context.js +337 -0
  45. package/dist/charts/chart-crosshair.js +262 -0
  46. package/dist/charts/chart-data-override.js +186 -0
  47. package/dist/charts/chart-defs.js +56 -0
  48. package/dist/charts/chart-formatters.js +77 -0
  49. package/dist/charts/chart-legend-hover.js +27 -0
  50. package/dist/charts/chart-legend.js +163 -0
  51. package/dist/charts/chart-loading-label.js +52 -0
  52. package/dist/charts/chart-parent-size.js +138 -0
  53. package/dist/charts/chart-phase.js +29 -0
  54. package/dist/charts/chart-reveal-clip.js +62 -0
  55. package/dist/charts/chart-scale.js +19 -0
  56. package/dist/charts/chart-stat-flow.js +72 -0
  57. package/dist/charts/choropleth/choropleth-background.js +29 -0
  58. package/dist/charts/choropleth/choropleth-camera.js +379 -0
  59. package/dist/charts/choropleth/choropleth-chart.js +644 -0
  60. package/dist/charts/choropleth/choropleth-context.js +152 -0
  61. package/dist/charts/choropleth/choropleth-dot-grid.js +293 -0
  62. package/dist/charts/choropleth/choropleth-feature.js +291 -0
  63. package/dist/charts/choropleth/choropleth-graticule.js +40 -0
  64. package/dist/charts/choropleth/choropleth-marker.js +222 -0
  65. package/dist/charts/choropleth/choropleth-projection.js +37 -0
  66. package/dist/charts/choropleth/choropleth-tooltip.js +82 -0
  67. package/dist/charts/choropleth/choropleth-zoom-matrix.js +150 -0
  68. package/dist/charts/choropleth/index.js +44 -0
  69. package/dist/charts/choropleth/use-choropleth-zoom-state.js +636 -0
  70. package/dist/charts/composed-chart.js +346 -0
  71. package/dist/charts/crosshair-store.js +49 -0
  72. package/dist/charts/dash-tail-stroke.js +58 -0
  73. package/dist/charts/date-pill-store.js +50 -0
  74. package/dist/charts/date-ranges.js +131 -0
  75. package/dist/charts/deadline.js +295 -0
  76. package/dist/charts/decimate-time-series.js +112 -0
  77. package/dist/charts/fade-edges.js +171 -0
  78. package/dist/charts/fast-path.js +78 -0
  79. package/dist/charts/filter-data-by-x-domain.js +45 -0
  80. package/dist/charts/funnel-chart.js +773 -0
  81. package/dist/charts/gauge-arc-layers.js +479 -0
  82. package/dist/charts/gauge-dial.js +329 -0
  83. package/dist/charts/gauge-label-layout.js +148 -0
  84. package/dist/charts/gauge.js +946 -0
  85. package/dist/charts/generate-chart-skeleton-data.js +27 -0
  86. package/dist/charts/gradients.js +158 -0
  87. package/dist/charts/grid-lines.js +114 -0
  88. package/dist/charts/grid.js +225 -0
  89. package/dist/charts/heatmap/generate-heatmap-skeleton-data.js +13 -0
  90. package/dist/charts/heatmap/heatmap-animation.js +128 -0
  91. package/dist/charts/heatmap/heatmap-cells.js +519 -0
  92. package/dist/charts/heatmap/heatmap-chart-loading.js +44 -0
  93. package/dist/charts/heatmap/heatmap-chart.js +548 -0
  94. package/dist/charts/heatmap/heatmap-colors.js +127 -0
  95. package/dist/charts/heatmap/heatmap-context.js +107 -0
  96. package/dist/charts/heatmap/heatmap-legend-gradient.js +97 -0
  97. package/dist/charts/heatmap/heatmap-legend-swatch.js +76 -0
  98. package/dist/charts/heatmap/heatmap-legend.js +165 -0
  99. package/dist/charts/heatmap/heatmap-pattern-defs.js +37 -0
  100. package/dist/charts/heatmap/heatmap-rect-cells.js +39 -0
  101. package/dist/charts/heatmap/heatmap-resolve-separator.js +84 -0
  102. package/dist/charts/heatmap/heatmap-separator.js +168 -0
  103. package/dist/charts/heatmap/heatmap-tooltip.js +73 -0
  104. package/dist/charts/heatmap/heatmap-utils.js +633 -0
  105. package/dist/charts/heatmap/heatmap-x-axis.js +79 -0
  106. package/dist/charts/heatmap/heatmap-y-axis.js +72 -0
  107. package/dist/charts/heatmap/index.js +208 -0
  108. package/dist/charts/highlight-segment-bounds.js +34 -0
  109. package/dist/charts/highlight-segment.js +119 -0
  110. package/dist/charts/index.js +944 -0
  111. package/dist/charts/indicator-fade.js +44 -0
  112. package/dist/charts/legend/index.js +22 -0
  113. package/dist/charts/legend/legend-context.js +47 -0
  114. package/dist/charts/legend/legend-item.js +30 -0
  115. package/dist/charts/legend/legend-label.js +14 -0
  116. package/dist/charts/legend/legend-marker.js +18 -0
  117. package/dist/charts/legend/legend-progress.js +39 -0
  118. package/dist/charts/legend/legend-value.js +31 -0
  119. package/dist/charts/legend/legend.js +69 -0
  120. package/dist/charts/line-backdrop.js +129 -0
  121. package/dist/charts/line-chart-loading.js +85 -0
  122. package/dist/charts/line-chart.js +182 -0
  123. package/dist/charts/line-loading-pulse.js +185 -0
  124. package/dist/charts/line-loading-timing.js +12 -0
  125. package/dist/charts/line-path.js +58 -0
  126. package/dist/charts/line-series-terminal-marker.js +75 -0
  127. package/dist/charts/line.js +499 -0
  128. package/dist/charts/live-line-chart.js +494 -0
  129. package/dist/charts/live-line.js +277 -0
  130. package/dist/charts/live-x-axis.js +116 -0
  131. package/dist/charts/live-y-axis.js +164 -0
  132. package/dist/charts/loading-sweep.js +344 -0
  133. package/dist/charts/local-point.js +56 -0
  134. package/dist/charts/markers/chart-markers.js +149 -0
  135. package/dist/charts/markers/index.js +14 -0
  136. package/dist/charts/markers/marker-group.js +411 -0
  137. package/dist/charts/motion-utils.js +29 -0
  138. package/dist/charts/notch-gauge-shared.js +146 -0
  139. package/dist/charts/particle-line.js +1340 -0
  140. package/dist/charts/path-stroke-utils.js +51 -0
  141. package/dist/charts/pattern-area.js +127 -0
  142. package/dist/charts/pattern-preset.js +287 -0
  143. package/dist/charts/pattern-tile.js +31 -0
  144. package/dist/charts/patterns/circles.js +62 -0
  145. package/dist/charts/patterns/constants.js +9 -0
  146. package/dist/charts/patterns/hexagons.js +38 -0
  147. package/dist/charts/patterns/index.js +16 -0
  148. package/dist/charts/patterns/lines.js +65 -0
  149. package/dist/charts/patterns/path.js +37 -0
  150. package/dist/charts/patterns/pattern.js +16 -0
  151. package/dist/charts/patterns/waves.js +40 -0
  152. package/dist/charts/pie-center-shell.js +127 -0
  153. package/dist/charts/pie-center.js +87 -0
  154. package/dist/charts/pie-chart.js +377 -0
  155. package/dist/charts/pie-context.js +116 -0
  156. package/dist/charts/pie-slice.js +394 -0
  157. package/dist/charts/pie-ticks.js +332 -0
  158. package/dist/charts/profit-loss-legend-hover.js +18 -0
  159. package/dist/charts/profit-loss-legend.js +53 -0
  160. package/dist/charts/profit-loss-line.js +162 -0
  161. package/dist/charts/profit-loss-segments.js +88 -0
  162. package/dist/charts/projection-config.js +90 -0
  163. package/dist/charts/projection-line-end-marker.js +50 -0
  164. package/dist/charts/projection-line.js +466 -0
  165. package/dist/charts/projection-utils.js +303 -0
  166. package/dist/charts/radar-area.js +192 -0
  167. package/dist/charts/radar-axis.js +41 -0
  168. package/dist/charts/radar-chart.js +184 -0
  169. package/dist/charts/radar-context.js +104 -0
  170. package/dist/charts/radar-grid.js +121 -0
  171. package/dist/charts/radar-labels.js +62 -0
  172. package/dist/charts/radial-area.js +498 -0
  173. package/dist/charts/radial-axis.js +652 -0
  174. package/dist/charts/radial-bar.js +316 -0
  175. package/dist/charts/radial-center.js +67 -0
  176. package/dist/charts/radial-chart.js +740 -0
  177. package/dist/charts/radial-dot-bar-layout.js +68 -0
  178. package/dist/charts/radial-dot-bar.js +387 -0
  179. package/dist/charts/radial-geometry.js +114 -0
  180. package/dist/charts/radial-legend.js +66 -0
  181. package/dist/charts/radial-line.js +280 -0
  182. package/dist/charts/radial-ring-bar.js +255 -0
  183. package/dist/charts/radial-scrubber.js +120 -0
  184. package/dist/charts/radial-series-color.js +10 -0
  185. package/dist/charts/reference-area-config.js +43 -0
  186. package/dist/charts/reference-area-geometry.js +76 -0
  187. package/dist/charts/reference-area-registration-context.js +10 -0
  188. package/dist/charts/reference-area.js +241 -0
  189. package/dist/charts/responsive-chart-label-count.js +18 -0
  190. package/dist/charts/ring-center.js +78 -0
  191. package/dist/charts/ring-chart.js +362 -0
  192. package/dist/charts/ring-context.js +122 -0
  193. package/dist/charts/ring-grid.js +175 -0
  194. package/dist/charts/ring-labels.js +109 -0
  195. package/dist/charts/ring.js +237 -0
  196. package/dist/charts/sankey/index.js +22 -0
  197. package/dist/charts/sankey/sankey-chart.js +199 -0
  198. package/dist/charts/sankey/sankey-context.js +32 -0
  199. package/dist/charts/sankey/sankey-link.js +232 -0
  200. package/dist/charts/sankey/sankey-node.js +348 -0
  201. package/dist/charts/sankey/sankey-tooltip.js +94 -0
  202. package/dist/charts/scatter-chart-shell.js +279 -0
  203. package/dist/charts/scatter-chart.js +150 -0
  204. package/dist/charts/scatter-jitter.js +86 -0
  205. package/dist/charts/scatter-svg.js +17 -0
  206. package/dist/charts/scatter.js +92 -0
  207. package/dist/charts/segment.js +107 -0
  208. package/dist/charts/series-bar-layout.js +42 -0
  209. package/dist/charts/series-bar.js +241 -0
  210. package/dist/charts/series-dash-tail-overlay.js +53 -0
  211. package/dist/charts/series-highlight-layer.js +43 -0
  212. package/dist/charts/series-hover-dim.js +32 -0
  213. package/dist/charts/series-markers.js +239 -0
  214. package/dist/charts/series-path-utils.js +185 -0
  215. package/dist/charts/series-point-marker.js +150 -0
  216. package/dist/charts/sparkline-geometry.js +197 -0
  217. package/dist/charts/sparkline.js +910 -0
  218. package/dist/charts/static-chart-preview-context.js +16 -0
  219. package/dist/charts/sunburst-breadcrumb.js +33 -0
  220. package/dist/charts/sunburst-center.js +37 -0
  221. package/dist/charts/sunburst-chart.js +408 -0
  222. package/dist/charts/sunburst-context.js +140 -0
  223. package/dist/charts/sunburst-data.js +0 -0
  224. package/dist/charts/sunburst-hint.js +42 -0
  225. package/dist/charts/sunburst-labels.js +130 -0
  226. package/dist/charts/sunburst-segment.js +144 -0
  227. package/dist/charts/sunburst.js +419 -0
  228. package/dist/charts/time-series-chart-shell.js +688 -0
  229. package/dist/charts/tooltip/chart-tooltip.js +516 -0
  230. package/dist/charts/tooltip/cursor-tooltip-box.js +183 -0
  231. package/dist/charts/tooltip/date-ticker.js +376 -0
  232. package/dist/charts/tooltip/index.js +32 -0
  233. package/dist/charts/tooltip/liquid-glass.js +142 -0
  234. package/dist/charts/tooltip/tooltip-box.js +502 -0
  235. package/dist/charts/tooltip/tooltip-content.js +59 -0
  236. package/dist/charts/tooltip/tooltip-dot.js +170 -0
  237. package/dist/charts/tooltip/tooltip-indicator.js +175 -0
  238. package/dist/charts/tooltip/use-delayed-tooltip-data.js +41 -0
  239. package/dist/charts/tooltip/use-velocity-stretch.js +52 -0
  240. package/dist/charts/trend-line-utils.js +64 -0
  241. package/dist/charts/trend-line.js +318 -0
  242. package/dist/charts/use-animated-band.js +175 -0
  243. package/dist/charts/use-animated-series-path.js +208 -0
  244. package/dist/charts/use-animated-y-domains.js +192 -0
  245. package/dist/charts/use-category-slots.js +29 -0
  246. package/dist/charts/use-chart-interaction.js +296 -0
  247. package/dist/charts/use-chart-phase-orchestrator.js +183 -0
  248. package/dist/charts/use-enter-complete.js +18 -0
  249. package/dist/charts/use-grid-shimmer.js +88 -0
  250. package/dist/charts/use-highlight-segment.js +65 -0
  251. package/dist/charts/use-mount-progress.js +23 -0
  252. package/dist/charts/use-scatter-chart-interaction.js +364 -0
  253. package/dist/charts/use-scheduled-tooltip.js +84 -0
  254. package/dist/charts/visx-pattern.js +30 -0
  255. package/dist/charts/weekends.js +174 -0
  256. package/dist/charts/x-axis-hover.js +46 -0
  257. package/dist/charts/x-axis.js +523 -0
  258. package/dist/charts/y-axis-scales.js +71 -0
  259. package/dist/charts/y-axis-ticks.js +22 -0
  260. package/dist/charts/y-axis.js +233 -0
  261. package/dist/charts/y-domain-utils.js +189 -0
  262. package/dist/index.d.ts +7346 -0
  263. package/dist/index.js +2 -0
  264. package/dist/lib/utils.js +8 -0
  265. package/dist/shimmering-text.js +54 -0
  266. package/package.json +78 -4
  267. package/tema.css +202 -0
@@ -0,0 +1,29 @@
1
+ const DEFAULT_CHART_STATUS = "ready";
2
+ const DEFAULT_Y_DOMAIN_TWEEN_MS = 500;
3
+ const Y_DOMAIN_TWEEN_SKIP_THRESHOLD = 0.02;
4
+ function resolveRestingChartPhase(status) {
5
+ return status === "loading" ? "loading" : "ready";
6
+ }
7
+ function isChartInteractionPhase(phase) {
8
+ return phase === "ready";
9
+ }
10
+ const DEFAULT_CHART_LIFECYCLE = {
11
+ chartPhase: "ready",
12
+ chartStatus: "ready",
13
+ loadingLabel: void 0,
14
+ yDomainTweenDuration: DEFAULT_Y_DOMAIN_TWEEN_MS,
15
+ yDomainSkeletonByAxis: { left: [0, 100] },
16
+ // No target: charts without a y-domain tween (bar, candlestick, scatter,
17
+ // live line) have no settled domain distinct from the live scale, so
18
+ // `useSettledYScale` must fall back to it. A placeholder here made `YAxis`
19
+ // tick 0–100 on top of the real data scale.
20
+ yDomainTargetByAxis: {}
21
+ };
22
+ export {
23
+ DEFAULT_CHART_LIFECYCLE,
24
+ DEFAULT_CHART_STATUS,
25
+ DEFAULT_Y_DOMAIN_TWEEN_MS,
26
+ Y_DOMAIN_TWEEN_SKIP_THRESHOLD,
27
+ isChartInteractionPhase,
28
+ resolveRestingChartPhase
29
+ };
@@ -0,0 +1,62 @@
1
+ "use client";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import { m } from "motion/react";
4
+ import { clipRevealTransition } from "./animation";
5
+ function ChartRevealClip({
6
+ clipPathId,
7
+ height,
8
+ targetWidth,
9
+ enterTransition,
10
+ revealEpoch,
11
+ padding = 0,
12
+ paddingY = padding,
13
+ animating = true,
14
+ mode = "reveal",
15
+ onComplete
16
+ }) {
17
+ const transition = clipRevealTransition(enterTransition);
18
+ const paddedWidth = Math.max(0, targetWidth + padding * 2);
19
+ const paddedHeight = height + paddingY * 2;
20
+ if (!animating) {
21
+ return /* @__PURE__ */ jsx("clipPath", { id: clipPathId, children: /* @__PURE__ */ jsx(
22
+ "rect",
23
+ {
24
+ height: paddedHeight,
25
+ width: paddedWidth,
26
+ x: -padding,
27
+ y: -paddingY
28
+ }
29
+ ) });
30
+ }
31
+ if (mode === "conceal") {
32
+ const rightEdge = -padding + paddedWidth;
33
+ return /* @__PURE__ */ jsx("clipPath", { id: clipPathId, children: /* @__PURE__ */ jsx(
34
+ m.rect,
35
+ {
36
+ animate: { width: 0, x: rightEdge },
37
+ height: paddedHeight,
38
+ initial: { width: paddedWidth, x: -padding },
39
+ onAnimationComplete: () => onComplete?.(),
40
+ transition,
41
+ y: -paddingY
42
+ },
43
+ `conceal-${revealEpoch}`
44
+ ) });
45
+ }
46
+ return /* @__PURE__ */ jsx("clipPath", { id: clipPathId, children: /* @__PURE__ */ jsx(
47
+ m.rect,
48
+ {
49
+ animate: { width: paddedWidth },
50
+ height: paddedHeight,
51
+ initial: { width: 0 },
52
+ transition,
53
+ width: paddedWidth,
54
+ x: -padding,
55
+ y: -paddingY
56
+ },
57
+ `reveal-${revealEpoch}`
58
+ ) });
59
+ }
60
+ export {
61
+ ChartRevealClip
62
+ };
@@ -0,0 +1,19 @@
1
+ const CHART_SCALE_VARS = [
2
+ "var(--chart-scale-01)",
3
+ "var(--chart-scale-02)",
4
+ "var(--chart-scale-03)",
5
+ "var(--chart-scale-04)",
6
+ "var(--chart-scale-05)"
7
+ ];
8
+ const chartScaleCssVars = {
9
+ scale01: CHART_SCALE_VARS[0],
10
+ scale02: CHART_SCALE_VARS[1],
11
+ scale03: CHART_SCALE_VARS[2],
12
+ scale04: CHART_SCALE_VARS[3],
13
+ scale05: CHART_SCALE_VARS[4],
14
+ patternColor: "var(--chart-scale-pattern-color)"
15
+ };
16
+ export {
17
+ CHART_SCALE_VARS,
18
+ chartScaleCssVars
19
+ };
@@ -0,0 +1,72 @@
1
+ "use client";
2
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
+ import NumberFlow from "@number-flow/react";
4
+ import { useEffect, useMemo, useState } from "react";
5
+ import { cn } from "../lib/utils";
6
+ const defaultChartStatFlowFormat = {
7
+ notation: "standard",
8
+ maximumFractionDigits: 0
9
+ };
10
+ function formatStatValue(value, formatOptions, prefix, suffix) {
11
+ const formatted = new Intl.NumberFormat(void 0, formatOptions).format(
12
+ value
13
+ );
14
+ return `${prefix ?? ""}${formatted}${suffix ?? ""}`;
15
+ }
16
+ function useNumberFlowElementReady() {
17
+ const [ready, setReady] = useState(
18
+ () => typeof customElements !== "undefined" && Boolean(customElements.get("number-flow-react"))
19
+ );
20
+ useEffect(() => {
21
+ if (ready) {
22
+ return;
23
+ }
24
+ let cancelled = false;
25
+ customElements.whenDefined("number-flow-react").then(() => {
26
+ if (!cancelled) {
27
+ setReady(true);
28
+ }
29
+ });
30
+ return () => {
31
+ cancelled = true;
32
+ };
33
+ }, [ready]);
34
+ return ready;
35
+ }
36
+ function ChartStatFlow({
37
+ value,
38
+ label,
39
+ formatOptions = defaultChartStatFlowFormat,
40
+ prefix,
41
+ suffix,
42
+ valueClassName = "text-2xl font-bold",
43
+ labelClassName = "text-xs",
44
+ icon
45
+ }) {
46
+ const numberFlowReady = useNumberFlowElementReady();
47
+ const staticValue = useMemo(
48
+ () => formatStatValue(value, formatOptions, prefix, suffix),
49
+ [value, formatOptions, prefix, suffix]
50
+ );
51
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
52
+ icon ? /* @__PURE__ */ jsx("div", { className: "mb-2 flex h-12 w-12 items-center justify-center rounded-full bg-chart-muted", children: icon }) : null,
53
+ /* @__PURE__ */ jsx("span", { className: cn("text-chart-foreground tabular-nums", valueClassName), children: numberFlowReady ? /* @__PURE__ */ jsx(
54
+ NumberFlow,
55
+ {
56
+ format: formatOptions,
57
+ isolate: true,
58
+ prefix,
59
+ suffix,
60
+ value,
61
+ willChange: true
62
+ }
63
+ ) : staticValue }),
64
+ /* @__PURE__ */ jsx("span", { className: cn("mt-0.5 text-chart-label", labelClassName), children: label })
65
+ ] });
66
+ }
67
+ ChartStatFlow.displayName = "ChartStatFlow";
68
+ export {
69
+ ChartStatFlow,
70
+ defaultChartStatFlowFormat,
71
+ useNumberFlowElementReady
72
+ };
@@ -0,0 +1,29 @@
1
+ "use client";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import { BackgroundPatternLayer } from "../background";
4
+ import { useChoroplethCamera } from "./choropleth-camera";
5
+ import { useChoroplethStable } from "./choropleth-context";
6
+ function ChoroplethBackground({
7
+ hideOnFocus = false,
8
+ ...props
9
+ }) {
10
+ const { width, height, isLoaded, enterTransition } = useChoroplethStable();
11
+ const { focusedKey } = useChoroplethCamera();
12
+ const hidden = hideOnFocus && focusedKey !== null;
13
+ return /* @__PURE__ */ jsx("g", { style: { opacity: hidden ? 0 : 1, transition: "opacity 0.3s ease-out" }, children: /* @__PURE__ */ jsx(
14
+ BackgroundPatternLayer,
15
+ {
16
+ ...props,
17
+ enterTransition,
18
+ height,
19
+ isLoaded,
20
+ width
21
+ }
22
+ ) });
23
+ }
24
+ ChoroplethBackground.displayName = "ChoroplethBackground";
25
+ var choropleth_background_default = ChoroplethBackground;
26
+ export {
27
+ ChoroplethBackground,
28
+ choropleth_background_default as default
29
+ };
@@ -0,0 +1,379 @@
1
+ "use client";
2
+ import { geoArea } from "d3-geo";
3
+ import {
4
+ animate,
5
+ useMotionValue,
6
+ useReducedMotion
7
+ } from "motion/react";
8
+ import {
9
+ createContext,
10
+ useCallback,
11
+ useContext,
12
+ useEffect,
13
+ useMemo,
14
+ useRef,
15
+ useState
16
+ } from "react";
17
+ const DEFAULT_CHOROPLETH_ZOOM_EASE = [0.83, 0, 0.17, 1];
18
+ const DEFAULT_YAW = 18;
19
+ const POINTER_SPRING = { type: "spring", stiffness: 90, damping: 20 };
20
+ const DRAG_CLICK_TOLERANCE = 5;
21
+ const CHOROPLETH_CAMERA_SCALE_VAR = "--choropleth-camera-k";
22
+ const noop = () => void 0;
23
+ const ChoroplethCameraContext = createContext({
24
+ enabled: false,
25
+ focusedKey: null,
26
+ focusedFeatureIndex: null,
27
+ maxZoom: 1,
28
+ isHoverable: () => true,
29
+ focusFeature: noop,
30
+ focusPoint: noop,
31
+ reset: noop,
32
+ wasDrag: () => false
33
+ });
34
+ function useChoroplethCamera() {
35
+ return useContext(ChoroplethCameraContext);
36
+ }
37
+ function mainland(feature) {
38
+ const geometry = feature.geometry;
39
+ if (geometry?.type !== "MultiPolygon") {
40
+ return feature;
41
+ }
42
+ let best = null;
43
+ let bestArea = -1;
44
+ for (const coordinates of geometry.coordinates) {
45
+ const polygon = { type: "Polygon", coordinates };
46
+ const area = geoArea(polygon);
47
+ if (area > bestArea) {
48
+ bestArea = area;
49
+ best = polygon;
50
+ }
51
+ }
52
+ return best ? { ...feature, geometry: best } : feature;
53
+ }
54
+ function signedYaw(side, yaw) {
55
+ if (side === "flat") {
56
+ return 0;
57
+ }
58
+ return side === "east" ? -yaw : yaw;
59
+ }
60
+ function useChoroplethCameraController({
61
+ width,
62
+ height,
63
+ mercator,
64
+ features,
65
+ clickToZoom,
66
+ focusMaxZoom,
67
+ perspective,
68
+ zoomTransition,
69
+ onBeforeFocus
70
+ }) {
71
+ const reduceMotion = useReducedMotion();
72
+ const cameraRef = useRef(null);
73
+ const svgRef = useRef(null);
74
+ const planeRef = useRef(null);
75
+ const viewRef = useRef({ cx: width / 2, cy: height / 2, k: 1 });
76
+ const flight = useRef(null);
77
+ const yawAnim = useRef(null);
78
+ const tiltAnim = useRef(null);
79
+ const pressRef = useRef(
80
+ null
81
+ );
82
+ const [focusedKey, setFocusedKey] = useState(null);
83
+ const overview = perspective?.overview;
84
+ const focus = perspective?.focus;
85
+ const overviewView = overview?.view ?? "flat";
86
+ const overviewYaw = overview?.yaw ?? DEFAULT_YAW;
87
+ const overviewTilt = overview?.tilt ?? 0;
88
+ const focusView = focus?.view;
89
+ const focusYaw = focus?.yaw ?? overviewYaw;
90
+ const focusTilt = focus?.tilt ?? overviewTilt;
91
+ const depth = perspective?.depth ?? 1600;
92
+ const overviewStaticYaw = overviewView === "pointer" ? 0 : signedYaw(overviewView, overviewYaw);
93
+ const yaw = useMotionValue(overviewStaticYaw);
94
+ const tilt = useMotionValue(overviewTilt);
95
+ const hasPerspective = perspective !== void 0 && (overviewView !== "flat" || overviewTilt !== 0 || focusView !== void 0 && focusView !== "flat" || focusTilt !== 0);
96
+ const duration = reduceMotion ? 0 : (zoomTransition?.duration ?? 700) / 1e3;
97
+ const easeKey = (zoomTransition?.ease ?? DEFAULT_CHOROPLETH_ZOOM_EASE).join(
98
+ ","
99
+ );
100
+ const ease = useMemo(
101
+ () => easeKey.split(",").map(Number),
102
+ [easeKey]
103
+ );
104
+ const applyView = useCallback(
105
+ (view) => {
106
+ viewRef.current = view;
107
+ const tx = width / 2 - view.k * view.cx;
108
+ const ty = height / 2 - view.k * view.cy;
109
+ cameraRef.current?.setAttribute(
110
+ "transform",
111
+ view.k === 1 && tx === 0 && ty === 0 ? "" : `translate(${tx} ${ty}) scale(${view.k})`
112
+ );
113
+ svgRef.current?.style.setProperty(
114
+ CHOROPLETH_CAMERA_SCALE_VAR,
115
+ String(view.k)
116
+ );
117
+ },
118
+ [width, height]
119
+ );
120
+ const moveAngles = useCallback(
121
+ (nextYaw, nextTilt) => {
122
+ yawAnim.current?.stop();
123
+ tiltAnim.current?.stop();
124
+ if (duration === 0) {
125
+ yaw.set(nextYaw);
126
+ tilt.set(nextTilt);
127
+ return;
128
+ }
129
+ const options = { duration, ease };
130
+ yawAnim.current = animate(yaw, nextYaw, options);
131
+ tiltAnim.current = animate(tilt, nextTilt, options);
132
+ },
133
+ [duration, ease, tilt, yaw]
134
+ );
135
+ const flyTo = useCallback(
136
+ (target) => {
137
+ flight.current?.stop();
138
+ if (duration === 0) {
139
+ applyView(target);
140
+ return;
141
+ }
142
+ const from = { ...viewRef.current };
143
+ const logFrom = Math.log(from.k);
144
+ const logTo = Math.log(target.k);
145
+ flight.current = animate(0, 1, {
146
+ duration,
147
+ ease,
148
+ onUpdate: (t) => applyView({
149
+ cx: from.cx + (target.cx - from.cx) * t,
150
+ cy: from.cy + (target.cy - from.cy) * t,
151
+ k: Math.exp(logFrom + (logTo - logFrom) * t)
152
+ })
153
+ });
154
+ },
155
+ [applyView, duration, ease]
156
+ );
157
+ const focusAngles = useCallback(
158
+ (cx) => {
159
+ if (!perspective) {
160
+ return;
161
+ }
162
+ let side;
163
+ if (focusView === "auto") {
164
+ side = cx < width / 2 ? "west" : "east";
165
+ } else if (focusView) {
166
+ side = focusView;
167
+ } else {
168
+ side = overviewView === "pointer" ? "flat" : overviewView;
169
+ }
170
+ moveAngles(signedYaw(side, focusYaw), focusTilt);
171
+ },
172
+ [
173
+ focusTilt,
174
+ focusView,
175
+ focusYaw,
176
+ moveAngles,
177
+ overviewView,
178
+ perspective,
179
+ width
180
+ ]
181
+ );
182
+ const reset = useCallback(() => {
183
+ setFocusedKey(null);
184
+ flyTo({ cx: width / 2, cy: height / 2, k: 1 });
185
+ moveAngles(overviewStaticYaw, overviewTilt);
186
+ }, [flyTo, height, moveAngles, overviewStaticYaw, overviewTilt, width]);
187
+ const focusFeature = useCallback(
188
+ (index) => {
189
+ const key = `feature:${index}`;
190
+ if (focusedKey === key) {
191
+ reset();
192
+ return;
193
+ }
194
+ const feature = features[index];
195
+ if (!feature) {
196
+ return;
197
+ }
198
+ const [[x0, y0], [x1, y1]] = mercator.path.bounds(mainland(feature));
199
+ if (![x0, y0, x1, y1].every(Number.isFinite)) {
200
+ return;
201
+ }
202
+ const k = Math.min(
203
+ focusMaxZoom,
204
+ Math.max(
205
+ 1,
206
+ 0.7 * Math.min(width / (x1 - x0 || 1), height / (y1 - y0 || 1))
207
+ )
208
+ );
209
+ const cx = (x0 + x1) / 2;
210
+ onBeforeFocus?.();
211
+ setFocusedKey(key);
212
+ flyTo({ cx, cy: (y0 + y1) / 2, k });
213
+ focusAngles(cx);
214
+ },
215
+ [
216
+ features,
217
+ flyTo,
218
+ focusAngles,
219
+ focusMaxZoom,
220
+ focusedKey,
221
+ height,
222
+ mercator,
223
+ onBeforeFocus,
224
+ reset,
225
+ width
226
+ ]
227
+ );
228
+ const focusPoint = useCallback(
229
+ (coordinates, key) => {
230
+ const pointKey = `point:${key}`;
231
+ if (focusedKey === pointKey) {
232
+ reset();
233
+ return;
234
+ }
235
+ const projected = mercator.projection(coordinates);
236
+ if (!projected) {
237
+ return;
238
+ }
239
+ const [cx, cy] = projected;
240
+ onBeforeFocus?.();
241
+ setFocusedKey(pointKey);
242
+ flyTo({ cx, cy, k: Math.min(focusMaxZoom, 4) });
243
+ focusAngles(cx);
244
+ },
245
+ [
246
+ focusAngles,
247
+ flyTo,
248
+ focusMaxZoom,
249
+ focusedKey,
250
+ mercator,
251
+ onBeforeFocus,
252
+ reset
253
+ ]
254
+ );
255
+ useEffect(() => {
256
+ flight.current?.stop();
257
+ applyView({ cx: width / 2, cy: height / 2, k: 1 });
258
+ setFocusedKey(null);
259
+ }, [applyView, width, height]);
260
+ useEffect(() => {
261
+ if (focusedKey === null) {
262
+ moveAngles(overviewStaticYaw, overviewTilt);
263
+ }
264
+ }, [focusedKey, moveAngles, overviewStaticYaw, overviewTilt]);
265
+ useEffect(() => {
266
+ const write = () => {
267
+ const plane = planeRef.current;
268
+ if (plane) {
269
+ plane.style.transform = `rotateX(${tilt.get()}deg) rotateY(${yaw.get()}deg)`;
270
+ }
271
+ };
272
+ write();
273
+ const offYaw = yaw.on("change", write);
274
+ const offTilt = tilt.on("change", write);
275
+ return () => {
276
+ offYaw();
277
+ offTilt();
278
+ };
279
+ }, [tilt, yaw]);
280
+ useEffect(() => {
281
+ if (!clickToZoom) {
282
+ return;
283
+ }
284
+ const onKey = (event) => {
285
+ if (event.key === "Escape" && focusedKey !== null) {
286
+ reset();
287
+ }
288
+ };
289
+ window.addEventListener("keydown", onKey);
290
+ return () => window.removeEventListener("keydown", onKey);
291
+ }, [clickToZoom, focusedKey, reset]);
292
+ useEffect(
293
+ () => () => {
294
+ flight.current?.stop();
295
+ yawAnim.current?.stop();
296
+ tiltAnim.current?.stop();
297
+ },
298
+ []
299
+ );
300
+ const followsPointer = perspective !== void 0 && overviewView === "pointer" && focusedKey === null && !reduceMotion;
301
+ const onPointerMove = useCallback(
302
+ (event) => {
303
+ const press = pressRef.current;
304
+ if (press && Math.hypot(event.clientX - press.x, event.clientY - press.y) > DRAG_CLICK_TOLERANCE) {
305
+ press.moved = true;
306
+ }
307
+ if (!followsPointer) {
308
+ return;
309
+ }
310
+ const box = event.currentTarget.getBoundingClientRect();
311
+ const t = Math.min(
312
+ 1,
313
+ Math.max(0, (event.clientX - box.left) / box.width)
314
+ );
315
+ yawAnim.current?.stop();
316
+ yawAnim.current = animate(
317
+ yaw,
318
+ overviewYaw - 2 * overviewYaw * t,
319
+ POINTER_SPRING
320
+ );
321
+ },
322
+ [followsPointer, overviewYaw, yaw]
323
+ );
324
+ const onPointerLeave = useCallback(() => {
325
+ if (!followsPointer) {
326
+ return;
327
+ }
328
+ yawAnim.current?.stop();
329
+ yawAnim.current = animate(yaw, 0, POINTER_SPRING);
330
+ }, [followsPointer, yaw]);
331
+ const onPointerDown = useCallback((event) => {
332
+ pressRef.current = { x: event.clientX, y: event.clientY, moved: false };
333
+ }, []);
334
+ const wasDrag = useCallback(() => pressRef.current?.moved ?? false, []);
335
+ const api = useMemo(
336
+ () => ({
337
+ enabled: clickToZoom,
338
+ focusedKey,
339
+ focusedFeatureIndex: focusedKey?.startsWith("feature:") ? Number(focusedKey.slice("feature:".length)) : null,
340
+ maxZoom: focusMaxZoom,
341
+ isHoverable: (key) => focusedKey === null || focusedKey === key,
342
+ focusFeature,
343
+ focusPoint,
344
+ reset,
345
+ wasDrag
346
+ }),
347
+ [
348
+ clickToZoom,
349
+ focusFeature,
350
+ focusMaxZoom,
351
+ focusPoint,
352
+ focusedKey,
353
+ reset,
354
+ wasDrag
355
+ ]
356
+ );
357
+ return {
358
+ api,
359
+ cameraRef,
360
+ svgRef,
361
+ planeRef,
362
+ yaw,
363
+ tilt,
364
+ depth,
365
+ hasPerspective,
366
+ edgeFade: hasPerspective ? Math.min(45, Math.max(0, perspective?.edgeFade ?? 0)) : 0,
367
+ edgeFadeActive: focusedKey !== null || overviewView !== "flat" || overviewTilt !== 0,
368
+ onPointerDown,
369
+ onPointerMove,
370
+ onPointerLeave
371
+ };
372
+ }
373
+ export {
374
+ CHOROPLETH_CAMERA_SCALE_VAR,
375
+ ChoroplethCameraContext,
376
+ DEFAULT_CHOROPLETH_ZOOM_EASE,
377
+ useChoroplethCamera,
378
+ useChoroplethCameraController
379
+ };