@react-magma/charts 14.1.1-rc.0 → 14.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.
- package/dist/charts.js +220 -187
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +221 -188
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +648 -376
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/CarbonChart/CarbonChart.d.ts +25 -0
- package/package.json +3 -3
- package/src/components/CarbonChart/CarbonChart.test.js +423 -0
- package/src/components/CarbonChart/CarbonChart.tsx +381 -246
- package/src/components/CarbonChart/CarbonChartArea.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartAreaStacked.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBarFloating.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBarGrouped.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBarStacked.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBoxplot.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBubble.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBullet.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartCombo.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartDonut.stories.tsx +3 -1
- package/src/components/CarbonChart/CarbonChartGauge.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartHistogram.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartLine.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartLollipop.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartMeter.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartPie.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartRadar.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartScatter.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartSparkline.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartStep.stories.tsx +1 -1
- package/src/components/CarbonChart/carbon-charts.css +89 -87
- package/src/components/ChartTable/ChartMoreOptionsButton.tsx +1 -0
- package/src/components/ChartTable/ChartTable.stories.tsx +140 -1
- package/src/components/ChartTable/ChartTable.test.js +452 -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
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
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
|
-
|
|
806
|
-
|
|
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
|
|
824
|
-
|
|
825
|
-
|
|
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
|
|
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
|
-
|
|
876
|
-
const legendItems = readLegendItems(wrapper);
|
|
877
|
-
const scale = 2;
|
|
904
|
+
if (!domUtils) return;
|
|
878
905
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
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
|
-
|
|
910
|
+
magmaWrapper?.classList.remove('has-magma-toolbar');
|
|
890
911
|
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
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
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
919
|
+
try {
|
|
920
|
+
if (format === 'jpg') {
|
|
921
|
+
domUtils.exportToJPG();
|
|
922
|
+
} else {
|
|
923
|
+
domUtils.exportToPNG();
|
|
905
924
|
}
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1001
|
-
}, [
|
|
1009
|
+
exportImage('png');
|
|
1010
|
+
}, [exportImage]);
|
|
1002
1011
|
|
|
1003
1012
|
const handleDownloadJpg = React.useCallback(() => {
|
|
1004
|
-
|
|
1005
|
-
}, [
|
|
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
|
-
<
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1182
|
+
requestAnimationFrame(() => {
|
|
1153
1183
|
lastTableTriggerRef.current?.focus();
|
|
1154
|
-
}
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
1389
|
+
scale: colorScale,
|
|
1377
1390
|
},
|
|
1378
1391
|
tooltip: {
|
|
1379
1392
|
...(options?.tooltip || {}),
|
|
@@ -1381,10 +1394,18 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1381
1394
|
type: 'none',
|
|
1382
1395
|
},
|
|
1383
1396
|
},
|
|
1384
|
-
...(chartToolbar
|
|
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 =
|
|
1408
|
+
const ChartType = ALL_CHARTS[type];
|
|
1388
1409
|
|
|
1389
1410
|
React.useEffect(() => {
|
|
1390
1411
|
const timer = setTimeout(() => {
|
|
@@ -1467,12 +1488,107 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1467
1488
|
return () => observer.disconnect();
|
|
1468
1489
|
}, [type, dataSet]);
|
|
1469
1490
|
|
|
1470
|
-
|
|
1491
|
+
// Make Carbon Charts data points keyboard-focusable.
|
|
1492
|
+
React.useEffect(() => {
|
|
1493
|
+
const wrapper = internalRef.current;
|
|
1494
|
+
|
|
1495
|
+
if (!wrapper) return;
|
|
1496
|
+
|
|
1497
|
+
const isDot = (el: EventTarget | null): el is SVGCircleElement =>
|
|
1498
|
+
el instanceof Element &&
|
|
1499
|
+
el.nodeName.toLowerCase() === 'circle' &&
|
|
1500
|
+
el.classList.contains('dot');
|
|
1501
|
+
|
|
1502
|
+
const onFocusIn = (e: FocusEvent) => {
|
|
1503
|
+
if (!isDot(e.target)) return;
|
|
1504
|
+
const dot = e.target as SVGCircleElement;
|
|
1505
|
+
|
|
1506
|
+
dot.style.opacity = '1';
|
|
1507
|
+
const { left, top, width, height } = dot.getBoundingClientRect();
|
|
1508
|
+
const cx = left + width / 2;
|
|
1509
|
+
const cy = top + height / 2;
|
|
1510
|
+
|
|
1511
|
+
dot.dispatchEvent(
|
|
1512
|
+
new MouseEvent('mouseover', {
|
|
1513
|
+
bubbles: true,
|
|
1514
|
+
clientX: cx,
|
|
1515
|
+
clientY: cy,
|
|
1516
|
+
screenX: cx,
|
|
1517
|
+
screenY: cy,
|
|
1518
|
+
})
|
|
1519
|
+
);
|
|
1520
|
+
dot.dispatchEvent(
|
|
1521
|
+
new MouseEvent('mousemove', {
|
|
1522
|
+
bubbles: true,
|
|
1523
|
+
clientX: cx,
|
|
1524
|
+
clientY: cy,
|
|
1525
|
+
screenX: cx,
|
|
1526
|
+
screenY: cy,
|
|
1527
|
+
})
|
|
1528
|
+
);
|
|
1529
|
+
};
|
|
1530
|
+
|
|
1531
|
+
const onFocusOut = (e: FocusEvent) => {
|
|
1532
|
+
if (!isDot(e.target)) return;
|
|
1533
|
+
const dot = e.target;
|
|
1534
|
+
|
|
1535
|
+
dot.style.opacity = '';
|
|
1536
|
+
dot.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
|
|
1537
|
+
};
|
|
1538
|
+
|
|
1539
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
1540
|
+
if (!isDot(e.target)) return;
|
|
1541
|
+
if (e.key !== 'Enter' && e.key !== ' ') return;
|
|
1542
|
+
e.preventDefault();
|
|
1543
|
+
const dot = e.target;
|
|
1544
|
+
const { left, top, width, height } = dot.getBoundingClientRect();
|
|
1545
|
+
const cx = left + width / 2;
|
|
1546
|
+
const cy = top + height / 2;
|
|
1547
|
+
|
|
1548
|
+
dot.dispatchEvent(
|
|
1549
|
+
new MouseEvent('click', {
|
|
1550
|
+
bubbles: true,
|
|
1551
|
+
clientX: cx,
|
|
1552
|
+
clientY: cy,
|
|
1553
|
+
})
|
|
1554
|
+
);
|
|
1555
|
+
};
|
|
1556
|
+
|
|
1557
|
+
const rafId = requestAnimationFrame(() => {
|
|
1558
|
+
wrapper
|
|
1559
|
+
.querySelectorAll<SVGCircleElement>('circle.dot')
|
|
1560
|
+
.forEach(dot => {
|
|
1561
|
+
if (!dot.hasAttribute('tabindex')) {
|
|
1562
|
+
dot.setAttribute('tabindex', '0');
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
});
|
|
1566
|
+
|
|
1567
|
+
wrapper.addEventListener('focusin', onFocusIn);
|
|
1568
|
+
wrapper.addEventListener('focusout', onFocusOut);
|
|
1569
|
+
wrapper.addEventListener('keydown', onKeyDown);
|
|
1570
|
+
|
|
1571
|
+
return () => {
|
|
1572
|
+
cancelAnimationFrame(rafId);
|
|
1573
|
+
wrapper.removeEventListener('focusin', onFocusIn);
|
|
1574
|
+
wrapper.removeEventListener('focusout', onFocusOut);
|
|
1575
|
+
wrapper.removeEventListener('keydown', onKeyDown);
|
|
1576
|
+
};
|
|
1577
|
+
}, [type]);
|
|
1578
|
+
|
|
1579
|
+
const groupsLength = Object.keys(colorScale).length;
|
|
1471
1580
|
|
|
1472
1581
|
const showTable = chartToolbar?.showAsTable !== false;
|
|
1473
1582
|
|
|
1583
|
+
if (!ChartType) return null;
|
|
1584
|
+
|
|
1474
1585
|
return (
|
|
1475
|
-
<FullscreenRoot
|
|
1586
|
+
<FullscreenRoot
|
|
1587
|
+
ref={mergedRef}
|
|
1588
|
+
isFlowLayout={isFlowLayout}
|
|
1589
|
+
isInverse={isInverse}
|
|
1590
|
+
theme={theme}
|
|
1591
|
+
>
|
|
1476
1592
|
<VisuallyHidden>
|
|
1477
1593
|
<Announce>{legendAnnouncement}</Announce>
|
|
1478
1594
|
</VisuallyHidden>
|
|
@@ -1483,29 +1599,48 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1483
1599
|
data-testid={testId}
|
|
1484
1600
|
isInverse={isInverse}
|
|
1485
1601
|
theme={theme}
|
|
1486
|
-
className={`carbon-chart-wrapper${
|
|
1602
|
+
className={`carbon-chart-wrapper${
|
|
1603
|
+
chartToolbar ? ' has-magma-toolbar' : ''
|
|
1604
|
+
}`}
|
|
1487
1605
|
groupsLength={groupsLength < 6 ? groupsLength : 14}
|
|
1488
1606
|
role="region"
|
|
1489
1607
|
aria-label={ariaLabel || chartTitle}
|
|
1490
1608
|
aria-roledescription="chart"
|
|
1491
1609
|
{...rest}
|
|
1492
1610
|
>
|
|
1493
|
-
<ChartContentWrapper>
|
|
1611
|
+
<ChartContentWrapper className="carbon-chart-content">
|
|
1494
1612
|
{chartToolbar && (
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1613
|
+
/*
|
|
1614
|
+
* Slot content comes from the adopter, so it resolves
|
|
1615
|
+
* `isInverse` from context rather than from our prop. Without
|
|
1616
|
+
* this provider a Magma component in a title slot would stay
|
|
1617
|
+
* light on an inverse chart.
|
|
1618
|
+
*/
|
|
1619
|
+
<InverseContext.Provider value={{ isInverse }}>
|
|
1620
|
+
<CarbonChartToolbar
|
|
1621
|
+
chartRef={chartComponentRef}
|
|
1622
|
+
config={chartToolbar}
|
|
1623
|
+
dataSet={dataSet as Array<Record<string, unknown>>}
|
|
1624
|
+
isFlowLayout={isFlowLayout}
|
|
1625
|
+
isInverse={isInverse}
|
|
1626
|
+
isTableOpen={isTableOpen}
|
|
1627
|
+
isFullscreen={isFullscreen}
|
|
1628
|
+
onOpenTable={openTableModal}
|
|
1629
|
+
onToggleFullscreen={toggleFullscreen}
|
|
1630
|
+
theme={theme}
|
|
1631
|
+
title={chartTitle}
|
|
1632
|
+
wrapperRef={internalRef}
|
|
1633
|
+
/>
|
|
1634
|
+
{isFlowLayout && (
|
|
1635
|
+
<ChartSlot theme={theme}>{additionalContent}</ChartSlot>
|
|
1636
|
+
)}
|
|
1637
|
+
</InverseContext.Provider>
|
|
1507
1638
|
)}
|
|
1508
|
-
<ChartType
|
|
1639
|
+
<ChartType
|
|
1640
|
+
ref={chartComponentRef}
|
|
1641
|
+
data={dataSet}
|
|
1642
|
+
options={newOptions}
|
|
1643
|
+
/>
|
|
1509
1644
|
</ChartContentWrapper>
|
|
1510
1645
|
</CarbonChartWrapper>
|
|
1511
1646
|
{chartToolbar && showTable && (
|