@automattic/charts 4.1.0 → 4.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 (80) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/TOKENS.md +7 -5
  3. package/dist/index.cjs +1115 -471
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.css +39 -1
  6. package/dist/index.d.cts +116 -16
  7. package/dist/index.d.ts +116 -16
  8. package/dist/index.js +1115 -473
  9. package/dist/index.js.map +1 -1
  10. package/package.json +11 -11
  11. package/src/charts/area-chart/area-chart.tsx +2 -1
  12. package/src/charts/area-chart/private/overlays.tsx +1 -2
  13. package/src/charts/area-chart/test/area-chart.test.tsx +31 -0
  14. package/src/charts/bar-chart/bar-chart.tsx +67 -17
  15. package/src/charts/bar-chart/index.ts +1 -1
  16. package/src/charts/bar-chart/private/band-highlight.tsx +85 -0
  17. package/src/charts/bar-chart/private/band-scale.ts +13 -0
  18. package/src/charts/bar-chart/private/band-tooltip.tsx +138 -0
  19. package/src/charts/bar-chart/private/comparison-bars.tsx +5 -10
  20. package/src/charts/bar-chart/private/index.ts +2 -0
  21. package/src/charts/bar-chart/private/test/band-highlight.test.tsx +109 -0
  22. package/src/charts/bar-chart/private/test/band-tooltip.test.ts +26 -0
  23. package/src/charts/bar-chart/private/truncated-tick-component.tsx +1 -1
  24. package/src/charts/bar-chart/private/use-bar-chart-options.ts +1 -1
  25. package/src/charts/bar-chart/test/bar-chart-pointer-events.test.tsx +11 -23
  26. package/src/charts/bar-chart/test/bar-chart.test.tsx +565 -1
  27. package/src/charts/bar-list-chart/bar-list-chart.tsx +5 -3
  28. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +1 -1
  29. package/src/charts/conversion-funnel-chart/types.ts +4 -2
  30. package/src/charts/geo-chart/types.ts +4 -2
  31. package/src/charts/heatmap-chart/{private/build-calendar-data.ts → build-calendar-data.ts} +15 -45
  32. package/src/charts/heatmap-chart/build-month-calendar-data.ts +124 -0
  33. package/src/charts/heatmap-chart/heatmap-chart.module.scss +32 -0
  34. package/src/charts/heatmap-chart/heatmap-chart.tsx +191 -114
  35. package/src/charts/heatmap-chart/index.ts +15 -3
  36. package/src/charts/heatmap-chart/private/civil-day.ts +26 -0
  37. package/src/charts/heatmap-chart/private/column-groups.ts +71 -0
  38. package/src/charts/heatmap-chart/private/heatmap-legend.tsx +19 -3
  39. package/src/charts/heatmap-chart/private/index.ts +2 -2
  40. package/src/charts/heatmap-chart/private/keyboard-navigation.ts +201 -0
  41. package/src/charts/heatmap-chart/test/build-calendar-data.test.ts +37 -1
  42. package/src/charts/heatmap-chart/test/build-month-calendar-data.test.ts +194 -0
  43. package/src/charts/heatmap-chart/test/column-groups.test.ts +83 -0
  44. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +428 -2
  45. package/src/charts/heatmap-chart/test/keyboard-navigation.test.ts +25 -0
  46. package/src/charts/heatmap-chart/test/use-calendar-heatmap-data.test.tsx +1 -1
  47. package/src/charts/heatmap-chart/test/use-month-calendar-heatmap-data.test.tsx +90 -0
  48. package/src/charts/heatmap-chart/test/zoned-calendar-data.test.ts +1 -1
  49. package/src/charts/heatmap-chart/test/zoned-month-calendar-data.test.ts +23 -0
  50. package/src/charts/heatmap-chart/types.ts +88 -3
  51. package/src/charts/heatmap-chart/use-calendar-heatmap-data.ts +2 -2
  52. package/src/charts/heatmap-chart/use-month-calendar-heatmap-data.ts +34 -0
  53. package/src/charts/leaderboard-chart/types.ts +13 -14
  54. package/src/charts/line-chart/line-chart.tsx +3 -2
  55. package/src/charts/line-chart/private/line-chart-annotation.tsx +1 -1
  56. package/src/charts/line-chart/test/line-chart.test.tsx +31 -0
  57. package/src/charts/line-chart/types.ts +2 -2
  58. package/src/charts/private/time-axis-options.ts +1 -1
  59. package/src/charts/private/with-responsive/with-responsive.tsx +2 -2
  60. package/src/charts/private/x-zoom/test/x-zoom.test.tsx +1 -3
  61. package/src/charts/private/x-zoom/x-zoom.tsx +1 -2
  62. package/src/components/legend/private/base-legend.tsx +1 -1
  63. package/src/components/tooltip/accessible-tooltip.tsx +146 -37
  64. package/src/components/tooltip/base-tooltip.module.scss +6 -1
  65. package/src/components/tooltip/private/bounded-tooltip.tsx +12 -6
  66. package/src/components/tooltip/test/accessible-tooltip.test.tsx +160 -0
  67. package/src/components/tooltip/test/bounded-tooltip.test.tsx +17 -0
  68. package/src/components/tooltip/test/xy-chart-tooltip.test.tsx +29 -3
  69. package/src/components/tooltip/xy-chart-tooltip.tsx +11 -11
  70. package/src/hooks/test/use-element-size.test.tsx +13 -13
  71. package/src/hooks/use-xychart-theme.ts +1 -1
  72. package/src/index.ts +7 -1
  73. package/src/providers/chart-context/themes.ts +1 -0
  74. package/src/styles/chart-scope.scss +11 -3
  75. package/src/styles/test/chart-scope.test.ts +36 -2
  76. package/src/types.ts +6 -13
  77. package/src/utils/format-metric-value.ts +4 -4
  78. package/src/utils/get-edge-tick-widths.ts +1 -1
  79. package/src/utils/get-styles.ts +1 -1
  80. package/src/visx/types.ts +7 -10
package/src/types.ts CHANGED
@@ -53,12 +53,7 @@ export type LegendShape< Data, Output > =
53
53
  | ComponentClass< LegendShapeRenderProps< Data, Output > >;
54
54
 
55
55
  export type GoogleDataTableColumnType =
56
- | 'string'
57
- | 'number'
58
- | 'boolean'
59
- | 'date'
60
- | 'datetime'
61
- | 'timeofday';
56
+ 'string' | 'number' | 'boolean' | 'date' | 'datetime' | 'timeofday';
62
57
 
63
58
  export enum GoogleDataTableColumnRoleType {
64
59
  annotation = 'annotation',
@@ -98,13 +93,7 @@ export type GoogleDataTableCell =
98
93
  export type GoogleDataTableRow = GoogleDataTableCell[];
99
94
 
100
95
  export type ChartType =
101
- | 'area'
102
- | 'bar'
103
- | 'conversion-funnel'
104
- | 'leaderboard'
105
- | 'line'
106
- | 'pie'
107
- | 'pie-semi-circle';
96
+ 'area' | 'bar' | 'conversion-funnel' | 'leaderboard' | 'line' | 'pie' | 'pie-semi-circle';
108
97
 
109
98
  export type OrientationType = ValueOf< typeof Orientation >;
110
99
 
@@ -145,6 +134,7 @@ export type ThemeAnnotationStyles = {
145
134
  export type DataPoint = {
146
135
  label: string;
147
136
  value: number;
137
+ color?: string;
148
138
  };
149
139
 
150
140
  /**
@@ -196,6 +186,7 @@ export type DataPointDate = {
196
186
  dateString?: string;
197
187
  value: number | null;
198
188
  label?: string;
189
+ color?: string;
199
190
  };
200
191
 
201
192
  export type LeaderboardEntry = {
@@ -424,6 +415,8 @@ export type ChartTheme = {
424
415
  compactCellGap?: number;
425
416
  /** Fixed square cell size in px for compact mode */
426
417
  compactCellSize?: number;
418
+ /** Gap in px between adjacent column groups; in compact mode the minimum, as the gaps share leftover width */
419
+ groupGap?: number;
427
420
  };
428
421
  };
429
422
 
@@ -47,13 +47,13 @@ export const formatMetricValue = (
47
47
  maximumFractionDigits: decimals ?? 2,
48
48
  signDisplay,
49
49
  },
50
- } )
50
+ } )
51
51
  : formatNumber( numericValue, {
52
52
  decimals: decimals ?? 2,
53
53
  numberFormatOptions: {
54
54
  signDisplay,
55
55
  },
56
- } );
56
+ } );
57
57
  return `$${ formatted }`;
58
58
  }
59
59
 
@@ -80,13 +80,13 @@ export const formatMetricValue = (
80
80
  maximumFractionDigits: decimals ?? 0,
81
81
  signDisplay,
82
82
  },
83
- } )
83
+ } )
84
84
  : formatNumber( numericValue, {
85
85
  decimals: decimals ?? 0,
86
86
  numberFormatOptions: {
87
87
  signDisplay,
88
88
  },
89
- } );
89
+ } );
90
90
  }
91
91
  }
92
92
  };
@@ -24,7 +24,7 @@ export const getEdgeTickWidths = < T extends AnyD3Scale >(
24
24
 
25
25
  const lastIndex = ticks.length - 1;
26
26
  const label = ( tick: ScaleInput< T >, index: number ) =>
27
- String( formatTick ? formatTick( tick, index, [] ) ?? '' : tick );
27
+ String( formatTick ? ( formatTick( tick, index, [] ) ?? '' ) : tick );
28
28
 
29
29
  return {
30
30
  first: getStringWidth( label( ticks[ 0 ], 0 ), labelStyle ) ?? 0,
@@ -103,7 +103,7 @@ export function getItemShapeStyles(
103
103
  const hasExplicitStyles = Object.values( explicitStyles ).some(
104
104
  value => value !== undefined && value !== null && value !== ''
105
105
  );
106
- const baseShapeStyles = hasExplicitStyles ? explicitStyles : themeShapeStyles ?? {};
106
+ const baseShapeStyles = hasExplicitStyles ? explicitStyles : ( themeShapeStyles ?? {} );
107
107
 
108
108
  // Layer the comparison bar opacity on top so the swatch matches the translucent bar
109
109
  // without discarding the base (custom or theme) shape styles.
package/src/visx/types.ts CHANGED
@@ -12,15 +12,10 @@ export interface RenderTooltipGlyphProps< Datum extends object > extends GlyphPr
12
12
  isNearestDatum: boolean;
13
13
  }
14
14
 
15
- export type TooltipPlacement = 'auto' | 'below-axis';
15
+ export type TooltipPlacement = 'auto' | 'below-axis' | 'beside';
16
16
 
17
17
  type CrosshairPaintProperty =
18
- | 'stroke'
19
- | 'strokeWidth'
20
- | 'strokeOpacity'
21
- | 'strokeDasharray'
22
- | 'strokeLinecap'
23
- | 'opacity';
18
+ 'stroke' | 'strokeWidth' | 'strokeOpacity' | 'strokeDasharray' | 'strokeLinecap' | 'opacity';
24
19
 
25
20
  export type CrosshairStyle = Pick< SVGProps< SVGLineElement >, CrosshairPaintProperty > & {
26
21
  className?: string;
@@ -31,11 +26,13 @@ export type XyChartTooltipProps< Datum extends object > = {
31
26
  renderTooltip: ( params: RenderTooltipParams< Datum > ) => ReactNode;
32
27
  renderGlyph?: ( params: RenderTooltipGlyphProps< Datum > ) => ReactNode;
33
28
  /**
34
- * Keep the panel below the x-axis label band, centered at the datum x and horizontally clamped.
35
- * Vertical bounds do not move this placement; clipping ancestors can still cut it off.
29
+ * Use below-axis placement for centered axis anchoring, or beside placement to avoid vertical flipping.
30
+ * Below-axis ignores vertical bounds; beside placement clamps to them.
36
31
  * @default 'auto'
37
32
  */
38
33
  tooltipPlacement?: TooltipPlacement;
34
+ /** Override the tooltip top anchor in SVG coordinates, including negative offsets. */
35
+ tooltipAnchorTop?: number;
39
36
  /** Merge overrides with the default box styles; use `unstyled` to strip the box styling. */
40
37
  style?: VisxTooltipProps[ 'style' ];
41
38
  snapTooltipToDatumX?: boolean;
@@ -52,7 +49,7 @@ export type XyChartTooltipProps< Datum extends object > = {
52
49
  * that clips its overflow, or the viewport when there is none. The box may
53
50
  * leave the chart wrapper. (It used to keep a body-level portal inside the
54
51
  * viewport.)
55
- * Ignored by `below-axis`.
52
+ * Non-auto placements always apply their own bounds handling.
56
53
  * @default true
57
54
  */
58
55
  detectBounds?: boolean;