@react-magma/charts 14.2.0-rc.1 → 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 +208 -193
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +208 -193
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +512 -290
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/CarbonChart/CarbonChart.d.ts +10 -0
- package/package.json +3 -3
- package/src/components/CarbonChart/CarbonChart.test.js +280 -0
- package/src/components/CarbonChart/CarbonChart.tsx +319 -238
- 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 +54 -0
- package/src/components/ChartTable/ChartTable.test.js +452 -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
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
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;
|
|
887
|
+
interface CarbonChartCore {
|
|
888
|
+
services?: {
|
|
889
|
+
domUtils?: { exportToPNG: () => void; exportToJPG: () => void };
|
|
890
|
+
};
|
|
837
891
|
}
|
|
838
892
|
|
|
839
|
-
|
|
840
|
-
|
|
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;
|
|
893
|
+
interface CarbonChartComponent {
|
|
894
|
+
chart?: CarbonChartCore;
|
|
855
895
|
}
|
|
856
896
|
|
|
857
|
-
function
|
|
858
|
-
|
|
859
|
-
|
|
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;
|
|
895
|
-
}
|
|
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
|
|
903
|
-
|
|
904
|
-
if (!wrapper) return;
|
|
905
|
-
const svg = wrapper.querySelector('svg.layout-svg-wrapper');
|
|
906
|
-
|
|
907
|
-
if (!svg) return;
|
|
908
|
-
|
|
909
|
-
const svgRect = svg.getBoundingClientRect();
|
|
910
|
-
const legendItems = readLegendItems(wrapper);
|
|
911
|
-
const scale = 2;
|
|
902
|
+
const domUtils = chart?.services?.domUtils;
|
|
912
903
|
|
|
913
|
-
|
|
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;
|
|
904
|
+
if (!domUtils) return;
|
|
922
905
|
|
|
923
|
-
|
|
906
|
+
const magmaWrapper = toolbarWrapper?.classList.contains('has-magma-toolbar')
|
|
907
|
+
? toolbarWrapper
|
|
908
|
+
: null;
|
|
924
909
|
|
|
925
|
-
|
|
926
|
-
tempCtx.font = `${fontSize}px sans-serif`;
|
|
927
|
-
let x = paddingX;
|
|
928
|
-
let rows = 1;
|
|
910
|
+
magmaWrapper?.classList.remove('has-magma-toolbar');
|
|
929
911
|
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
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
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
919
|
+
try {
|
|
920
|
+
if (format === 'jpg') {
|
|
921
|
+
domUtils.exportToJPG();
|
|
922
|
+
} else {
|
|
923
|
+
domUtils.exportToPNG();
|
|
939
924
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1035
|
-
}, [
|
|
1009
|
+
exportImage('png');
|
|
1010
|
+
}, [exportImage]);
|
|
1036
1011
|
|
|
1037
1012
|
const handleDownloadJpg = React.useCallback(() => {
|
|
1038
|
-
|
|
1039
|
-
}, [
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
1182
|
+
requestAnimationFrame(() => {
|
|
1199
1183
|
lastTableTriggerRef.current?.focus();
|
|
1200
|
-
}
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
1389
|
+
scale: colorScale,
|
|
1423
1390
|
},
|
|
1424
1391
|
tooltip: {
|
|
1425
1392
|
...(options?.tooltip || {}),
|
|
@@ -1427,10 +1394,18 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1427
1394
|
type: 'none',
|
|
1428
1395
|
},
|
|
1429
1396
|
},
|
|
1430
|
-
...(chartToolbar
|
|
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 =
|
|
1408
|
+
const ChartType = ALL_CHARTS[type];
|
|
1434
1409
|
|
|
1435
1410
|
React.useEffect(() => {
|
|
1436
1411
|
const timer = setTimeout(() => {
|
|
@@ -1513,12 +1488,107 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1513
1488
|
return () => observer.disconnect();
|
|
1514
1489
|
}, [type, dataSet]);
|
|
1515
1490
|
|
|
1516
|
-
|
|
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;
|
|
1517
1580
|
|
|
1518
1581
|
const showTable = chartToolbar?.showAsTable !== false;
|
|
1519
1582
|
|
|
1583
|
+
if (!ChartType) return null;
|
|
1584
|
+
|
|
1520
1585
|
return (
|
|
1521
|
-
<FullscreenRoot
|
|
1586
|
+
<FullscreenRoot
|
|
1587
|
+
ref={mergedRef}
|
|
1588
|
+
isFlowLayout={isFlowLayout}
|
|
1589
|
+
isInverse={isInverse}
|
|
1590
|
+
theme={theme}
|
|
1591
|
+
>
|
|
1522
1592
|
<VisuallyHidden>
|
|
1523
1593
|
<Announce>{legendAnnouncement}</Announce>
|
|
1524
1594
|
</VisuallyHidden>
|
|
@@ -1529,14 +1599,16 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1529
1599
|
data-testid={testId}
|
|
1530
1600
|
isInverse={isInverse}
|
|
1531
1601
|
theme={theme}
|
|
1532
|
-
className={`carbon-chart-wrapper${
|
|
1602
|
+
className={`carbon-chart-wrapper${
|
|
1603
|
+
chartToolbar ? ' has-magma-toolbar' : ''
|
|
1604
|
+
}`}
|
|
1533
1605
|
groupsLength={groupsLength < 6 ? groupsLength : 14}
|
|
1534
1606
|
role="region"
|
|
1535
1607
|
aria-label={ariaLabel || chartTitle}
|
|
1536
1608
|
aria-roledescription="chart"
|
|
1537
1609
|
{...rest}
|
|
1538
1610
|
>
|
|
1539
|
-
<ChartContentWrapper>
|
|
1611
|
+
<ChartContentWrapper className="carbon-chart-content">
|
|
1540
1612
|
{chartToolbar && (
|
|
1541
1613
|
/*
|
|
1542
1614
|
* Slot content comes from the adopter, so it resolves
|
|
@@ -1546,8 +1618,10 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1546
1618
|
*/
|
|
1547
1619
|
<InverseContext.Provider value={{ isInverse }}>
|
|
1548
1620
|
<CarbonChartToolbar
|
|
1621
|
+
chartRef={chartComponentRef}
|
|
1549
1622
|
config={chartToolbar}
|
|
1550
1623
|
dataSet={dataSet as Array<Record<string, unknown>>}
|
|
1624
|
+
isFlowLayout={isFlowLayout}
|
|
1551
1625
|
isInverse={isInverse}
|
|
1552
1626
|
isTableOpen={isTableOpen}
|
|
1553
1627
|
isFullscreen={isFullscreen}
|
|
@@ -1557,9 +1631,16 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1557
1631
|
title={chartTitle}
|
|
1558
1632
|
wrapperRef={internalRef}
|
|
1559
1633
|
/>
|
|
1634
|
+
{isFlowLayout && (
|
|
1635
|
+
<ChartSlot theme={theme}>{additionalContent}</ChartSlot>
|
|
1636
|
+
)}
|
|
1560
1637
|
</InverseContext.Provider>
|
|
1561
1638
|
)}
|
|
1562
|
-
<ChartType
|
|
1639
|
+
<ChartType
|
|
1640
|
+
ref={chartComponentRef}
|
|
1641
|
+
data={dataSet}
|
|
1642
|
+
options={newOptions}
|
|
1643
|
+
/>
|
|
1563
1644
|
</ChartContentWrapper>
|
|
1564
1645
|
</CarbonChartWrapper>
|
|
1565
1646
|
{chartToolbar && showTable && (
|