@react-magma/charts 13.0.0 → 13.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/charts.js +2450 -1
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +2428 -1
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +49165 -1
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/CarbonChart/CarbonChart.d.ts +2 -1
- package/dist/components/CarbonChart/CarbonChartArea.stories.d.ts +310 -0
- package/dist/components/CarbonChart/CarbonChartAreaStacked.stories.d.ts +136 -0
- package/dist/components/CarbonChart/CarbonChartBar.stories.d.ts +214 -0
- package/dist/components/CarbonChart/CarbonChartBarFloating.stories.d.ts +82 -0
- package/dist/components/CarbonChart/CarbonChartBarGrouped.stories.d.ts +234 -0
- package/dist/components/CarbonChart/CarbonChartBarStacked.stories.d.ts +266 -0
- package/dist/components/CarbonChart/CarbonChartBoxplot.stories.d.ts +53 -0
- package/dist/components/CarbonChart/CarbonChartBubble.stories.d.ts +196 -0
- package/dist/components/CarbonChart/CarbonChartBullet.stories.d.ts +35 -0
- package/dist/components/CarbonChart/CarbonChartCombo.stories.d.ts +580 -0
- package/dist/components/CarbonChart/CarbonChartDonut.stories.d.ts +120 -0
- package/dist/components/CarbonChart/CarbonChartGauge.stories.d.ts +43 -0
- package/dist/components/CarbonChart/CarbonChartHistogram.stories.d.ts +89 -0
- package/dist/components/CarbonChart/CarbonChartLine.stories.d.ts +577 -0
- package/dist/components/CarbonChart/CarbonChartLollipop.stories.d.ts +55 -0
- package/dist/components/CarbonChart/CarbonChartMeter.stories.d.ts +127 -0
- package/dist/components/CarbonChart/CarbonChartPie.stories.d.ts +101 -0
- package/dist/components/CarbonChart/CarbonChartRadar.stories.d.ts +98 -0
- package/dist/components/CarbonChart/CarbonChartScatter.stories.d.ts +173 -0
- package/dist/components/CarbonChart/CarbonChartSparkline.stories.d.ts +51 -0
- package/dist/components/CarbonChart/CarbonChartStep.stories.d.ts +38 -0
- package/dist/components/LineChart/Chart.d.ts +27 -0
- package/dist/components/LineChart/ChartDataTable.d.ts +17 -0
- package/dist/components/LineChart/CustomAxisComponent.d.ts +3 -0
- package/dist/components/LineChart/CustomPointComponent.d.ts +17 -0
- package/dist/components/LineChart/DataTable.d.ts +5 -0
- package/dist/components/LineChart/GraphTooltip.d.ts +3 -0
- package/dist/components/LineChart/LegendButton.d.ts +6 -0
- package/dist/components/LineChart/LineChart.d.ts +67 -0
- package/dist/components/LineChart/LineChart.stories.d.ts +113 -0
- package/dist/components/LineChart/index.d.ts +1 -0
- package/dist/components/LineChart/magma-charts.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/package.json +28 -26
- package/src/components/CarbonChart/CarbonChart.test.js +506 -0
- package/src/components/CarbonChart/CarbonChart.tsx +53 -57
- 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 +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 +1 -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 +24931 -0
- package/src/components/LineChart/Chart.tsx +241 -0
- package/src/components/LineChart/ChartDataTable.test.js +165 -0
- package/src/components/LineChart/ChartDataTable.tsx +135 -0
- package/src/components/LineChart/CustomAxisComponent.tsx +30 -0
- package/src/components/LineChart/CustomPointComponent.tsx +93 -0
- package/src/components/LineChart/DataTable.tsx +57 -0
- package/src/components/LineChart/GraphTooltip.tsx +100 -0
- package/src/components/LineChart/LegendButton.tsx +77 -0
- package/src/components/LineChart/LineChart.stories.tsx +399 -0
- package/src/components/LineChart/LineChart.test.js +501 -0
- package/src/components/LineChart/LineChart.tsx +669 -0
- package/src/components/LineChart/index.ts +1 -0
- package/src/components/LineChart/magma-charts.ts +279 -0
- package/src/components/LineChart/test/exampleChartData.js +289 -0
- package/src/index.ts +1 -0
- package/dist/components/CarbonChart/CarbonChart.test.d.ts +0 -1
- package/dist/components/CarbonChart/embeddedStyles.d.ts +0 -1
- package/src/components/CarbonChart/embeddedStyles.ts +0 -24880
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { css } from '@emotion/react';
|
|
4
|
+
import {
|
|
5
|
+
Announce,
|
|
6
|
+
ButtonVariant,
|
|
7
|
+
Card,
|
|
8
|
+
I18nContext,
|
|
9
|
+
IconButton,
|
|
10
|
+
Paragraph,
|
|
11
|
+
Tab,
|
|
12
|
+
TabPanel,
|
|
13
|
+
TabPanelsContainer,
|
|
14
|
+
Tabs,
|
|
15
|
+
TabsContainer,
|
|
16
|
+
ThemeContext,
|
|
17
|
+
Tooltip,
|
|
18
|
+
TypographyVisualStyle,
|
|
19
|
+
useDescendants,
|
|
20
|
+
styled,
|
|
21
|
+
} from 'react-magma-dom';
|
|
22
|
+
import { KeyboardIcon } from 'react-magma-icons';
|
|
23
|
+
|
|
24
|
+
import { ChartDataTable } from './ChartDataTable';
|
|
25
|
+
import { LineChart, LineChartProps } from './LineChart';
|
|
26
|
+
|
|
27
|
+
interface BaseChartProps {
|
|
28
|
+
/**
|
|
29
|
+
* Description of what the line chart data represents placed above the chart
|
|
30
|
+
*/
|
|
31
|
+
description?: string;
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
testId?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Title of the line chart
|
|
38
|
+
*/
|
|
39
|
+
title: string;
|
|
40
|
+
/**
|
|
41
|
+
* Type of chart - for now just 'line' is accepted
|
|
42
|
+
*/
|
|
43
|
+
type: string;
|
|
44
|
+
}
|
|
45
|
+
export interface ChartProps<T extends any>
|
|
46
|
+
extends BaseChartProps,
|
|
47
|
+
Omit<React.HTMLAttributes<HTMLDivElement>, 'title'>,
|
|
48
|
+
LineChartProps<T> {}
|
|
49
|
+
|
|
50
|
+
const StyledTitle = styled.span`
|
|
51
|
+
color: ${props => props.theme.colors.neutral};
|
|
52
|
+
font-size: ${props => props.theme.typeScale.size04.fontSize};
|
|
53
|
+
font-weight: 600;
|
|
54
|
+
font-family: ${props => props.theme.bodyFont};
|
|
55
|
+
line-height: ${props => props.theme.typeScale.size04.lineHeight};
|
|
56
|
+
margin: 0 0 12px 0;
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
const StyledParagraph = styled(Paragraph)`
|
|
60
|
+
font-size: ${props => props.theme.typeScale.size02.fontSize};
|
|
61
|
+
margin: 0 0 18px 0;
|
|
62
|
+
`;
|
|
63
|
+
|
|
64
|
+
const StyledTabsContainer = styled(TabsContainer)`
|
|
65
|
+
width: 800px;
|
|
66
|
+
ul {
|
|
67
|
+
box-shadow: inset 0 -1px 0 ${props => props.theme.colors.neutral300};
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
const StyledTabPanel = styled(TabPanel)`
|
|
72
|
+
padding: 22px 0;
|
|
73
|
+
`;
|
|
74
|
+
|
|
75
|
+
const KeyboardInstructionsCard = styled(Card)<{
|
|
76
|
+
isOpen?: boolean;
|
|
77
|
+
maxHeight?: string;
|
|
78
|
+
width?: string;
|
|
79
|
+
}>`
|
|
80
|
+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
|
|
81
|
+
display: ${props => (props.isOpen ? 'block' : 'none')};
|
|
82
|
+
right: ${props => props.theme.spaceScale.spacing02};
|
|
83
|
+
max-height: ${props =>
|
|
84
|
+
props.maxHeight ? props.maxHeight : props.theme.dropdown.content.maxHeight};
|
|
85
|
+
opacity: ${props => (props.isOpen ? '1' : '0')};
|
|
86
|
+
outline: 0;
|
|
87
|
+
overflow-y: auto;
|
|
88
|
+
padding: ${props => props.theme.spaceScale.spacing05}
|
|
89
|
+
${props => props.theme.spaceScale.spacing05};
|
|
90
|
+
position: absolute;
|
|
91
|
+
transition: opacity 0.3s;
|
|
92
|
+
white-space: nowrap;
|
|
93
|
+
z-index: 2;
|
|
94
|
+
|
|
95
|
+
${props =>
|
|
96
|
+
props.width &&
|
|
97
|
+
css`
|
|
98
|
+
white-space: normal;
|
|
99
|
+
width: ${props.width};
|
|
100
|
+
`}
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
function BaseChart<T>(props: ChartProps<T>, ref: React.Ref<HTMLDivElement>) {
|
|
104
|
+
const { description, title, testId, type, ...other } = props;
|
|
105
|
+
const keyboardInstructionsRef = React.useRef<HTMLButtonElement>(null);
|
|
106
|
+
const lastFocusedScatterPoint = React.useRef<SVGPathElement>(null);
|
|
107
|
+
const theme = React.useContext(ThemeContext);
|
|
108
|
+
const i18n = React.useContext(I18nContext);
|
|
109
|
+
|
|
110
|
+
const [pointRefArray, registerPoint, unregisterPoint] = useDescendants();
|
|
111
|
+
|
|
112
|
+
const [isKeyboardInstructionsOpen, setIsKeyboardInstructionsOpen] =
|
|
113
|
+
React.useState<boolean>(false);
|
|
114
|
+
|
|
115
|
+
function handleKeyboardInstructionsButtonBlur() {
|
|
116
|
+
setIsKeyboardInstructionsOpen(false);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function handleKeyboardInstructionsButtonClick() {
|
|
120
|
+
setIsKeyboardInstructionsOpen(prevOpen => !prevOpen);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function handleKeyboardInstructionsButtonKeydown(event: {
|
|
124
|
+
preventDefault?: any;
|
|
125
|
+
key?: any;
|
|
126
|
+
shiftKey?: any;
|
|
127
|
+
}) {
|
|
128
|
+
const { key, shiftKey } = event;
|
|
129
|
+
|
|
130
|
+
switch (key) {
|
|
131
|
+
case 'Escape': {
|
|
132
|
+
setIsKeyboardInstructionsOpen(false);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case 'Tab': {
|
|
136
|
+
if (
|
|
137
|
+
!shiftKey &&
|
|
138
|
+
lastFocusedScatterPoint &&
|
|
139
|
+
lastFocusedScatterPoint.current &&
|
|
140
|
+
pointRefArray.current.find(
|
|
141
|
+
point => point.current === lastFocusedScatterPoint.current
|
|
142
|
+
)
|
|
143
|
+
) {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
lastFocusedScatterPoint.current.focus();
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<div ref={ref}>
|
|
154
|
+
<StyledTitle theme={theme}>{title}</StyledTitle>
|
|
155
|
+
{description && (
|
|
156
|
+
<StyledParagraph
|
|
157
|
+
theme={theme}
|
|
158
|
+
visualStyle={TypographyVisualStyle.bodySmall}
|
|
159
|
+
>
|
|
160
|
+
{description}
|
|
161
|
+
</StyledParagraph>
|
|
162
|
+
)}
|
|
163
|
+
<StyledTabsContainer theme={theme}>
|
|
164
|
+
<Tabs>
|
|
165
|
+
<Tab>{i18n.charts.line.chartTabLabel}</Tab>
|
|
166
|
+
<Tab>{i18n.charts.line.dataTabLabel}</Tab>
|
|
167
|
+
<div
|
|
168
|
+
onBlur={handleKeyboardInstructionsButtonBlur}
|
|
169
|
+
style={{
|
|
170
|
+
display: 'inline-block',
|
|
171
|
+
marginLeft: 'auto',
|
|
172
|
+
}}
|
|
173
|
+
>
|
|
174
|
+
<Tooltip
|
|
175
|
+
content={i18n.charts.line.keyboardInstructionsTooltip}
|
|
176
|
+
ref={keyboardInstructionsRef}
|
|
177
|
+
>
|
|
178
|
+
<IconButton
|
|
179
|
+
aria-controls="keyboardInstructions"
|
|
180
|
+
aria-label={i18n.charts.line.keyboardInstructionsTooltip}
|
|
181
|
+
aria-expanded={Boolean(isKeyboardInstructionsOpen)}
|
|
182
|
+
icon={<KeyboardIcon />}
|
|
183
|
+
onClick={handleKeyboardInstructionsButtonClick}
|
|
184
|
+
onKeyDown={handleKeyboardInstructionsButtonKeydown}
|
|
185
|
+
variant={ButtonVariant.link}
|
|
186
|
+
/>
|
|
187
|
+
</Tooltip>
|
|
188
|
+
<Announce>
|
|
189
|
+
<KeyboardInstructionsCard
|
|
190
|
+
id="keyboardInstructions"
|
|
191
|
+
isOpen={isKeyboardInstructionsOpen}
|
|
192
|
+
theme={theme}
|
|
193
|
+
width="350px"
|
|
194
|
+
>
|
|
195
|
+
<Paragraph
|
|
196
|
+
visualStyle={TypographyVisualStyle.headingXSmall}
|
|
197
|
+
style={{ margin: '0 0 16px' }}
|
|
198
|
+
>
|
|
199
|
+
{i18n.charts.line.keyboardInstructionsHeader}
|
|
200
|
+
</Paragraph>
|
|
201
|
+
{i18n.charts.line.keyboardInstructions}
|
|
202
|
+
</KeyboardInstructionsCard>
|
|
203
|
+
</Announce>
|
|
204
|
+
</div>
|
|
205
|
+
</Tabs>
|
|
206
|
+
<TabPanelsContainer>
|
|
207
|
+
<StyledTabPanel theme={theme}>
|
|
208
|
+
{type === 'line' && (
|
|
209
|
+
<LineChart<T>
|
|
210
|
+
{...other}
|
|
211
|
+
lastFocusedScatterPoint={lastFocusedScatterPoint}
|
|
212
|
+
pointRefArray={pointRefArray}
|
|
213
|
+
registerPoint={registerPoint}
|
|
214
|
+
tabRef={keyboardInstructionsRef}
|
|
215
|
+
unregisterPoint={unregisterPoint}
|
|
216
|
+
/>
|
|
217
|
+
)}
|
|
218
|
+
</StyledTabPanel>
|
|
219
|
+
<StyledTabPanel theme={theme}>
|
|
220
|
+
<ChartDataTable
|
|
221
|
+
data={other.data}
|
|
222
|
+
xData={{
|
|
223
|
+
keyValue: other.x,
|
|
224
|
+
label: other.componentProps?.xAxis?.label,
|
|
225
|
+
tickFormat: other.componentProps?.xAxis?.tickFormat,
|
|
226
|
+
}}
|
|
227
|
+
yData={{
|
|
228
|
+
keyValue: other.y,
|
|
229
|
+
tickFormat: other.componentProps?.yAxis?.tickFormat,
|
|
230
|
+
}}
|
|
231
|
+
/>
|
|
232
|
+
</StyledTabPanel>
|
|
233
|
+
</TabPanelsContainer>
|
|
234
|
+
</StyledTabsContainer>
|
|
235
|
+
</div>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export const Chart = React.forwardRef(BaseChart) as <T>(
|
|
240
|
+
props: ChartProps<T> & { ref?: React.MutableRefObject<HTMLDivElement> }
|
|
241
|
+
) => ReturnType<typeof BaseChart>;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { render } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
import { ChartDataTable } from './ChartDataTable';
|
|
6
|
+
import { basicData, explicitData } from './test/exampleChartData';
|
|
7
|
+
|
|
8
|
+
describe('Chart Data Table', () => {
|
|
9
|
+
describe('Basic Data', () => {
|
|
10
|
+
it('should render chart data with x values values', () => {
|
|
11
|
+
const label = '2019 Annual Sales Figures';
|
|
12
|
+
const { getByText, getAllByText } = render(
|
|
13
|
+
<ChartDataTable
|
|
14
|
+
data={basicData}
|
|
15
|
+
xData={{
|
|
16
|
+
label,
|
|
17
|
+
}}
|
|
18
|
+
yData={{
|
|
19
|
+
tickFormat: t => `$${t}k`,
|
|
20
|
+
}}
|
|
21
|
+
/>
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
expect(getByText(label)).toBeInTheDocument();
|
|
25
|
+
expect(getByText(basicData[0].data[0].x)).toBeInTheDocument();
|
|
26
|
+
expect(getByText(basicData[0].name)).toBeInTheDocument();
|
|
27
|
+
expect(
|
|
28
|
+
getAllByText(`$${basicData[0].data[0].y}k`)[0]
|
|
29
|
+
).toBeInTheDocument();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should render chart data with x tick values and an x tick format function', () => {
|
|
33
|
+
const label = '2019 Annual Sales Figures';
|
|
34
|
+
const { getByText } = render(
|
|
35
|
+
<ChartDataTable
|
|
36
|
+
data={basicData}
|
|
37
|
+
xData={{
|
|
38
|
+
label,
|
|
39
|
+
tickFormat: t => `Month of ${t}`,
|
|
40
|
+
tickValues: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
41
|
+
}}
|
|
42
|
+
yData={{
|
|
43
|
+
tickFormat: t => `$${t}k`,
|
|
44
|
+
}}
|
|
45
|
+
/>
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(getByText('Month of Jan')).toBeInTheDocument();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should render chart data with an x tick format array', () => {
|
|
52
|
+
const label = '2019 Annual Sales Figures';
|
|
53
|
+
const { getByText, getAllByText } = render(
|
|
54
|
+
<ChartDataTable
|
|
55
|
+
data={basicData}
|
|
56
|
+
xData={{
|
|
57
|
+
label,
|
|
58
|
+
tickFormat: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
59
|
+
}}
|
|
60
|
+
yData={{
|
|
61
|
+
tickFormat: t => `$${t}k`,
|
|
62
|
+
}}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(getByText(label)).toBeInTheDocument();
|
|
67
|
+
expect(getByText('Jan')).toBeInTheDocument();
|
|
68
|
+
expect(getByText(basicData[0].name)).toBeInTheDocument();
|
|
69
|
+
expect(
|
|
70
|
+
getAllByText(`$${basicData[0].data[0].y}k`)[0]
|
|
71
|
+
).toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('Explicit Data', () => {
|
|
76
|
+
it('should render chart data with x tick values', () => {
|
|
77
|
+
const label = '2019 Annual Sales Figures';
|
|
78
|
+
const { getByText, getAllByText } = render(
|
|
79
|
+
<ChartDataTable
|
|
80
|
+
data={explicitData}
|
|
81
|
+
xData={{
|
|
82
|
+
keyValue: 'month',
|
|
83
|
+
label,
|
|
84
|
+
tickValues: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
85
|
+
}}
|
|
86
|
+
yData={{
|
|
87
|
+
keyValue: 'sales',
|
|
88
|
+
tickFormat: t => `$${t}k`,
|
|
89
|
+
}}
|
|
90
|
+
/>
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(getByText(label)).toBeInTheDocument();
|
|
94
|
+
expect(getByText('Jan')).toBeInTheDocument();
|
|
95
|
+
expect(getByText(explicitData[0].name)).toBeInTheDocument();
|
|
96
|
+
expect(
|
|
97
|
+
getAllByText(`$${explicitData[0].data[0].sales}k`)[0]
|
|
98
|
+
).toBeInTheDocument();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should render column header for x values using the x key value when no label is provided', () => {
|
|
102
|
+
const { getByText } = render(
|
|
103
|
+
<ChartDataTable
|
|
104
|
+
data={explicitData}
|
|
105
|
+
xData={{
|
|
106
|
+
keyValue: 'month',
|
|
107
|
+
tickValues: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
108
|
+
}}
|
|
109
|
+
yData={{
|
|
110
|
+
keyValue: 'sales',
|
|
111
|
+
tickFormat: t => `$${t}k`,
|
|
112
|
+
}}
|
|
113
|
+
/>
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(getByText('month')).toBeInTheDocument();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should render chart data with x tick values and an x tick format function', () => {
|
|
120
|
+
const label = '2019 Annual Sales Figures';
|
|
121
|
+
const { getByText } = render(
|
|
122
|
+
<ChartDataTable
|
|
123
|
+
data={explicitData}
|
|
124
|
+
xData={{
|
|
125
|
+
label,
|
|
126
|
+
keyValue: 'month',
|
|
127
|
+
tickFormat: t => `Month of ${t}`,
|
|
128
|
+
tickValues: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
129
|
+
}}
|
|
130
|
+
yData={{
|
|
131
|
+
keyValue: 'sales',
|
|
132
|
+
tickFormat: t => `$${t}k`,
|
|
133
|
+
}}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
expect(getByText('Month of Jan')).toBeInTheDocument();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should render chart data with an x tick format array', () => {
|
|
141
|
+
const label = '2019 Annual Sales Figures';
|
|
142
|
+
const { getByText, getAllByText } = render(
|
|
143
|
+
<ChartDataTable
|
|
144
|
+
data={explicitData}
|
|
145
|
+
xData={{
|
|
146
|
+
label,
|
|
147
|
+
keyValue: 'month',
|
|
148
|
+
tickFormat: ['Jan', 'Feb', 'March', 'April', 'May'],
|
|
149
|
+
}}
|
|
150
|
+
yData={{
|
|
151
|
+
keyValue: 'sales',
|
|
152
|
+
tickFormat: t => `$${t}k`,
|
|
153
|
+
}}
|
|
154
|
+
/>
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
expect(getByText(label)).toBeInTheDocument();
|
|
158
|
+
expect(getByText('Jan')).toBeInTheDocument();
|
|
159
|
+
expect(getByText(explicitData[0].name)).toBeInTheDocument();
|
|
160
|
+
expect(
|
|
161
|
+
getAllByText(`$${explicitData[0].data[0].sales}k`)[0]
|
|
162
|
+
).toBeInTheDocument();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { Card, Datagrid, Spinner } from 'react-magma-dom';
|
|
4
|
+
import { VictoryAxisProps } from 'victory';
|
|
5
|
+
|
|
6
|
+
export function toCamelCase(str: string) {
|
|
7
|
+
return str
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
.replace(/[^a-z 0-9]/gi, '')
|
|
10
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (ltr, index) =>
|
|
11
|
+
index === 0 ? ltr.toLowerCase() : ltr.toUpperCase()
|
|
12
|
+
)
|
|
13
|
+
.replace(/\s+/g, '');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface DataTableProps {
|
|
17
|
+
data?: any[];
|
|
18
|
+
xData: {
|
|
19
|
+
keyValue?: string | number | symbol;
|
|
20
|
+
label?: VictoryAxisProps['label'];
|
|
21
|
+
tickFormat?: Partial<VictoryAxisProps['tickFormat']>;
|
|
22
|
+
tickValues?: VictoryAxisProps['tickValues'];
|
|
23
|
+
};
|
|
24
|
+
yData: {
|
|
25
|
+
keyValue?: string | number | symbol;
|
|
26
|
+
tickFormat?: Partial<VictoryAxisProps['tickFormat']>;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const ChartDataTable = (props: DataTableProps) => {
|
|
31
|
+
const {
|
|
32
|
+
data = [],
|
|
33
|
+
xData: {
|
|
34
|
+
keyValue: xKeyValue,
|
|
35
|
+
label: xAxisLabel,
|
|
36
|
+
tickFormat: xTickFormat,
|
|
37
|
+
tickValues: xTickValues,
|
|
38
|
+
},
|
|
39
|
+
yData: { keyValue: yKeyValue, tickFormat: yTickFormat },
|
|
40
|
+
} = props;
|
|
41
|
+
const [dataForTable, setDataForTable] = React.useState({
|
|
42
|
+
columns: [],
|
|
43
|
+
rows: [],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
setDataForTable(convertData());
|
|
48
|
+
}, [data]);
|
|
49
|
+
|
|
50
|
+
function convertData() {
|
|
51
|
+
const xField = toCamelCase((xKeyValue || xAxisLabel || 'x') as string);
|
|
52
|
+
const xTickValuesArray = data.reduce((valuesArray, { data: dataset }) => {
|
|
53
|
+
dataset.forEach((datum: any) => {
|
|
54
|
+
const value = datum.x || (xKeyValue && datum[xKeyValue]);
|
|
55
|
+
!valuesArray.includes(value) && valuesArray.push(value);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return valuesArray;
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
const baseTableData = {
|
|
62
|
+
columns:
|
|
63
|
+
xTickValuesArray.length > 0
|
|
64
|
+
? [
|
|
65
|
+
{
|
|
66
|
+
field: xField,
|
|
67
|
+
header: xAxisLabel || xKeyValue || 'X',
|
|
68
|
+
isRowHeader: true,
|
|
69
|
+
},
|
|
70
|
+
]
|
|
71
|
+
: [],
|
|
72
|
+
rows: xTickValuesArray.reduce(
|
|
73
|
+
(agg: any[], tick: number, index: number) => {
|
|
74
|
+
const tickValue =
|
|
75
|
+
xTickValues &&
|
|
76
|
+
typeof tick === 'number' &&
|
|
77
|
+
xTickValues.length === xTickValuesArray.length
|
|
78
|
+
? xTickFormat && typeof xTickFormat === 'function'
|
|
79
|
+
? xTickFormat(xTickValues[tick - 1])
|
|
80
|
+
: xTickValues[tick - 1]
|
|
81
|
+
: xTickFormat && Array.isArray(xTickFormat)
|
|
82
|
+
? xTickFormat[tick - 1]
|
|
83
|
+
: xTickFormat && typeof xTickFormat === 'function'
|
|
84
|
+
? xTickFormat(tick)
|
|
85
|
+
: tick;
|
|
86
|
+
agg.push({
|
|
87
|
+
[xField]: tickValue,
|
|
88
|
+
id: index,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return agg;
|
|
92
|
+
},
|
|
93
|
+
[]
|
|
94
|
+
),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
return data.reduce((tableData, datum) => {
|
|
98
|
+
const { name: header, data: dataset } = datum;
|
|
99
|
+
const field = toCamelCase(header);
|
|
100
|
+
|
|
101
|
+
tableData.columns.push({
|
|
102
|
+
field,
|
|
103
|
+
header,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
dataset.forEach((d: any, i: number) => {
|
|
107
|
+
const yValue = d.y !== undefined ? d.y : yKeyValue && d[yKeyValue];
|
|
108
|
+
tableData.rows[i] = {
|
|
109
|
+
...tableData.rows[i],
|
|
110
|
+
id: baseTableData.rows.length > 0 ? i + 1 : i,
|
|
111
|
+
[field]:
|
|
112
|
+
yTickFormat && typeof yTickFormat === 'function'
|
|
113
|
+
? yTickFormat(yValue)
|
|
114
|
+
: yValue,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return tableData;
|
|
119
|
+
}, baseTableData);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<Card>
|
|
124
|
+
{dataForTable.rows.length > 0 ? (
|
|
125
|
+
<Datagrid
|
|
126
|
+
hasPagination={false}
|
|
127
|
+
columns={dataForTable.columns}
|
|
128
|
+
rows={dataForTable.rows}
|
|
129
|
+
/>
|
|
130
|
+
) : (
|
|
131
|
+
<Spinner />
|
|
132
|
+
)}
|
|
133
|
+
</Card>
|
|
134
|
+
);
|
|
135
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { LineSegment, LineSegmentProps } from 'victory';
|
|
4
|
+
|
|
5
|
+
export const CustomAxisComponent: React.FunctionComponent<LineSegmentProps> = ({
|
|
6
|
+
events,
|
|
7
|
+
...props
|
|
8
|
+
}: any) => {
|
|
9
|
+
return (
|
|
10
|
+
<g>
|
|
11
|
+
<LineSegment
|
|
12
|
+
{...props}
|
|
13
|
+
events={events}
|
|
14
|
+
style={{
|
|
15
|
+
strokeWidth: '50px',
|
|
16
|
+
stroke: 'transparent',
|
|
17
|
+
}}
|
|
18
|
+
/>
|
|
19
|
+
<LineSegment
|
|
20
|
+
{...props}
|
|
21
|
+
events={events}
|
|
22
|
+
style={{
|
|
23
|
+
strokeWidth: '1px',
|
|
24
|
+
stroke: 'black',
|
|
25
|
+
strokeOpacity: '0.2',
|
|
26
|
+
}}
|
|
27
|
+
/>
|
|
28
|
+
</g>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { useForceUpdate } from 'react-magma-dom';
|
|
4
|
+
import { Point, PointProps } from 'victory';
|
|
5
|
+
|
|
6
|
+
export interface CustomScatterDataComponentInterface extends PointProps {
|
|
7
|
+
lineIndex: number;
|
|
8
|
+
pointRefArray: React.MutableRefObject<React.MutableRefObject<Element>[]>;
|
|
9
|
+
registerPoint: (
|
|
10
|
+
refArray: React.MutableRefObject<React.MutableRefObject<Element>[]>,
|
|
11
|
+
ref: React.MutableRefObject<Element>
|
|
12
|
+
) => void;
|
|
13
|
+
unregisterPoint: (
|
|
14
|
+
refArray: React.MutableRefObject<React.MutableRefObject<Element>[]>,
|
|
15
|
+
ref: React.MutableRefObject<Element>
|
|
16
|
+
) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CustomPointComponentInterface {
|
|
20
|
+
lineIndex: number;
|
|
21
|
+
pointIndex: PointProps['index'];
|
|
22
|
+
pointRefArray: React.MutableRefObject<React.MutableRefObject<Element>[]>;
|
|
23
|
+
registerPoint: (
|
|
24
|
+
refArray: React.MutableRefObject<React.MutableRefObject<Element>[]>,
|
|
25
|
+
ref: React.MutableRefObject<Element>
|
|
26
|
+
) => void;
|
|
27
|
+
unregisterPoint: (
|
|
28
|
+
refArray: React.MutableRefObject<React.MutableRefObject<Element>[]>,
|
|
29
|
+
ref: React.MutableRefObject<Element>
|
|
30
|
+
) => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const CustomScatterDataComponent = (
|
|
34
|
+
props: CustomScatterDataComponentInterface
|
|
35
|
+
) => {
|
|
36
|
+
const {
|
|
37
|
+
datum,
|
|
38
|
+
index: pointIndex,
|
|
39
|
+
lineIndex,
|
|
40
|
+
pointRefArray,
|
|
41
|
+
registerPoint,
|
|
42
|
+
unregisterPoint,
|
|
43
|
+
...other
|
|
44
|
+
} = props;
|
|
45
|
+
return (
|
|
46
|
+
<Point
|
|
47
|
+
{...other}
|
|
48
|
+
ariaLabel={datum.label}
|
|
49
|
+
pathComponent={
|
|
50
|
+
<CustomPointComponent
|
|
51
|
+
lineIndex={lineIndex}
|
|
52
|
+
pointIndex={pointIndex}
|
|
53
|
+
pointRefArray={pointRefArray}
|
|
54
|
+
registerPoint={registerPoint}
|
|
55
|
+
unregisterPoint={unregisterPoint}
|
|
56
|
+
/>
|
|
57
|
+
}
|
|
58
|
+
role="button"
|
|
59
|
+
tabIndex={0}
|
|
60
|
+
/>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const CustomPointComponent = (props: CustomPointComponentInterface) => {
|
|
65
|
+
const {
|
|
66
|
+
lineIndex,
|
|
67
|
+
pointRefArray,
|
|
68
|
+
pointIndex,
|
|
69
|
+
registerPoint,
|
|
70
|
+
unregisterPoint,
|
|
71
|
+
...other
|
|
72
|
+
} = props;
|
|
73
|
+
const forceUpdate = useForceUpdate();
|
|
74
|
+
const ref = React.useRef<SVGPathElement | null>(null);
|
|
75
|
+
|
|
76
|
+
React.useEffect(() => {
|
|
77
|
+
registerPoint(pointRefArray, ref as React.MutableRefObject<Element>);
|
|
78
|
+
|
|
79
|
+
forceUpdate();
|
|
80
|
+
|
|
81
|
+
return () =>
|
|
82
|
+
unregisterPoint(pointRefArray, ref as React.MutableRefObject<Element>);
|
|
83
|
+
}, []);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<path
|
|
87
|
+
ref={ref}
|
|
88
|
+
data-line-index={lineIndex}
|
|
89
|
+
data-point-index={pointIndex}
|
|
90
|
+
{...other}
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
93
|
+
};
|