@react-magma/charts 15.0.0-rc.5 → 16.0.0-next.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 (35) hide show
  1. package/dist/charts.js +259 -188
  2. package/dist/charts.js.map +1 -1
  3. package/dist/charts.modern.module.js +260 -189
  4. package/dist/charts.modern.module.js.map +1 -1
  5. package/dist/charts.umd.js +858 -413
  6. package/dist/charts.umd.js.map +1 -1
  7. package/dist/components/CarbonChart/CarbonChart.d.ts +25 -0
  8. package/package.json +3 -3
  9. package/src/components/CarbonChart/CarbonChart.test.js +431 -0
  10. package/src/components/CarbonChart/CarbonChart.tsx +431 -247
  11. package/src/components/CarbonChart/CarbonChartArea.stories.tsx +1 -1
  12. package/src/components/CarbonChart/CarbonChartAreaStacked.stories.tsx +1 -1
  13. package/src/components/CarbonChart/CarbonChartBarFloating.stories.tsx +1 -1
  14. package/src/components/CarbonChart/CarbonChartBarGrouped.stories.tsx +1 -1
  15. package/src/components/CarbonChart/CarbonChartBarStacked.stories.tsx +1 -1
  16. package/src/components/CarbonChart/CarbonChartBoxplot.stories.tsx +1 -1
  17. package/src/components/CarbonChart/CarbonChartBubble.stories.tsx +1 -1
  18. package/src/components/CarbonChart/CarbonChartBullet.stories.tsx +1 -1
  19. package/src/components/CarbonChart/CarbonChartCombo.stories.tsx +1 -1
  20. package/src/components/CarbonChart/CarbonChartDonut.stories.tsx +3 -1
  21. package/src/components/CarbonChart/CarbonChartGauge.stories.tsx +1 -1
  22. package/src/components/CarbonChart/CarbonChartHistogram.stories.tsx +1 -1
  23. package/src/components/CarbonChart/CarbonChartLine.stories.tsx +1 -1
  24. package/src/components/CarbonChart/CarbonChartLollipop.stories.tsx +1 -1
  25. package/src/components/CarbonChart/CarbonChartMeter.stories.tsx +1 -1
  26. package/src/components/CarbonChart/CarbonChartPie.stories.tsx +1 -1
  27. package/src/components/CarbonChart/CarbonChartRadar.stories.tsx +1 -1
  28. package/src/components/CarbonChart/CarbonChartScatter.stories.tsx +1 -1
  29. package/src/components/CarbonChart/CarbonChartSparkline.stories.tsx +1 -1
  30. package/src/components/CarbonChart/CarbonChartStep.stories.tsx +1 -1
  31. package/src/components/CarbonChart/carbon-charts.css +89 -87
  32. package/src/components/ChartTable/ChartMoreOptionsButton.tsx +1 -0
  33. package/src/components/ChartTable/ChartTable.stories.tsx +140 -1
  34. package/dist/components/ChartTable/ChartTable.test.d.ts +0 -1
  35. /package/src/components/ChartTable/{ChartTable.test.tsx → ChartTable.test.js} +0 -0
@@ -27,6 +27,7 @@ import {
27
27
  Announce,
28
28
  DropdownDivider,
29
29
  DropdownMenuItem,
30
+ InverseContext,
30
31
  ThemeInterface,
31
32
  ThemeContext,
32
33
  useDeviceDetect,
@@ -111,6 +112,22 @@ export interface ChartToolbarConfig {
111
112
  * @default 2
112
113
  */
113
114
  titleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
115
+ /**
116
+ * Custom content rendered before the chart title.
117
+ *
118
+ * The content is placed inline in the title row and follows the same
119
+ * accessibility rules as `titleSuffix`.
120
+ */
121
+ titlePrefix?: React.ReactNode;
122
+
123
+ /**
124
+ * Custom content rendered after the chart title and before toolbar actions.
125
+ *
126
+ * The content is placed inline in the title row and renders outside the
127
+ * heading element to preserve `options.title` as the heading's accessible name.
128
+ * Useful for patterns like Tooltip + IconButton.
129
+ */
130
+ titleSuffix?: React.ReactNode;
114
131
  }
115
132
 
116
133
  export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -139,6 +156,16 @@ export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
139
156
  * toolbar is automatically disabled.
140
157
  */
141
158
  chartToolbar?: ChartToolbarConfig;
159
+ /**
160
+ * Custom content rendered below the toolbar row and above the chart itself,
161
+ * for example a filter. Requires `chartToolbar`; ignored without it.
162
+ *
163
+ * Providing this lays the toolbar out in normal flow instead of overlaying
164
+ * the single row Carbon reserves for its title, so the rendered height
165
+ * becomes toolbar + content + chart rather than the chart box alone. Set
166
+ * `options.height` so the chart still has a definite height of its own.
167
+ */
168
+ additionalContent?: React.ReactNode;
142
169
  }
143
170
 
144
171
  const ChartContentWrapper = styled.div`
@@ -147,6 +174,7 @@ const ChartContentWrapper = styled.div`
147
174
  `;
148
175
 
149
176
  const FullscreenRoot = styled.div<{
177
+ isFlowLayout?: boolean;
150
178
  isInverse?: boolean;
151
179
  theme: ThemeInterface;
152
180
  }>`
@@ -159,9 +187,34 @@ const FullscreenRoot = styled.div<{
159
187
  props.isInverse
160
188
  ? props.theme.colors.primary700
161
189
  : props.theme.colors.neutral100};
190
+
191
+ ${props =>
192
+ props.isFlowLayout
193
+ ? `
194
+ box-sizing: border-box;
195
+ display: flex;
196
+ flex-direction: column;
197
+ padding: 2em;
198
+
199
+ .carbon-chart-wrapper,
200
+ .carbon-chart-content {
201
+ display: flex;
202
+ flex: 1 1 auto;
203
+ flex-direction: column;
204
+ min-height: 0;
205
+ }
206
+
207
+ .cds--chart-holder {
208
+ flex: 1 1 auto;
209
+ height: auto !important;
210
+ min-height: 0;
211
+ }
212
+ `
213
+ : `
162
214
  .cds--chart-holder {
163
215
  height: 100vh !important;
164
216
  }
217
+ `}
165
218
  }
166
219
  `;
167
220
 
@@ -199,6 +252,14 @@ const CarbonChartWrapper = styled.div<{
199
252
  }
200
253
  }
201
254
 
255
+ .cds--chart-holder.filled,
256
+ .cds--chart-holder.filled .cds--cc--chart-wrapper {
257
+ background-color: ${props =>
258
+ props.isInverse
259
+ ? props.theme.colors.primary600
260
+ : props.theme.colors.neutral100};
261
+ }
262
+
202
263
  .cds--data-table thead tr th {
203
264
  background: ${props =>
204
265
  props.isInverse ? props.theme.colors.primary700 : ''} !important;
@@ -516,6 +577,9 @@ const CarbonChartWrapper = styled.div<{
516
577
  : props.theme.colors.focus} !important;
517
578
  outline-offset: 0;
518
579
  }
580
+ circle.dot:focus {
581
+ outline: none !important;
582
+ }
519
583
  .cds--overflow-menu-options__btn:focus,
520
584
  .cds--overflow-menu:focus,
521
585
  .cds--overflow-menu__trigger:focus,
@@ -620,6 +684,17 @@ const CarbonChartWrapper = styled.div<{
620
684
  overflow: visible;
621
685
  }
622
686
 
687
+ circle.dot:focus {
688
+ outline: none;
689
+ stroke: ${props =>
690
+ props.isInverse
691
+ ? props.theme.colors.focusInverse
692
+ : props.theme.colors.focus} !important;
693
+ stroke-width: 6px !important;
694
+ stroke-opacity: 1 !important;
695
+ paint-order: stroke fill;
696
+ }
697
+
623
698
  .cds--cc--chart-wrapper text {
624
699
  font-size: ${props => props.theme.typeScale.size02.fontSize};
625
700
  }
@@ -663,7 +738,13 @@ interface ColorsObject {
663
738
  [key: string]: string;
664
739
  }
665
740
 
741
+ type ExtendedChartOptions = ChartOptions & {
742
+ title?: string;
743
+ colors?: string[];
744
+ };
745
+
666
746
  const ToolbarWrapper = styled.div<{
747
+ isFlowLayout?: boolean;
667
748
  isFullscreen?: boolean;
668
749
  isInverse?: boolean;
669
750
  theme: ThemeInterface;
@@ -671,12 +752,20 @@ const ToolbarWrapper = styled.div<{
671
752
  align-items: center;
672
753
  display: flex;
673
754
  justify-content: space-between;
674
- left: ${props => (props.isFullscreen ? '2em' : '0')};
675
- position: absolute;
676
- right: ${props => (props.isFullscreen ? '2em' : '0')};
677
- top: ${props => (props.isFullscreen ? '2em' : '0')};
678
755
  z-index: 2;
679
756
 
757
+ ${props =>
758
+ props.isFlowLayout
759
+ ? `
760
+ position: relative;
761
+ `
762
+ : `
763
+ left: ${props.isFullscreen ? '2em' : '0'};
764
+ position: absolute;
765
+ right: ${props.isFullscreen ? '2em' : '0'};
766
+ top: ${props.isFullscreen ? '2em' : '0'};
767
+ `}
768
+
680
769
  button {
681
770
  color: ${props =>
682
771
  props.isInverse
@@ -710,6 +799,22 @@ const ChartTitle = styled.h2<{
710
799
  : props.theme.colors.neutral700} !important;
711
800
  `;
712
801
 
802
+ const TitleGroup = styled.div<{ theme: ThemeInterface }>`
803
+ align-items: center;
804
+ display: flex;
805
+ gap: ${props => props.theme.spaceScale.spacing02};
806
+ min-width: 0;
807
+ `;
808
+
809
+ const TitleSlot = styled.div`
810
+ align-items: center;
811
+ display: flex;
812
+ `;
813
+
814
+ const ChartSlot = styled.div<{ theme: ThemeInterface }>`
815
+ margin: ${props => props.theme.spaceScale.spacing05} 0 0;
816
+ `;
817
+
713
818
  const ToolbarActions = styled.div<{ theme: ThemeInterface }>`
714
819
  align-items: center;
715
820
  display: flex;
@@ -779,192 +884,83 @@ function downloadCsv(dataSet: Array<Record<string, unknown>>, title: string) {
779
884
  URL.revokeObjectURL(url);
780
885
  }
781
886
 
782
- function inlineStyles(source: Element, target: Element) {
783
- const computed = window.getComputedStyle(source);
784
-
785
- if (target instanceof HTMLElement || target instanceof SVGElement) {
786
- target.setAttribute(
787
- 'style',
788
- Array.from(computed)
789
- .map(prop => `${prop}:${computed.getPropertyValue(prop)}`)
790
- .join(';')
791
- );
792
- }
793
- for (let i = 0; i < source.children.length; i++) {
794
- if (target.children[i]) {
795
- inlineStyles(source.children[i], target.children[i]);
796
- }
797
- }
798
- }
799
-
800
- interface LegendItem {
801
- label: string;
802
- color: string;
887
+ interface CarbonChartCore {
888
+ services?: {
889
+ domUtils?: { exportToPNG: () => void; exportToJPG: () => void };
890
+ };
803
891
  }
804
892
 
805
- function readLegendItems(wrapper: HTMLElement): LegendItem[] {
806
- const items: LegendItem[] = [];
807
-
808
- wrapper.querySelectorAll('.legend-item').forEach(item => {
809
- const checkbox = item.querySelector<HTMLElement>('.checkbox');
810
- const label = item.querySelector('p');
811
-
812
- if (checkbox && label) {
813
- items.push({
814
- color: checkbox.style.background || checkbox.style.backgroundColor,
815
- label: label.textContent || '',
816
- });
817
- }
818
- });
819
-
820
- return items;
893
+ interface CarbonChartComponent {
894
+ chart?: CarbonChartCore;
821
895
  }
822
896
 
823
- function drawLegend(
824
- ctx: CanvasRenderingContext2D,
825
- items: LegendItem[],
826
- startY: number,
827
- canvasWidth: number,
828
- scale: number
829
- ) {
830
- const fontSize = 13 * scale;
831
- const swatchSize = 12 * scale;
832
- const gap = 8 * scale;
833
- const itemGap = 16 * scale;
834
- const paddingX = 16 * scale;
835
-
836
- ctx.font = `${fontSize}px sans-serif`;
837
- ctx.textBaseline = 'middle';
838
-
839
- let x = paddingX;
840
- let y = startY;
841
-
842
- for (const item of items) {
843
- const textWidth = ctx.measureText(item.label).width;
844
- const itemWidth = swatchSize + gap + textWidth + itemGap;
845
-
846
- if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
847
- x = paddingX;
848
- y += fontSize + gap;
849
- }
850
-
851
- ctx.fillStyle = item.color;
852
- ctx.fillRect(x, y - swatchSize / 2, swatchSize, swatchSize);
853
-
854
- ctx.fillStyle = '#161616';
855
- ctx.fillText(item.label, x + swatchSize + gap, y);
856
-
857
- x += itemWidth;
858
- }
859
-
860
- return y + fontSize + gap;
861
- }
862
-
863
- function downloadImage(
864
- wrapperRef: React.RefObject<HTMLDivElement | null>,
865
- title: string,
897
+ function exportCarbonChartImage(
898
+ chart: CarbonChartCore | null | undefined,
899
+ toolbarWrapper: Element | null | undefined,
866
900
  format: 'png' | 'jpg'
867
901
  ) {
868
- const wrapper = wrapperRef.current;
869
-
870
- if (!wrapper) return;
871
- const svg = wrapper.querySelector('svg.layout-svg-wrapper');
872
-
873
- if (!svg) return;
902
+ const domUtils = chart?.services?.domUtils;
874
903
 
875
- const svgRect = svg.getBoundingClientRect();
876
- const legendItems = readLegendItems(wrapper);
877
- const scale = 2;
904
+ if (!domUtils) return;
878
905
 
879
- // Measure legend height
880
- const tempCanvas = document.createElement('canvas');
881
- const tempCtx = tempCanvas.getContext('2d');
882
- const fontSize = 13 * scale;
883
- const swatchSize = 12 * scale;
884
- const gap = 8 * scale;
885
- const itemGap = 16 * scale;
886
- const paddingX = 16 * scale;
887
- const canvasWidth = svgRect.width * scale;
906
+ const magmaWrapper = toolbarWrapper?.classList.contains('has-magma-toolbar')
907
+ ? toolbarWrapper
908
+ : null;
888
909
 
889
- let legendHeight = 0;
910
+ magmaWrapper?.classList.remove('has-magma-toolbar');
890
911
 
891
- if (legendItems.length > 0 && tempCtx) {
892
- tempCtx.font = `${fontSize}px sans-serif`;
893
- let x = paddingX;
894
- let rows = 1;
895
-
896
- for (const item of legendItems) {
897
- const textWidth = tempCtx.measureText(item.label).width;
898
- const itemWidth = swatchSize + gap + textWidth + itemGap;
912
+ let restored = false;
913
+ const restore = () => {
914
+ if (restored) return;
915
+ restored = true;
916
+ magmaWrapper?.classList.add('has-magma-toolbar');
917
+ };
899
918
 
900
- if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
901
- x = paddingX;
902
- rows++;
903
- }
904
- x += itemWidth;
919
+ try {
920
+ if (format === 'jpg') {
921
+ domUtils.exportToJPG();
922
+ } else {
923
+ domUtils.exportToPNG();
905
924
  }
906
- legendHeight = rows * (fontSize + gap) + gap * 2;
907
- }
908
-
909
- const width = svgRect.width * scale;
910
- const height = svgRect.height * scale + legendHeight;
911
-
912
- const clone = svg.cloneNode(true) as SVGSVGElement;
913
-
914
- clone.setAttribute('width', String(svgRect.width));
915
- clone.setAttribute('height', String(svgRect.height));
916
- clone.setAttribute('viewBox', `0 0 ${svgRect.width} ${svgRect.height}`);
917
- clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
918
-
919
- inlineStyles(svg, clone);
920
-
921
- const serializer = new XMLSerializer();
922
- const svgString = serializer.serializeToString(clone);
923
- const svgBlob = new Blob([svgString], {
924
- type: 'image/svg+xml;charset=utf-8',
925
- });
926
- const url = URL.createObjectURL(svgBlob);
927
-
928
- const mimeType = format === 'jpg' ? 'image/jpeg' : 'image/png';
929
- const ext = format === 'jpg' ? 'jpg' : 'png';
930
-
931
- const img = new Image();
932
-
933
- img.onload = () => {
934
- const canvas = document.createElement('canvas');
935
-
936
- canvas.width = width;
937
- canvas.height = height;
938
- const ctx = canvas.getContext('2d');
939
-
940
- if (!ctx) return;
941
-
942
- ctx.fillStyle = '#ffffff';
943
- ctx.fillRect(0, 0, canvas.width, canvas.height);
944
- ctx.drawImage(img, 0, 0, svgRect.width * scale, svgRect.height * scale);
945
- URL.revokeObjectURL(url);
946
-
947
- if (legendItems.length > 0) {
948
- drawLegend(ctx, legendItems, svgRect.height * scale + gap, width, scale);
925
+ } catch (error) {
926
+ console.warn('Carbon chart image export failed', error);
927
+ } finally {
928
+ // Re-add before the next paint so the title swap is never visible on
929
+ // screen; the timeout is only a fallback for when requestAnimationFrame is
930
+ // paused (e.g. the tab is in the background).
931
+ if (typeof requestAnimationFrame === 'function') {
932
+ requestAnimationFrame(restore);
949
933
  }
950
-
951
- canvas.toBlob(blob => {
952
- if (!blob) return;
953
- const imgUrl = URL.createObjectURL(blob);
954
- const a = document.createElement('a');
955
-
956
- a.href = imgUrl;
957
- a.download = `${title || 'chart'}.${ext}`;
958
- a.click();
959
- URL.revokeObjectURL(imgUrl);
960
- }, mimeType);
961
- };
962
- img.src = url;
934
+ setTimeout(restore, 1000);
935
+ }
963
936
  }
964
937
 
938
+ const ALL_CHARTS: Record<CarbonChartType, React.ComponentType<any>> = {
939
+ area: AreaChart,
940
+ areaStacked: StackedAreaChart,
941
+ bar: SimpleBarChart,
942
+ barGrouped: GroupedBarChart,
943
+ barStacked: StackedBarChart,
944
+ donut: DonutChart,
945
+ line: LineChart,
946
+ lollipop: LollipopChart,
947
+ pie: PieChart,
948
+ radar: RadarChart,
949
+ boxplot: BoxplotChart,
950
+ bubble: BubbleChart,
951
+ bullet: BulletChart,
952
+ gauge: GaugeChart,
953
+ histogram: HistogramChart,
954
+ meter: MeterChart,
955
+ scatter: ScatterChart,
956
+ combo: ComboChart,
957
+ };
958
+
965
959
  interface InternalToolbarProps {
960
+ chartRef: React.MutableRefObject<CarbonChartComponent | null>;
966
961
  config: ChartToolbarConfig;
967
962
  dataSet: Array<Record<string, unknown>>;
963
+ isFlowLayout: boolean;
968
964
  isInverse: boolean;
969
965
  isTableOpen: boolean;
970
966
  isFullscreen: boolean;
@@ -976,8 +972,10 @@ interface InternalToolbarProps {
976
972
  }
977
973
 
978
974
  function CarbonChartToolbar({
975
+ chartRef,
979
976
  config,
980
977
  dataSet,
978
+ isFlowLayout,
981
979
  isInverse,
982
980
  isTableOpen,
983
981
  isFullscreen,
@@ -992,17 +990,28 @@ function CarbonChartToolbar({
992
990
  const showFullscreen = config.fullscreen !== false;
993
991
  const resolvedTitle = title || t.defaultTitle;
994
992
 
993
+ const exportImage = React.useCallback(
994
+ (format: 'png' | 'jpg') => {
995
+ exportCarbonChartImage(
996
+ chartRef.current?.chart,
997
+ wrapperRef.current?.querySelector('.carbon-chart-wrapper'),
998
+ format
999
+ );
1000
+ },
1001
+ [chartRef, wrapperRef]
1002
+ );
1003
+
995
1004
  const handleDownloadCsv = React.useCallback(() => {
996
1005
  downloadCsv(dataSet, title);
997
1006
  }, [dataSet, title]);
998
1007
 
999
1008
  const handleDownloadPng = React.useCallback(() => {
1000
- downloadImage(wrapperRef, title, 'png');
1001
- }, [wrapperRef, title]);
1009
+ exportImage('png');
1010
+ }, [exportImage]);
1002
1011
 
1003
1012
  const handleDownloadJpg = React.useCallback(() => {
1004
- downloadImage(wrapperRef, title, 'jpg');
1005
- }, [wrapperRef, title]);
1013
+ exportImage('jpg');
1014
+ }, [exportImage]);
1006
1015
 
1007
1016
  const moreOptionsContent = (
1008
1017
  <>
@@ -1026,17 +1035,26 @@ function CarbonChartToolbar({
1026
1035
 
1027
1036
  return (
1028
1037
  <ToolbarWrapper
1038
+ isFlowLayout={isFlowLayout}
1029
1039
  isFullscreen={isFullscreen}
1030
1040
  isInverse={isInverse}
1031
1041
  theme={theme}
1032
1042
  >
1033
- <ChartTitle
1034
- as={`h${config.titleLevel ?? 2}` as keyof JSX.IntrinsicElements}
1035
- isInverse={isInverse}
1036
- theme={theme}
1037
- >
1038
- {resolvedTitle}
1039
- </ChartTitle>
1043
+ <TitleGroup theme={theme}>
1044
+ {config.titlePrefix ? (
1045
+ <TitleSlot>{config.titlePrefix}</TitleSlot>
1046
+ ) : null}
1047
+ <ChartTitle
1048
+ as={`h${config.titleLevel ?? 2}` as keyof JSX.IntrinsicElements}
1049
+ isInverse={isInverse}
1050
+ theme={theme}
1051
+ >
1052
+ {resolvedTitle}
1053
+ </ChartTitle>
1054
+ {config.titleSuffix ? (
1055
+ <TitleSlot>{config.titleSuffix}</TitleSlot>
1056
+ ) : null}
1057
+ </TitleGroup>
1040
1058
  <ToolbarActions theme={theme}>
1041
1059
  {showTable && (
1042
1060
  <ChartTableButton
@@ -1076,6 +1094,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1076
1094
  type,
1077
1095
  dataSet,
1078
1096
  options,
1097
+ additionalContent,
1079
1098
  ariaLabel,
1080
1099
  chartToolbar,
1081
1100
  ...rest
@@ -1084,6 +1103,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1084
1103
  const isInverse = useIsInverse(isInverseProp);
1085
1104
  const toolbarI18n = useChartToolbarI18n();
1086
1105
  const internalRef = React.useRef<HTMLDivElement | null>(null);
1106
+ const chartComponentRef = React.useRef<CarbonChartComponent | null>(null);
1087
1107
 
1088
1108
  const [isTableOpen, setIsTableOpen] = React.useState(false);
1089
1109
  const [isFullscreen, setIsFullscreen] = React.useState(false);
@@ -1110,6 +1130,8 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1110
1130
  ? chartToolbar.fullscreen !== false
1111
1131
  : false;
1112
1132
 
1133
+ const isFlowLayout = Boolean(chartToolbar && additionalContent);
1134
+
1113
1135
  const savedHeightRef = React.useRef<string>('');
1114
1136
 
1115
1137
  React.useEffect(() => {
@@ -1123,7 +1145,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1123
1145
  const chartHolder =
1124
1146
  internalRef.current?.querySelector<HTMLElement>('.cds--chart-holder');
1125
1147
 
1126
- if (chartHolder) {
1148
+ if (chartHolder && !isFlowLayout) {
1127
1149
  if (isFs) {
1128
1150
  savedHeightRef.current = chartHolder.style.height;
1129
1151
  chartHolder.style.height = '100vh';
@@ -1135,9 +1157,17 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1135
1157
 
1136
1158
  document.addEventListener('fullscreenchange', onFullscreenChange);
1137
1159
 
1138
- return () =>
1160
+ return () => {
1139
1161
  document.removeEventListener('fullscreenchange', onFullscreenChange);
1140
- }, [fullscreenEnabled]);
1162
+ // Restore height if the component unmounts while in fullscreen.
1163
+ const chartHolder =
1164
+ internalRef.current?.querySelector<HTMLElement>('.cds--chart-holder');
1165
+
1166
+ if (chartHolder && document.fullscreenElement) {
1167
+ chartHolder.style.height = savedHeightRef.current;
1168
+ }
1169
+ };
1170
+ }, [fullscreenEnabled, isFlowLayout]);
1141
1171
 
1142
1172
  const openTableModal = React.useCallback(
1143
1173
  (event: React.MouseEvent<HTMLButtonElement>) => {
@@ -1149,9 +1179,9 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1149
1179
 
1150
1180
  const closeTableModal = React.useCallback(() => {
1151
1181
  setIsTableOpen(false);
1152
- setTimeout(() => {
1182
+ requestAnimationFrame(() => {
1153
1183
  lastTableTriggerRef.current?.focus();
1154
- }, 0);
1184
+ });
1155
1185
  }, []);
1156
1186
 
1157
1187
  const toggleFullscreen = React.useCallback(() => {
@@ -1167,7 +1197,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1167
1197
  }, []);
1168
1198
 
1169
1199
  const chartTitle: string =
1170
- (options as any).title || toolbarI18n.defaultTitle;
1200
+ (options as ExtendedChartOptions).title || toolbarI18n.defaultTitle;
1171
1201
 
1172
1202
  const legendLabel = `${ariaLabel || chartTitle}. ${toolbarI18n.legendInstructions}`;
1173
1203
 
@@ -1322,58 +1352,41 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1322
1352
  }, [dataSet, chartTitle]);
1323
1353
 
1324
1354
  useCarbonModalFocusManagement(internalRef);
1325
- const allCharts = {
1326
- area: AreaChart,
1327
- areaStacked: StackedAreaChart,
1328
- bar: SimpleBarChart,
1329
- barGrouped: GroupedBarChart,
1330
- barStacked: StackedBarChart,
1331
- donut: DonutChart,
1332
- line: LineChart,
1333
- lollipop: LollipopChart,
1334
- pie: PieChart,
1335
- radar: RadarChart,
1336
- boxplot: BoxplotChart,
1337
- bubble: BubbleChart,
1338
- bullet: BulletChart,
1339
- gauge: GaugeChart,
1340
- histogram: HistogramChart,
1341
- meter: MeterChart,
1342
- scatter: ScatterChart,
1343
- combo: ComboChart,
1344
- };
1345
1355
 
1346
- function buildColors() {
1356
+ const colorScale = React.useMemo(() => {
1347
1357
  const scaleColorsObj: ColorsObject = {};
1348
-
1349
- const allGroups = dataSet.map(item => {
1350
- return 'group' in item ? item['group'] : null;
1351
- });
1352
- const uniqueGroups = allGroups.filter(
1353
- (g, index) => allGroups.indexOf(g) === index
1354
- );
1355
- const customColors = ((options as any).colors as string[]) || [];
1358
+ const customColors = (options as ExtendedChartOptions).colors || [];
1356
1359
  const allColors = [...customColors, ...theme.chartColors];
1357
1360
  const allInverseColors = [...customColors, ...theme.chartColorsInverse];
1361
+ const allGroups = dataSet.map(item =>
1362
+ 'group' in item ? (item as Record<string, unknown>)['group'] : null
1363
+ );
1364
+ const uniqueGroups = Array.from(new Set(allGroups));
1365
+
1366
+ if (uniqueGroups.length <= allColors.length) {
1367
+ for (let i = 0; i < uniqueGroups.length; i++) {
1368
+ const group = uniqueGroups[i];
1358
1369
 
1359
- uniqueGroups.forEach((group, i) => {
1360
- if (uniqueGroups.length <= allColors.length) {
1361
- return (scaleColorsObj[group || ('null' as any)] = isInverse
1370
+ scaleColorsObj[String(group ?? 'null')] = isInverse
1362
1371
  ? allInverseColors[i]
1363
- : allColors[i]);
1372
+ : allColors[i];
1364
1373
  }
1365
-
1366
- return {};
1367
- });
1374
+ }
1368
1375
 
1369
1376
  return scaleColorsObj;
1370
- }
1377
+ }, [
1378
+ dataSet,
1379
+ options,
1380
+ theme.chartColors,
1381
+ theme.chartColorsInverse,
1382
+ isInverse,
1383
+ ]);
1371
1384
 
1372
1385
  const newOptions = {
1373
1386
  ...options,
1374
1387
  theme: isInverse ? ChartTheme.G100 : ChartTheme.WHITE,
1375
1388
  color: {
1376
- scale: buildColors(),
1389
+ scale: colorScale,
1377
1390
  },
1378
1391
  tooltip: {
1379
1392
  ...(options?.tooltip || {}),
@@ -1381,16 +1394,30 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1381
1394
  type: 'none',
1382
1395
  },
1383
1396
  },
1384
- ...(chartToolbar ? { toolbar: { enabled: false } } : {}),
1397
+ ...(chartToolbar
1398
+ ? {
1399
+ toolbar: { enabled: false },
1400
+ title: chartTitle,
1401
+ ...(options.fileDownload
1402
+ ? {}
1403
+ : { fileDownload: { fileName: chartTitle } }),
1404
+ }
1405
+ : {}),
1385
1406
  };
1386
1407
 
1387
- const ChartType = allCharts[type] as any;
1408
+ const ChartType = ALL_CHARTS[type];
1388
1409
 
1389
1410
  React.useEffect(() => {
1390
1411
  const timer = setTimeout(() => {
1391
1412
  if (internalRef.current) {
1392
1413
  internalRef.current
1393
- .querySelectorAll<SVGGElement>('g[aria-label]')
1414
+ .querySelectorAll<SVGGElement>('g.ruler[aria-label]')
1415
+ .forEach(g => {
1416
+ g.setAttribute('aria-hidden', 'true');
1417
+ });
1418
+
1419
+ internalRef.current
1420
+ .querySelectorAll<SVGGElement>('g[aria-label]:not(.ruler)')
1394
1421
  .forEach(g => {
1395
1422
  const role = g.getAttribute('role');
1396
1423
 
@@ -1406,6 +1433,30 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1406
1433
  return () => clearTimeout(timer);
1407
1434
  }, [type, dataSet]);
1408
1435
 
1436
+ React.useEffect(() => {
1437
+ const container = internalRef.current;
1438
+
1439
+ if (!container) return;
1440
+
1441
+ const hideRulerFromAT = () => {
1442
+ container
1443
+ .querySelectorAll<SVGGElement>('g.ruler[aria-label]')
1444
+ .forEach(g => {
1445
+ if (!g.hasAttribute('aria-hidden')) {
1446
+ g.setAttribute('aria-hidden', 'true');
1447
+ }
1448
+ });
1449
+ };
1450
+
1451
+ hideRulerFromAT();
1452
+
1453
+ const observer = new MutationObserver(hideRulerFromAT);
1454
+
1455
+ observer.observe(container, { childList: true, subtree: true });
1456
+
1457
+ return () => observer.disconnect();
1458
+ }, [type, dataSet]);
1459
+
1409
1460
  React.useEffect(() => {
1410
1461
  const wrapper = internalRef.current;
1411
1462
 
@@ -1467,12 +1518,126 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1467
1518
  return () => observer.disconnect();
1468
1519
  }, [type, dataSet]);
1469
1520
 
1470
- const groupsLength = Object.keys(buildColors()).length;
1521
+ // Make Carbon Charts data points keyboard-focusable.
1522
+ React.useEffect(() => {
1523
+ const wrapper = internalRef.current;
1524
+
1525
+ if (!wrapper) return;
1526
+
1527
+ const stampDotTabIndex = () => {
1528
+ wrapper
1529
+ .querySelectorAll<SVGCircleElement>('circle.dot')
1530
+ .forEach(dot => {
1531
+ if (!dot.hasAttribute('tabindex')) {
1532
+ dot.setAttribute('tabindex', '0');
1533
+ }
1534
+ });
1535
+ };
1536
+
1537
+ const isDot = (el: EventTarget | null): el is SVGCircleElement =>
1538
+ el instanceof Element &&
1539
+ el.nodeName.toLowerCase() === 'circle' &&
1540
+ el.classList.contains('dot');
1541
+
1542
+ const onFocusIn = (e: FocusEvent) => {
1543
+ if (!isDot(e.target)) return;
1544
+ const dot = e.target as SVGCircleElement;
1545
+
1546
+ const { left, top, width, height } = dot.getBoundingClientRect();
1547
+ const cx = left + width / 2;
1548
+ const cy = top + height / 2;
1549
+
1550
+ dot.dispatchEvent(
1551
+ new MouseEvent('mouseover', {
1552
+ bubbles: true,
1553
+ clientX: cx,
1554
+ clientY: cy,
1555
+ screenX: cx,
1556
+ screenY: cy,
1557
+ })
1558
+ );
1559
+ dot.dispatchEvent(
1560
+ new MouseEvent('mousemove', {
1561
+ bubbles: true,
1562
+ clientX: cx,
1563
+ clientY: cy,
1564
+ screenX: cx,
1565
+ screenY: cy,
1566
+ })
1567
+ );
1568
+ };
1569
+
1570
+ const onFocusOut = (e: FocusEvent) => {
1571
+ if (!isDot(e.target)) return;
1572
+ const dot = e.target;
1573
+
1574
+ // Keep tooltip context while Tab focus moves between chart points.
1575
+ if (isDot(e.relatedTarget)) {
1576
+ return;
1577
+ }
1578
+
1579
+ dot.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
1580
+ };
1581
+
1582
+ const onKeyDown = (e: KeyboardEvent) => {
1583
+ if (!isDot(e.target)) return;
1584
+ if (e.key !== 'Enter' && e.key !== ' ') return;
1585
+ e.preventDefault();
1586
+ const dot = e.target;
1587
+ const { left, top, width, height } = dot.getBoundingClientRect();
1588
+ const cx = left + width / 2;
1589
+ const cy = top + height / 2;
1590
+
1591
+ dot.dispatchEvent(
1592
+ new MouseEvent('click', {
1593
+ bubbles: true,
1594
+ clientX: cx,
1595
+ clientY: cy,
1596
+ })
1597
+ );
1598
+ };
1599
+
1600
+ const rafId = requestAnimationFrame(() => {
1601
+ stampDotTabIndex();
1602
+ });
1603
+
1604
+ const dotObserver = new MutationObserver(() => {
1605
+ stampDotTabIndex();
1606
+ });
1607
+
1608
+ dotObserver.observe(wrapper, {
1609
+ childList: true,
1610
+ subtree: true,
1611
+ attributes: true,
1612
+ attributeFilter: ['class', 'style', 'tabindex'],
1613
+ });
1614
+
1615
+ wrapper.addEventListener('focusin', onFocusIn);
1616
+ wrapper.addEventListener('focusout', onFocusOut);
1617
+ wrapper.addEventListener('keydown', onKeyDown);
1618
+
1619
+ return () => {
1620
+ cancelAnimationFrame(rafId);
1621
+ dotObserver.disconnect();
1622
+ wrapper.removeEventListener('focusin', onFocusIn);
1623
+ wrapper.removeEventListener('focusout', onFocusOut);
1624
+ wrapper.removeEventListener('keydown', onKeyDown);
1625
+ };
1626
+ }, [type, dataSet]);
1627
+
1628
+ const groupsLength = Object.keys(colorScale).length;
1471
1629
 
1472
1630
  const showTable = chartToolbar?.showAsTable !== false;
1473
1631
 
1632
+ if (!ChartType) return null;
1633
+
1474
1634
  return (
1475
- <FullscreenRoot ref={mergedRef} isInverse={isInverse} theme={theme}>
1635
+ <FullscreenRoot
1636
+ ref={mergedRef}
1637
+ isFlowLayout={isFlowLayout}
1638
+ isInverse={isInverse}
1639
+ theme={theme}
1640
+ >
1476
1641
  <VisuallyHidden>
1477
1642
  <Announce>{legendAnnouncement}</Announce>
1478
1643
  </VisuallyHidden>
@@ -1483,29 +1648,48 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1483
1648
  data-testid={testId}
1484
1649
  isInverse={isInverse}
1485
1650
  theme={theme}
1486
- className={`carbon-chart-wrapper${chartToolbar ? ' has-magma-toolbar' : ''}`}
1651
+ className={`carbon-chart-wrapper${
1652
+ chartToolbar ? ' has-magma-toolbar' : ''
1653
+ }`}
1487
1654
  groupsLength={groupsLength < 6 ? groupsLength : 14}
1488
1655
  role="region"
1489
1656
  aria-label={ariaLabel || chartTitle}
1490
1657
  aria-roledescription="chart"
1491
1658
  {...rest}
1492
1659
  >
1493
- <ChartContentWrapper>
1660
+ <ChartContentWrapper className="carbon-chart-content">
1494
1661
  {chartToolbar && (
1495
- <CarbonChartToolbar
1496
- config={chartToolbar}
1497
- dataSet={dataSet as Array<Record<string, unknown>>}
1498
- isInverse={isInverse}
1499
- isTableOpen={isTableOpen}
1500
- isFullscreen={isFullscreen}
1501
- onOpenTable={openTableModal}
1502
- onToggleFullscreen={toggleFullscreen}
1503
- theme={theme}
1504
- title={chartTitle}
1505
- wrapperRef={internalRef}
1506
- />
1662
+ /*
1663
+ * Slot content comes from the adopter, so it resolves
1664
+ * `isInverse` from context rather than from our prop. Without
1665
+ * this provider a Magma component in a title slot would stay
1666
+ * light on an inverse chart.
1667
+ */
1668
+ <InverseContext.Provider value={{ isInverse }}>
1669
+ <CarbonChartToolbar
1670
+ chartRef={chartComponentRef}
1671
+ config={chartToolbar}
1672
+ dataSet={dataSet as Array<Record<string, unknown>>}
1673
+ isFlowLayout={isFlowLayout}
1674
+ isInverse={isInverse}
1675
+ isTableOpen={isTableOpen}
1676
+ isFullscreen={isFullscreen}
1677
+ onOpenTable={openTableModal}
1678
+ onToggleFullscreen={toggleFullscreen}
1679
+ theme={theme}
1680
+ title={chartTitle}
1681
+ wrapperRef={internalRef}
1682
+ />
1683
+ {isFlowLayout && (
1684
+ <ChartSlot theme={theme}>{additionalContent}</ChartSlot>
1685
+ )}
1686
+ </InverseContext.Provider>
1507
1687
  )}
1508
- <ChartType data={dataSet} options={newOptions} />
1688
+ <ChartType
1689
+ ref={chartComponentRef}
1690
+ data={dataSet}
1691
+ options={newOptions}
1692
+ />
1509
1693
  </ChartContentWrapper>
1510
1694
  </CarbonChartWrapper>
1511
1695
  {chartToolbar && showTable && (