@react-magma/charts 14.0.0-next.3 → 14.0.0-next.5
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 +793 -69
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +789 -72
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +2546 -434
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/CarbonChart/CarbonChart.d.ts +46 -0
- package/dist/components/CarbonChart/CarbonChartBar.stories.d.ts +27 -9
- package/dist/components/ChartTable/ChartDataTable.d.ts +19 -0
- package/dist/components/ChartTable/ChartFullscreenButton.d.ts +26 -0
- package/dist/components/ChartTable/ChartMoreOptionsButton.d.ts +18 -0
- package/dist/components/ChartTable/ChartTable.stories.d.ts +116 -0
- package/dist/components/ChartTable/ChartTableButton.d.ts +24 -0
- package/dist/components/ChartTable/ChartTableModal.d.ts +44 -0
- package/dist/components/ChartTable/ChartToolbar.d.ts +19 -0
- package/dist/components/ChartTable/chartToolbarI18n.d.ts +17 -0
- package/dist/components/ChartTable/index.d.ts +14 -0
- package/dist/components/LineChart/DataTable.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/package.json +5 -5
- package/src/components/CarbonChart/CarbonChart.test.js +541 -5
- package/src/components/CarbonChart/CarbonChart.tsx +1054 -60
- package/src/components/CarbonChart/CarbonChartArea.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartAreaStacked.stories.tsx +1 -1
- package/src/components/CarbonChart/CarbonChartBar.stories.tsx +9 -2
- 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/ChartTable/ChartDataTable.tsx +72 -0
- package/src/components/ChartTable/ChartFullscreenButton.tsx +59 -0
- package/src/components/ChartTable/ChartMoreOptionsButton.tsx +48 -0
- package/src/components/ChartTable/ChartTable.stories.tsx +152 -0
- package/src/components/ChartTable/ChartTable.test.js +444 -0
- package/src/components/ChartTable/ChartTableButton.tsx +55 -0
- package/src/components/ChartTable/ChartTableModal.tsx +135 -0
- package/src/components/ChartTable/ChartToolbar.tsx +50 -0
- package/src/components/ChartTable/chartToolbarI18n.ts +59 -0
- package/src/components/ChartTable/index.ts +23 -0
- package/src/components/LineChart/DataTable.tsx +3 -3
- package/src/components/LineChart/LineChart.tsx +1 -1
- package/src/index.ts +1 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable complexity */
|
|
1
2
|
import * as React from 'react';
|
|
2
3
|
|
|
3
4
|
import {
|
|
@@ -23,10 +24,34 @@ import {
|
|
|
23
24
|
} from '@carbon/charts-react';
|
|
24
25
|
import styled from '@emotion/styled';
|
|
25
26
|
import { transparentize } from 'polished';
|
|
26
|
-
import {
|
|
27
|
+
import {
|
|
28
|
+
Announce,
|
|
29
|
+
DropdownDivider,
|
|
30
|
+
DropdownMenuItem,
|
|
31
|
+
ThemeInterface,
|
|
32
|
+
ThemeContext,
|
|
33
|
+
useDeviceDetect,
|
|
34
|
+
useIsInverse,
|
|
35
|
+
VisuallyHidden,
|
|
36
|
+
} from 'react-magma-dom';
|
|
37
|
+
import {
|
|
38
|
+
FullscreenExitIcon,
|
|
39
|
+
FullscreenIcon,
|
|
40
|
+
MoreVertIcon,
|
|
41
|
+
TableIcon,
|
|
42
|
+
} from 'react-magma-icons';
|
|
27
43
|
|
|
28
44
|
import { useCarbonModalFocusManagement } from '../../hooks/useCarbonModalFocusManagement';
|
|
45
|
+
// @ts-ignore
|
|
29
46
|
import './carbon-charts.css';
|
|
47
|
+
import {
|
|
48
|
+
ChartFullscreenButton,
|
|
49
|
+
ChartMoreOptionsButton,
|
|
50
|
+
ChartTableButton,
|
|
51
|
+
ChartTableModal,
|
|
52
|
+
} from '../ChartTable';
|
|
53
|
+
import type { ChartDataTableColumn } from '../ChartTable';
|
|
54
|
+
import { useChartToolbarI18n } from '../ChartTable/chartToolbarI18n';
|
|
30
55
|
|
|
31
56
|
export enum CarbonChartType {
|
|
32
57
|
area = 'area',
|
|
@@ -49,6 +74,46 @@ export enum CarbonChartType {
|
|
|
49
74
|
combo = 'combo',
|
|
50
75
|
}
|
|
51
76
|
|
|
77
|
+
export interface ChartToolbarConfig {
|
|
78
|
+
/**
|
|
79
|
+
* When true, renders a "Show as table" button that opens a Magma Modal
|
|
80
|
+
* with the chart data in an accessible table.
|
|
81
|
+
* @default true
|
|
82
|
+
*/
|
|
83
|
+
showAsTable?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* When true, renders a fullscreen toggle button.
|
|
86
|
+
* @default true
|
|
87
|
+
*/
|
|
88
|
+
fullscreen?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Additional menu items rendered inside a "More options" dropdown,
|
|
91
|
+
* below the built-in "Download as CSV", "Download as PNG", and "Download as JPG" items.
|
|
92
|
+
* Pass DropdownMenuItem elements.
|
|
93
|
+
*/
|
|
94
|
+
moreOptions?: React.ReactNode;
|
|
95
|
+
/**
|
|
96
|
+
* Custom column definitions for the table modal.
|
|
97
|
+
* If omitted, columns are auto-derived from the dataset object keys.
|
|
98
|
+
*/
|
|
99
|
+
tableColumns?: ChartDataTableColumn[];
|
|
100
|
+
/**
|
|
101
|
+
* First line of the modal heading.
|
|
102
|
+
* @default "Tabular representation"
|
|
103
|
+
*/
|
|
104
|
+
tableHeaderLabel?: string;
|
|
105
|
+
/**
|
|
106
|
+
* Heading level for the modal header.
|
|
107
|
+
* @default 2
|
|
108
|
+
*/
|
|
109
|
+
tableHeaderLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
110
|
+
/**
|
|
111
|
+
* Heading level for the chart title.
|
|
112
|
+
* @default 2
|
|
113
|
+
*/
|
|
114
|
+
titleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
115
|
+
}
|
|
116
|
+
|
|
52
117
|
export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
53
118
|
dataSet: Array<Object>;
|
|
54
119
|
isInverse?: boolean;
|
|
@@ -69,13 +134,72 @@ export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
69
134
|
* Text for the aria-label attribute for main SVG container, if provided
|
|
70
135
|
*/
|
|
71
136
|
ariaLabel?: string;
|
|
137
|
+
/**
|
|
138
|
+
* When provided, renders an accessible Magma toolbar above the chart with
|
|
139
|
+
* "Show as table", fullscreen, and "More options" buttons. Carbon's built-in
|
|
140
|
+
* toolbar is automatically disabled.
|
|
141
|
+
*/
|
|
142
|
+
chartToolbar?: ChartToolbarConfig;
|
|
72
143
|
}
|
|
73
144
|
|
|
145
|
+
const ChartContentWrapper = styled.div`
|
|
146
|
+
height: 100%;
|
|
147
|
+
position: relative;
|
|
148
|
+
`;
|
|
149
|
+
|
|
150
|
+
const FullscreenRoot = styled.div<{
|
|
151
|
+
isInverse?: boolean;
|
|
152
|
+
theme: ThemeInterface;
|
|
153
|
+
}>`
|
|
154
|
+
height: 100%;
|
|
155
|
+
width: 100%;
|
|
156
|
+
|
|
157
|
+
&:fullscreen,
|
|
158
|
+
&:-webkit-full-screen {
|
|
159
|
+
background: ${props =>
|
|
160
|
+
props.isInverse
|
|
161
|
+
? props.theme.colors.primary700
|
|
162
|
+
: props.theme.colors.neutral100};
|
|
163
|
+
.cds--chart-holder {
|
|
164
|
+
height: 100vh !important;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
`;
|
|
168
|
+
|
|
74
169
|
const CarbonChartWrapper = styled.div<{
|
|
75
170
|
isInverse?: boolean;
|
|
76
171
|
groupsLength: number;
|
|
77
172
|
theme: ThemeInterface;
|
|
78
173
|
}>`
|
|
174
|
+
.cds--cc--legend-fieldset {
|
|
175
|
+
border: 0;
|
|
176
|
+
margin: 0;
|
|
177
|
+
min-inline-size: 0;
|
|
178
|
+
padding: 0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.cds--cc--legend-fieldset > legend {
|
|
182
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
183
|
+
height: 1px;
|
|
184
|
+
overflow: hidden;
|
|
185
|
+
position: absolute;
|
|
186
|
+
top: auto;
|
|
187
|
+
white-space: nowrap;
|
|
188
|
+
width: 1px;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
&:fullscreen,
|
|
192
|
+
&:-webkit-full-screen {
|
|
193
|
+
background: ${props =>
|
|
194
|
+
props.isInverse
|
|
195
|
+
? props.theme.colors.primary700
|
|
196
|
+
: props.theme.colors.neutral100};
|
|
197
|
+
|
|
198
|
+
.cds--chart-holder {
|
|
199
|
+
height: 100vh !important;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
79
203
|
.cds--data-table thead tr th {
|
|
80
204
|
background: ${props =>
|
|
81
205
|
props.isInverse ? props.theme.colors.primary700 : ''} !important;
|
|
@@ -131,9 +255,9 @@ const CarbonChartWrapper = styled.div<{
|
|
|
131
255
|
}
|
|
132
256
|
}
|
|
133
257
|
|
|
134
|
-
p,
|
|
135
|
-
div,
|
|
136
|
-
text,
|
|
258
|
+
.chart-holder p,
|
|
259
|
+
.chart-holder div,
|
|
260
|
+
.chart-holder text,
|
|
137
261
|
.cds--cc--axes g.axis .axis-title,
|
|
138
262
|
.cds--cc--title p.title,
|
|
139
263
|
.cds--cc--axes g.axis g.tick text {
|
|
@@ -392,6 +516,9 @@ const CarbonChartWrapper = styled.div<{
|
|
|
392
516
|
: props.theme.colors.focus} !important;
|
|
393
517
|
outline-offset: 0;
|
|
394
518
|
}
|
|
519
|
+
circle.dot:focus {
|
|
520
|
+
outline: none !important;
|
|
521
|
+
}
|
|
395
522
|
.cds--overflow-menu-options__btn:focus,
|
|
396
523
|
.cds--overflow-menu:focus,
|
|
397
524
|
.cds--overflow-menu__trigger:focus,
|
|
@@ -488,10 +615,25 @@ const CarbonChartWrapper = styled.div<{
|
|
|
488
615
|
}
|
|
489
616
|
}
|
|
490
617
|
|
|
618
|
+
&.has-magma-toolbar .cds--cc--title p.title {
|
|
619
|
+
visibility: hidden;
|
|
620
|
+
}
|
|
621
|
+
|
|
491
622
|
svg:not(:root) {
|
|
492
623
|
overflow: visible;
|
|
493
624
|
}
|
|
494
625
|
|
|
626
|
+
circle.dot:focus {
|
|
627
|
+
outline: none;
|
|
628
|
+
stroke: ${props =>
|
|
629
|
+
props.isInverse
|
|
630
|
+
? props.theme.colors.focusInverse
|
|
631
|
+
: props.theme.colors.focus} !important;
|
|
632
|
+
stroke-width: 6px !important;
|
|
633
|
+
stroke-opacity: 1 !important;
|
|
634
|
+
paint-order: stroke fill;
|
|
635
|
+
}
|
|
636
|
+
|
|
495
637
|
.cds--cc--chart-wrapper text {
|
|
496
638
|
font-size: ${props => props.theme.typeScale.size02.fontSize};
|
|
497
639
|
}
|
|
@@ -535,6 +677,436 @@ interface ColorsObject {
|
|
|
535
677
|
[key: string]: string;
|
|
536
678
|
}
|
|
537
679
|
|
|
680
|
+
interface ExtendedChartOptions extends ChartOptions {
|
|
681
|
+
title?: string;
|
|
682
|
+
colors?: string[];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const ToolbarWrapper = styled.div<{
|
|
686
|
+
isFullscreen?: boolean;
|
|
687
|
+
isInverse?: boolean;
|
|
688
|
+
theme: ThemeInterface;
|
|
689
|
+
}>`
|
|
690
|
+
align-items: center;
|
|
691
|
+
display: flex;
|
|
692
|
+
justify-content: space-between;
|
|
693
|
+
left: ${props => (props.isFullscreen ? '2em' : '0')};
|
|
694
|
+
position: absolute;
|
|
695
|
+
right: ${props => (props.isFullscreen ? '2em' : '0')};
|
|
696
|
+
top: ${props => (props.isFullscreen ? '2em' : '0')};
|
|
697
|
+
z-index: 2;
|
|
698
|
+
|
|
699
|
+
button {
|
|
700
|
+
color: ${props =>
|
|
701
|
+
props.isInverse
|
|
702
|
+
? props.theme.colors.neutral100
|
|
703
|
+
: props.theme.colors.primary500};
|
|
704
|
+
|
|
705
|
+
&:focus {
|
|
706
|
+
outline: 2px solid
|
|
707
|
+
${props =>
|
|
708
|
+
props.isInverse
|
|
709
|
+
? props.theme.colors.focusInverse
|
|
710
|
+
: props.theme.colors.focus};
|
|
711
|
+
outline-offset: 0;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
`;
|
|
715
|
+
|
|
716
|
+
const ChartTitle = styled.h2<{
|
|
717
|
+
isInverse?: boolean;
|
|
718
|
+
theme: ThemeInterface;
|
|
719
|
+
}>`
|
|
720
|
+
font-family: ${props => props.theme.bodyFont} !important;
|
|
721
|
+
font-size: 16px !important;
|
|
722
|
+
font-weight: 700 !important;
|
|
723
|
+
letter-spacing: normal !important;
|
|
724
|
+
line-height: 1.25 !important;
|
|
725
|
+
margin: 0 !important;
|
|
726
|
+
color: ${props =>
|
|
727
|
+
props.isInverse
|
|
728
|
+
? props.theme.colors.neutral100
|
|
729
|
+
: props.theme.colors.neutral700} !important;
|
|
730
|
+
`;
|
|
731
|
+
|
|
732
|
+
const ToolbarActions = styled.div<{ theme: ThemeInterface }>`
|
|
733
|
+
align-items: center;
|
|
734
|
+
display: flex;
|
|
735
|
+
gap: ${props => props.theme.spaceScale.spacing02};
|
|
736
|
+
|
|
737
|
+
> * button,
|
|
738
|
+
> button {
|
|
739
|
+
height: 2rem;
|
|
740
|
+
min-height: 2rem;
|
|
741
|
+
min-width: 2rem;
|
|
742
|
+
padding: 4px;
|
|
743
|
+
width: 2rem;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
[role='tooltip'] {
|
|
747
|
+
padding: ${props => props.theme.spaceScale.spacing03}
|
|
748
|
+
${props => props.theme.spaceScale.spacing04};
|
|
749
|
+
text-align: center;
|
|
750
|
+
white-space: nowrap;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
[data-testid='dropdownContent'] {
|
|
754
|
+
padding: ${props => props.theme.spaceScale.spacing03} 0;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
[role='menuitem'],
|
|
758
|
+
[data-testid='dropdownMenuItem'] {
|
|
759
|
+
padding: ${props => props.theme.spaceScale.spacing03}
|
|
760
|
+
${props => props.theme.spaceScale.spacing05};
|
|
761
|
+
}
|
|
762
|
+
`;
|
|
763
|
+
|
|
764
|
+
function sanitizeCsvValue(value: string): string {
|
|
765
|
+
const trimmed = value.trimStart();
|
|
766
|
+
if (/^[=+\-@\t\r]/.test(trimmed)) {
|
|
767
|
+
return `'${value}`;
|
|
768
|
+
}
|
|
769
|
+
return value;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function downloadCsv(dataSet: Array<Record<string, unknown>>, title: string) {
|
|
773
|
+
if (!dataSet.length) return;
|
|
774
|
+
const keys = Object.keys(dataSet[0]);
|
|
775
|
+
const header = keys.map(k => sanitizeCsvValue(k)).join(',');
|
|
776
|
+
const rows = dataSet.map(row =>
|
|
777
|
+
keys
|
|
778
|
+
.map(k => {
|
|
779
|
+
const v = row[k];
|
|
780
|
+
const s = sanitizeCsvValue(String(v ?? ''));
|
|
781
|
+
return s.includes(',') || s.includes('"') || s.includes('\n')
|
|
782
|
+
? `"${s.replace(/"/g, '""')}"`
|
|
783
|
+
: s;
|
|
784
|
+
})
|
|
785
|
+
.join(',')
|
|
786
|
+
);
|
|
787
|
+
const csv = [header, ...rows].join('\n');
|
|
788
|
+
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
|
|
789
|
+
const url = URL.createObjectURL(blob);
|
|
790
|
+
const a = document.createElement('a');
|
|
791
|
+
a.href = url;
|
|
792
|
+
a.download = `${title || 'chart-data'}.csv`;
|
|
793
|
+
a.click();
|
|
794
|
+
URL.revokeObjectURL(url);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function inlineStyles(source: Element, target: Element) {
|
|
798
|
+
const computed = window.getComputedStyle(source);
|
|
799
|
+
if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
800
|
+
target.setAttribute(
|
|
801
|
+
'style',
|
|
802
|
+
Array.from(computed)
|
|
803
|
+
.map(prop => `${prop}:${computed.getPropertyValue(prop)}`)
|
|
804
|
+
.join(';')
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
for (let i = 0; i < source.children.length; i++) {
|
|
808
|
+
if (target.children[i]) {
|
|
809
|
+
inlineStyles(source.children[i], target.children[i]);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
interface LegendItem {
|
|
815
|
+
label: string;
|
|
816
|
+
color: string;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function readLegendItems(wrapper: HTMLElement): LegendItem[] {
|
|
820
|
+
const items: LegendItem[] = [];
|
|
821
|
+
wrapper.querySelectorAll('.legend-item').forEach(item => {
|
|
822
|
+
const checkbox = item.querySelector<HTMLElement>('.checkbox');
|
|
823
|
+
const label = item.querySelector('p');
|
|
824
|
+
if (checkbox && label) {
|
|
825
|
+
items.push({
|
|
826
|
+
color: checkbox.style.background || checkbox.style.backgroundColor,
|
|
827
|
+
label: label.textContent || '',
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
return items;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function drawLegend(
|
|
835
|
+
ctx: CanvasRenderingContext2D,
|
|
836
|
+
items: LegendItem[],
|
|
837
|
+
startY: number,
|
|
838
|
+
canvasWidth: number,
|
|
839
|
+
scale: number
|
|
840
|
+
) {
|
|
841
|
+
const fontSize = 13 * scale;
|
|
842
|
+
const swatchSize = 12 * scale;
|
|
843
|
+
const gap = 8 * scale;
|
|
844
|
+
const itemGap = 16 * scale;
|
|
845
|
+
const paddingX = 16 * scale;
|
|
846
|
+
|
|
847
|
+
ctx.font = `${fontSize}px sans-serif`;
|
|
848
|
+
ctx.textBaseline = 'middle';
|
|
849
|
+
|
|
850
|
+
let x = paddingX;
|
|
851
|
+
let y = startY;
|
|
852
|
+
|
|
853
|
+
for (const item of items) {
|
|
854
|
+
const textWidth = ctx.measureText(item.label).width;
|
|
855
|
+
const itemWidth = swatchSize + gap + textWidth + itemGap;
|
|
856
|
+
|
|
857
|
+
if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
|
|
858
|
+
x = paddingX;
|
|
859
|
+
y += fontSize + gap;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
ctx.fillStyle = item.color;
|
|
863
|
+
ctx.fillRect(x, y - swatchSize / 2, swatchSize, swatchSize);
|
|
864
|
+
|
|
865
|
+
ctx.fillStyle = '#161616';
|
|
866
|
+
ctx.fillText(item.label, x + swatchSize + gap, y);
|
|
867
|
+
|
|
868
|
+
x += itemWidth;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
return y + fontSize + gap;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function downloadImage(
|
|
875
|
+
wrapperRef: React.RefObject<HTMLDivElement | null>,
|
|
876
|
+
title: string,
|
|
877
|
+
format: 'png' | 'jpg'
|
|
878
|
+
) {
|
|
879
|
+
const wrapper = wrapperRef.current;
|
|
880
|
+
if (!wrapper) return;
|
|
881
|
+
const svg = wrapper.querySelector('svg.layout-svg-wrapper');
|
|
882
|
+
if (!svg) return;
|
|
883
|
+
|
|
884
|
+
const svgRect = svg.getBoundingClientRect();
|
|
885
|
+
const legendItems = readLegendItems(wrapper);
|
|
886
|
+
|
|
887
|
+
const doWork = () => {
|
|
888
|
+
const scale = 2;
|
|
889
|
+
|
|
890
|
+
// Measure legend height
|
|
891
|
+
const tempCanvas = document.createElement('canvas');
|
|
892
|
+
const tempCtx = tempCanvas.getContext('2d');
|
|
893
|
+
const fontSize = 13 * scale;
|
|
894
|
+
const swatchSize = 12 * scale;
|
|
895
|
+
const gap = 8 * scale;
|
|
896
|
+
const itemGap = 16 * scale;
|
|
897
|
+
const paddingX = 16 * scale;
|
|
898
|
+
const canvasWidth = svgRect.width * scale;
|
|
899
|
+
|
|
900
|
+
let legendHeight = 0;
|
|
901
|
+
if (legendItems.length > 0 && tempCtx) {
|
|
902
|
+
tempCtx.font = `${fontSize}px sans-serif`;
|
|
903
|
+
let x = paddingX;
|
|
904
|
+
let rows = 1;
|
|
905
|
+
for (const item of legendItems) {
|
|
906
|
+
const textWidth = tempCtx.measureText(item.label).width;
|
|
907
|
+
const itemWidth = swatchSize + gap + textWidth + itemGap;
|
|
908
|
+
if (x + itemWidth > canvasWidth - paddingX && x > paddingX) {
|
|
909
|
+
x = paddingX;
|
|
910
|
+
rows++;
|
|
911
|
+
}
|
|
912
|
+
x += itemWidth;
|
|
913
|
+
}
|
|
914
|
+
legendHeight = rows * (fontSize + gap) + gap * 2;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
const width = svgRect.width * scale;
|
|
918
|
+
const height = svgRect.height * scale + legendHeight;
|
|
919
|
+
|
|
920
|
+
const clone = svg.cloneNode(true) as SVGSVGElement;
|
|
921
|
+
clone.setAttribute('width', String(svgRect.width));
|
|
922
|
+
clone.setAttribute('height', String(svgRect.height));
|
|
923
|
+
clone.setAttribute('viewBox', `0 0 ${svgRect.width} ${svgRect.height}`);
|
|
924
|
+
clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
|
925
|
+
|
|
926
|
+
inlineStyles(svg, clone);
|
|
927
|
+
|
|
928
|
+
const serializer = new XMLSerializer();
|
|
929
|
+
const svgString = serializer.serializeToString(clone);
|
|
930
|
+
const svgBlob = new Blob([svgString], {
|
|
931
|
+
type: 'image/svg+xml;charset=utf-8',
|
|
932
|
+
});
|
|
933
|
+
const url = URL.createObjectURL(svgBlob);
|
|
934
|
+
|
|
935
|
+
const mimeType = format === 'jpg' ? 'image/jpeg' : 'image/png';
|
|
936
|
+
const ext = format === 'jpg' ? 'jpg' : 'png';
|
|
937
|
+
|
|
938
|
+
const img = new Image();
|
|
939
|
+
img.onload = () => {
|
|
940
|
+
const canvas = document.createElement('canvas');
|
|
941
|
+
canvas.width = width;
|
|
942
|
+
canvas.height = height;
|
|
943
|
+
const ctx = canvas.getContext('2d');
|
|
944
|
+
if (!ctx) return;
|
|
945
|
+
|
|
946
|
+
ctx.fillStyle = '#ffffff';
|
|
947
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
948
|
+
ctx.drawImage(img, 0, 0, svgRect.width * scale, svgRect.height * scale);
|
|
949
|
+
URL.revokeObjectURL(url);
|
|
950
|
+
|
|
951
|
+
if (legendItems.length > 0) {
|
|
952
|
+
drawLegend(
|
|
953
|
+
ctx,
|
|
954
|
+
legendItems,
|
|
955
|
+
svgRect.height * scale + gap,
|
|
956
|
+
width,
|
|
957
|
+
scale
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
canvas.toBlob(blob => {
|
|
962
|
+
if (!blob) return;
|
|
963
|
+
const imgUrl = URL.createObjectURL(blob);
|
|
964
|
+
const a = document.createElement('a');
|
|
965
|
+
a.href = imgUrl;
|
|
966
|
+
a.download = `${title || 'chart'}.${ext}`;
|
|
967
|
+
a.click();
|
|
968
|
+
URL.revokeObjectURL(imgUrl);
|
|
969
|
+
}, mimeType);
|
|
970
|
+
};
|
|
971
|
+
img.onerror = () => URL.revokeObjectURL(url);
|
|
972
|
+
img.src = url;
|
|
973
|
+
};
|
|
974
|
+
|
|
975
|
+
// Defer to idle time with a 2-second deadline so it always runs.
|
|
976
|
+
if (typeof requestIdleCallback === 'function') {
|
|
977
|
+
requestIdleCallback(doWork, { timeout: 2000 });
|
|
978
|
+
} else {
|
|
979
|
+
setTimeout(doWork, 0);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
const ALL_CHARTS: Record<CarbonChartType, React.ComponentType<any>> = {
|
|
984
|
+
area: AreaChart,
|
|
985
|
+
areaStacked: StackedAreaChart,
|
|
986
|
+
bar: SimpleBarChart,
|
|
987
|
+
barGrouped: GroupedBarChart,
|
|
988
|
+
barStacked: StackedBarChart,
|
|
989
|
+
donut: DonutChart,
|
|
990
|
+
line: LineChart,
|
|
991
|
+
lollipop: LollipopChart,
|
|
992
|
+
pie: PieChart,
|
|
993
|
+
radar: RadarChart,
|
|
994
|
+
boxplot: BoxplotChart,
|
|
995
|
+
bubble: BubbleChart,
|
|
996
|
+
bullet: BulletChart,
|
|
997
|
+
gauge: GaugeChart,
|
|
998
|
+
histogram: HistogramChart,
|
|
999
|
+
meter: MeterChart,
|
|
1000
|
+
scatter: ScatterChart,
|
|
1001
|
+
combo: ComboChart,
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
interface InternalToolbarProps {
|
|
1005
|
+
config: ChartToolbarConfig;
|
|
1006
|
+
dataSet: Array<Record<string, unknown>>;
|
|
1007
|
+
isInverse: boolean;
|
|
1008
|
+
isTableOpen: boolean;
|
|
1009
|
+
isFullscreen: boolean;
|
|
1010
|
+
onOpenTable: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
|
1011
|
+
onToggleFullscreen: () => void;
|
|
1012
|
+
theme: ThemeInterface;
|
|
1013
|
+
title: string;
|
|
1014
|
+
wrapperRef: React.RefObject<HTMLDivElement | null>;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
function CarbonChartToolbar({
|
|
1018
|
+
config,
|
|
1019
|
+
dataSet,
|
|
1020
|
+
isInverse,
|
|
1021
|
+
isTableOpen,
|
|
1022
|
+
isFullscreen,
|
|
1023
|
+
onOpenTable,
|
|
1024
|
+
onToggleFullscreen,
|
|
1025
|
+
theme,
|
|
1026
|
+
title,
|
|
1027
|
+
wrapperRef,
|
|
1028
|
+
}: InternalToolbarProps) {
|
|
1029
|
+
const t = useChartToolbarI18n();
|
|
1030
|
+
const showTable = config.showAsTable !== false;
|
|
1031
|
+
const showFullscreen = config.fullscreen !== false;
|
|
1032
|
+
const resolvedTitle = title || t.defaultTitle;
|
|
1033
|
+
|
|
1034
|
+
const handleDownloadCsv = React.useCallback(() => {
|
|
1035
|
+
downloadCsv(dataSet, title);
|
|
1036
|
+
}, [dataSet, title]);
|
|
1037
|
+
|
|
1038
|
+
const handleDownloadPng = React.useCallback(() => {
|
|
1039
|
+
downloadImage(wrapperRef, title, 'png');
|
|
1040
|
+
}, [wrapperRef, title]);
|
|
1041
|
+
|
|
1042
|
+
const handleDownloadJpg = React.useCallback(() => {
|
|
1043
|
+
downloadImage(wrapperRef, title, 'jpg');
|
|
1044
|
+
}, [wrapperRef, title]);
|
|
1045
|
+
|
|
1046
|
+
const moreOptionsContent = (
|
|
1047
|
+
<>
|
|
1048
|
+
<DropdownMenuItem onClick={handleDownloadCsv}>
|
|
1049
|
+
{t.downloadAsCsv}
|
|
1050
|
+
</DropdownMenuItem>
|
|
1051
|
+
<DropdownMenuItem onClick={handleDownloadPng}>
|
|
1052
|
+
{t.downloadAsPng}
|
|
1053
|
+
</DropdownMenuItem>
|
|
1054
|
+
<DropdownMenuItem onClick={handleDownloadJpg}>
|
|
1055
|
+
{t.downloadAsJpg}
|
|
1056
|
+
</DropdownMenuItem>
|
|
1057
|
+
{config.moreOptions && (
|
|
1058
|
+
<>
|
|
1059
|
+
<DropdownDivider />
|
|
1060
|
+
{config.moreOptions}
|
|
1061
|
+
</>
|
|
1062
|
+
)}
|
|
1063
|
+
</>
|
|
1064
|
+
);
|
|
1065
|
+
|
|
1066
|
+
return (
|
|
1067
|
+
<ToolbarWrapper
|
|
1068
|
+
isFullscreen={isFullscreen}
|
|
1069
|
+
isInverse={isInverse}
|
|
1070
|
+
theme={theme}
|
|
1071
|
+
>
|
|
1072
|
+
<ChartTitle
|
|
1073
|
+
as={`h${config.titleLevel ?? 2}` as keyof JSX.IntrinsicElements}
|
|
1074
|
+
isInverse={isInverse}
|
|
1075
|
+
theme={theme}
|
|
1076
|
+
>
|
|
1077
|
+
{resolvedTitle}
|
|
1078
|
+
</ChartTitle>
|
|
1079
|
+
<ToolbarActions theme={theme}>
|
|
1080
|
+
{showTable && (
|
|
1081
|
+
<ChartTableButton
|
|
1082
|
+
ariaLabel={resolvedTitle}
|
|
1083
|
+
icon={<TableIcon size={20} />}
|
|
1084
|
+
isInverse={isInverse}
|
|
1085
|
+
isTableOpen={isTableOpen}
|
|
1086
|
+
onClick={onOpenTable}
|
|
1087
|
+
/>
|
|
1088
|
+
)}
|
|
1089
|
+
{showFullscreen && (
|
|
1090
|
+
<ChartFullscreenButton
|
|
1091
|
+
ariaLabel={`${isFullscreen ? 'Exit' : 'View'} ${resolvedTitle} full screen`}
|
|
1092
|
+
icon={<FullscreenIcon size={20} />}
|
|
1093
|
+
exitIcon={<FullscreenExitIcon size={20} />}
|
|
1094
|
+
isInverse={isInverse}
|
|
1095
|
+
isFullscreen={isFullscreen}
|
|
1096
|
+
onClick={onToggleFullscreen}
|
|
1097
|
+
/>
|
|
1098
|
+
)}
|
|
1099
|
+
<ChartMoreOptionsButton
|
|
1100
|
+
icon={<MoreVertIcon size={20} />}
|
|
1101
|
+
isInverse={isInverse}
|
|
1102
|
+
>
|
|
1103
|
+
{moreOptionsContent}
|
|
1104
|
+
</ChartMoreOptionsButton>
|
|
1105
|
+
</ToolbarActions>
|
|
1106
|
+
</ToolbarWrapper>
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
538
1110
|
export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
539
1111
|
(props, ref) => {
|
|
540
1112
|
const {
|
|
@@ -544,12 +1116,23 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
544
1116
|
dataSet,
|
|
545
1117
|
options,
|
|
546
1118
|
ariaLabel,
|
|
1119
|
+
chartToolbar,
|
|
547
1120
|
...rest
|
|
548
1121
|
} = props;
|
|
549
1122
|
const theme = React.useContext(ThemeContext);
|
|
550
1123
|
const isInverse = useIsInverse(isInverseProp);
|
|
1124
|
+
const toolbarI18n = useChartToolbarI18n();
|
|
551
1125
|
const internalRef = React.useRef<HTMLDivElement | null>(null);
|
|
552
1126
|
|
|
1127
|
+
const [isTableOpen, setIsTableOpen] = React.useState(false);
|
|
1128
|
+
const [isFullscreen, setIsFullscreen] = React.useState(false);
|
|
1129
|
+
const [legendAnnouncement, setLegendAnnouncement] = React.useState('');
|
|
1130
|
+
const legendGroupAnnounceRef = React.useRef<HTMLDivElement | null>(null);
|
|
1131
|
+
const lastTableTriggerRef = React.useRef<HTMLButtonElement | null>(null);
|
|
1132
|
+
const legendAnnouncedRef = React.useRef(false);
|
|
1133
|
+
const selectionSignatureRef = React.useRef<string | undefined>(undefined);
|
|
1134
|
+
const { isMacOS, isChrome } = useDeviceDetect();
|
|
1135
|
+
|
|
553
1136
|
const mergedRef = React.useCallback(
|
|
554
1137
|
(node: HTMLDivElement | null) => {
|
|
555
1138
|
internalRef.current = node;
|
|
@@ -562,58 +1145,259 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
562
1145
|
[ref]
|
|
563
1146
|
);
|
|
564
1147
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
areaStacked: StackedAreaChart,
|
|
569
|
-
bar: SimpleBarChart,
|
|
570
|
-
barGrouped: GroupedBarChart,
|
|
571
|
-
barStacked: StackedBarChart,
|
|
572
|
-
donut: DonutChart,
|
|
573
|
-
line: LineChart,
|
|
574
|
-
lollipop: LollipopChart,
|
|
575
|
-
pie: PieChart,
|
|
576
|
-
radar: RadarChart,
|
|
577
|
-
boxplot: BoxplotChart,
|
|
578
|
-
bubble: BubbleChart,
|
|
579
|
-
bullet: BulletChart,
|
|
580
|
-
gauge: GaugeChart,
|
|
581
|
-
histogram: HistogramChart,
|
|
582
|
-
meter: MeterChart,
|
|
583
|
-
scatter: ScatterChart,
|
|
584
|
-
combo: ComboChart,
|
|
585
|
-
};
|
|
1148
|
+
const fullscreenEnabled = chartToolbar
|
|
1149
|
+
? chartToolbar.fullscreen !== false
|
|
1150
|
+
: false;
|
|
586
1151
|
|
|
587
|
-
|
|
588
|
-
|
|
1152
|
+
const savedHeightRef = React.useRef<string>('');
|
|
1153
|
+
|
|
1154
|
+
React.useEffect(() => {
|
|
1155
|
+
if (!fullscreenEnabled) return;
|
|
589
1156
|
|
|
590
|
-
const
|
|
591
|
-
|
|
1157
|
+
const onFullscreenChange = () => {
|
|
1158
|
+
const isFs = !!document.fullscreenElement;
|
|
1159
|
+
setIsFullscreen(isFs);
|
|
1160
|
+
|
|
1161
|
+
const chartHolder =
|
|
1162
|
+
internalRef.current?.querySelector<HTMLElement>('.cds--chart-holder');
|
|
1163
|
+
if (chartHolder) {
|
|
1164
|
+
if (isFs) {
|
|
1165
|
+
savedHeightRef.current = chartHolder.style.height;
|
|
1166
|
+
chartHolder.style.height = '100vh';
|
|
1167
|
+
} else {
|
|
1168
|
+
chartHolder.style.height = savedHeightRef.current;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
document.addEventListener('fullscreenchange', onFullscreenChange);
|
|
1173
|
+
return () => {
|
|
1174
|
+
document.removeEventListener('fullscreenchange', onFullscreenChange);
|
|
1175
|
+
// Restore height if the component unmounts while in fullscreen.
|
|
1176
|
+
const chartHolder =
|
|
1177
|
+
internalRef.current?.querySelector<HTMLElement>('.cds--chart-holder');
|
|
1178
|
+
if (chartHolder && document.fullscreenElement) {
|
|
1179
|
+
chartHolder.style.height = savedHeightRef.current;
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
}, [fullscreenEnabled]);
|
|
1183
|
+
|
|
1184
|
+
const openTableModal = React.useCallback(
|
|
1185
|
+
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
1186
|
+
lastTableTriggerRef.current = event.currentTarget;
|
|
1187
|
+
setIsTableOpen(true);
|
|
1188
|
+
},
|
|
1189
|
+
[]
|
|
1190
|
+
);
|
|
1191
|
+
|
|
1192
|
+
const closeTableModal = React.useCallback(() => {
|
|
1193
|
+
setIsTableOpen(false);
|
|
1194
|
+
requestAnimationFrame(() => {
|
|
1195
|
+
lastTableTriggerRef.current?.focus();
|
|
592
1196
|
});
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
1197
|
+
}, []);
|
|
1198
|
+
|
|
1199
|
+
const toggleFullscreen = React.useCallback(() => {
|
|
1200
|
+
if (!document.fullscreenElement && internalRef.current) {
|
|
1201
|
+
internalRef.current.requestFullscreen().catch(() => {
|
|
1202
|
+
// Fullscreen request may be denied by the browser or user agent
|
|
1203
|
+
});
|
|
1204
|
+
} else if (document.fullscreenElement) {
|
|
1205
|
+
document.exitFullscreen().catch(() => {
|
|
1206
|
+
// Exit fullscreen may fail if already exited
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
}, []);
|
|
1210
|
+
|
|
1211
|
+
const chartTitle: string =
|
|
1212
|
+
(options as ExtendedChartOptions).title || toolbarI18n.defaultTitle;
|
|
1213
|
+
|
|
1214
|
+
const legendLabel = `${ariaLabel || chartTitle}. ${toolbarI18n.legendInstructions}`;
|
|
1215
|
+
|
|
1216
|
+
React.useEffect(() => {
|
|
1217
|
+
const container = internalRef.current;
|
|
1218
|
+
|
|
1219
|
+
if (!container) return;
|
|
1220
|
+
|
|
1221
|
+
const applyListSemantics = (legend: Element) => {
|
|
1222
|
+
const itemsHost = legend.matches('[data-name="legend-items"]')
|
|
1223
|
+
? legend
|
|
1224
|
+
: legend.querySelector('[data-name="legend-items"]') || legend;
|
|
1225
|
+
|
|
1226
|
+
itemsHost.removeAttribute('aria-label');
|
|
1227
|
+
|
|
1228
|
+
if (itemsHost.getAttribute('role') !== 'list') {
|
|
1229
|
+
itemsHost.setAttribute('role', 'list');
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
legend.querySelectorAll('.legend-item').forEach(item => {
|
|
1233
|
+
if (item.getAttribute('role') !== 'listitem') {
|
|
1234
|
+
item.setAttribute('role', 'listitem');
|
|
1235
|
+
}
|
|
1236
|
+
});
|
|
1237
|
+
|
|
1238
|
+
container.querySelectorAll('[role="list"]').forEach(el => {
|
|
1239
|
+
if (el !== itemsHost && el.contains(itemsHost)) {
|
|
1240
|
+
el.removeAttribute('role');
|
|
1241
|
+
}
|
|
1242
|
+
});
|
|
1243
|
+
container.querySelectorAll('[aria-label="Data groups"]').forEach(el => {
|
|
1244
|
+
if (el !== itemsHost && el.contains(itemsHost)) {
|
|
1245
|
+
el.removeAttribute('aria-label');
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
const unwrapFieldset = (fieldset: Element) => {
|
|
1251
|
+
fieldset.querySelector(':scope > legend')?.remove();
|
|
1252
|
+
fieldset.replaceWith(...Array.from(fieldset.childNodes));
|
|
1253
|
+
};
|
|
1254
|
+
|
|
1255
|
+
const wrapLegend = () => {
|
|
1256
|
+
const legend = container.querySelector('.cds--cc--legend');
|
|
1257
|
+
|
|
1258
|
+
if (!legend) return;
|
|
1259
|
+
|
|
1260
|
+
applyListSemantics(legend);
|
|
1261
|
+
|
|
1262
|
+
const closestFieldset = legend.closest(
|
|
1263
|
+
'fieldset.cds--cc--legend-fieldset'
|
|
1264
|
+
);
|
|
1265
|
+
|
|
1266
|
+
container
|
|
1267
|
+
.querySelectorAll('fieldset.cds--cc--legend-fieldset')
|
|
1268
|
+
.forEach(fieldset => {
|
|
1269
|
+
if (fieldset !== closestFieldset) {
|
|
1270
|
+
unwrapFieldset(fieldset);
|
|
1271
|
+
}
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
if (closestFieldset) {
|
|
1275
|
+
const existingCaption =
|
|
1276
|
+
closestFieldset.querySelector(':scope > legend');
|
|
1277
|
+
|
|
1278
|
+
if (existingCaption && existingCaption.textContent !== legendLabel) {
|
|
1279
|
+
existingCaption.textContent = legendLabel;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const fieldset = document.createElement('fieldset');
|
|
1286
|
+
const caption = document.createElement('legend');
|
|
1287
|
+
|
|
1288
|
+
fieldset.className = 'cds--cc--legend-fieldset';
|
|
1289
|
+
caption.textContent = legendLabel;
|
|
1290
|
+
legend.before(fieldset);
|
|
1291
|
+
fieldset.append(caption, legend);
|
|
1292
|
+
};
|
|
1293
|
+
|
|
1294
|
+
wrapLegend();
|
|
1295
|
+
const observer = new MutationObserver(wrapLegend);
|
|
1296
|
+
|
|
1297
|
+
observer.observe(container, {
|
|
1298
|
+
childList: true,
|
|
1299
|
+
subtree: true,
|
|
1300
|
+
attributes: true,
|
|
1301
|
+
attributeFilter: ['role', 'aria-label'],
|
|
1302
|
+
});
|
|
1303
|
+
|
|
1304
|
+
return () => observer.disconnect();
|
|
1305
|
+
}, [legendLabel]);
|
|
1306
|
+
|
|
1307
|
+
React.useEffect(() => {
|
|
1308
|
+
const container = internalRef.current;
|
|
1309
|
+
|
|
1310
|
+
if (!container || !isMacOS || !isChrome) return;
|
|
1311
|
+
|
|
1312
|
+
let announceTimer: ReturnType<typeof setTimeout>;
|
|
1313
|
+
let alternateSuffix = false;
|
|
1314
|
+
|
|
1315
|
+
const closestLegend = (node: EventTarget | null) =>
|
|
1316
|
+
node instanceof Element ? node.closest('.cds--cc--legend') : null;
|
|
1317
|
+
|
|
1318
|
+
const handleFocusIn = (event: FocusEvent) => {
|
|
1319
|
+
const enteredLegend = closestLegend(event.target);
|
|
1320
|
+
const cameFromLegend = closestLegend(event.relatedTarget);
|
|
1321
|
+
|
|
1322
|
+
if (enteredLegend && !cameFromLegend) {
|
|
1323
|
+
if (legendGroupAnnounceRef.current) {
|
|
1324
|
+
legendGroupAnnounceRef.current.textContent = '';
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
clearTimeout(announceTimer);
|
|
1328
|
+
announceTimer = setTimeout(() => {
|
|
1329
|
+
if (legendGroupAnnounceRef.current) {
|
|
1330
|
+
alternateSuffix = !alternateSuffix;
|
|
1331
|
+
legendGroupAnnounceRef.current.textContent =
|
|
1332
|
+
legendLabel + (alternateSuffix ? ' ' : '');
|
|
1333
|
+
}
|
|
1334
|
+
}, 250);
|
|
1335
|
+
}
|
|
1336
|
+
};
|
|
1337
|
+
|
|
1338
|
+
const handleFocusOut = (event: FocusEvent) => {
|
|
1339
|
+
const leftLegend =
|
|
1340
|
+
closestLegend(event.target) && !closestLegend(event.relatedTarget);
|
|
1341
|
+
|
|
1342
|
+
// Reset once focus leaves the legend so re-entering announces again.
|
|
1343
|
+
if (leftLegend) {
|
|
1344
|
+
clearTimeout(announceTimer);
|
|
1345
|
+
|
|
1346
|
+
if (legendGroupAnnounceRef.current) {
|
|
1347
|
+
legendGroupAnnounceRef.current.textContent = '';
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
container.addEventListener('focusin', handleFocusIn);
|
|
1353
|
+
container.addEventListener('focusout', handleFocusOut);
|
|
1354
|
+
|
|
1355
|
+
return () => {
|
|
1356
|
+
clearTimeout(announceTimer);
|
|
1357
|
+
container.removeEventListener('focusin', handleFocusIn);
|
|
1358
|
+
container.removeEventListener('focusout', handleFocusOut);
|
|
1359
|
+
};
|
|
1360
|
+
}, [isMacOS, isChrome, legendLabel]);
|
|
1361
|
+
|
|
1362
|
+
const handleModalDownloadCsv = React.useCallback(() => {
|
|
1363
|
+
downloadCsv(dataSet as Array<Record<string, unknown>>, chartTitle);
|
|
1364
|
+
}, [dataSet, chartTitle]);
|
|
1365
|
+
|
|
1366
|
+
useCarbonModalFocusManagement(internalRef);
|
|
1367
|
+
|
|
1368
|
+
const colorScale = React.useMemo(() => {
|
|
1369
|
+
const scaleColorsObj: ColorsObject = {};
|
|
1370
|
+
const customColors = (options as ExtendedChartOptions).colors || [];
|
|
597
1371
|
const allColors = [...customColors, ...theme.chartColors];
|
|
598
1372
|
const allInverseColors = [...customColors, ...theme.chartColorsInverse];
|
|
1373
|
+
const allGroups = dataSet.map(item =>
|
|
1374
|
+
'group' in item ? (item as Record<string, unknown>)['group'] : null
|
|
1375
|
+
);
|
|
1376
|
+
const uniqueGroups = Array.from(new Set(allGroups));
|
|
599
1377
|
|
|
600
|
-
uniqueGroups.
|
|
601
|
-
|
|
602
|
-
|
|
1378
|
+
if (uniqueGroups.length <= allColors.length) {
|
|
1379
|
+
for (let i = 0; i < uniqueGroups.length; i++) {
|
|
1380
|
+
const group = uniqueGroups[i];
|
|
1381
|
+
scaleColorsObj[String(group ?? 'null')] = isInverse
|
|
603
1382
|
? allInverseColors[i]
|
|
604
|
-
: allColors[i]
|
|
1383
|
+
: allColors[i];
|
|
605
1384
|
}
|
|
606
|
-
|
|
607
|
-
});
|
|
1385
|
+
}
|
|
608
1386
|
|
|
609
1387
|
return scaleColorsObj;
|
|
610
|
-
}
|
|
1388
|
+
}, [
|
|
1389
|
+
dataSet,
|
|
1390
|
+
options,
|
|
1391
|
+
theme.chartColors,
|
|
1392
|
+
theme.chartColorsInverse,
|
|
1393
|
+
isInverse,
|
|
1394
|
+
]);
|
|
611
1395
|
|
|
612
1396
|
const newOptions = {
|
|
613
1397
|
...options,
|
|
614
1398
|
theme: isInverse ? ChartTheme.G100 : ChartTheme.WHITE,
|
|
615
1399
|
color: {
|
|
616
|
-
scale:
|
|
1400
|
+
scale: colorScale,
|
|
617
1401
|
},
|
|
618
1402
|
tooltip: {
|
|
619
1403
|
...(options?.tooltip || {}),
|
|
@@ -621,33 +1405,243 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
621
1405
|
type: 'none',
|
|
622
1406
|
},
|
|
623
1407
|
},
|
|
1408
|
+
...(chartToolbar ? { toolbar: { enabled: false } } : {}),
|
|
624
1409
|
};
|
|
625
1410
|
|
|
626
|
-
const ChartType =
|
|
1411
|
+
const ChartType = ALL_CHARTS[type];
|
|
627
1412
|
|
|
628
|
-
// Adding aria-label to main SVG container
|
|
629
1413
|
React.useEffect(() => {
|
|
630
|
-
if (ariaLabel)
|
|
631
|
-
|
|
632
|
-
|
|
1414
|
+
if (!ariaLabel) return;
|
|
1415
|
+
const rafId = requestAnimationFrame(() => {
|
|
1416
|
+
const svgEl = internalRef.current?.querySelector('.graph-frame');
|
|
1417
|
+
if (!svgEl) return;
|
|
1418
|
+
svgEl.setAttribute('aria-label', ariaLabel);
|
|
1419
|
+
});
|
|
1420
|
+
return () => cancelAnimationFrame(rafId);
|
|
1421
|
+
}, [ariaLabel]);
|
|
1422
|
+
|
|
1423
|
+
// Make Carbon Charts data points keyboard-focusable.
|
|
1424
|
+
React.useEffect(() => {
|
|
1425
|
+
const wrapper = internalRef.current;
|
|
1426
|
+
if (!wrapper) return;
|
|
1427
|
+
|
|
1428
|
+
const isDot = (el: EventTarget | null): el is SVGCircleElement =>
|
|
1429
|
+
el instanceof Element &&
|
|
1430
|
+
el.nodeName.toLowerCase() === 'circle' &&
|
|
1431
|
+
el.classList.contains('dot');
|
|
1432
|
+
|
|
1433
|
+
const onFocusIn = (e: FocusEvent) => {
|
|
1434
|
+
if (!isDot(e.target)) return;
|
|
1435
|
+
const dot = e.target as SVGCircleElement;
|
|
1436
|
+
dot.style.opacity = '1';
|
|
1437
|
+
const { left, top, width, height } = dot.getBoundingClientRect();
|
|
1438
|
+
const cx = left + width / 2;
|
|
1439
|
+
const cy = top + height / 2;
|
|
1440
|
+
dot.dispatchEvent(
|
|
1441
|
+
new MouseEvent('mouseover', {
|
|
1442
|
+
bubbles: true,
|
|
1443
|
+
clientX: cx,
|
|
1444
|
+
clientY: cy,
|
|
1445
|
+
screenX: cx,
|
|
1446
|
+
screenY: cy,
|
|
1447
|
+
})
|
|
1448
|
+
);
|
|
1449
|
+
dot.dispatchEvent(
|
|
1450
|
+
new MouseEvent('mousemove', {
|
|
1451
|
+
bubbles: true,
|
|
1452
|
+
clientX: cx,
|
|
1453
|
+
clientY: cy,
|
|
1454
|
+
screenX: cx,
|
|
1455
|
+
screenY: cy,
|
|
1456
|
+
})
|
|
1457
|
+
);
|
|
1458
|
+
};
|
|
1459
|
+
|
|
1460
|
+
const onFocusOut = (e: FocusEvent) => {
|
|
1461
|
+
if (!isDot(e.target)) return;
|
|
1462
|
+
const dot = e.target;
|
|
1463
|
+
dot.style.opacity = '';
|
|
1464
|
+
dot.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
|
|
1465
|
+
};
|
|
1466
|
+
|
|
1467
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
1468
|
+
if (!isDot(e.target)) return;
|
|
1469
|
+
if (e.key !== 'Enter' && e.key !== ' ') return;
|
|
1470
|
+
e.preventDefault();
|
|
1471
|
+
const dot = e.target;
|
|
1472
|
+
const { left, top, width, height } = dot.getBoundingClientRect();
|
|
1473
|
+
const cx = left + width / 2;
|
|
1474
|
+
const cy = top + height / 2;
|
|
1475
|
+
dot.dispatchEvent(
|
|
1476
|
+
new MouseEvent('click', {
|
|
1477
|
+
bubbles: true,
|
|
1478
|
+
clientX: cx,
|
|
1479
|
+
clientY: cy,
|
|
1480
|
+
})
|
|
1481
|
+
);
|
|
1482
|
+
};
|
|
1483
|
+
|
|
1484
|
+
const rafId = requestAnimationFrame(() => {
|
|
1485
|
+
wrapper
|
|
1486
|
+
.querySelectorAll<SVGCircleElement>('circle.dot')
|
|
1487
|
+
.forEach(dot => {
|
|
1488
|
+
if (!dot.hasAttribute('tabindex')) {
|
|
1489
|
+
dot.setAttribute('tabindex', '0');
|
|
1490
|
+
}
|
|
1491
|
+
});
|
|
1492
|
+
});
|
|
1493
|
+
|
|
1494
|
+
wrapper.addEventListener('focusin', onFocusIn);
|
|
1495
|
+
wrapper.addEventListener('focusout', onFocusOut);
|
|
1496
|
+
wrapper.addEventListener('keydown', onKeyDown);
|
|
1497
|
+
|
|
1498
|
+
return () => {
|
|
1499
|
+
cancelAnimationFrame(rafId);
|
|
1500
|
+
wrapper.removeEventListener('focusin', onFocusIn);
|
|
1501
|
+
wrapper.removeEventListener('focusout', onFocusOut);
|
|
1502
|
+
wrapper.removeEventListener('keydown', onKeyDown);
|
|
1503
|
+
};
|
|
1504
|
+
}, [type]);
|
|
1505
|
+
|
|
1506
|
+
React.useEffect(() => {
|
|
1507
|
+
const timer = setTimeout(() => {
|
|
1508
|
+
if (internalRef.current) {
|
|
1509
|
+
internalRef.current
|
|
1510
|
+
.querySelectorAll<SVGGElement>('g[aria-label]')
|
|
1511
|
+
.forEach(g => {
|
|
1512
|
+
const role = g.getAttribute('role');
|
|
1513
|
+
|
|
1514
|
+
if (!role) {
|
|
1515
|
+
g.setAttribute('role', 'img');
|
|
1516
|
+
} else if (role === 'graphics-object group') {
|
|
1517
|
+
g.setAttribute('role', 'graphics-object');
|
|
1518
|
+
}
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
}, 0);
|
|
1522
|
+
|
|
1523
|
+
return () => clearTimeout(timer);
|
|
1524
|
+
}, [type, dataSet]);
|
|
1525
|
+
|
|
1526
|
+
React.useEffect(() => {
|
|
1527
|
+
const wrapper = internalRef.current;
|
|
1528
|
+
|
|
1529
|
+
if (!wrapper) return;
|
|
1530
|
+
|
|
1531
|
+
const observer = new MutationObserver(() => {
|
|
1532
|
+
const allItems = wrapper.querySelectorAll<HTMLElement>(
|
|
1533
|
+
'.legend-item:not(.additional)'
|
|
1534
|
+
);
|
|
1535
|
+
|
|
1536
|
+
if (allItems.length <= 1) return;
|
|
1537
|
+
|
|
1538
|
+
const activeLabels: string[] = [];
|
|
1539
|
+
|
|
1540
|
+
allItems.forEach(el => {
|
|
1541
|
+
const checkbox = el.querySelector<HTMLElement>('.checkbox');
|
|
1542
|
+
const label = el.querySelector('p');
|
|
1543
|
+
|
|
1544
|
+
if (checkbox?.getAttribute('aria-checked') === 'true' && label) {
|
|
1545
|
+
activeLabels.push(label.textContent?.trim() || '');
|
|
1546
|
+
}
|
|
633
1547
|
});
|
|
634
|
-
}
|
|
635
|
-
});
|
|
636
1548
|
|
|
637
|
-
|
|
1549
|
+
// Carbon re-applies aria-checked (with the same values) on every
|
|
1550
|
+
// render, which still triggers this observer. Only announce when the
|
|
1551
|
+
// selection actually changed, otherwise unrelated re-renders (e.g.
|
|
1552
|
+
// the legend group announcement) cause phantom announcements.
|
|
1553
|
+
const selectionSignature = `${activeLabels.join('|')}::${allItems.length}`;
|
|
1554
|
+
|
|
1555
|
+
if (selectionSignatureRef.current === selectionSignature) return;
|
|
1556
|
+
|
|
1557
|
+
const isFirstRun = selectionSignatureRef.current === undefined;
|
|
1558
|
+
|
|
1559
|
+
selectionSignatureRef.current = selectionSignature;
|
|
1560
|
+
|
|
1561
|
+
if (isFirstRun) return;
|
|
1562
|
+
|
|
1563
|
+
if (activeLabels.length === 1) {
|
|
1564
|
+
if (!legendAnnouncedRef.current) {
|
|
1565
|
+
legendAnnouncedRef.current = true;
|
|
1566
|
+
setLegendAnnouncement(`Only ${activeLabels[0]} is selected`);
|
|
1567
|
+
}
|
|
1568
|
+
} else if (activeLabels.length === allItems.length) {
|
|
1569
|
+
legendAnnouncedRef.current = false;
|
|
1570
|
+
setLegendAnnouncement('All items selected');
|
|
1571
|
+
} else {
|
|
1572
|
+
legendAnnouncedRef.current = false;
|
|
1573
|
+
setLegendAnnouncement('');
|
|
1574
|
+
}
|
|
1575
|
+
});
|
|
1576
|
+
|
|
1577
|
+
observer.observe(wrapper, {
|
|
1578
|
+
childList: true,
|
|
1579
|
+
subtree: true,
|
|
1580
|
+
attributes: true,
|
|
1581
|
+
attributeFilter: ['aria-checked'],
|
|
1582
|
+
});
|
|
1583
|
+
|
|
1584
|
+
return () => observer.disconnect();
|
|
1585
|
+
}, [type, dataSet]);
|
|
1586
|
+
|
|
1587
|
+
const groupsLength = Object.keys(colorScale).length;
|
|
1588
|
+
|
|
1589
|
+
const showTable = chartToolbar?.showAsTable !== false;
|
|
1590
|
+
|
|
1591
|
+
if (!ChartType) return null;
|
|
638
1592
|
|
|
639
1593
|
return (
|
|
640
|
-
<
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
1594
|
+
<FullscreenRoot ref={mergedRef} isInverse={isInverse} theme={theme}>
|
|
1595
|
+
<VisuallyHidden>
|
|
1596
|
+
<Announce>{legendAnnouncement}</Announce>
|
|
1597
|
+
</VisuallyHidden>
|
|
1598
|
+
<VisuallyHidden>
|
|
1599
|
+
<div aria-live="polite" ref={legendGroupAnnounceRef} />
|
|
1600
|
+
</VisuallyHidden>
|
|
1601
|
+
<CarbonChartWrapper
|
|
1602
|
+
data-testid={testId}
|
|
1603
|
+
isInverse={isInverse}
|
|
1604
|
+
theme={theme}
|
|
1605
|
+
className={`carbon-chart-wrapper${chartToolbar ? ' has-magma-toolbar' : ''}`}
|
|
1606
|
+
groupsLength={groupsLength < 6 ? groupsLength : 14}
|
|
1607
|
+
role="region"
|
|
1608
|
+
aria-label={ariaLabel || chartTitle}
|
|
1609
|
+
aria-roledescription="chart"
|
|
1610
|
+
{...rest}
|
|
1611
|
+
>
|
|
1612
|
+
<ChartContentWrapper>
|
|
1613
|
+
{chartToolbar && (
|
|
1614
|
+
<CarbonChartToolbar
|
|
1615
|
+
config={chartToolbar}
|
|
1616
|
+
dataSet={dataSet as Array<Record<string, unknown>>}
|
|
1617
|
+
isInverse={isInverse}
|
|
1618
|
+
isTableOpen={isTableOpen}
|
|
1619
|
+
isFullscreen={isFullscreen}
|
|
1620
|
+
onOpenTable={openTableModal}
|
|
1621
|
+
onToggleFullscreen={toggleFullscreen}
|
|
1622
|
+
theme={theme}
|
|
1623
|
+
title={chartTitle}
|
|
1624
|
+
wrapperRef={internalRef}
|
|
1625
|
+
/>
|
|
1626
|
+
)}
|
|
1627
|
+
<ChartType data={dataSet} options={newOptions} />
|
|
1628
|
+
</ChartContentWrapper>
|
|
1629
|
+
</CarbonChartWrapper>
|
|
1630
|
+
{chartToolbar && showTable && (
|
|
1631
|
+
<ChartTableModal
|
|
1632
|
+
columns={chartToolbar.tableColumns}
|
|
1633
|
+
portalContainer={isFullscreen ? internalRef.current : undefined}
|
|
1634
|
+
dataSet={dataSet as Array<Record<string, React.ReactNode>>}
|
|
1635
|
+
headerLabel={chartToolbar.tableHeaderLabel}
|
|
1636
|
+
headerLevel={chartToolbar.tableHeaderLevel}
|
|
1637
|
+
isInverse={isInverse}
|
|
1638
|
+
isOpen={isTableOpen}
|
|
1639
|
+
onClose={closeTableModal}
|
|
1640
|
+
onDownloadCsv={handleModalDownloadCsv}
|
|
1641
|
+
title={chartTitle}
|
|
1642
|
+
/>
|
|
1643
|
+
)}
|
|
1644
|
+
</FullscreenRoot>
|
|
651
1645
|
);
|
|
652
1646
|
}
|
|
653
1647
|
);
|