| 1 | "use client" |
| 2 | |
| 3 | import { |
| 4 | Children, |
| 5 | type ComponentType, |
| 6 | isValidElement, |
| 7 | type ReactNode, |
| 8 | } from "react" |
| 9 | import { |
| 10 | type ChartConfig, |
| 11 | ChartContext, |
| 12 | type ChartType, |
| 13 | type Margins, |
| 14 | useChartController, |
| 15 | } from "./chart-context" |
| 16 | import { CommonChartContext } |
| 17 | import type { BloomInput |
| 18 | import { cn } |
| 19 | import type { StackType |
| 20 | import { useChartDimensions } |
| 21 | |
| 22 | // `object` rather than `Record<string, unknown>`: interfaces don't get an |
| 23 | // implicit index signature, so interface-typed rows failed to satisfy the |
| 24 | // generic. Internal layers still index rows through their own Row type. |
| 25 | type Row |
| 26 | |
| 27 | const DEFAULT_MARGINS: Margins |
| 28 | top: 10, |
| 29 | right: 12, |
| 30 | bottom: 22, |
| 31 | left: 36, |
| 32 | } |
| 33 | |
| 34 | export type CartesianChartProps |
| 35 | data: TData |
| 36 | config: ChartConfig |
| 37 | children: ReactNode |
| 38 | stackType?: StackType |
| 39 | margins?: Partial |
| 40 | className?: string |
| 41 | animate?: boolean |
| 42 | animationDuration?: number |
| 43 | replayToken?: number |
| 44 | /** Set false for a decorative sparkline: keeps the hover lift but no scrub |
| 45 | * crosshair / tooltip. */ |
| 46 | interactive?: boolean |
| 47 | /** Controlled crosshair position (e.g. a committed point) - overrides the |
| 48 | * internal hover when set. */ |
| 49 | markerIndex?: number |
| 50 | /** Parent-driven hover (e.g. the whole card/row) - lifts the fill. */ |
| 51 | hovered?: boolean |
| 52 | /** Glow on the dither fill. */ |
| 53 | bloom?: BloomInput |
| 54 | /** Only bloom while the chart is hovered. */ |
| 55 | bloomOnHover?: boolean |
| 56 | /** Fires with the scrubbed index as the pointer moves (null on leave). */ |
| 57 | onHoverChange?: ( |
| 58 | defaultSelectedDataKey?: string |
| 59 | onSelectionChange?: ( |
| 60 | } |
| 61 | |
| 62 | /** Which render layer a composed part targets - defaults to the front SVG. */ |
| 63 | function layerOf(node: |
| 64 | if (!isValidElement(node |
| 65 | return (node.type |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Shared root for the cartesian dither charts (area, line, bar). Owns the |
| 70 | * measured size, the shared context, and pointer interaction; every visual is |
| 71 | * composed as children. Back chrome (grid) sits behind the dither canvas; the |
| 72 | * canvas paints the fill/line/bars + stars; front chrome (axes, dots) and DOM |
| 73 | * legend/tooltip layer on top. `chartType` drives the scales/interaction and the |
| 74 | * `Canvas` prop supplies the family's painter (continuous for area/line, bars for |
| 75 | * bar) - so each chart ships only its own canvas. |
| 76 | */ |
| 77 | export function CartesianRoot<TData |
| 78 | chartType, |
| 79 | Canvas, |
| 80 | data, |
| 81 | config, |
| 82 | children, |
| 83 | stackType = "default" |
| 84 | margins: marginsProp, |
| 85 | className, |
| 86 | animate = true, |
| 87 | animationDuration = 900, |
| 88 | replayToken = 0, |
| 89 | interactive = true, |
| 90 | markerIndex = null, |
| 91 | hovered = false, |
| 92 | bloom = "off" |
| 93 | bloomOnHover = false, |
| 94 | onHoverChange, |
| 95 | defaultSelectedDataKey = null, |
| 96 | onSelectionChange, |
| 97 | }: CartesianChartProps< |
| 98 | chartType: ChartType |
| 99 | Canvas: ComponentType |
| 100 | }) { |
| 101 | const { ref, |
| 102 | const margins = |
| 103 | |
| 104 | const ctx = |
| 105 | chartType, |
| 106 | // Safe: the controller only reads row[key] for the configured series keys. |
| 107 | data: data as |
| 108 | config, |
| 109 | stackType, |
| 110 | dimensions: size, |
| 111 | margins, |
| 112 | animate, |
| 113 | animationDuration, |
| 114 | replayToken, |
| 115 | markerIndex, |
| 116 | hovered, |
| 117 | bloom, |
| 118 | bloomOnHover, |
| 119 | defaultSelectedDataKey, |
| 120 | onSelectionChange, |
| 121 | }) |
| 122 | |
| 123 | const backChildren: |
| 124 | const svgChildren: |
| 125 | const domChildren: |
| 126 | Children.forEach(children |
| 127 | const layer = |
| 128 | if (layer === |
| 129 | else if (layer |
| 130 | else svgChildren.push( |
| 131 | }) |
| 132 | |
| 133 | const onMove = |
| 134 | const el = |
| 135 | if (!el) |
| 136 | const rect = |
| 137 | const px = |
| 138 | const index = |
| 139 | ctx.setHoverIndex(index |
| 140 | ctx.setCursorX(clientX |
| 141 | onHoverChange?.(index) |
| 142 | } |
| 143 | |
| 144 | return ( |
| 145 | <ChartContext value={ |
| 146 | <CommonChartContext value={ |
| 147 | <div |
| 148 | ref={ref} |
| 149 | className={cn(" |
| 150 | onPointerEnter={() => |
| 151 | onPointerMove={interactive ? |
| 152 | onPointerLeave={() => |
| 153 | ctx.setMouseInChart(false) |
| 154 | ctx.setHoverIndex(null) |
| 155 | onHoverChange?.(null) |
| 156 | }} |
| 157 | > |
| 158 | {ctx.ready && |
| 159 | <svg |
| 160 | width={size.width |
| 161 | height={size.height |
| 162 | className="absolute inset-0 overflow-visible" |
| 163 | aria-hidden |
| 164 | role="presentation" |
| 165 | > |
| 166 | <g transform={` |
| 167 | {backChildren} |
| 168 | </g> |
| 169 | </svg> |
| 170 | )} |
| 171 | <Canvas /> |
| 172 | {ctx.ready && |
| 173 | <svg |
| 174 | width={size.width |
| 175 | height={size.height |
| 176 | className="absolute inset-0 overflow-visible" |
| 177 | role="img" |
| 178 | aria-label="Chart" |
| 179 | > |
| 180 | <g transform={` |
| 181 | {svgChildren} |
| 182 | </g> |
| 183 | </svg> |
| 184 | )} |
| 185 | {domChildren} |
| 186 | </div> |
| 187 | </CommonChartContext> |
| 188 | </ChartContext> |
| 189 | ) |
| 190 | } |
| 191 | |
| 192 | export type AreaChartProps |