p1-charts 0.1.0 → 0.2.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 (51) hide show
  1. package/README.md +15 -1
  2. package/dist/CategoryBarChart-YEJ9VB8h.js +453 -0
  3. package/dist/CategoryBarChart-YEJ9VB8h.js.map +1 -0
  4. package/dist/{PolarChart-D_ULWctY.js → PolarChart-BUEhn3CI.js} +135 -83
  5. package/dist/PolarChart-BUEhn3CI.js.map +1 -0
  6. package/dist/{TimeSeriesChart-tYmzRNzV.js → TimeSeriesChart-BIwJxgnD.js} +483 -418
  7. package/dist/TimeSeriesChart-BIwJxgnD.js.map +1 -0
  8. package/dist/category.d.ts +2 -2
  9. package/dist/category.d.ts.map +1 -1
  10. package/dist/category.js +1 -1
  11. package/dist/charts/CategoryBarChart/index.d.ts +1 -1
  12. package/dist/charts/CategoryBarChart/index.d.ts.map +1 -1
  13. package/dist/charts/PolarChart/index.d.ts.map +1 -1
  14. package/dist/charts/ScatterChart/index.d.ts +1 -1
  15. package/dist/charts/ScatterChart/index.d.ts.map +1 -1
  16. package/dist/charts/TimeSeriesChart/index.d.ts +1 -1
  17. package/dist/charts/TimeSeriesChart/index.d.ts.map +1 -1
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +3 -3
  21. package/dist/scatter.js +1 -1
  22. package/dist/series.d.ts +1 -1
  23. package/dist/series.d.ts.map +1 -1
  24. package/dist/series.js +1 -1
  25. package/dist/types/category.d.ts +41 -2
  26. package/dist/types/category.d.ts.map +1 -1
  27. package/dist/types/polar.d.ts +7 -1
  28. package/dist/types/polar.d.ts.map +1 -1
  29. package/dist/types/scatter.d.ts +13 -0
  30. package/dist/types/scatter.d.ts.map +1 -1
  31. package/dist/types/timeSeries.d.ts +31 -0
  32. package/dist/types/timeSeries.d.ts.map +1 -1
  33. package/dist/utils/category.utils.d.ts +46 -7
  34. package/dist/utils/category.utils.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/category.ts +13 -2
  37. package/src/charts/CategoryBarChart/index.tsx +301 -50
  38. package/src/charts/PolarChart/index.tsx +63 -18
  39. package/src/charts/ScatterChart/index.tsx +51 -7
  40. package/src/charts/TimeSeriesChart/index.tsx +117 -2
  41. package/src/index.ts +13 -1
  42. package/src/series.ts +4 -0
  43. package/src/types/category.ts +47 -2
  44. package/src/types/polar.ts +11 -1
  45. package/src/types/scatter.ts +13 -0
  46. package/src/types/timeSeries.ts +35 -0
  47. package/src/utils/category.utils.ts +321 -43
  48. package/dist/CategoryBarChart-CV58kvIr.js +0 -182
  49. package/dist/CategoryBarChart-CV58kvIr.js.map +0 -1
  50. package/dist/PolarChart-D_ULWctY.js.map +0 -1
  51. package/dist/TimeSeriesChart-tYmzRNzV.js.map +0 -1
@@ -13,6 +13,7 @@ import {
13
13
  LineStyle,
14
14
  createChart,
15
15
  createSeriesMarkers,
16
+ type AutoscaleInfoProvider,
16
17
  type IChartApi,
17
18
  type ISeriesApi,
18
19
  type LineWidth,
@@ -36,6 +37,7 @@ import { TOOLTIP } from "../../styles/shared.js";
36
37
  import { useChartTheme } from "../../hooks/useChartTheme.js";
37
38
  import { useWheelZoom } from "../../hooks/useWheelZoom.js";
38
39
  import { RibbonPrimitive } from "../../primitives/RibbonPrimitive.js";
40
+ import { TimeRangePrimitive } from "../../primitives/TimeRangePrimitive.js";
39
41
  import { patchSquareCanvasLabels } from "../../utils/canvas.utils.js";
40
42
  import { withAlpha } from "../../utils/color.utils.js";
41
43
  import { deepMerge } from "../../utils/merge.utils.js";
@@ -65,6 +67,8 @@ const DEFINITION = {
65
67
 
66
68
  const NO_CATEGORIES: readonly string[] = [];
67
69
  const NO_RIBBONS: NonNullable<TimeSeriesChartProps["ribbons"]> = [];
70
+ const NO_RANGES: NonNullable<TimeSeriesChartProps["ranges"]> = [];
71
+ const NO_GUIDES: NonNullable<TimeSeriesChartProps["guides"]> = [];
68
72
  const NO_MARKERS: NonNullable<TimeSeriesChartProps["markers"]> = [];
69
73
 
70
74
  const TIP: CSSProperties = {
@@ -99,7 +103,10 @@ function seriesColor(
99
103
  export function TimeSeriesChart({
100
104
  series,
101
105
  ribbons = NO_RIBBONS,
106
+ ranges = NO_RANGES,
107
+ guides = NO_GUIDES,
102
108
  markers = NO_MARKERS,
109
+ onPointClick,
103
110
  height = 340,
104
111
  className,
105
112
  ariaLabel = "Time series chart",
@@ -119,8 +126,20 @@ export function TimeSeriesChart({
119
126
  const resolved = useChartTheme({ theme, unstyled });
120
127
  const [chart, setChart] = useState<IChartApi | null>(null);
121
128
  const [tip, setTip] = useState<TipState | null>(null);
122
- const callbacks = useRef({ formatTime, formatValue, extraTooltipRows, renderTooltip });
123
- callbacks.current = { formatTime, formatValue, extraTooltipRows, renderTooltip };
129
+ const callbacks = useRef({
130
+ formatTime,
131
+ formatValue,
132
+ extraTooltipRows,
133
+ renderTooltip,
134
+ onPointClick,
135
+ });
136
+ callbacks.current = {
137
+ formatTime,
138
+ formatValue,
139
+ extraTooltipRows,
140
+ renderTooltip,
141
+ onPointClick,
142
+ };
124
143
  const editorial = profile === "editorial";
125
144
  const labels = axis === "ordinal" ? (categories ?? NO_CATEGORIES) : null;
126
145
  const look = resolved ?? defaultTheme;
@@ -252,6 +271,83 @@ export function TimeSeriesChart({
252
271
  );
253
272
  }
254
273
 
274
+ const rangeHost = painted[0]?.apiSeries;
275
+
276
+ if (rangeHost && ranges.length > 0) {
277
+ rangeHost.attachPrimitive(
278
+ new TimeRangePrimitive({
279
+ items: ranges.map((range) => ({
280
+ from: range.from,
281
+ to: range.to,
282
+ key: range.id,
283
+ label: range.label,
284
+ color: range.fill,
285
+ })),
286
+ style: (key) => {
287
+ const range = ranges.find((item) => item.id === key);
288
+ const hatch = range?.hatch;
289
+
290
+ return {
291
+ fill: range?.fill,
292
+ hatch:
293
+ hatch === false
294
+ ? false
295
+ : {
296
+ color: hatch ?? range?.fill ?? palette.down,
297
+ spacing: 8,
298
+ width: 1,
299
+ opacity: 0.45,
300
+ },
301
+ };
302
+ },
303
+ }),
304
+ );
305
+ }
306
+
307
+ const guideValues = new Map<ISeriesApi<SeriesType>, number[]>();
308
+
309
+ for (const guide of guides) {
310
+ const host = hosts.get(guide.scale ?? "right") ?? painted[0]?.apiSeries;
311
+
312
+ if (!host) continue;
313
+
314
+ host.createPriceLine({
315
+ price: guide.value,
316
+ color: guide.color ?? palette.down,
317
+ lineWidth: 1,
318
+ lineStyle: LineStyle.Dashed,
319
+ axisLabelVisible: Boolean(guide.label),
320
+ title: guide.label ?? "",
321
+ });
322
+
323
+ if (!Number.isFinite(guide.value)) continue;
324
+
325
+ const values = guideValues.get(host) ?? [];
326
+
327
+ values.push(guide.value);
328
+ guideValues.set(host, values);
329
+ }
330
+
331
+ for (const [host, values] of guideValues) {
332
+ host.applyOptions({
333
+ autoscaleInfoProvider: ((original) => {
334
+ const info = original();
335
+
336
+ if (!info?.priceRange) return info;
337
+
338
+ return {
339
+ ...info,
340
+ priceRange: {
341
+ minValue: Math.min(info.priceRange.minValue, ...values),
342
+ maxValue: Math.max(info.priceRange.maxValue, ...values),
343
+ },
344
+ };
345
+ }) satisfies AutoscaleInfoProvider,
346
+ });
347
+ }
348
+
349
+ api.timeScale().fitContent();
350
+
255
351
  const onMove = (param: { time?: Time; point?: { x: number; y: number } }) => {
256
352
  const unix = typeof param.time === "number" ? param.time : null;
257
353
  const point = param.point;
@@ -291,18 +387,37 @@ export function TimeSeriesChart({
291
387
  });
292
388
  };
293
389
 
390
+ const onClick = (param: { time?: Time }) => {
391
+ const handler = callbacks.current.onPointClick;
392
+ const unix = typeof param.time === "number" ? param.time : null;
393
+
394
+ if (!handler || unix === null) return;
395
+
396
+ const points = painted.flatMap(({ spec, rows: data }) => {
397
+ const hit = data.find((item) => item.time === unix);
398
+
399
+ return hit ? [{ seriesId: spec.id, value: hit.value }] : [];
400
+ });
401
+
402
+ if (points.length > 0) handler({ time: unix, points });
403
+ };
404
+
294
405
  api.subscribeCrosshairMove(onMove);
406
+ api.subscribeClick(onClick);
295
407
  setChart(api);
296
408
 
297
409
  return () => {
298
410
  setChart(null);
299
411
  setTip(null);
300
412
  api.unsubscribeCrosshairMove(onMove);
413
+ api.unsubscribeClick(onClick);
301
414
  api.remove();
302
415
  };
303
416
  }, [
304
417
  series,
305
418
  ribbons,
419
+ ranges,
420
+ guides,
306
421
  markers,
307
422
  resolved,
308
423
  height,
package/src/index.ts CHANGED
@@ -211,16 +211,28 @@ export type {
211
211
  TimeSeriesKind,
212
212
  TimeSeriesMarkerSpec,
213
213
  TimeSeriesPoint,
214
+ TimeSeriesClick,
215
+ TimeSeriesGuide,
216
+ TimeSeriesPointHit,
214
217
  TimeSeriesProfile,
218
+ TimeSeriesRange,
215
219
  TimeSeriesRibbon,
216
220
  TimeSeriesSpec,
217
221
  TimeSeriesTooltipRow,
218
222
  } from "./types/timeSeries.js";
219
223
  export type {
224
+ CategoryBand,
220
225
  CategoryBarChartProps,
226
+ CategoryBarDatum,
221
227
  CategoryBarSeries,
228
+ CategoryReference,
222
229
  } from "./types/category.js";
223
- export type { CategoryBarMode } from "./utils/category.utils.js";
230
+ export type {
231
+ CategoryBarMode,
232
+ CategoryDomain,
233
+ CategoryOrientation,
234
+ CategoryScale,
235
+ } from "./utils/category.utils.js";
224
236
  export type { Bin, BinOptions } from "./utils/bin.utils.js";
225
237
  export type { ScatterChartProps, ScatterSeries } from "./types/scatter.js";
226
238
  export type { PolarChartProps } from "./types/polar.js";
package/src/series.ts CHANGED
@@ -15,7 +15,11 @@ export type {
15
15
  TimeSeriesKind,
16
16
  TimeSeriesMarkerSpec,
17
17
  TimeSeriesPoint,
18
+ TimeSeriesClick,
19
+ TimeSeriesGuide,
20
+ TimeSeriesPointHit,
18
21
  TimeSeriesProfile,
22
+ TimeSeriesRange,
19
23
  TimeSeriesRibbon,
20
24
  TimeSeriesScale,
21
25
  TimeSeriesSpec,
@@ -1,12 +1,24 @@
1
1
  import type { ReactNode } from "react";
2
- import type { CategoryBarMode } from "../utils/category.utils.js";
2
+ import type {
3
+ CategoryBarMode,
4
+ CategoryDomain,
5
+ CategoryOrientation,
6
+ CategoryScale,
7
+ } from "../utils/category.utils.js";
3
8
  import type { ThemeProps } from "./theme.js";
4
9
 
10
+ /** A bar. A number is the value. An object can colour and label that one bar. */
11
+ export type CategoryBarDatum =
12
+ | number
13
+ | { value: number; color?: string; label?: string };
14
+
5
15
  export interface CategoryBarSeries {
6
16
  id: string;
7
17
  name: string;
8
- data: readonly number[];
18
+ data: readonly CategoryBarDatum[];
9
19
  color?: string;
20
+ /** Second value axis. Defaults to the left (bottom, on a horizontal chart). */
21
+ scale?: CategoryScale;
10
22
  }
11
23
 
12
24
  export interface CategoryTooltipRow {
@@ -15,17 +27,50 @@ export interface CategoryTooltipRow {
15
27
  color?: string;
16
28
  }
17
29
 
30
+ export interface CategoryReference {
31
+ value: number;
32
+ label?: string;
33
+ color?: string;
34
+ scale?: CategoryScale;
35
+ }
36
+
37
+ /** A tint between two values, such as a low-pressure band. */
38
+ export interface CategoryBand {
39
+ from: number;
40
+ to: number;
41
+ color?: string;
42
+ scale?: CategoryScale;
43
+ }
44
+
18
45
  export interface CategoryBarChartProps extends ThemeProps {
19
46
  categories: readonly string[];
20
47
  series: readonly CategoryBarSeries[];
21
48
  mode?: CategoryBarMode;
49
+ /** Columns are vertical. A gantt or a ranked bar is horizontal. */
50
+ orientation?: CategoryOrientation;
51
+ /** Fixed extent for the left (or only) value axis. */
52
+ domain?: CategoryDomain;
53
+ /** Fixed extent for series with `scale: "right"`. */
54
+ rightDomain?: CategoryDomain;
55
+ /** Dashed line across the plot, such as a baseline at 100. */
56
+ reference?: CategoryReference;
57
+ /** Further dashed lines. `reference` is drawn with these. */
58
+ references?: readonly CategoryReference[];
59
+ /** Tinted spans between two values, drawn behind the bars. */
60
+ bands?: readonly CategoryBand[];
61
+ /** Category to mark when the pointer is elsewhere. */
62
+ activeIndex?: number;
63
+ /** Caption on the value axis. */
64
+ valueLabel?: string;
22
65
  width?: number;
23
66
  height?: number;
24
67
  className?: string;
25
68
  ariaLabel?: string;
26
69
  formatValue?: (seriesId: string, value: number) => string;
70
+ formatTick?: (value: number) => string;
27
71
  renderTooltip?: (
28
72
  category: string,
29
73
  rows: readonly CategoryTooltipRow[],
30
74
  ) => ReactNode;
75
+ onSelect?: (category: string, index: number) => void;
31
76
  }
@@ -37,4 +37,14 @@ export interface PolarScatterChartProps extends PolarCommon {
37
37
  series: readonly PolarScatterSeries[];
38
38
  }
39
39
 
40
- export type PolarChartProps = PolarBarChartProps | PolarScatterChartProps;
40
+ /** A closed polygon per series. Categories are the spokes. A radar chart. */
41
+ export interface PolarLineChartProps extends PolarCommon {
42
+ mode: "line";
43
+ categories: readonly string[];
44
+ series: readonly PolarBarSeries[];
45
+ }
46
+
47
+ export type PolarChartProps =
48
+ | PolarBarChartProps
49
+ | PolarScatterChartProps
50
+ | PolarLineChartProps;
@@ -4,6 +4,9 @@ import type { ThemeProps } from "./theme.js";
4
4
  export interface ScatterPoint {
5
5
  x: number;
6
6
  y: number;
7
+ color?: string;
8
+ /** Drawn radius. The point under the pointer is still larger. */
9
+ radius?: number;
7
10
  }
8
11
 
9
12
  export interface ScatterSeries {
@@ -11,6 +14,11 @@ export interface ScatterSeries {
11
14
  name: string;
12
15
  data: readonly ScatterPoint[];
13
16
  color?: string;
17
+ /** Connect this series in data order. A Pareto front uses this with `markers: false`. */
18
+ line?: boolean;
19
+ /** Default true. False draws only the line. */
20
+ markers?: boolean;
21
+ lineStyle?: "solid" | "dashed" | "dotted";
14
22
  }
15
23
 
16
24
  export interface ScatterChartProps extends ThemeProps {
@@ -21,6 +29,11 @@ export interface ScatterChartProps extends ThemeProps {
21
29
  ariaLabel?: string;
22
30
  xLabel?: string;
23
31
  yLabel?: string;
32
+ onSelect?: (point: {
33
+ series: ScatterSeries;
34
+ point: ScatterPoint;
35
+ index: number;
36
+ }) => void;
24
37
  renderTooltip?: (point: {
25
38
  series: ScatterSeries;
26
39
  point: ScatterPoint;
@@ -37,6 +37,36 @@ export interface TimeSeriesMarkerSpec {
37
37
  position?: "aboveBar" | "belowBar" | "inBar";
38
38
  }
39
39
 
40
+ /** A full-height band over a time span, such as a hatched forecast region. */
41
+ export interface TimeSeriesRange {
42
+ id: string;
43
+ from: Time;
44
+ to: Time;
45
+ fill?: string;
46
+ /** Hatch colour. False draws the fill only. */
47
+ hatch?: string | false;
48
+ label?: string;
49
+ }
50
+
51
+ export interface TimeSeriesPointHit {
52
+ seriesId: string;
53
+ value: number;
54
+ }
55
+
56
+ export interface TimeSeriesClick {
57
+ time: number;
58
+ points: readonly TimeSeriesPointHit[];
59
+ }
60
+
61
+ /** A dashed horizontal guide, such as a Beaufort threshold. */
62
+ export interface TimeSeriesGuide {
63
+ id: string;
64
+ value: number;
65
+ label?: string;
66
+ color?: string;
67
+ scale?: TimeSeriesScale;
68
+ }
69
+
40
70
  /** A fill between two lines, such as P10–P90. Not a full-height regime band. */
41
71
  export interface TimeSeriesRibbon {
42
72
  id?: string;
@@ -58,7 +88,12 @@ export interface TimeSeriesTooltipRow {
58
88
  export interface TimeSeriesChartProps extends ThemeProps {
59
89
  series: readonly TimeSeriesSpec[];
60
90
  ribbons?: readonly TimeSeriesRibbon[];
91
+ /** Full-height bands. A hatch marks a forecast that is not yet observed. */
92
+ ranges?: readonly TimeSeriesRange[];
93
+ /** Dashed value guides. Drawn on the series that shares their scale. */
94
+ guides?: readonly TimeSeriesGuide[];
61
95
  markers?: readonly TimeSeriesMarkerSpec[];
96
+ onPointClick?: (event: TimeSeriesClick) => void;
62
97
  height?: number;
63
98
  className?: string;
64
99
  ariaLabel?: string;