p1-charts 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +100 -2
  3. package/dist/CategoryBarChart-1Kn3Vozc.js +580 -0
  4. package/dist/CategoryBarChart-1Kn3Vozc.js.map +1 -0
  5. package/dist/ChartFrame-B1bVFM2E.js +403 -0
  6. package/dist/ChartFrame-B1bVFM2E.js.map +1 -0
  7. package/dist/{Heatmap-C1XcTUg1.js → Heatmap-CFC753mV.js} +278 -272
  8. package/dist/Heatmap-CFC753mV.js.map +1 -0
  9. package/dist/{PieLegend-BZsv9IbF.js → PieLegend-CbpjaOJH.js} +94 -111
  10. package/dist/PieLegend-CbpjaOJH.js.map +1 -0
  11. package/dist/PolarChart-hcNTWKfJ.js +450 -0
  12. package/dist/PolarChart-hcNTWKfJ.js.map +1 -0
  13. package/dist/Sparkline-BApLVWfg.js +110 -0
  14. package/dist/Sparkline-BApLVWfg.js.map +1 -0
  15. package/dist/{TimeSeriesChart-BIwJxgnD.js → TimeSeriesChart-BzeHEtQl.js} +120 -119
  16. package/dist/{TimeSeriesChart-BIwJxgnD.js.map → TimeSeriesChart-BzeHEtQl.js.map} +1 -1
  17. package/dist/category.d.ts +1 -1
  18. package/dist/category.d.ts.map +1 -1
  19. package/dist/category.js +1 -1
  20. package/dist/charts/CategoryBarChart/index.d.ts +1 -1
  21. package/dist/charts/CategoryBarChart/index.d.ts.map +1 -1
  22. package/dist/charts/Heatmap/index.d.ts.map +1 -1
  23. package/dist/charts/PolarChart/index.d.ts.map +1 -1
  24. package/dist/charts/ScatterChart/index.d.ts +1 -1
  25. package/dist/charts/ScatterChart/index.d.ts.map +1 -1
  26. package/dist/charts/Sparkline/index.d.ts +5 -2
  27. package/dist/charts/Sparkline/index.d.ts.map +1 -1
  28. package/dist/components/ChartFrame/index.d.ts +7 -0
  29. package/dist/components/ChartFrame/index.d.ts.map +1 -0
  30. package/dist/components/ChartStory/index.d.ts +7 -0
  31. package/dist/components/ChartStory/index.d.ts.map +1 -0
  32. package/dist/components/HeatmapCells/index.d.ts +1 -1
  33. package/dist/components/HeatmapCells/index.d.ts.map +1 -1
  34. package/dist/{css-C-rsk7-l.js → css-BtFQ2bM4.js} +2 -2
  35. package/dist/{css-C-rsk7-l.js.map → css-BtFQ2bM4.js.map} +1 -1
  36. package/dist/heatmap.js +1 -1
  37. package/dist/hooks/useMarkMemory.d.ts +9 -0
  38. package/dist/hooks/useMarkMemory.d.ts.map +1 -0
  39. package/dist/hooks/usePieTween.d.ts.map +1 -1
  40. package/dist/hooks/useSteps.d.ts +4 -0
  41. package/dist/hooks/useSteps.d.ts.map +1 -0
  42. package/dist/hooks/useTween.d.ts +12 -0
  43. package/dist/hooks/useTween.d.ts.map +1 -0
  44. package/dist/index.d.ts +11 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +122 -118
  47. package/dist/index.js.map +1 -1
  48. package/dist/motion.utils-DFoqptDi.js +55 -0
  49. package/dist/motion.utils-DFoqptDi.js.map +1 -0
  50. package/dist/pie.js +1 -1
  51. package/dist/scatter.js +1 -1
  52. package/dist/series.js +1 -1
  53. package/dist/{tooltip.utils-D7xaBSUK.js → shared-C1pGJV4T.js} +3 -7
  54. package/dist/shared-C1pGJV4T.js.map +1 -0
  55. package/dist/sparkline.d.ts +1 -1
  56. package/dist/sparkline.d.ts.map +1 -1
  57. package/dist/sparkline.js +2 -2
  58. package/dist/story.d.ts +11 -0
  59. package/dist/story.d.ts.map +1 -0
  60. package/dist/story.js +7 -0
  61. package/dist/theme.js +3 -3
  62. package/dist/{theme.utils-CP1XtGIe.js → theme.utils-DgmWROoe.js} +40 -20
  63. package/dist/theme.utils-DgmWROoe.js.map +1 -0
  64. package/dist/tooltip.utils-CXD_NapA.js +9 -0
  65. package/dist/tooltip.utils-CXD_NapA.js.map +1 -0
  66. package/dist/types/category.d.ts +10 -0
  67. package/dist/types/category.d.ts.map +1 -1
  68. package/dist/types/heatmap.d.ts +3 -0
  69. package/dist/types/heatmap.d.ts.map +1 -1
  70. package/dist/types/heatmapParts.d.ts +2 -0
  71. package/dist/types/heatmapParts.d.ts.map +1 -1
  72. package/dist/types/motion.d.ts +31 -0
  73. package/dist/types/motion.d.ts.map +1 -0
  74. package/dist/types/pie.d.ts +3 -4
  75. package/dist/types/pie.d.ts.map +1 -1
  76. package/dist/types/polar.d.ts +3 -0
  77. package/dist/types/polar.d.ts.map +1 -1
  78. package/dist/types/scatter.d.ts +3 -0
  79. package/dist/types/scatter.d.ts.map +1 -1
  80. package/dist/types/sparkline.d.ts +3 -0
  81. package/dist/types/sparkline.d.ts.map +1 -1
  82. package/dist/types/story.d.ts +73 -0
  83. package/dist/types/story.d.ts.map +1 -0
  84. package/dist/{useChartTheme-B06p_jvu.js → useChartTheme-KKtc1C4K.js} +2 -2
  85. package/dist/{useChartTheme-B06p_jvu.js.map → useChartTheme-KKtc1C4K.js.map} +1 -1
  86. package/dist/useControllable-Crsr0byh.js +11 -0
  87. package/dist/useControllable-Crsr0byh.js.map +1 -0
  88. package/dist/useMarkMemory-CLNY6TtT.js +15 -0
  89. package/dist/useMarkMemory-CLNY6TtT.js.map +1 -0
  90. package/dist/useTween-B0G0CcvM.js +46 -0
  91. package/dist/useTween-B0G0CcvM.js.map +1 -0
  92. package/dist/utils/category.utils.d.ts +2 -0
  93. package/dist/utils/category.utils.d.ts.map +1 -1
  94. package/dist/utils/color.utils.d.ts +9 -0
  95. package/dist/utils/color.utils.d.ts.map +1 -1
  96. package/dist/utils/csv.utils.d.ts +6 -0
  97. package/dist/utils/csv.utils.d.ts.map +1 -0
  98. package/dist/utils/motion.utils.d.ts +25 -0
  99. package/dist/utils/motion.utils.d.ts.map +1 -1
  100. package/dist/utils/pieTween.utils.d.ts +1 -1
  101. package/dist/utils/pieTween.utils.d.ts.map +1 -1
  102. package/dist/utils/sparkline.utils.d.ts +15 -0
  103. package/dist/utils/sparkline.utils.d.ts.map +1 -1
  104. package/dist/{visibility.utils-nMI5a_Et.js → visibility.utils-wuc3ZS8D.js} +18 -25
  105. package/dist/visibility.utils-wuc3ZS8D.js.map +1 -0
  106. package/package.json +10 -3
  107. package/src/category.ts +1 -0
  108. package/src/charts/CategoryBarChart/index.tsx +203 -44
  109. package/src/charts/Heatmap/index.tsx +4 -0
  110. package/src/charts/PolarChart/index.tsx +98 -5
  111. package/src/charts/ScatterChart/index.tsx +96 -13
  112. package/src/charts/Sparkline/index.tsx +54 -7
  113. package/src/components/ChartFrame/index.tsx +180 -0
  114. package/src/components/ChartStory/index.tsx +184 -0
  115. package/src/components/HeatmapCells/index.tsx +5 -2
  116. package/src/hooks/useMarkMemory.ts +29 -0
  117. package/src/hooks/usePieTween.ts +9 -44
  118. package/src/hooks/useSteps.ts +77 -0
  119. package/src/hooks/useTween.ts +112 -0
  120. package/src/index.ts +19 -0
  121. package/src/sparkline.ts +1 -1
  122. package/src/story.ts +19 -0
  123. package/src/types/category.ts +11 -0
  124. package/src/types/heatmap.ts +3 -0
  125. package/src/types/heatmapParts.ts +2 -0
  126. package/src/types/motion.ts +35 -0
  127. package/src/types/pie.ts +3 -3
  128. package/src/types/polar.ts +3 -0
  129. package/src/types/scatter.ts +3 -0
  130. package/src/types/sparkline.ts +3 -0
  131. package/src/types/story.ts +103 -0
  132. package/src/utils/category.utils.ts +22 -0
  133. package/src/utils/color.utils.ts +48 -0
  134. package/src/utils/csv.utils.ts +28 -0
  135. package/src/utils/motion.utils.ts +124 -0
  136. package/src/utils/pieTween.utils.ts +1 -2
  137. package/src/utils/sparkline.utils.ts +69 -0
  138. package/dist/CategoryBarChart-YEJ9VB8h.js +0 -453
  139. package/dist/CategoryBarChart-YEJ9VB8h.js.map +0 -1
  140. package/dist/Heatmap-C1XcTUg1.js.map +0 -1
  141. package/dist/PieLegend-BZsv9IbF.js.map +0 -1
  142. package/dist/PolarChart-BUEhn3CI.js +0 -334
  143. package/dist/PolarChart-BUEhn3CI.js.map +0 -1
  144. package/dist/Sparkline-DtNhxGVo.js +0 -67
  145. package/dist/Sparkline-DtNhxGVo.js.map +0 -1
  146. package/dist/motion.utils-C1eaZJdQ.js +0 -9
  147. package/dist/motion.utils-C1eaZJdQ.js.map +0 -1
  148. package/dist/theme.utils-CP1XtGIe.js.map +0 -1
  149. package/dist/tooltip.utils-D7xaBSUK.js.map +0 -1
  150. package/dist/visibility.utils-nMI5a_Et.js.map +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0
4
+
5
+ Motion, stories and chart furniture, in the spirit of Flourish's built-in animation and storytelling.
6
+
7
+ ### Added
8
+
9
+ - **`animate` on every SVG chart**: `CategoryBarChart`, `ScatterChart`, `PolarChart`, `Sparkline`, `Heatmap`, plus `PieChart` as before. It takes `true` (default), `false`, or `{ duration, easing, stagger, appear }`. Marks are matched by a stable key and tween between data states. New marks grow in and removed marks shrink out. Reduced motion always jumps to the final frame. The first render is final unless `appear` is set.
10
+ - **Axis motion**: value ticks on `CategoryBarChart`, and x/y ticks and grid lines on `ScatterChart`, slide and fade between scales.
11
+ - **Scrolling sparkline**: when new `Sparkline` data is the old data moved along (a live window), the line scrolls instead of morphing.
12
+ - **Racing bars**: `CategoryBarChart` `sort="descending" | "ascending"` ranks categories by total. With `animate`, bars glide to their new rank. `onSelect` still reports the index in your `categories`.
13
+ - **`ChartStory`**: steps a chart through states with prev/next, play/pause, dots or a slider, captions, autoplay, interval and loop. Autoplay is off under reduced motion, and picking a step pauses.
14
+ - **`useSteps`**: the stepping logic on its own, for custom controls or scroll-driven steps.
15
+ - **`ChartFrame`**: title, subtitle, a summary for screen readers, source and notes around any chart. With `data`, it adds "Show data" (a real table) and "Download CSV".
16
+ - **Helpers**: `useTween` and `useMarkMemory` (motion for custom SVG marks); `toCsv` and `downloadCsv` (RFC 4180, with spreadsheet formula injection defused); `readableInk` and `contrastRatio` (WCAG contrast for labels on marks); `sparklineShapes` and `shiftOffset`; easings `linear`, `easeOutCubic`, `easeInOutCubic`.
17
+ - **New subpath**: `p1-charts/story`.
18
+
19
+ ### Changed
20
+
21
+ - `PieAnimate` is now an alias of the shared `ChartAnimate` type. Existing values still work.
22
+ - `CategoryBarChart` bars are keyed by series and category label, and carry `data-key`. `data-category` is the displayed position, which differs from the input index only when `sort` is set.
23
+ - `ScatterChart` ticks and `CategoryBarChart` category and value ticks are keyed by value or label rather than position.
24
+ - Themed `Heatmap` cells carry a `transition` style for fill and opacity. Unstyled cells carry none. Pass `animate={false}` to turn it off.
25
+
26
+ ### Not animated
27
+
28
+ - The canvas charts (`TimeSeriesChart`, `LineChart`, `BarChart`, `HistogramChart`, `CandlestickChart`). Lightweight Charts redraws everything on each `setData`.
package/README.md CHANGED
@@ -27,6 +27,7 @@ Subpath imports keep a page from loading charts it does not draw:
27
27
  | `p1-charts/category` | `CategoryBarChart`, `bin` |
28
28
  | `p1-charts/sparkline` | `Sparkline` |
29
29
  | `p1-charts/theme` | `p1Theme`, `p1ThemeFromCss`, `createTheme` |
30
+ | `p1-charts/story` | `ChartStory`, `ChartFrame`, `useSteps`, `useTween`, `useMarkMemory`, `toCsv`, `readableInk` |
30
31
 
31
32
  Peer dependencies: `react`, `react-dom` (>= 18), and `lightweight-charts` (^5.2.1). The sparkline, pie, heatmap, scatter, and category entries do not mount a Lightweight Charts instance. `lightweight-charts` stays a peer so the app and this package share one copy.
32
33
 
@@ -146,11 +147,13 @@ Square axis and crosshair labels are a `roundRect` patch scoped to the chart ele
146
147
  | `reference`, `references` | Dashed lines across the plot at `value`, with an optional `label`. Both are drawn |
147
148
  | `bands` | A tint between `from` and `to`, behind the bars |
148
149
  | `activeIndex` | Category marked when the pointer is not over a bar |
149
- | `onSelect` | Called with the category label and its index when a bar is clicked or chosen from the keyboard |
150
+ | `onSelect` | Called with the category label and its index in `categories` when a bar is clicked or chosen from the keyboard. The index ignores `sort` |
151
+ | `sort` | `"none"` (default), `"descending"` or `"ascending"` by each category's total across series. Ties keep input order |
152
+ | `animate` | Bars and category ticks tween between data states, keyed by series and category label, so a sorted chart races. See [Motion](#motion) |
150
153
  | Keyboard | Left and right move the active category and stop at the ends |
151
154
  | Pointer | Entering a bar selects its category |
152
155
  | Tooltip | Always shows the active category. `formatValue(seriesId, value)` formats each row. `renderTooltip(category, rows)` replaces the default |
153
- | Parts | Root `data-mode`. Bars are `data-part="bar"` with `data-series`, `data-category`, and `data-active`. Also `axis`, `tick`, `tooltip`, `tooltip-time`, `tooltip-row` |
156
+ | Parts | Root `data-mode`. Bars are `data-part="bar"` with `data-key` (the category label), `data-series`, `data-category` (displayed position), and `data-active`. Also `axis`, `tick`, `tooltip`, `tooltip-time`, `tooltip-row` |
154
157
  | Label | `ariaLabel` defaults to `"Bar chart"` |
155
158
 
156
159
  `bin(samples, options?)` turns raw observations into `{ from, to, count, label }[]`. The count defaults to Sturges' rule. `bins` sets an equal-width count. `min` and `max` set the domain. `edges` are explicit boundaries: a value lands in `[edge[i], edge[i + 1])`, and the last edge is inclusive. An empty sample list returns `[]`. `HistogramChart` draws columns on a time axis.
@@ -315,6 +318,101 @@ Selection props do nothing until `selectionMode` is set, the same as React Aria.
315
318
 
316
319
  Cells carry `data-step` (1 to N, 0 for a gap), `data-missing`, `data-dimmed`, `data-filtered`, `data-selected` and `data-extreme`; the root carries `data-emphasis`. Style them with the theme's `heatmap` slot (`gap`, `dimOpacity`, `filteredOpacity`, `missing`, `active`, `focus`, `selection`, `extreme`, `valueInk`), `classNames` / `styles`, or CSS on those attributes; `unstyled` emits no fills at all. Below 14px per cell the grid scrolls sideways inside its root instead of shrinking. `rangeStats`, `normaliseRange`, `extremes`, `heatmapLayout`, `useHeatmapState`, `useHeatmapSelection` and `useHeatmapSteps` are exported for a custom renderer.
317
320
 
321
+ ## Motion
322
+
323
+ Every SVG chart takes `animate`, and it is on by default. When the data changes, each mark moves from where it was drawn to where it belongs. Marks are matched by a stable key, so a bar keeps its identity when it changes rank. The first render draws the final frame. `prefers-reduced-motion: reduce` always jumps straight to it.
324
+
325
+ ```tsx
326
+ <CategoryBarChart animate={false} … />
327
+ <ScatterChart animate={{ duration: 600, stagger: 12 }} … />
328
+ <CategoryBarChart animate={{ appear: true }} … /> // grow from the baseline on mount
329
+ ```
330
+
331
+ | Option | Default | Detail |
332
+ | --- | --- | --- |
333
+ | `duration` | `240` | Milliseconds per mark |
334
+ | `easing` | `easeInOutCubic` | `(t) => number`. `linear`, `easeOutCubic` and `easeInOutCubic` are exported |
335
+ | `stagger` | `0` | Delay between successive marks |
336
+ | `appear` | `false` | Tween in from the baseline on first mount |
337
+
338
+ | Chart | What moves | Key | A new mark | A removed mark |
339
+ | --- | --- | --- | --- | --- |
340
+ | `CategoryBarChart` | Bar x, y, width and height; category labels; value-axis ticks | `series:category label` | Grows from the zero line, clamped to the axis | Shrinks back to the zero line |
341
+ | `ScatterChart` | Point position and radius, lines through them; x and y ticks and grid lines | `series:index` | Grows from radius 0 in place | Shrinks to radius 0 |
342
+ | `PolarChart` | Wedge and radar vertex radius; scatter position | `series:category` or `series:index` | Grows from the centre | Returns to the centre |
343
+ | `Sparkline` | Each value, so the scale moves with it. A window that slid along scrolls | absolute position | Slides in from the right, or appears in place | Slides out to the left |
344
+ | `PieChart` | Slice angles (as before) | slice key | Opens from its start angle | Dropped |
345
+ | `Heatmap` | Cell fill and opacity, through a CSS transition with the same easing | cell | | |
346
+
347
+ Axis ticks move like a D3 axis. A tick that stays slides to its new place. A new tick fades in from where its value sat on the old scale. A tick that goes fades out towards where its value sits on the new scale. At rest, nothing carries an `opacity` attribute.
348
+
349
+ A leaving mark keeps `data-part` and gains `data-leaving` and `pointer-events="none"`, so tooltips, keys and selection only ever see live data. `Sparkline` scrolls when the new values are the old ones moved along by k places, with at least two values in common. Anything else tweens value by value. The heatmap samples `easing` into CSS `linear()`, and draws no transition when `unstyled`, so your CSS decides.
350
+
351
+ The canvas charts (`TimeSeriesChart`, line, bar, histogram and candlestick) do not tween, because Lightweight Charts redraws everything on every `setData`. Live ticks there keep the existing price flash.
352
+
353
+ `useTween(frames, animate, { enter, exit })` is exported for a custom SVG mark. Pass `{ [key]: number[] }` and draw what it returns. While a removed key moves to `exit`, it stays in the returned frame. `useMarkMemory(current, drawn)` remembers how to draw each mark after its data is gone.
354
+
355
+ ## Stories
356
+
357
+ `ChartStory` steps a chart through states, such as slides with captions or a time slider. The chart stays mounted between steps, so `animate` carries each change. Combined with `sort` it makes a racing bar chart.
358
+
359
+ ```tsx
360
+ <ChartStory steps={years} controls="slider" loop interval={900} stepLabel={String}>
361
+ {(year) => (
362
+ <CategoryBarChart
363
+ orientation="horizontal"
364
+ sort="descending"
365
+ categories={ports}
366
+ series={[{ id: "teu", name: "TEU", data: byYear[year] }]}
367
+ animate={{ duration: 600 }}
368
+ />
369
+ )}
370
+ </ChartStory>
371
+
372
+ <ChartStory steps={slides} caption={(slide) => slide.text}>
373
+ {(slide) => <ScatterChart series={slide.series} />}
374
+ </ChartStory>
375
+ ```
376
+
377
+ | Behaviour | Detail |
378
+ | --- | --- |
379
+ | `controls` | `"dots"` (default) for a few slides, `"slider"` to scrub, `"none"` to drive it yourself. Prev, next and play/pause are always shown with dots or slider |
380
+ | `index`, `defaultIndex`, `onIndexChange` | Controlled or uncontrolled step |
381
+ | `autoplay`, `interval`, `loop` | Off by default. `interval` is 3000ms. Without `loop`, play stops on the last step, and pressing play there starts again from the first. Reduced motion never autoplays. Picking a dot or moving the slider pauses |
382
+ | `caption(step, index)` | Shown above the chart. It is `aria-live="polite"` while paused and silent while playing |
383
+ | `stepLabel(step, index)` | Names each dot, the slider value (`aria-valuetext`) and the position readout. Default `"Step n"` |
384
+ | Accessibility | The root is a `section` with `aria-roledescription="carousel"`. The stage is a group labelled `"<label>, n of N"` and described by the caption |
385
+ | Parts | `root`, `caption`, `stage`, `controls`, `play`, `prev`, `next`, `dots`, `dot` (`aria-current="step"`), `slider`, `position`. `classNames`, `styles` and `unstyled` work as on the charts |
386
+
387
+ `useSteps({ count, autoplay, interval, loop, … })` returns `{ index, playing, next, prev, goTo, play, pause, toggle, isFirst, isLast }` for your own controls, for example to drive scroll-linked steps from an `IntersectionObserver`.
388
+
389
+ ## Frame, data table and CSV
390
+
391
+ `ChartFrame` wraps any chart in a `figure` with a title, a subtitle, a source line and notes. Given `data`, it also adds a table view and a CSV download.
392
+
393
+ ```tsx
394
+ <ChartFrame
395
+ title="Container throughput by port"
396
+ subtitle="Thousand TEU, 2025"
397
+ summary="Sines handles twice the volume of any other port."
398
+ source="Port authority"
399
+ data={{ columns: ["Port", "TEU"], rows: [["Sines", 1800], ["Leixões", 700]] }}
400
+ filename="throughput.csv"
401
+ >
402
+ <CategoryBarChart … />
403
+ </ChartFrame>
404
+ ```
405
+
406
+ | Behaviour | Detail |
407
+ | --- | --- |
408
+ | Title | `h3` by default (`level` 2–6). It labels the `figure` |
409
+ | `subtitle`, `summary` | Both describe the `figure`. `summary` is visible to screen readers only |
410
+ | `source`, `note` | Footer lines. `source` is printed as `"Source: …"` |
411
+ | `data` | `{ columns, rows }`. "Show data" toggles a real `table` (first column is a row header). "Download CSV" saves `filename` (default `data.csv`) |
412
+ | Parts | `root`, `header`, `title`, `subtitle`, `summary`, `body`, `footer`, `source`, `note`, `actions`, `table-toggle`, `download`, `table` |
413
+
414
+ `toCsv(data)` writes RFC 4180 CSV with CRLF line ends. Text starting with `=`, `+`, `-` or `@` is prefixed with `'`, so a spreadsheet does not run it as a formula. `downloadCsv(data, filename)` saves the CSV with a UTF-8 BOM. `readableInk(background, candidates?)` returns the candidate with the highest WCAG contrast against `background` (default black or white), for labels drawn on marks. `contrastRatio(a, b)` gives the ratio itself.
415
+
318
416
  ## Development
319
417
 
320
418
  ```bash