@automattic/charts 1.8.1 → 1.10.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 +25 -0
  2. package/dist/index.cjs +615 -58
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.css +124 -36
  5. package/dist/index.d.cts +104 -4
  6. package/dist/index.d.ts +104 -4
  7. package/dist/index.js +615 -63
  8. package/dist/index.js.map +1 -1
  9. package/package.json +10 -10
  10. package/src/charts/area-chart/area-chart.module.scss +3 -1
  11. package/src/charts/area-chart/area-chart.tsx +5 -2
  12. package/src/charts/bar-chart/bar-chart.module.scss +6 -2
  13. package/src/charts/bar-chart/private/comparison-bars.tsx +6 -0
  14. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +15 -12
  15. package/src/charts/geo-chart/geo-chart.tsx +153 -3
  16. package/src/charts/geo-chart/index.ts +1 -1
  17. package/src/charts/geo-chart/test/geo-chart.test.tsx +132 -2
  18. package/src/charts/geo-chart/types.ts +11 -0
  19. package/src/charts/heatmap-chart/heatmap-chart.module.scss +103 -0
  20. package/src/charts/heatmap-chart/heatmap-chart.tsx +422 -0
  21. package/src/charts/heatmap-chart/index.ts +4 -0
  22. package/src/charts/heatmap-chart/private/build-calendar-data.ts +81 -0
  23. package/src/charts/heatmap-chart/private/heatmap-legend.tsx +53 -0
  24. package/src/charts/heatmap-chart/private/index.ts +5 -0
  25. package/src/charts/heatmap-chart/private/use-heatmap-colors.ts +45 -0
  26. package/src/charts/heatmap-chart/test/build-calendar-data.test.ts +88 -0
  27. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +301 -0
  28. package/src/charts/heatmap-chart/test/use-heatmap-colors.test.ts +34 -0
  29. package/src/charts/heatmap-chart/types.ts +42 -0
  30. package/src/charts/index.ts +1 -0
  31. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +20 -8
  32. package/src/charts/line-chart/line-chart.module.scss +6 -4
  33. package/src/charts/line-chart/line-chart.tsx +6 -2
  34. package/src/charts/line-chart/private/line-chart-annotation.tsx +16 -3
  35. package/src/charts/private/grid-control/grid-control.module.scss +1 -4
  36. package/src/charts/private/svg-empty-state/svg-empty-state.module.scss +1 -1
  37. package/src/charts/private/with-responsive/test/with-responsive.test.tsx +14 -0
  38. package/src/charts/private/with-responsive/with-responsive.tsx +12 -0
  39. package/src/charts/private/x-zoom.module.scss +6 -3
  40. package/src/components/legend/private/base-legend.module.scss +3 -1
  41. package/src/components/tooltip/base-tooltip.module.scss +4 -1
  42. package/src/components/trend-indicator/trend-indicator.module.scss +5 -3
  43. package/src/hooks/use-xychart-theme.ts +24 -0
  44. package/src/index.ts +13 -1
  45. package/src/providers/chart-context/themes.ts +32 -16
  46. package/src/types.ts +19 -2
  47. package/src/utils/color-utils.ts +36 -0
  48. package/src/utils/test/color-utils.test.ts +33 -0
package/dist/index.d.ts CHANGED
@@ -188,8 +188,8 @@ type GradientStop = {
188
188
  };
189
189
  type SeriesDataOptions = {
190
190
  gradient?: {
191
- from: string;
192
- to: string;
191
+ from?: string;
192
+ to?: string;
193
193
  fromOpacity?: number;
194
194
  toOpacity?: number;
195
195
  stops?: GradientStop[];
@@ -311,6 +311,19 @@ type ChartTheme = {
311
311
  }; /** Stroke width for the sparkline line */
312
312
  strokeWidth?: number;
313
313
  };
314
+ /**
315
+ * HeatmapChart settings. Cell gap, radius, value size and the selection ring come from
316
+ * WPDS tokens in CSS, so only the scale color and the compact sizing live here.
317
+ */
318
+ heatmapChart?: {
319
+ /**
320
+ * Color the cell scale interpolates toward at the highest value (prop > this >
321
+ * palette `colors[0]`), fed to CSS `color-mix`. Omit to use the palette color.
322
+ */
323
+ primaryColor?: string; /** Gap in px between cells in compact mode */
324
+ compactCellGap?: number; /** Fixed square cell size in px for compact mode */
325
+ compactCellSize?: number;
326
+ };
314
327
  };
315
328
  /**
316
329
  * Theme configuration with all properties guaranteed to be defined.
@@ -329,6 +342,7 @@ type CompleteChartTheme = Required<ChartTheme> & {
329
342
  sparkline: Required<NonNullable<ChartTheme['sparkline']>> & {
330
343
  margin: Required<NonNullable<ChartTheme['sparkline']>['margin']>;
331
344
  };
345
+ heatmapChart: Omit<Required<NonNullable<ChartTheme['heatmapChart']>>, 'primaryColor'> & Pick<NonNullable<ChartTheme['heatmapChart']>, 'primaryColor'>;
332
346
  };
333
347
  type AxisOptions = {
334
348
  orientation?: OrientationType;
@@ -1029,6 +1043,12 @@ type GeoRegion = 'world' | (string & {});
1029
1043
  * - 'metros': Metropolitan areas (US only)
1030
1044
  */
1031
1045
  type GeoResolution = 'countries' | 'provinces' | 'metros';
1046
+ interface GeoChartError {
1047
+ id?: string;
1048
+ message?: string;
1049
+ detailedMessage?: string;
1050
+ options?: Record<string, unknown>;
1051
+ }
1032
1052
  interface GeoChartProps extends Pick<BaseChartProps, 'className' | 'chartId' | 'width' | 'height'> {
1033
1053
  /**
1034
1054
  * Data in Google Charts native format for maximum flexibility.
@@ -1052,6 +1072,10 @@ interface GeoChartProps extends Pick<BaseChartProps, 'className' | 'chartId' | '
1052
1072
  * @default 'countries'
1053
1073
  */
1054
1074
  resolution?: GeoResolution;
1075
+ /**
1076
+ * Callback fired when Google Charts emits a chart error.
1077
+ */
1078
+ onError?: (error: GeoChartError) => void;
1055
1079
  /**
1056
1080
  * Optional render function for the loading placeholder.
1057
1081
  * Called while Google Charts is loading.
@@ -1075,6 +1099,66 @@ declare const GeoChartResponsive: ({
1075
1099
  size?: number;
1076
1100
  } & ResponsiveConfig) => import("react/jsx-runtime").JSX.Element;
1077
1101
  //#endregion
1102
+ //#region src/charts/heatmap-chart/types.d.ts
1103
+ /** A single heatmap cell. `value: null` marks an empty cell. */
1104
+ type HeatmapCell = {
1105
+ /** Per-cell label used in the tooltip / accessible name. */label?: string;
1106
+ value: number | null;
1107
+ };
1108
+ /** A heatmap column (rendered left→right); its cells render top→bottom. */
1109
+ type HeatmapColumn = {
1110
+ /** x-axis label for this column. Empty/omitted renders blank. */label?: string;
1111
+ data: HeatmapCell[];
1112
+ };
1113
+ type HeatmapTooltipData = {
1114
+ value: number | null;
1115
+ rowLabel?: string;
1116
+ columnLabel?: string;
1117
+ cellLabel?: string;
1118
+ row: number;
1119
+ column: number;
1120
+ };
1121
+ interface HeatmapChartProps extends Omit<BaseChartProps<HeatmapColumn[]>, 'showLegend' | 'legend' | 'gridVisibility'> {
1122
+ /** y-axis labels by row index. Empty entries render blank. */
1123
+ rowLabels?: string[];
1124
+ /** Compact mode: hide in-cell values, tighten gap, thin axis labels. Default false. */
1125
+ compact?: boolean;
1126
+ /** Render the numeric value inside each cell. Default `! compact`. */
1127
+ showValues?: boolean;
1128
+ /**
1129
+ * Color the cell scale interpolates toward at the highest value
1130
+ * (this prop > theme `heatmapChart.primaryColor` > palette `colors[0]`).
1131
+ */
1132
+ primaryColor?: string;
1133
+ renderTooltip?: (data: HeatmapTooltipData) => ReactNode;
1134
+ children?: ReactNode;
1135
+ }
1136
+ //#endregion
1137
+ //#region src/charts/heatmap-chart/private/build-calendar-data.d.ts
1138
+ type CalendarHeatmapResult = {
1139
+ data: HeatmapColumn[];
1140
+ rowLabels: string[];
1141
+ };
1142
+ declare const buildCalendarHeatmapData: (series: DataPointDate[], options?: {
1143
+ weekStartsOn?: 0 | 1;
1144
+ }) => CalendarHeatmapResult;
1145
+ //#endregion
1146
+ //#region src/charts/heatmap-chart/private/heatmap-legend.d.ts
1147
+ interface HeatmapLegendProps {
1148
+ /** Number of swatches in the scale. Default 5. */
1149
+ steps?: number;
1150
+ lessLabel?: string;
1151
+ moreLabel?: string;
1152
+ }
1153
+ declare const HeatmapLegend: FC<HeatmapLegendProps>;
1154
+ //#endregion
1155
+ //#region src/charts/heatmap-chart/heatmap-chart.d.ts
1156
+ interface HeatmapChartSubComponents {
1157
+ Legend: typeof HeatmapLegend;
1158
+ }
1159
+ declare const HeatmapChart: FC<HeatmapChartProps> & HeatmapChartSubComponents;
1160
+ declare const HeatmapChartResponsive: FC<HeatmapChartProps & ResponsiveConfig> & HeatmapChartSubComponents;
1161
+ //#endregion
1078
1162
  //#region src/charts/leaderboard-chart/types.d.ts
1079
1163
  interface LeaderboardChartProps extends Pick<BaseChartProps<LeaderboardEntry>, 'className' | 'data' | 'showLegend' | 'legend' | 'chartId' | 'width' | 'height' | 'size' | 'gap' | 'animation'> {
1080
1164
  /**
@@ -1331,6 +1415,22 @@ declare const normalizeColorToHex: (color: string, element?: HTMLElement | null,
1331
1415
  * @throws {Error} if hex string is malformed
1332
1416
  */
1333
1417
  declare const lightenHexColor: (hex: string, blend: number) => string;
1418
+ /**
1419
+ * WCAG relative luminance of a hex color (0 = black, 1 = white).
1420
+ *
1421
+ * @param hex - Hex color string (e.g., '#98C8DF')
1422
+ * @return Relative luminance in the range [0, 1]
1423
+ * @throws {Error} if hex string is malformed
1424
+ */
1425
+ declare const relativeLuminance: (hex: string) => number;
1426
+ /**
1427
+ * Whether light text reads better than dark text on the given background, using the W3C
1428
+ * luminance threshold (0.179) that maximizes contrast against black vs white.
1429
+ *
1430
+ * @param backgroundHex - Hex background color
1431
+ * @return true if light text should be used; false (dark text) for malformed colors
1432
+ */
1433
+ declare const prefersLightText: (backgroundHex: string) => boolean;
1334
1434
  //#endregion
1335
1435
  //#region src/charts/leaderboard-chart/hooks/use-leaderboard-legend-items.d.ts
1336
1436
  /**
@@ -1700,7 +1800,7 @@ declare const BaseTooltip: ({
1700
1800
  className,
1701
1801
  style,
1702
1802
  renderContainer
1703
- }: BaseTooltipProps) => string | number | true | Iterable<ReactNode> | import("react/jsx-runtime").JSX.Element;
1803
+ }: BaseTooltipProps) => string | number | true | import("react/jsx-runtime").JSX.Element | Iterable<ReactNode>;
1704
1804
  //#endregion
1705
1805
  //#region src/components/tooltip/accessible-tooltip.d.ts
1706
1806
  type FlattenedTooltipData = {
@@ -1845,5 +1945,5 @@ declare const useGlobalChartsTheme: () => CompleteChartTheme;
1845
1945
  */
1846
1946
  declare const defaultTheme: CompleteChartTheme;
1847
1947
  //#endregion
1848
- 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 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 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, 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, defaultTheme, formatMetricValue, formatPercentage, getColorDistance, hexToRgba, isValidHexColor, lightenHexColor, mergeThemes, normalizeColorToHex, parseAsLocalDate, parseHslString, parseRgbString, useChartLegendItems, useGlobalChartsContext, useGlobalChartsTheme, useLeaderboardLegendItems, validateHexColor };
1948
+ 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, normalizeColorToHex, parseAsLocalDate, parseHslString, parseRgbString, prefersLightText, relativeLuminance, useChartLegendItems, useGlobalChartsContext, useGlobalChartsTheme, useLeaderboardLegendItems, validateHexColor };
1849
1949
  //# sourceMappingURL=index.d.ts.map