@automattic/charts 4.2.0 → 4.4.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 (48) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/index.cjs +533 -127
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +27 -2
  5. package/dist/index.d.ts +27 -2
  6. package/dist/index.js +535 -129
  7. package/dist/index.js.map +1 -1
  8. package/package.json +1 -1
  9. package/src/charts/area-chart/area-chart.tsx +42 -10
  10. package/src/charts/area-chart/private/validate-data.ts +2 -3
  11. package/src/charts/area-chart/test/area-chart.test.tsx +72 -1
  12. package/src/charts/bar-chart/bar-chart.tsx +135 -96
  13. package/src/charts/bar-chart/index.ts +1 -1
  14. package/src/charts/bar-chart/private/band-highlight.tsx +3 -1
  15. package/src/charts/bar-chart/private/band-tooltip.tsx +1 -1
  16. package/src/charts/bar-chart/private/classified-bar-series.tsx +85 -0
  17. package/src/charts/bar-chart/private/comparison-bars-geometry.ts +3 -6
  18. package/src/charts/bar-chart/private/index.ts +2 -0
  19. package/src/charts/bar-chart/private/test/band-highlight.test.tsx +28 -2
  20. package/src/charts/bar-chart/private/test/comparison-bars-geometry.test.ts +2 -3
  21. package/src/charts/bar-chart/private/test/value-domain.test.ts +100 -0
  22. package/src/charts/bar-chart/private/use-bar-chart-options.ts +12 -39
  23. package/src/charts/bar-chart/private/value-domain.ts +90 -0
  24. package/src/charts/bar-chart/test/bar-chart.test.tsx +614 -10
  25. package/src/charts/bar-chart/types.ts +43 -0
  26. package/src/charts/line-chart/line-chart.tsx +148 -35
  27. package/src/charts/line-chart/private/index.ts +1 -0
  28. package/src/charts/line-chart/private/line-chart-glyph.tsx +17 -3
  29. package/src/charts/line-chart/private/nearest-pointer-events.tsx +155 -0
  30. package/src/charts/line-chart/test/line-chart.test.tsx +604 -9
  31. package/src/charts/line-chart/types.ts +2 -1
  32. package/src/charts/private/readings.ts +34 -0
  33. package/src/charts/private/test/readings.test.ts +29 -0
  34. package/src/charts/private/test/whole-number-ticks.test.tsx +57 -0
  35. package/src/charts/private/whole-number-ticks.tsx +79 -0
  36. package/src/components/legend/hooks/test/use-chart-legend-items.test.tsx +124 -0
  37. package/src/components/legend/hooks/use-chart-legend-items.ts +60 -2
  38. package/src/components/legend/private/base-legend.tsx +36 -21
  39. package/src/components/legend/test/legend.test.tsx +84 -0
  40. package/src/components/legend/types.ts +2 -0
  41. package/src/components/legend/utils/index.ts +0 -1
  42. package/src/components/tooltip/accessible-tooltip.tsx +1 -1
  43. package/src/hooks/test/use-chart-margin.test.tsx +35 -0
  44. package/src/hooks/use-chart-margin.tsx +7 -2
  45. package/src/providers/chart-context/global-charts-provider.tsx +1 -1
  46. package/src/providers/chart-context/test/chart-context.test.tsx +4 -4
  47. package/src/types.ts +8 -0
  48. package/src/components/legend/utils/label-transform-factory.ts +0 -24
package/dist/index.d.cts CHANGED
@@ -91,6 +91,7 @@ type ThemeAnnotationStyles = {
91
91
  type DataPoint = {
92
92
  label: string;
93
93
  value: number;
94
+ /** Per-point fill override for BarChart; other built-in charts ignore it. */
94
95
  color?: string;
95
96
  };
96
97
  /**
@@ -141,6 +142,7 @@ type DataPointDate = {
141
142
  dateString?: string;
142
143
  value: number | null;
143
144
  label?: string;
145
+ /** Per-point fill override for BarChart; other built-in charts ignore it. */
144
146
  color?: string;
145
147
  };
146
148
  type LeaderboardEntry = {
@@ -573,6 +575,12 @@ type SeriesChartLegendConfig = ChartLegendConfig<SeriesData[]> & {
573
575
  * uncollapsed one toggles only its own.
574
576
  */
575
577
  collapseGroups?: boolean;
578
+ /**
579
+ * Append a static, non-interactive item explaining the comparison overlay whenever a series
580
+ * has `options.type === 'comparison'`. Skipped when that series already has its own item.
581
+ * Pass a string to replace the default label.
582
+ */
583
+ comparisonItem?: boolean | string;
576
584
  };
577
585
  /**
578
586
  * Initial visibility options for charts built from labelled series.
@@ -738,6 +746,8 @@ type BaseLegendItem = {
738
746
  * the interactive legend to toggle a whole group's visibility from one item.
739
747
  */
740
748
  seriesLabels?: string[];
749
+ /** Set to false for a static item that cannot be toggled or dimmed. */
750
+ interactive?: boolean;
741
751
  };
742
752
  //#endregion
743
753
  //#region src/components/legend/legend.d.ts
@@ -760,6 +770,12 @@ interface ChartLegendOptions {
760
770
  * primary series. Off by default, so every series gets its own item.
761
771
  */
762
772
  collapseGroups?: boolean;
773
+ /**
774
+ * Append a static item explaining the comparison overlay whenever a series has
775
+ * `options.type === 'comparison'`. Skipped when that series already has its own
776
+ * item. Pass a string to replace the default label.
777
+ */
778
+ comparisonItem?: boolean | string;
763
779
  }
764
780
  /**
765
781
  * Hook to transform chart data into legend items
@@ -928,7 +944,8 @@ interface LineChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityP
928
944
  }
929
945
  type TooltipDatum = {
930
946
  key: string;
931
- value: number;
947
+ /** The series' reading, or null when the bucket has none. */
948
+ value: number | null;
932
949
  };
933
950
  //#endregion
934
951
  //#region src/charts/area-chart/types.d.ts
@@ -1031,9 +1048,11 @@ type AreaChartResponsiveComponent = React.ForwardRefExoticComponent<AreaChartBas
1031
1048
  declare const AreaChart: AreaChartComponent;
1032
1049
  declare const AreaChartResponsive: AreaChartResponsiveComponent;
1033
1050
  //#endregion
1034
- //#region src/charts/bar-chart/bar-chart.d.ts
1051
+ //#region src/charts/bar-chart/types.d.ts
1035
1052
  type BandHighlightSelection = {
1036
1053
  datum: DataPointDate;
1054
+ key: string;
1055
+ index: number;
1037
1056
  x: number;
1038
1057
  y: number;
1039
1058
  width: number;
@@ -1049,6 +1068,10 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1049
1068
  tooltipPlacement?: 'auto' | 'beside';
1050
1069
  /** Tooltip top anchor in SVG coordinates; negative offsets are supported. */
1051
1070
  tooltipAnchorTop?: number;
1071
+ /** Inline styles merged over the tooltip box defaults. */
1072
+ tooltipStyle?: CSSProperties;
1073
+ /** CSS class for each primary bar, in addition to visx-bar. */
1074
+ barClassName?: (datum: DataPointDate) => string | undefined;
1052
1075
  orientation?: 'horizontal' | 'vertical';
1053
1076
  withPatterns?: boolean;
1054
1077
  showZeroValues?: boolean;
@@ -1058,6 +1081,8 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1058
1081
  onBandHighlightChange?: (selection: BandHighlightSelection | null) => void;
1059
1082
  children?: ReactNode;
1060
1083
  }
1084
+ //#endregion
1085
+ //#region src/charts/bar-chart/bar-chart.d.ts
1061
1086
  type BarChartBaseProps = Optional<BarChartProps, 'width' | 'height' | 'size'>;
1062
1087
  interface BarChartSubComponents {
1063
1088
  Legend: ComponentType<React.ComponentProps<typeof Legend>>;
package/dist/index.d.ts CHANGED
@@ -91,6 +91,7 @@ type ThemeAnnotationStyles = {
91
91
  type DataPoint = {
92
92
  label: string;
93
93
  value: number;
94
+ /** Per-point fill override for BarChart; other built-in charts ignore it. */
94
95
  color?: string;
95
96
  };
96
97
  /**
@@ -141,6 +142,7 @@ type DataPointDate = {
141
142
  dateString?: string;
142
143
  value: number | null;
143
144
  label?: string;
145
+ /** Per-point fill override for BarChart; other built-in charts ignore it. */
144
146
  color?: string;
145
147
  };
146
148
  type LeaderboardEntry = {
@@ -573,6 +575,12 @@ type SeriesChartLegendConfig = ChartLegendConfig<SeriesData[]> & {
573
575
  * uncollapsed one toggles only its own.
574
576
  */
575
577
  collapseGroups?: boolean;
578
+ /**
579
+ * Append a static, non-interactive item explaining the comparison overlay whenever a series
580
+ * has `options.type === 'comparison'`. Skipped when that series already has its own item.
581
+ * Pass a string to replace the default label.
582
+ */
583
+ comparisonItem?: boolean | string;
576
584
  };
577
585
  /**
578
586
  * Initial visibility options for charts built from labelled series.
@@ -738,6 +746,8 @@ type BaseLegendItem = {
738
746
  * the interactive legend to toggle a whole group's visibility from one item.
739
747
  */
740
748
  seriesLabels?: string[];
749
+ /** Set to false for a static item that cannot be toggled or dimmed. */
750
+ interactive?: boolean;
741
751
  };
742
752
  //#endregion
743
753
  //#region src/components/legend/legend.d.ts
@@ -760,6 +770,12 @@ interface ChartLegendOptions {
760
770
  * primary series. Off by default, so every series gets its own item.
761
771
  */
762
772
  collapseGroups?: boolean;
773
+ /**
774
+ * Append a static item explaining the comparison overlay whenever a series has
775
+ * `options.type === 'comparison'`. Skipped when that series already has its own
776
+ * item. Pass a string to replace the default label.
777
+ */
778
+ comparisonItem?: boolean | string;
763
779
  }
764
780
  /**
765
781
  * Hook to transform chart data into legend items
@@ -928,7 +944,8 @@ interface LineChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityP
928
944
  }
929
945
  type TooltipDatum = {
930
946
  key: string;
931
- value: number;
947
+ /** The series' reading, or null when the bucket has none. */
948
+ value: number | null;
932
949
  };
933
950
  //#endregion
934
951
  //#region src/charts/area-chart/types.d.ts
@@ -1031,9 +1048,11 @@ type AreaChartResponsiveComponent = React.ForwardRefExoticComponent<AreaChartBas
1031
1048
  declare const AreaChart: AreaChartComponent;
1032
1049
  declare const AreaChartResponsive: AreaChartResponsiveComponent;
1033
1050
  //#endregion
1034
- //#region src/charts/bar-chart/bar-chart.d.ts
1051
+ //#region src/charts/bar-chart/types.d.ts
1035
1052
  type BandHighlightSelection = {
1036
1053
  datum: DataPointDate;
1054
+ key: string;
1055
+ index: number;
1037
1056
  x: number;
1038
1057
  y: number;
1039
1058
  width: number;
@@ -1049,6 +1068,10 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1049
1068
  tooltipPlacement?: 'auto' | 'beside';
1050
1069
  /** Tooltip top anchor in SVG coordinates; negative offsets are supported. */
1051
1070
  tooltipAnchorTop?: number;
1071
+ /** Inline styles merged over the tooltip box defaults. */
1072
+ tooltipStyle?: CSSProperties;
1073
+ /** CSS class for each primary bar, in addition to visx-bar. */
1074
+ barClassName?: (datum: DataPointDate) => string | undefined;
1052
1075
  orientation?: 'horizontal' | 'vertical';
1053
1076
  withPatterns?: boolean;
1054
1077
  showZeroValues?: boolean;
@@ -1058,6 +1081,8 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1058
1081
  onBandHighlightChange?: (selection: BandHighlightSelection | null) => void;
1059
1082
  children?: ReactNode;
1060
1083
  }
1084
+ //#endregion
1085
+ //#region src/charts/bar-chart/bar-chart.d.ts
1061
1086
  type BarChartBaseProps = Optional<BarChartProps, 'width' | 'height' | 'size'>;
1062
1087
  interface BarChartSubComponents {
1063
1088
  Legend: ComponentType<React.ComponentProps<typeof Legend>>;