@react-magma/charts 14.2.0-rc.1 → 14.2.1

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 +247 -194
  2. package/dist/charts.js.map +1 -1
  3. package/dist/charts.modern.module.js +247 -194
  4. package/dist/charts.modern.module.js.map +1 -1
  5. package/dist/charts.umd.js +726 -331
  6. package/dist/charts.umd.js.map +1 -1
  7. package/dist/components/CarbonChart/CarbonChart.d.ts +10 -0
  8. package/package.json +3 -3
  9. package/src/components/CarbonChart/CarbonChart.test.js +288 -0
  10. package/src/components/CarbonChart/CarbonChart.tsx +369 -239
  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 +54 -0
  34. package/dist/components/ChartTable/ChartTable.test.d.ts +0 -1
  35. /package/src/components/ChartTable/{ChartTable.test.tsx → ChartTable.test.js} +0 -0
@@ -156,6 +156,16 @@ export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
156
156
  * toolbar is automatically disabled.
157
157
  */
158
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;
159
169
  }
160
170
 
161
171
  const ChartContentWrapper = styled.div`
@@ -164,6 +174,7 @@ const ChartContentWrapper = styled.div`
164
174
  `;
165
175
 
166
176
  const FullscreenRoot = styled.div<{
177
+ isFlowLayout?: boolean;
167
178
  isInverse?: boolean;
168
179
  theme: ThemeInterface;
169
180
  }>`
@@ -176,9 +187,34 @@ const FullscreenRoot = styled.div<{
176
187
  props.isInverse
177
188
  ? props.theme.colors.primary700
178
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
+ : `
179
214
  .cds--chart-holder {
180
215
  height: 100vh !important;
181
216
  }
217
+ `}
182
218
  }
183
219
  `;
184
220
 
@@ -216,6 +252,14 @@ const CarbonChartWrapper = styled.div<{
216
252
  }
217
253
  }
218
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
+
219
263
  .cds--data-table thead tr th {
220
264
  background: ${props =>
221
265
  props.isInverse ? props.theme.colors.primary700 : ''} !important;
@@ -533,6 +577,9 @@ const CarbonChartWrapper = styled.div<{
533
577
  : props.theme.colors.focus} !important;
534
578
  outline-offset: 0;
535
579
  }
580
+ circle.dot:focus {
581
+ outline: none !important;
582
+ }
536
583
  .cds--overflow-menu-options__btn:focus,
537
584
  .cds--overflow-menu:focus,
538
585
  .cds--overflow-menu__trigger:focus,
@@ -637,6 +684,17 @@ const CarbonChartWrapper = styled.div<{
637
684
  overflow: visible;
638
685
  }
639
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
+
640
698
  .cds--cc--chart-wrapper text {
641
699
  font-size: ${props => props.theme.typeScale.size02.fontSize};
642
700
  }
@@ -680,7 +738,13 @@ interface ColorsObject {
680
738
  [key: string]: string;
681
739
  }
682
740
 
741
+ type ExtendedChartOptions = ChartOptions & {
742
+ title?: string;
743
+ colors?: string[];
744
+ };
745
+
683
746
  const ToolbarWrapper = styled.div<{
747
+ isFlowLayout?: boolean;
684
748
  isFullscreen?: boolean;
685
749
  isInverse?: boolean;
686
750
  theme: ThemeInterface;
@@ -688,12 +752,20 @@ const ToolbarWrapper = styled.div<{
688
752
  align-items: center;
689
753
  display: flex;
690
754
  justify-content: space-between;
691
- left: ${props => (props.isFullscreen ? '2em' : '0')};
692
- position: absolute;
693
- right: ${props => (props.isFullscreen ? '2em' : '0')};
694
- top: ${props => (props.isFullscreen ? '2em' : '0')};
695
755
  z-index: 2;
696
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
+
697
769
  button {
698
770
  color: ${props =>
699
771
  props.isInverse
@@ -734,16 +806,15 @@ const TitleGroup = styled.div<{ theme: ThemeInterface }>`
734
806
  min-width: 0;
735
807
  `;
736
808
 
737
- /*
738
- * The `magma-chart-slot` class is what keeps the tag reset in
739
- * carbon-charts.css out of this subtree, so adopter components keep their own
740
- * styling. Renaming it means updating the `:not()` selectors in that file.
741
- */
742
809
  const TitleSlot = styled.div`
743
810
  align-items: center;
744
811
  display: flex;
745
812
  `;
746
813
 
814
+ const ChartSlot = styled.div<{ theme: ThemeInterface }>`
815
+ margin: ${props => props.theme.spaceScale.spacing05} 0 0;
816
+ `;
817
+
747
818
  const ToolbarActions = styled.div<{ theme: ThemeInterface }>`
748
819
  align-items: center;
749
820
  display: flex;
@@ -813,192 +884,83 @@ function downloadCsv(dataSet: Array<Record<string, unknown>>, title: string) {
813
884
  URL.revokeObjectURL(url);
814
885
  }
815
886
 
816
- function inlineStyles(source: Element, target: Element) {
817
- const computed = window.getComputedStyle(source);
818
-
819
- if (target instanceof HTMLElement || target instanceof SVGElement) {
820
- target.setAttribute(
821
- 'style',
822
- Array.from(computed)
823
- .map(prop => `${prop}:${computed.getPropertyValue(prop)}`)
824
- .join(';')
825
- );
826
- }
827
- for (let i = 0; i < source.children.length; i++) {
828
- if (target.children[i]) {
829
- inlineStyles(source.children[i], target.children[i]);
830
- }
831
- }
832
- }
833
-
834
- interface LegendItem {
835
- label: string;
836
- color: string;
837
- }
838
-
839
- function readLegendItems(wrapper: HTMLElement): LegendItem[] {
840
- const items: LegendItem[] = [];
841
-
842
- wrapper.querySelectorAll('.legend-item').forEach(item => {
843
- const checkbox = item.querySelector<HTMLElement>('.checkbox');
844
- const label = item.querySelector('p');
845
-
846
- if (checkbox && label) {
847
- items.push({
848
- color: checkbox.style.background || checkbox.style.backgroundColor,
849
- label: label.textContent || '',
850
- });
851
- }
852
- });
853
-
854
- return items;
887
+ interface CarbonChartCore {
888
+ services?: {
889
+ domUtils?: { exportToPNG: () => void; exportToJPG: () => void };
890
+ };
855
891
  }
856
892
 
857
- function drawLegend(
858
- ctx: CanvasRenderingContext2D,
859
- items: LegendItem[],
860
- startY: number,
861
- canvasWidth: number,
862
- scale: number
863
- ) {
864
- const fontSize = 13 * scale;
865
- const swatchSize = 12 * scale;
866
- const gap = 8 * scale;
867
- const itemGap = 16 * scale;
868
- const paddingX = 16 * scale;
869
-
870
- ctx.font = `${fontSize}px sans-serif`;
871
- ctx.textBaseline = 'middle';
872
-
873
- let x = paddingX;
874
- let y = startY;
875
-
876
- for (const item of items) {
877
- const textWidth = ctx.measureText(item.label).width;
878
- const itemWidth = swatchSize + gap + textWidth + itemGap;
879
-
880
- if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
881
- x = paddingX;
882
- y += fontSize + gap;
883
- }
884
-
885
- ctx.fillStyle = item.color;
886
- ctx.fillRect(x, y - swatchSize / 2, swatchSize, swatchSize);
887
-
888
- ctx.fillStyle = '#161616';
889
- ctx.fillText(item.label, x + swatchSize + gap, y);
890
-
891
- x += itemWidth;
892
- }
893
-
894
- return y + fontSize + gap;
893
+ interface CarbonChartComponent {
894
+ chart?: CarbonChartCore;
895
895
  }
896
896
 
897
- function downloadImage(
898
- wrapperRef: React.RefObject<HTMLDivElement | null>,
899
- title: string,
897
+ function exportCarbonChartImage(
898
+ chart: CarbonChartCore | null | undefined,
899
+ toolbarWrapper: Element | null | undefined,
900
900
  format: 'png' | 'jpg'
901
901
  ) {
902
- const wrapper = wrapperRef.current;
903
-
904
- if (!wrapper) return;
905
- const svg = wrapper.querySelector('svg.layout-svg-wrapper');
906
-
907
- if (!svg) return;
902
+ const domUtils = chart?.services?.domUtils;
908
903
 
909
- const svgRect = svg.getBoundingClientRect();
910
- const legendItems = readLegendItems(wrapper);
911
- const scale = 2;
904
+ if (!domUtils) return;
912
905
 
913
- // Measure legend height
914
- const tempCanvas = document.createElement('canvas');
915
- const tempCtx = tempCanvas.getContext('2d');
916
- const fontSize = 13 * scale;
917
- const swatchSize = 12 * scale;
918
- const gap = 8 * scale;
919
- const itemGap = 16 * scale;
920
- const paddingX = 16 * scale;
921
- const canvasWidth = svgRect.width * scale;
906
+ const magmaWrapper = toolbarWrapper?.classList.contains('has-magma-toolbar')
907
+ ? toolbarWrapper
908
+ : null;
922
909
 
923
- let legendHeight = 0;
910
+ magmaWrapper?.classList.remove('has-magma-toolbar');
924
911
 
925
- if (legendItems.length > 0 && tempCtx) {
926
- tempCtx.font = `${fontSize}px sans-serif`;
927
- let x = paddingX;
928
- let rows = 1;
929
-
930
- for (const item of legendItems) {
931
- const textWidth = tempCtx.measureText(item.label).width;
932
- 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
+ };
933
918
 
934
- if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
935
- x = paddingX;
936
- rows++;
937
- }
938
- x += itemWidth;
919
+ try {
920
+ if (format === 'jpg') {
921
+ domUtils.exportToJPG();
922
+ } else {
923
+ domUtils.exportToPNG();
939
924
  }
940
- legendHeight = rows * (fontSize + gap) + gap * 2;
941
- }
942
-
943
- const width = svgRect.width * scale;
944
- const height = svgRect.height * scale + legendHeight;
945
-
946
- const clone = svg.cloneNode(true) as SVGSVGElement;
947
-
948
- clone.setAttribute('width', String(svgRect.width));
949
- clone.setAttribute('height', String(svgRect.height));
950
- clone.setAttribute('viewBox', `0 0 ${svgRect.width} ${svgRect.height}`);
951
- clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
952
-
953
- inlineStyles(svg, clone);
954
-
955
- const serializer = new XMLSerializer();
956
- const svgString = serializer.serializeToString(clone);
957
- const svgBlob = new Blob([svgString], {
958
- type: 'image/svg+xml;charset=utf-8',
959
- });
960
- const url = URL.createObjectURL(svgBlob);
961
-
962
- const mimeType = format === 'jpg' ? 'image/jpeg' : 'image/png';
963
- const ext = format === 'jpg' ? 'jpg' : 'png';
964
-
965
- const img = new Image();
966
-
967
- img.onload = () => {
968
- const canvas = document.createElement('canvas');
969
-
970
- canvas.width = width;
971
- canvas.height = height;
972
- const ctx = canvas.getContext('2d');
973
-
974
- if (!ctx) return;
975
-
976
- ctx.fillStyle = '#ffffff';
977
- ctx.fillRect(0, 0, canvas.width, canvas.height);
978
- ctx.drawImage(img, 0, 0, svgRect.width * scale, svgRect.height * scale);
979
- URL.revokeObjectURL(url);
980
-
981
- if (legendItems.length > 0) {
982
- 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);
983
933
  }
984
-
985
- canvas.toBlob(blob => {
986
- if (!blob) return;
987
- const imgUrl = URL.createObjectURL(blob);
988
- const a = document.createElement('a');
989
-
990
- a.href = imgUrl;
991
- a.download = `${title || 'chart'}.${ext}`;
992
- a.click();
993
- URL.revokeObjectURL(imgUrl);
994
- }, mimeType);
995
- };
996
- img.src = url;
934
+ setTimeout(restore, 1000);
935
+ }
997
936
  }
998
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
+
999
959
  interface InternalToolbarProps {
960
+ chartRef: React.MutableRefObject<CarbonChartComponent | null>;
1000
961
  config: ChartToolbarConfig;
1001
962
  dataSet: Array<Record<string, unknown>>;
963
+ isFlowLayout: boolean;
1002
964
  isInverse: boolean;
1003
965
  isTableOpen: boolean;
1004
966
  isFullscreen: boolean;
@@ -1010,8 +972,10 @@ interface InternalToolbarProps {
1010
972
  }
1011
973
 
1012
974
  function CarbonChartToolbar({
975
+ chartRef,
1013
976
  config,
1014
977
  dataSet,
978
+ isFlowLayout,
1015
979
  isInverse,
1016
980
  isTableOpen,
1017
981
  isFullscreen,
@@ -1026,17 +990,28 @@ function CarbonChartToolbar({
1026
990
  const showFullscreen = config.fullscreen !== false;
1027
991
  const resolvedTitle = title || t.defaultTitle;
1028
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
+
1029
1004
  const handleDownloadCsv = React.useCallback(() => {
1030
1005
  downloadCsv(dataSet, title);
1031
1006
  }, [dataSet, title]);
1032
1007
 
1033
1008
  const handleDownloadPng = React.useCallback(() => {
1034
- downloadImage(wrapperRef, title, 'png');
1035
- }, [wrapperRef, title]);
1009
+ exportImage('png');
1010
+ }, [exportImage]);
1036
1011
 
1037
1012
  const handleDownloadJpg = React.useCallback(() => {
1038
- downloadImage(wrapperRef, title, 'jpg');
1039
- }, [wrapperRef, title]);
1013
+ exportImage('jpg');
1014
+ }, [exportImage]);
1040
1015
 
1041
1016
  const moreOptionsContent = (
1042
1017
  <>
@@ -1060,15 +1035,14 @@ function CarbonChartToolbar({
1060
1035
 
1061
1036
  return (
1062
1037
  <ToolbarWrapper
1038
+ isFlowLayout={isFlowLayout}
1063
1039
  isFullscreen={isFullscreen}
1064
1040
  isInverse={isInverse}
1065
1041
  theme={theme}
1066
1042
  >
1067
1043
  <TitleGroup theme={theme}>
1068
1044
  {config.titlePrefix ? (
1069
- <TitleSlot className="magma-chart-slot">
1070
- {config.titlePrefix}
1071
- </TitleSlot>
1045
+ <TitleSlot>{config.titlePrefix}</TitleSlot>
1072
1046
  ) : null}
1073
1047
  <ChartTitle
1074
1048
  as={`h${config.titleLevel ?? 2}` as keyof JSX.IntrinsicElements}
@@ -1078,9 +1052,7 @@ function CarbonChartToolbar({
1078
1052
  {resolvedTitle}
1079
1053
  </ChartTitle>
1080
1054
  {config.titleSuffix ? (
1081
- <TitleSlot className="magma-chart-slot">
1082
- {config.titleSuffix}
1083
- </TitleSlot>
1055
+ <TitleSlot>{config.titleSuffix}</TitleSlot>
1084
1056
  ) : null}
1085
1057
  </TitleGroup>
1086
1058
  <ToolbarActions theme={theme}>
@@ -1122,6 +1094,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1122
1094
  type,
1123
1095
  dataSet,
1124
1096
  options,
1097
+ additionalContent,
1125
1098
  ariaLabel,
1126
1099
  chartToolbar,
1127
1100
  ...rest
@@ -1130,6 +1103,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1130
1103
  const isInverse = useIsInverse(isInverseProp);
1131
1104
  const toolbarI18n = useChartToolbarI18n();
1132
1105
  const internalRef = React.useRef<HTMLDivElement | null>(null);
1106
+ const chartComponentRef = React.useRef<CarbonChartComponent | null>(null);
1133
1107
 
1134
1108
  const [isTableOpen, setIsTableOpen] = React.useState(false);
1135
1109
  const [isFullscreen, setIsFullscreen] = React.useState(false);
@@ -1156,6 +1130,8 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1156
1130
  ? chartToolbar.fullscreen !== false
1157
1131
  : false;
1158
1132
 
1133
+ const isFlowLayout = Boolean(chartToolbar && additionalContent);
1134
+
1159
1135
  const savedHeightRef = React.useRef<string>('');
1160
1136
 
1161
1137
  React.useEffect(() => {
@@ -1169,7 +1145,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1169
1145
  const chartHolder =
1170
1146
  internalRef.current?.querySelector<HTMLElement>('.cds--chart-holder');
1171
1147
 
1172
- if (chartHolder) {
1148
+ if (chartHolder && !isFlowLayout) {
1173
1149
  if (isFs) {
1174
1150
  savedHeightRef.current = chartHolder.style.height;
1175
1151
  chartHolder.style.height = '100vh';
@@ -1181,9 +1157,17 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1181
1157
 
1182
1158
  document.addEventListener('fullscreenchange', onFullscreenChange);
1183
1159
 
1184
- return () =>
1160
+ return () => {
1185
1161
  document.removeEventListener('fullscreenchange', onFullscreenChange);
1186
- }, [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]);
1187
1171
 
1188
1172
  const openTableModal = React.useCallback(
1189
1173
  (event: React.MouseEvent<HTMLButtonElement>) => {
@@ -1195,9 +1179,9 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1195
1179
 
1196
1180
  const closeTableModal = React.useCallback(() => {
1197
1181
  setIsTableOpen(false);
1198
- setTimeout(() => {
1182
+ requestAnimationFrame(() => {
1199
1183
  lastTableTriggerRef.current?.focus();
1200
- }, 0);
1184
+ });
1201
1185
  }, []);
1202
1186
 
1203
1187
  const toggleFullscreen = React.useCallback(() => {
@@ -1213,7 +1197,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1213
1197
  }, []);
1214
1198
 
1215
1199
  const chartTitle: string =
1216
- (options as any).title || toolbarI18n.defaultTitle;
1200
+ (options as ExtendedChartOptions).title || toolbarI18n.defaultTitle;
1217
1201
 
1218
1202
  const legendLabel = `${ariaLabel || chartTitle}. ${toolbarI18n.legendInstructions}`;
1219
1203
 
@@ -1368,58 +1352,41 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1368
1352
  }, [dataSet, chartTitle]);
1369
1353
 
1370
1354
  useCarbonModalFocusManagement(internalRef);
1371
- const allCharts = {
1372
- area: AreaChart,
1373
- areaStacked: StackedAreaChart,
1374
- bar: SimpleBarChart,
1375
- barGrouped: GroupedBarChart,
1376
- barStacked: StackedBarChart,
1377
- donut: DonutChart,
1378
- line: LineChart,
1379
- lollipop: LollipopChart,
1380
- pie: PieChart,
1381
- radar: RadarChart,
1382
- boxplot: BoxplotChart,
1383
- bubble: BubbleChart,
1384
- bullet: BulletChart,
1385
- gauge: GaugeChart,
1386
- histogram: HistogramChart,
1387
- meter: MeterChart,
1388
- scatter: ScatterChart,
1389
- combo: ComboChart,
1390
- };
1391
1355
 
1392
- function buildColors() {
1356
+ const colorScale = React.useMemo(() => {
1393
1357
  const scaleColorsObj: ColorsObject = {};
1394
-
1395
- const allGroups = dataSet.map(item => {
1396
- return 'group' in item ? item['group'] : null;
1397
- });
1398
- const uniqueGroups = allGroups.filter(
1399
- (g, index) => allGroups.indexOf(g) === index
1400
- );
1401
- const customColors = ((options as any).colors as string[]) || [];
1358
+ const customColors = (options as ExtendedChartOptions).colors || [];
1402
1359
  const allColors = [...customColors, ...theme.chartColors];
1403
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];
1404
1369
 
1405
- uniqueGroups.forEach((group, i) => {
1406
- if (uniqueGroups.length <= allColors.length) {
1407
- return (scaleColorsObj[group || ('null' as any)] = isInverse
1370
+ scaleColorsObj[String(group ?? 'null')] = isInverse
1408
1371
  ? allInverseColors[i]
1409
- : allColors[i]);
1372
+ : allColors[i];
1410
1373
  }
1411
-
1412
- return {};
1413
- });
1374
+ }
1414
1375
 
1415
1376
  return scaleColorsObj;
1416
- }
1377
+ }, [
1378
+ dataSet,
1379
+ options,
1380
+ theme.chartColors,
1381
+ theme.chartColorsInverse,
1382
+ isInverse,
1383
+ ]);
1417
1384
 
1418
1385
  const newOptions = {
1419
1386
  ...options,
1420
1387
  theme: isInverse ? ChartTheme.G100 : ChartTheme.WHITE,
1421
1388
  color: {
1422
- scale: buildColors(),
1389
+ scale: colorScale,
1423
1390
  },
1424
1391
  tooltip: {
1425
1392
  ...(options?.tooltip || {}),
@@ -1427,16 +1394,30 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1427
1394
  type: 'none',
1428
1395
  },
1429
1396
  },
1430
- ...(chartToolbar ? { toolbar: { enabled: false } } : {}),
1397
+ ...(chartToolbar
1398
+ ? {
1399
+ toolbar: { enabled: false },
1400
+ title: chartTitle,
1401
+ ...(options.fileDownload
1402
+ ? {}
1403
+ : { fileDownload: { fileName: chartTitle } }),
1404
+ }
1405
+ : {}),
1431
1406
  };
1432
1407
 
1433
- const ChartType = allCharts[type] as any;
1408
+ const ChartType = ALL_CHARTS[type];
1434
1409
 
1435
1410
  React.useEffect(() => {
1436
1411
  const timer = setTimeout(() => {
1437
1412
  if (internalRef.current) {
1438
1413
  internalRef.current
1439
- .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)')
1440
1421
  .forEach(g => {
1441
1422
  const role = g.getAttribute('role');
1442
1423
 
@@ -1452,6 +1433,30 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1452
1433
  return () => clearTimeout(timer);
1453
1434
  }, [type, dataSet]);
1454
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
+
1455
1460
  React.useEffect(() => {
1456
1461
  const wrapper = internalRef.current;
1457
1462
 
@@ -1513,12 +1518,126 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1513
1518
  return () => observer.disconnect();
1514
1519
  }, [type, dataSet]);
1515
1520
 
1516
- 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;
1517
1629
 
1518
1630
  const showTable = chartToolbar?.showAsTable !== false;
1519
1631
 
1632
+ if (!ChartType) return null;
1633
+
1520
1634
  return (
1521
- <FullscreenRoot ref={mergedRef} isInverse={isInverse} theme={theme}>
1635
+ <FullscreenRoot
1636
+ ref={mergedRef}
1637
+ isFlowLayout={isFlowLayout}
1638
+ isInverse={isInverse}
1639
+ theme={theme}
1640
+ >
1522
1641
  <VisuallyHidden>
1523
1642
  <Announce>{legendAnnouncement}</Announce>
1524
1643
  </VisuallyHidden>
@@ -1529,14 +1648,16 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1529
1648
  data-testid={testId}
1530
1649
  isInverse={isInverse}
1531
1650
  theme={theme}
1532
- className={`carbon-chart-wrapper${chartToolbar ? ' has-magma-toolbar' : ''}`}
1651
+ className={`carbon-chart-wrapper${
1652
+ chartToolbar ? ' has-magma-toolbar' : ''
1653
+ }`}
1533
1654
  groupsLength={groupsLength < 6 ? groupsLength : 14}
1534
1655
  role="region"
1535
1656
  aria-label={ariaLabel || chartTitle}
1536
1657
  aria-roledescription="chart"
1537
1658
  {...rest}
1538
1659
  >
1539
- <ChartContentWrapper>
1660
+ <ChartContentWrapper className="carbon-chart-content">
1540
1661
  {chartToolbar && (
1541
1662
  /*
1542
1663
  * Slot content comes from the adopter, so it resolves
@@ -1546,8 +1667,10 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1546
1667
  */
1547
1668
  <InverseContext.Provider value={{ isInverse }}>
1548
1669
  <CarbonChartToolbar
1670
+ chartRef={chartComponentRef}
1549
1671
  config={chartToolbar}
1550
1672
  dataSet={dataSet as Array<Record<string, unknown>>}
1673
+ isFlowLayout={isFlowLayout}
1551
1674
  isInverse={isInverse}
1552
1675
  isTableOpen={isTableOpen}
1553
1676
  isFullscreen={isFullscreen}
@@ -1557,9 +1680,16 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1557
1680
  title={chartTitle}
1558
1681
  wrapperRef={internalRef}
1559
1682
  />
1683
+ {isFlowLayout && (
1684
+ <ChartSlot theme={theme}>{additionalContent}</ChartSlot>
1685
+ )}
1560
1686
  </InverseContext.Provider>
1561
1687
  )}
1562
- <ChartType data={dataSet} options={newOptions} />
1688
+ <ChartType
1689
+ ref={chartComponentRef}
1690
+ data={dataSet}
1691
+ options={newOptions}
1692
+ />
1563
1693
  </ChartContentWrapper>
1564
1694
  </CarbonChartWrapper>
1565
1695
  {chartToolbar && showTable && (