@automattic/charts 1.10.3 → 1.12.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 (49) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/TOKENS.md +83 -0
  3. package/dist/index.cjs +894 -139
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.css +58 -62
  6. package/dist/index.d.cts +75 -13
  7. package/dist/index.d.ts +75 -13
  8. package/dist/index.js +894 -139
  9. package/dist/index.js.map +1 -1
  10. package/package.json +2 -1
  11. package/src/charts/area-chart/area-chart.tsx +28 -8
  12. package/src/charts/area-chart/test/area-chart.test.tsx +102 -0
  13. package/src/charts/area-chart/types.ts +20 -6
  14. package/src/charts/bar-chart/bar-chart.tsx +33 -9
  15. package/src/charts/bar-chart/test/bar-chart.test.tsx +117 -0
  16. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +7 -7
  17. package/src/charts/heatmap-chart/heatmap-chart.module.scss +18 -13
  18. package/src/charts/heatmap-chart/heatmap-chart.tsx +11 -7
  19. package/src/charts/heatmap-chart/private/heatmap-legend.tsx +3 -3
  20. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +13 -7
  21. package/src/charts/leaderboard-chart/hooks/index.ts +1 -0
  22. package/src/charts/leaderboard-chart/hooks/use-fitted-row-count.ts +115 -0
  23. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +39 -12
  24. package/src/charts/leaderboard-chart/leaderboard-chart.tsx +67 -14
  25. package/src/charts/leaderboard-chart/test/leaderboard-chart-fit-rows.test.tsx +365 -0
  26. package/src/charts/leaderboard-chart/test/leaderboard-chart.test.tsx +73 -2
  27. package/src/charts/leaderboard-chart/types.ts +12 -0
  28. package/src/charts/line-chart/line-chart.module.scss +2 -2
  29. package/src/charts/line-chart/line-chart.tsx +40 -5
  30. package/src/charts/line-chart/test/line-chart.test.tsx +190 -1
  31. package/src/charts/line-chart/types.ts +14 -0
  32. package/src/charts/private/grid-control/grid-control.module.scss +1 -1
  33. package/src/charts/private/svg-empty-state/svg-empty-state.module.scss +1 -1
  34. package/src/charts/private/x-zoom/index.ts +1 -0
  35. package/src/charts/private/{test → x-zoom/test}/x-zoom.test.tsx +46 -3
  36. package/src/charts/private/x-zoom/x-zoom.module.scss +43 -0
  37. package/src/charts/private/{x-zoom.tsx → x-zoom/x-zoom.tsx} +52 -24
  38. package/src/components/legend/hooks/test/use-chart-legend-items.test.tsx +152 -0
  39. package/src/components/legend/hooks/use-chart-legend-items.ts +118 -16
  40. package/src/components/legend/private/base-legend.tsx +21 -9
  41. package/src/components/legend/test/legend-config-types.test.tsx +32 -0
  42. package/src/components/legend/test/legend.test.tsx +121 -1
  43. package/src/components/legend/types.ts +6 -0
  44. package/src/components/tooltip/base-tooltip.module.scss +3 -2
  45. package/src/components/trend-indicator/trend-indicator.module.scss +6 -5
  46. package/src/index.ts +1 -0
  47. package/src/providers/chart-context/themes.ts +33 -19
  48. package/src/types.ts +18 -2
  49. package/src/charts/private/x-zoom.module.scss +0 -48
package/dist/index.d.ts CHANGED
@@ -152,8 +152,8 @@ type LeaderboardEntry = {
152
152
  */
153
153
  previousShare?: number;
154
154
  /**
155
- * Delta of the entry. Omit when this row has no matching comparison-period
156
- * value.
155
+ * Delta of the entry. Omit when the percentage change is unavailable, such
156
+ * as when the row has no comparison value or its previous value is zero.
157
157
  */
158
158
  delta?: number;
159
159
  /**
@@ -481,6 +481,21 @@ type ChartLegendConfig<T = DataPoint | DataPointDate | LeaderboardEntry> = {
481
481
  */
482
482
  shapeStyles?: LegendShapeStyles;
483
483
  };
484
+ /**
485
+ * Legend config for charts built from `SeriesData` (line, bar, area). Adds `collapseGroups` on top
486
+ * of the shared config. It is intentionally absent from the base `ChartLegendConfig` so point-based
487
+ * charts (pie, semi-circle pie) — whose data points carry `group` only to coordinate colours — can't
488
+ * set it.
489
+ */
490
+ type SeriesChartLegendConfig = ChartLegendConfig<SeriesData[]> & {
491
+ /**
492
+ * Collapse series that share a `group` into a single legend item, labelled by the group's
493
+ * primary series (its first non-comparison member). Off by default, so every series keeps its
494
+ * own item. Combines with `interactive`: a collapsed item toggles every series in its group, an
495
+ * uncollapsed one toggles only its own.
496
+ */
497
+ collapseGroups?: boolean;
498
+ };
484
499
  /**
485
500
  * Base properties shared across all chart components
486
501
  */
@@ -644,6 +659,12 @@ type BaseLegendItem = {
644
659
  glyphSize?: number;
645
660
  renderGlyph?: <Datum extends object>(props: GlyphProps<Datum>) => ReactNode;
646
661
  shapeStyle?: CSSProperties & LineStyles$1;
662
+ /**
663
+ * The series labels this legend item controls. For a grouped item this is every series
664
+ * sharing the group (primary first); for a single series it is just its own label. Used by
665
+ * the interactive legend to toggle a whole group's visibility from one item.
666
+ */
667
+ seriesLabels?: string[];
647
668
  };
648
669
  //#endregion
649
670
  //#region src/components/legend/legend.d.ts
@@ -661,6 +682,11 @@ interface ChartLegendOptions {
661
682
  showValues?: boolean;
662
683
  legendValueDisplay?: LegendValueDisplay;
663
684
  legendShape?: LegendShape<SeriesData[], number>;
685
+ /**
686
+ * Collapse series that share a `group` into a single legend item, labelled by the group's
687
+ * primary series. Off by default, so every series gets its own item.
688
+ */
689
+ collapseGroups?: boolean;
664
690
  }
665
691
  /**
666
692
  * Hook to transform chart data into legend items
@@ -736,6 +762,10 @@ type RenderLineGlyphProps<Datum extends object> = GlyphProps<Datum> & {
736
762
  position?: 'start' | 'end';
737
763
  };
738
764
  interface LineChartProps extends BaseChartProps<SeriesData[]> {
765
+ /**
766
+ * Legend configuration. Supports `collapseGroups` on top of the shared options.
767
+ */
768
+ legend?: SeriesChartLegendConfig;
739
769
  withGradientFill: boolean;
740
770
  smoothing?: boolean;
741
771
  curveType?: CurveType;
@@ -755,6 +785,15 @@ interface LineChartProps extends BaseChartProps<SeriesData[]> {
755
785
  * button appears in the top-right of the chart while zoomed.
756
786
  */
757
787
  zoomable?: boolean;
788
+ /**
789
+ * Whether the Y axis rescales to fit only the visible series when series are
790
+ * hidden or shown through the interactive legend.
791
+ * Defaults to `true` (the pre-existing behaviour). Set to `false` to pin the Y
792
+ * axis to the full data extent so hiding series does not move the chart's
793
+ * baseline — useful for comparison charts. Matches `AreaChart`.
794
+ * @default true
795
+ */
796
+ rescaleYOnVisibilityChange?: boolean;
758
797
  children?: ReactNode;
759
798
  }
760
799
  type TooltipDatum = {
@@ -764,6 +803,10 @@ type TooltipDatum = {
764
803
  //#endregion
765
804
  //#region src/charts/area-chart/types.d.ts
766
805
  interface AreaChartProps extends BaseChartProps<SeriesData[]> {
806
+ /**
807
+ * Legend configuration. Supports `collapseGroups` on top of the shared options.
808
+ */
809
+ legend?: SeriesChartLegendConfig;
767
810
  /**
768
811
  * Whether series should be stacked on top of each other.
769
812
  * When false, series are rendered as overlapping filled areas.
@@ -814,13 +857,18 @@ interface AreaChartProps extends BaseChartProps<SeriesData[]> {
814
857
  */
815
858
  zoomable?: boolean;
816
859
  /**
817
- * When using an interactive legend, controls whether the Y axis rescales
818
- * to fit only the visible series. Defaults to `true`, matching the
819
- * intuitive default for LineChart and BarChart. Set to `false` to pin
820
- * the Y axis to the full data extent so toggling legend items off does
821
- * not move the chart's baseline.
860
+ * Whether the Y axis rescales to fit only the visible series when series are
861
+ * hidden or shown through the interactive legend.
862
+ * Defaults to `true`, matching LineChart. Set to `false` to pin the Y axis to
863
+ * the full data extent so hiding series does not move the chart's baseline.
822
864
  * @default true
823
865
  */
866
+ rescaleYOnVisibilityChange?: boolean;
867
+ /**
868
+ * @deprecated Use `rescaleYOnVisibilityChange`. The behaviour keys off series
869
+ * visibility changing, not specifically a legend toggle. Still honoured when
870
+ * `rescaleYOnVisibilityChange` is not set.
871
+ */
824
872
  rescaleYOnLegendToggle?: boolean;
825
873
  children?: ReactNode;
826
874
  }
@@ -859,6 +907,10 @@ declare const AreaChartResponsive: AreaChartResponsiveComponent;
859
907
  //#endregion
860
908
  //#region src/charts/bar-chart/bar-chart.d.ts
861
909
  interface BarChartProps extends BaseChartProps<SeriesData[]> {
910
+ /**
911
+ * Legend configuration. Supports `collapseGroups` on top of the shared options.
912
+ */
913
+ legend?: SeriesChartLegendConfig;
862
914
  renderTooltip?: (params: RenderTooltipParams<DataPointDate>) => ReactNode;
863
915
  orientation?: 'horizontal' | 'vertical';
864
916
  withPatterns?: boolean;
@@ -946,7 +998,7 @@ declare const BarListChartResponsive: ({
946
998
  width,
947
999
  height,
948
1000
  ...chartProps
949
- }: Omit<BarListChartProps, "width" | "height" | "size"> & {
1001
+ }: Omit<BarListChartProps, "height" | "size" | "width"> & {
950
1002
  width?: number;
951
1003
  height?: number;
952
1004
  size?: number;
@@ -1100,7 +1152,7 @@ declare const GeoChartResponsive: ({
1100
1152
  width,
1101
1153
  height,
1102
1154
  ...chartProps
1103
- }: Omit<GeoChartProps, "width" | "height" | "size"> & {
1155
+ }: Omit<GeoChartProps, "height" | "size" | "width"> & {
1104
1156
  width?: number;
1105
1157
  height?: number;
1106
1158
  size?: number;
@@ -1231,10 +1283,20 @@ interface LeaderboardChartProps extends Pick<BaseChartProps<LeaderboardEntry>, '
1231
1283
  * Whether the chart is in loading state
1232
1284
  */
1233
1285
  loading?: boolean;
1286
+ /**
1287
+ * Show only complete rows that fit the chart height instead of scrolling.
1288
+ *
1289
+ * Requires a container that constrains the chart's height; otherwise every
1290
+ * row fits. Refits are debounced with the parent resize on the responsive
1291
+ * export, so `LeaderboardChartUnresponsive` tracks a live drag more closely.
1292
+ * @default false
1293
+ */
1294
+ fitRows?: boolean;
1234
1295
  /**
1235
1296
  * Custom styling for the chart container
1236
1297
  */
1237
1298
  style?: React.CSSProperties & {
1299
+ '--a8c-charts-border-radius-leaderboard-bar'?: string; /** @deprecated Use `--a8c-charts-border-radius-leaderboard-bar`. */
1238
1300
  '--a8c--charts--leaderboard--bar--border-radius'?: string;
1239
1301
  };
1240
1302
  /**
@@ -1271,7 +1333,7 @@ declare const LeaderboardChartResponsive: (({
1271
1333
  width,
1272
1334
  height,
1273
1335
  ...chartProps
1274
- }: Omit<LeaderboardChartProps, "width" | "height" | "size"> & {
1336
+ }: Omit<LeaderboardChartProps, "height" | "size" | "width"> & {
1275
1337
  width?: number;
1276
1338
  height?: number;
1277
1339
  size?: number;
@@ -1804,7 +1866,7 @@ declare const Sparkline: ({
1804
1866
  width,
1805
1867
  height,
1806
1868
  ...chartProps
1807
- }: Omit<SparklineProps, "width" | "height" | "size"> & {
1869
+ }: Omit<SparklineProps, "height" | "size" | "width"> & {
1808
1870
  width?: number;
1809
1871
  height?: number;
1810
1872
  size?: number;
@@ -1852,7 +1914,7 @@ declare const BaseTooltip: ({
1852
1914
  className,
1853
1915
  style,
1854
1916
  renderContainer
1855
- }: BaseTooltipProps) => string | number | true | Iterable<ReactNode> | import("react/jsx-runtime").JSX.Element;
1917
+ }: BaseTooltipProps) => string | number | true | import("react/jsx-runtime").JSX.Element | Iterable<ReactNode>;
1856
1918
  //#endregion
1857
1919
  //#region src/components/tooltip/accessible-tooltip.d.ts
1858
1920
  type FlattenedTooltipData = {
@@ -1998,5 +2060,5 @@ declare const useGlobalChartsTheme: () => CompleteChartTheme;
1998
2060
  */
1999
2061
  declare const defaultTheme: CompleteChartTheme;
2000
2062
  //#endregion
2001
- export { AccessibleTooltip, type AnnotationStyles, type ArcData, AreaChartResponsive as AreaChart, type AreaChartProps, AreaChart as AreaChartUnresponsive, type AxisOptions, BarChartResponsive as BarChart, type BarChartProps, BarChart as BarChartUnresponsive, BarListChartResponsive as BarListChart, type BarListChartProps, BarListChart as BarListChartUnresponsive, type BaseChartProps, type BaseLegendItem, type BaseLegendProps, BaseTooltip, type BaseTooltipProps, type CalendarHeatmapResult, type ChartLegendConfig, type ChartLegendOptions, type ChartTheme, type CompleteChartTheme, ConversionFunnelChartWithProvider as ConversionFunnelChart, type ConversionFunnelChartProps, type CurveType, type DataPoint, type DataPointDate, type DataPointPercentage, type EventHandlerParams, type FunnelStep, GeoChartResponsive as GeoChart, type GeoChartError, type GeoChartProps, GeoChartWithProvider as GeoChartUnresponsive, type GeoData, type GeoRegion, type GeoResolution, GlobalChartsContext, GlobalChartsProvider, GlobalChartsProvider as ThemeProvider, type GoogleDataTableColumn, type GoogleDataTableColumnRoleType, type GoogleDataTableRow, type GradientConfig, type GradientStop, type GridProps, type GridStyles, type HeatmapCell, HeatmapChartResponsive as HeatmapChart, type HeatmapChartProps, HeatmapChart as HeatmapChartUnresponsive, type HeatmapColumn, type HeatmapTooltipData, LeaderboardChartResponsive as LeaderboardChart, type LeaderboardChartProps, LeaderboardChart as LeaderboardChartUnresponsive, type LeaderboardEntry, Legend, type LegendItemStyles, type LegendLabelStyles, type LegendPosition, type LegendProps, type LegendShape, type LegendShapeLabel, type LegendShapeRenderProps, type LegendShapeStyles, type LegendValueDisplay, LineChartResponsive as LineChart, type LineChartAnnotationProps, type LineChartProps, LineChart as LineChartUnresponsive, type LineStyles, type MainMetricRenderProps, type MetricValueType, type MultipleDataPointsDate, type Optional, type OrientationType, PieChartResponsive as PieChart, type PieChartProps, type PieChartRenderTooltipParams, PieChart as PieChartUnresponsive, PieSemiCircleChartResponsive as PieSemiCircleChart, type PieSemiCircleChartProps, type PieSemiCircleChartRenderTooltipParams, PieSemiCircleChart as PieSemiCircleChartUnresponsive, type RenderLabelProps, type RenderLineGlyphProps, type RenderTooltipGlyphProps, type RenderTooltipParams, type RenderValueProps, type ScaleOptions, type SeriesData, type SeriesDataOptions, Sparkline, type SparklineDataPoint, type SparklineProps, SparklineUnresponsive, type StepLabelRenderProps, type StepRateRenderProps, type TooltipData, type TooltipDatum, type TooltipProps, type TooltipRenderProps, type TrendDirection, TrendIndicator, type TrendIndicatorProps, type XyChartTooltipProps, buildCalendarHeatmapData, defaultTheme, formatMetricValue, formatPercentage, getColorDistance, hexToRgba, isValidHexColor, lightenHexColor, mergeThemes, mixHexColors, normalizeColorToHex, parseAsLocalDate, parseHslString, parseRgbString, prefersLightText, relativeLuminance, useChartLegendItems, useGlobalChartsContext, useGlobalChartsTheme, useLeaderboardLegendItems, validateHexColor };
2063
+ export { AccessibleTooltip, type AnnotationStyles, type ArcData, AreaChartResponsive as AreaChart, type AreaChartProps, AreaChart as AreaChartUnresponsive, type AxisOptions, BarChartResponsive as BarChart, type BarChartProps, BarChart as BarChartUnresponsive, BarListChartResponsive as BarListChart, type BarListChartProps, BarListChart as BarListChartUnresponsive, type BaseChartProps, type BaseLegendItem, type BaseLegendProps, BaseTooltip, type BaseTooltipProps, type CalendarHeatmapResult, type ChartLegendConfig, type ChartLegendOptions, type ChartTheme, type CompleteChartTheme, ConversionFunnelChartWithProvider as ConversionFunnelChart, type ConversionFunnelChartProps, type CurveType, type DataPoint, type DataPointDate, type DataPointPercentage, type EventHandlerParams, type FunnelStep, GeoChartResponsive as GeoChart, type GeoChartError, type GeoChartProps, GeoChartWithProvider as GeoChartUnresponsive, type GeoData, type GeoRegion, type GeoResolution, GlobalChartsContext, GlobalChartsProvider, GlobalChartsProvider as ThemeProvider, type GoogleDataTableColumn, type GoogleDataTableColumnRoleType, type GoogleDataTableRow, type GradientConfig, type GradientStop, type GridProps, type GridStyles, type HeatmapCell, HeatmapChartResponsive as HeatmapChart, type HeatmapChartProps, HeatmapChart as HeatmapChartUnresponsive, type HeatmapColumn, type HeatmapTooltipData, LeaderboardChartResponsive as LeaderboardChart, type LeaderboardChartProps, LeaderboardChart as LeaderboardChartUnresponsive, type LeaderboardEntry, Legend, type LegendItemStyles, type LegendLabelStyles, type LegendPosition, type LegendProps, type LegendShape, type LegendShapeLabel, type LegendShapeRenderProps, type LegendShapeStyles, type LegendValueDisplay, LineChartResponsive as LineChart, type LineChartAnnotationProps, type LineChartProps, LineChart as LineChartUnresponsive, type LineStyles, type MainMetricRenderProps, type MetricValueType, type MultipleDataPointsDate, type Optional, type OrientationType, PieChartResponsive as PieChart, type PieChartProps, type PieChartRenderTooltipParams, PieChart as PieChartUnresponsive, PieSemiCircleChartResponsive as PieSemiCircleChart, type PieSemiCircleChartProps, type PieSemiCircleChartRenderTooltipParams, PieSemiCircleChart as PieSemiCircleChartUnresponsive, type RenderLabelProps, type RenderLineGlyphProps, type RenderTooltipGlyphProps, type RenderTooltipParams, type RenderValueProps, type ScaleOptions, type SeriesChartLegendConfig, type SeriesData, type SeriesDataOptions, Sparkline, type SparklineDataPoint, type SparklineProps, SparklineUnresponsive, type StepLabelRenderProps, type StepRateRenderProps, type TooltipData, type TooltipDatum, type TooltipProps, type TooltipRenderProps, type TrendDirection, TrendIndicator, type TrendIndicatorProps, type XyChartTooltipProps, buildCalendarHeatmapData, defaultTheme, formatMetricValue, formatPercentage, getColorDistance, hexToRgba, isValidHexColor, lightenHexColor, mergeThemes, mixHexColors, normalizeColorToHex, parseAsLocalDate, parseHslString, parseRgbString, prefersLightText, relativeLuminance, useChartLegendItems, useGlobalChartsContext, useGlobalChartsTheme, useLeaderboardLegendItems, validateHexColor };
2002
2064
  //# sourceMappingURL=index.d.ts.map