@redsift/charts 9.2.3-patch → 9.2.3
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/coverage/clover.xml +1096 -0
- package/coverage/coverage-final.json +60 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/components/Arc/Arc.tsx.html +304 -0
- package/coverage/lcov-report/components/Arc/index.html +146 -0
- package/coverage/lcov-report/components/Arc/index.ts.html +94 -0
- package/coverage/lcov-report/components/Arc/styles.ts.html +208 -0
- package/coverage/lcov-report/components/Arcs/Arcs.tsx.html +409 -0
- package/coverage/lcov-report/components/Arcs/index.html +146 -0
- package/coverage/lcov-report/components/Arcs/index.ts.html +94 -0
- package/coverage/lcov-report/components/Arcs/styles.ts.html +106 -0
- package/coverage/lcov-report/components/Axis/Axis.tsx.html +754 -0
- package/coverage/lcov-report/components/Axis/computeTicks.ts.html +481 -0
- package/coverage/lcov-report/components/Axis/index.html +176 -0
- package/coverage/lcov-report/components/Axis/index.ts.html +94 -0
- package/coverage/lcov-report/components/Axis/styles.ts.html +148 -0
- package/coverage/lcov-report/components/Axis/types.ts.html +253 -0
- package/coverage/lcov-report/components/Bar/Bar.tsx.html +421 -0
- package/coverage/lcov-report/components/Bar/index.html +161 -0
- package/coverage/lcov-report/components/Bar/index.ts.html +94 -0
- package/coverage/lcov-report/components/Bar/styles.ts.html +247 -0
- package/coverage/lcov-report/components/Bar/types.ts.html +178 -0
- package/coverage/lcov-report/components/BarChart/BarChart.tsx.html +352 -0
- package/coverage/lcov-report/components/BarChart/EmptyBarChart.tsx.html +259 -0
- package/coverage/lcov-report/components/BarChart/LoadingBarChart.tsx.html +145 -0
- package/coverage/lcov-report/components/BarChart/RenderedBarChart.tsx.html +430 -0
- package/coverage/lcov-report/components/BarChart/index.html +206 -0
- package/coverage/lcov-report/components/BarChart/index.ts.html +94 -0
- package/coverage/lcov-report/components/BarChart/styles.ts.html +190 -0
- package/coverage/lcov-report/components/BarChart/utils.ts.html +145 -0
- package/coverage/lcov-report/components/BottomAxis/BottomAxis.tsx.html +247 -0
- package/coverage/lcov-report/components/BottomAxis/index.html +146 -0
- package/coverage/lcov-report/components/BottomAxis/index.ts.html +94 -0
- package/coverage/lcov-report/components/BottomAxis/styles.ts.html +139 -0
- package/coverage/lcov-report/components/ChartContainer/ChartContainer.tsx.html +361 -0
- package/coverage/lcov-report/components/ChartContainer/index.html +146 -0
- package/coverage/lcov-report/components/ChartContainer/index.ts.html +94 -0
- package/coverage/lcov-report/components/ChartContainer/intl/index.html +116 -0
- package/coverage/lcov-report/components/ChartContainer/intl/index.ts.html +106 -0
- package/coverage/lcov-report/components/ChartContainer/styles.ts.html +208 -0
- package/coverage/lcov-report/components/DataPoint/DataPoint.tsx.html +391 -0
- package/coverage/lcov-report/components/DataPoint/index.html +146 -0
- package/coverage/lcov-report/components/DataPoint/index.ts.html +94 -0
- package/coverage/lcov-report/components/DataPoint/styles.ts.html +109 -0
- package/coverage/lcov-report/components/Dot/Dot.tsx.html +232 -0
- package/coverage/lcov-report/components/Dot/index.html +146 -0
- package/coverage/lcov-report/components/Dot/index.ts.html +94 -0
- package/coverage/lcov-report/components/Dot/styles.ts.html +184 -0
- package/coverage/lcov-report/components/HorizontalBar/HorizontalBar.tsx.html +541 -0
- package/coverage/lcov-report/components/HorizontalBar/index.html +161 -0
- package/coverage/lcov-report/components/HorizontalBar/index.ts.html +94 -0
- package/coverage/lcov-report/components/HorizontalBar/styles.ts.html +187 -0
- package/coverage/lcov-report/components/HorizontalBar/types.ts.html +301 -0
- package/coverage/lcov-report/components/HorizontalBarChart/HorizontalBarChart.tsx.html +691 -0
- package/coverage/lcov-report/components/HorizontalBarChart/index.html +146 -0
- package/coverage/lcov-report/components/HorizontalBarChart/index.ts.html +94 -0
- package/coverage/lcov-report/components/HorizontalBarChart/styles.ts.html +196 -0
- package/coverage/lcov-report/components/HorizontalBarChart/types.ts.html +301 -0
- package/coverage/lcov-report/components/Legend/Legend.tsx.html +268 -0
- package/coverage/lcov-report/components/Legend/index.html +146 -0
- package/coverage/lcov-report/components/Legend/index.ts.html +94 -0
- package/coverage/lcov-report/components/Legend/styles.ts.html +130 -0
- package/coverage/lcov-report/components/LegendItem/LegendItem.tsx.html +325 -0
- package/coverage/lcov-report/components/LegendItem/index.html +146 -0
- package/coverage/lcov-report/components/LegendItem/index.ts.html +94 -0
- package/coverage/lcov-report/components/LegendItem/styles.ts.html +205 -0
- package/coverage/lcov-report/components/PieChart/EmptyPieChart.tsx.html +343 -0
- package/coverage/lcov-report/components/PieChart/LoadingPieChart.tsx.html +145 -0
- package/coverage/lcov-report/components/PieChart/PieChart.tsx.html +385 -0
- package/coverage/lcov-report/components/PieChart/RenderedPieChart.tsx.html +607 -0
- package/coverage/lcov-report/components/PieChart/index.html +221 -0
- package/coverage/lcov-report/components/PieChart/index.ts.html +94 -0
- package/coverage/lcov-report/components/PieChart/styles.ts.html +370 -0
- package/coverage/lcov-report/components/PieChart/types.ts.html +367 -0
- package/coverage/lcov-report/components/PieChart/utils.ts.html +199 -0
- package/coverage/lcov-report/components/ScatterPlot/EmptyScatterPlot.tsx.html +295 -0
- package/coverage/lcov-report/components/ScatterPlot/LoadingScatterPlot.tsx.html +145 -0
- package/coverage/lcov-report/components/ScatterPlot/RenderedScatterPlot.tsx.html +1048 -0
- package/coverage/lcov-report/components/ScatterPlot/ScatterPlot.tsx.html +367 -0
- package/coverage/lcov-report/components/ScatterPlot/index.html +221 -0
- package/coverage/lcov-report/components/ScatterPlot/index.ts.html +94 -0
- package/coverage/lcov-report/components/ScatterPlot/styles.ts.html +190 -0
- package/coverage/lcov-report/components/ScatterPlot/types.ts.html +394 -0
- package/coverage/lcov-report/components/ScatterPlot/utils.ts.html +388 -0
- package/coverage/lcov-report/core/Arc/Arc.tsx.html +304 -0
- package/coverage/lcov-report/core/Arc/index.html +146 -0
- package/coverage/lcov-report/core/Arc/index.ts.html +94 -0
- package/coverage/lcov-report/core/Arc/styles.ts.html +202 -0
- package/coverage/lcov-report/core/Arcs/Arcs.tsx.html +421 -0
- package/coverage/lcov-report/core/Arcs/index.html +146 -0
- package/coverage/lcov-report/core/Arcs/index.ts.html +94 -0
- package/coverage/lcov-report/core/Arcs/styles.ts.html +106 -0
- package/coverage/lcov-report/core/Axis/Axis.tsx.html +754 -0
- package/coverage/lcov-report/core/Axis/computeTicks.ts.html +481 -0
- package/coverage/lcov-report/core/Axis/index.html +176 -0
- package/coverage/lcov-report/core/Axis/index.ts.html +94 -0
- package/coverage/lcov-report/core/Axis/styles.ts.html +145 -0
- package/coverage/lcov-report/core/Axis/types.ts.html +253 -0
- package/coverage/lcov-report/core/Bar/Bar.tsx.html +421 -0
- package/coverage/lcov-report/core/Bar/index.html +161 -0
- package/coverage/lcov-report/core/Bar/index.ts.html +94 -0
- package/coverage/lcov-report/core/Bar/styles.ts.html +190 -0
- package/coverage/lcov-report/core/Bar/types.ts.html +178 -0
- package/coverage/lcov-report/core/BottomAxis/BottomAxis.tsx.html +277 -0
- package/coverage/lcov-report/core/BottomAxis/index.html +146 -0
- package/coverage/lcov-report/core/BottomAxis/index.ts.html +94 -0
- package/coverage/lcov-report/core/BottomAxis/styles.ts.html +139 -0
- package/coverage/lcov-report/core/ChartConfig/ChartConfig.ts.html +97 -0
- package/coverage/lcov-report/core/ChartConfig/index.html +131 -0
- package/coverage/lcov-report/core/ChartConfig/index.ts.html +88 -0
- package/coverage/lcov-report/core/ChartContainer/ChartContainer.tsx.html +421 -0
- package/coverage/lcov-report/core/ChartContainer/index.html +146 -0
- package/coverage/lcov-report/core/ChartContainer/index.ts.html +94 -0
- package/coverage/lcov-report/core/ChartContainer/intl/index.html +116 -0
- package/coverage/lcov-report/core/ChartContainer/intl/index.ts.html +106 -0
- package/coverage/lcov-report/core/ChartContainer/styles.ts.html +205 -0
- package/coverage/lcov-report/core/DataPoint/DataPoint.tsx.html +442 -0
- package/coverage/lcov-report/core/DataPoint/index.html +146 -0
- package/coverage/lcov-report/core/DataPoint/index.ts.html +94 -0
- package/coverage/lcov-report/core/DataPoint/styles.ts.html +109 -0
- package/coverage/lcov-report/core/Dot/Dot.tsx.html +232 -0
- package/coverage/lcov-report/core/Dot/index.html +146 -0
- package/coverage/lcov-report/core/Dot/index.ts.html +94 -0
- package/coverage/lcov-report/core/Dot/styles.ts.html +184 -0
- package/coverage/lcov-report/core/Legend/Legend.tsx.html +277 -0
- package/coverage/lcov-report/core/Legend/index.html +146 -0
- package/coverage/lcov-report/core/Legend/index.ts.html +94 -0
- package/coverage/lcov-report/core/Legend/styles.ts.html +172 -0
- package/coverage/lcov-report/core/Point/Point.tsx.html +484 -0
- package/coverage/lcov-report/core/Point/index.html +146 -0
- package/coverage/lcov-report/core/Point/index.ts.html +94 -0
- package/coverage/lcov-report/core/Point/styles.ts.html +169 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/gallery/HorizontalBarChart/HorizontalBarChart.tsx.html +706 -0
- package/coverage/lcov-report/gallery/HorizontalBarChart/index.html +161 -0
- package/coverage/lcov-report/gallery/HorizontalBarChart/index.ts.html +94 -0
- package/coverage/lcov-report/gallery/HorizontalBarChart/styles.ts.html +316 -0
- package/coverage/lcov-report/gallery/HorizontalBarChart/types.ts.html +310 -0
- package/coverage/lcov-report/gallery/PieChart/PieChart.tsx.html +1252 -0
- package/coverage/lcov-report/gallery/PieChart/index.html +161 -0
- package/coverage/lcov-report/gallery/PieChart/index.ts.html +94 -0
- package/coverage/lcov-report/gallery/PieChart/styles.ts.html +517 -0
- package/coverage/lcov-report/gallery/PieChart/types.ts.html +388 -0
- package/coverage/lcov-report/gallery/ScatterPlot/index.html +146 -0
- package/coverage/lcov-report/gallery/ScatterPlot/index.ts.html +94 -0
- package/coverage/lcov-report/gallery/ScatterPlot/styles.ts.html +343 -0
- package/coverage/lcov-report/gallery/ScatterPlot/types.ts.html +433 -0
- package/coverage/lcov-report/hooks/index.html +176 -0
- package/coverage/lcov-report/hooks/index.ts.html +97 -0
- package/coverage/lcov-report/hooks/useBrush.tsx.html +430 -0
- package/coverage/lcov-report/hooks/useColor.tsx.html +163 -0
- package/coverage/lcov-report/hooks/useFormatCategoricalData.tsx.html +289 -0
- package/coverage/lcov-report/hooks/useIsEmpty.tsx.html +118 -0
- package/coverage/lcov-report/hooks/useZoom.tsx.html +235 -0
- package/coverage/lcov-report/index.html +311 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +2345 -0
- package/coverage/storybook/coverage-storybook.json +79382 -0
- package/dist/package.json +98 -0
- package/index.ts +1 -0
- package/jest.config.js +3 -0
- package/package.json +2 -3
- package/rollup.config.js +13 -0
- package/src/components/Arc/Arc.test.tsx +80 -0
- package/src/components/Arc/Arc.tsx +73 -0
- package/src/components/Arc/index.ts +3 -0
- package/src/components/Arc/styles.ts +41 -0
- package/src/components/Arc/types.ts +18 -0
- package/src/components/Arcs/Arcs.stories.tsx +177 -0
- package/src/components/Arcs/Arcs.tsx +108 -0
- package/src/components/Arcs/index.ts +3 -0
- package/src/components/Arcs/styles.ts +7 -0
- package/src/components/Arcs/types.ts +26 -0
- package/src/components/Axis/Axis.stories.tsx +297 -0
- package/src/components/Axis/Axis.tsx +223 -0
- package/src/components/Axis/computeTicks.ts +132 -0
- package/src/components/Axis/index.ts +3 -0
- package/src/components/Axis/styles.ts +21 -0
- package/src/components/Axis/types.ts +56 -0
- package/src/components/Bar/Bar.stories.tsx +152 -0
- package/src/components/Bar/Bar.test.tsx +156 -0
- package/src/components/Bar/Bar.tsx +112 -0
- package/src/components/Bar/index.ts +3 -0
- package/src/components/Bar/styles.ts +54 -0
- package/src/components/Bar/types.ts +31 -0
- package/src/components/BarChart/BarChart.stories.tsx +74 -0
- package/src/components/BarChart/BarChart.test.tsx +178 -0
- package/src/components/BarChart/BarChart.tsx +89 -0
- package/src/components/BarChart/EmptyBarChart.tsx +58 -0
- package/src/components/BarChart/LoadingBarChart.tsx +20 -0
- package/src/components/BarChart/RenderedBarChart.tsx +115 -0
- package/src/components/BarChart/__snapshots__/BarChart.test.tsx.snap +7028 -0
- package/src/components/BarChart/index.ts +3 -0
- package/src/components/BarChart/styles.ts +35 -0
- package/src/components/BarChart/types.ts +58 -0
- package/src/components/BarChart/utils.ts +20 -0
- package/src/components/ChartContainer/ChartContainer.stories.tsx +81 -0
- package/src/components/ChartContainer/ChartContainer.test.tsx +71 -0
- package/src/components/ChartContainer/ChartContainer.tsx +92 -0
- package/src/components/ChartContainer/index.ts +3 -0
- package/src/components/ChartContainer/intl/en-US.json +3 -0
- package/src/components/ChartContainer/intl/fr-FR.json +3 -0
- package/src/components/ChartContainer/intl/index.ts +7 -0
- package/src/components/ChartContainer/styles.ts +41 -0
- package/src/components/ChartContainer/types.ts +26 -0
- package/src/components/DataPoint/DataPoint.tsx +102 -0
- package/src/components/DataPoint/index.ts +3 -0
- package/src/components/DataPoint/styles.ts +8 -0
- package/src/components/DataPoint/types.ts +33 -0
- package/src/components/Dot/Dot.stories.tsx +157 -0
- package/src/components/Dot/Dot.test.tsx +136 -0
- package/src/components/Dot/Dot.tsx +49 -0
- package/src/components/Dot/index.ts +3 -0
- package/src/components/Dot/styles.ts +33 -0
- package/src/components/Dot/types.ts +16 -0
- package/src/components/Legend/Legend.stories.tsx +108 -0
- package/src/components/Legend/Legend.tsx +61 -0
- package/src/components/Legend/index.ts +3 -0
- package/src/components/Legend/styles.ts +15 -0
- package/src/components/Legend/types.ts +27 -0
- package/src/components/LegendItem/LegendItem.test.tsx +69 -0
- package/src/components/LegendItem/LegendItem.tsx +80 -0
- package/src/components/LegendItem/index.ts +3 -0
- package/src/components/LegendItem/styles.ts +40 -0
- package/src/components/LegendItem/types.ts +30 -0
- package/src/components/PieChart/EmptyPieChart.tsx +86 -0
- package/src/components/PieChart/LoadingPieChart.tsx +20 -0
- package/src/components/PieChart/PieChart.stories.tsx +85 -0
- package/src/components/PieChart/PieChart.test.tsx +183 -0
- package/src/components/PieChart/PieChart.tsx +100 -0
- package/src/components/PieChart/RenderedPieChart.tsx +174 -0
- package/src/components/PieChart/__snapshots__/PieChart.test.tsx.snap +21020 -0
- package/src/components/PieChart/index.ts +3 -0
- package/src/components/PieChart/styles.ts +95 -0
- package/src/components/PieChart/types.ts +94 -0
- package/src/components/PieChart/utils.ts +38 -0
- package/src/components/ScatterPlot/EmptyScatterPlot.tsx +70 -0
- package/src/components/ScatterPlot/LoadingScatterPlot.tsx +20 -0
- package/src/components/ScatterPlot/RenderedScatterPlot.tsx +321 -0
- package/src/components/ScatterPlot/ScatterPlot.stories.tsx +101 -0
- package/src/components/ScatterPlot/ScatterPlot.test.tsx +61 -0
- package/src/components/ScatterPlot/ScatterPlot.tsx +94 -0
- package/src/components/ScatterPlot/__snapshots__/ScatterPlot.test.tsx.snap +91712 -0
- package/src/components/ScatterPlot/index.ts +3 -0
- package/src/components/ScatterPlot/styles.ts +35 -0
- package/src/components/ScatterPlot/types.ts +103 -0
- package/src/components/ScatterPlot/utils.ts +101 -0
- package/src/config.ts +10 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/useBrush.tsx +115 -0
- package/src/hooks/useColor.tsx +26 -0
- package/src/hooks/useFormatCategoricalData.tsx +68 -0
- package/src/hooks/useZoom.tsx +50 -0
- package/src/index.ts +15 -0
- package/src/scheme.ts +213 -0
- package/src/types/data.ts +52 -0
- package/src/types/index.ts +5 -0
- package/src/types/legend.ts +19 -0
- package/src/types/scale.ts +79 -0
- package/src/types/size.ts +17 -0
- package/src/types/theme.ts +27 -0
- package/tsconfig.json +3 -0
- /package/{CONTRIBUTING.md → dist/CONTRIBUTING.md} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{index.js.map → dist/index.js.map} +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
|
|
5
|
+
import { composeStory } from '@storybook/react';
|
|
6
|
+
import Meta, { Controls } from './BarChart.stories';
|
|
7
|
+
|
|
8
|
+
import { BarChart } from './BarChart';
|
|
9
|
+
import { ChartSize, ChartTheme, ColorTheme, TooltipVariant } from '../../types';
|
|
10
|
+
import { PresentationColorPalette } from '@redsift/design-system';
|
|
11
|
+
|
|
12
|
+
jest.mock('@redsift/design-system', () => ({
|
|
13
|
+
...jest.requireActual('@redsift/design-system'),
|
|
14
|
+
useId: () => ['123'],
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
const themes = {
|
|
18
|
+
...ColorTheme,
|
|
19
|
+
'success-warning-danger': {
|
|
20
|
+
success: 'Bread',
|
|
21
|
+
warning: 'Tea',
|
|
22
|
+
danger: 'Coffee',
|
|
23
|
+
},
|
|
24
|
+
'success-warning-danger-neutral': {
|
|
25
|
+
success: 'Bread',
|
|
26
|
+
warning: 'Tea',
|
|
27
|
+
danger: 'Coffee',
|
|
28
|
+
neutral: 'Pastry',
|
|
29
|
+
},
|
|
30
|
+
custom: {
|
|
31
|
+
Coffee: PresentationColorPalette.purple,
|
|
32
|
+
Bread: PresentationColorPalette.pink,
|
|
33
|
+
Tea: PresentationColorPalette.aqua,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const data = [
|
|
38
|
+
{ key: 'Dogs', value: 350 },
|
|
39
|
+
{ key: 'Cats', value: 500 },
|
|
40
|
+
{ key: 'Fish', value: 150 },
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
describe('BarChart', () => {
|
|
44
|
+
const onClickSpy = jest.fn();
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
onClickSpy.mockClear();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should match snapshot', () => {
|
|
51
|
+
const ComposedStory = composeStory(Controls, Meta);
|
|
52
|
+
const tree = render(
|
|
53
|
+
<>
|
|
54
|
+
{Object.values(ChartSize).map((size, i) =>
|
|
55
|
+
Object.values(TooltipVariant).map((tooltipVariant, j) => (
|
|
56
|
+
<ComposedStory
|
|
57
|
+
key={`${size}-${tooltipVariant}`}
|
|
58
|
+
tooltipVariant={tooltipVariant}
|
|
59
|
+
size={size}
|
|
60
|
+
theme={
|
|
61
|
+
Object.keys(themes)[
|
|
62
|
+
(i * Object.values(TooltipVariant).length + j) % Object.keys(themes).length
|
|
63
|
+
] as ChartTheme
|
|
64
|
+
}
|
|
65
|
+
/>
|
|
66
|
+
))
|
|
67
|
+
)}
|
|
68
|
+
</>
|
|
69
|
+
);
|
|
70
|
+
expect(tree).toMatchSnapshot();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('supports custom className', () => {
|
|
74
|
+
const tree = render(<BarChart aria-label="Chart" data={data} className="test" />);
|
|
75
|
+
const component = tree.asFragment().firstChild;
|
|
76
|
+
|
|
77
|
+
expect(component).toHaveAttribute('class', expect.stringContaining('test'));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('supports custom data attributes', () => {
|
|
81
|
+
const tree = render(<BarChart aria-label="Chart" data={data} data-testid="test" />);
|
|
82
|
+
const component = tree.asFragment().firstChild;
|
|
83
|
+
expect(component).toHaveAttribute('data-testid', 'test');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('can be labelled using a title', () => {
|
|
87
|
+
const { getByLabelText } = render(<BarChart data={data} title="Label" />);
|
|
88
|
+
const component = getByLabelText('Label');
|
|
89
|
+
expect(component).toHaveAttribute('aria-labelledby', 'id123__title');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('can be labelled using an aria-label', () => {
|
|
93
|
+
const { getByLabelText } = render(<BarChart data={data} aria-label="Label" />);
|
|
94
|
+
const component = getByLabelText('Label');
|
|
95
|
+
expect(component).toHaveAttribute('aria-label', 'Label');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('can be labelled using an aria-labelledby', () => {
|
|
99
|
+
const { getByLabelText } = render(
|
|
100
|
+
<>
|
|
101
|
+
<h1 id="123">Label</h1>
|
|
102
|
+
<BarChart data={data} aria-labelledby="123" />
|
|
103
|
+
</>
|
|
104
|
+
);
|
|
105
|
+
const component = getByLabelText('Label');
|
|
106
|
+
expect(component).toHaveAttribute('aria-labelledby', '123');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('supports click on the bars', async () => {
|
|
110
|
+
const user = userEvent.setup();
|
|
111
|
+
const { getAllByRole } = render(<BarChart aria-label="Chart" data={data} onBarClick={onClickSpy} />);
|
|
112
|
+
|
|
113
|
+
const bars = getAllByRole('button');
|
|
114
|
+
expect(bars.length).toBe(3);
|
|
115
|
+
|
|
116
|
+
await user.click(bars[0]);
|
|
117
|
+
await user.click(bars[1]);
|
|
118
|
+
await user.click(bars[2]);
|
|
119
|
+
|
|
120
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
121
|
+
data: { key: 'Cats', value: 500, percent: 0.5 },
|
|
122
|
+
});
|
|
123
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
124
|
+
data: { key: 'Dogs', value: 350, percent: 0.35 },
|
|
125
|
+
});
|
|
126
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
127
|
+
data: { key: 'Fish', value: 150, percent: 0.15 },
|
|
128
|
+
});
|
|
129
|
+
expect(onClickSpy).toHaveBeenCalledTimes(3);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('supports keydown on the bars', async () => {
|
|
133
|
+
const { getAllByRole } = render(<BarChart aria-label="Chart" data={data} onBarClick={onClickSpy} />);
|
|
134
|
+
|
|
135
|
+
const bars = getAllByRole('button');
|
|
136
|
+
expect(bars.length).toBe(3);
|
|
137
|
+
|
|
138
|
+
userEvent.keyboard('[Tab]');
|
|
139
|
+
userEvent.keyboard('[Enter]');
|
|
140
|
+
userEvent.keyboard('[Tab]');
|
|
141
|
+
userEvent.keyboard('[Space]');
|
|
142
|
+
userEvent.keyboard('[Tab]');
|
|
143
|
+
userEvent.keyboard('[Enter]');
|
|
144
|
+
|
|
145
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
146
|
+
data: { key: 'Cats', value: 500, percent: 0.5 },
|
|
147
|
+
});
|
|
148
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
149
|
+
data: { key: 'Dogs', value: 350, percent: 0.35 },
|
|
150
|
+
});
|
|
151
|
+
expect(onClickSpy).toHaveBeenCalledWith({
|
|
152
|
+
data: { key: 'Fish', value: 150, percent: 0.15 },
|
|
153
|
+
});
|
|
154
|
+
expect(onClickSpy).toHaveBeenCalledTimes(3);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('shows a tooltip when hovering on the bars ', async () => {
|
|
158
|
+
const user = userEvent.setup();
|
|
159
|
+
const { getAllByRole } = render(
|
|
160
|
+
<BarChart
|
|
161
|
+
aria-label="Chart"
|
|
162
|
+
data={data}
|
|
163
|
+
// The onBarClick gives the bar the role "button" which is then used to access the bars.
|
|
164
|
+
onBarClick={onClickSpy}
|
|
165
|
+
/>
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const bars = getAllByRole('button');
|
|
169
|
+
expect(bars.length).toBe(3);
|
|
170
|
+
|
|
171
|
+
await user.hover(bars[0]);
|
|
172
|
+
await waitFor(() => {
|
|
173
|
+
expect(screen.getByRole('tooltip', { exact: true })).toBeInTheDocument();
|
|
174
|
+
});
|
|
175
|
+
const tooltip = screen.getByRole('tooltip');
|
|
176
|
+
expect(tooltip.textContent).toContain('Cats');
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
|
|
4
|
+
import { Comp, useId } from '@redsift/design-system';
|
|
5
|
+
|
|
6
|
+
import { ChartSize, ColorTheme, TooltipVariant } from '../../types';
|
|
7
|
+
import { BarChartProps } from './types';
|
|
8
|
+
import { EmptyBarChart } from './EmptyBarChart';
|
|
9
|
+
import { LoadingBarChart } from './LoadingBarChart';
|
|
10
|
+
import { RenderedBarChart } from './RenderedBarChart';
|
|
11
|
+
|
|
12
|
+
const COMPONENT_NAME = 'BarChart';
|
|
13
|
+
const CLASSNAME = 'redsift-barchart';
|
|
14
|
+
const DEFAULT_PROPS: Partial<BarChartProps> = {
|
|
15
|
+
isBarSelected: () => true,
|
|
16
|
+
others: true,
|
|
17
|
+
size: ChartSize.medium,
|
|
18
|
+
theme: ColorTheme.default,
|
|
19
|
+
tooltipVariant: TooltipVariant.value,
|
|
20
|
+
localeText: {
|
|
21
|
+
emptyChartText: 'No Data',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const BarChart: Comp<BarChartProps, HTMLDivElement> = forwardRef((props, ref) => {
|
|
26
|
+
const {
|
|
27
|
+
areXLabelsRotated,
|
|
28
|
+
caping,
|
|
29
|
+
chartRef,
|
|
30
|
+
className,
|
|
31
|
+
data: propsData,
|
|
32
|
+
emptyComponent,
|
|
33
|
+
id: propsId,
|
|
34
|
+
isBarSelected,
|
|
35
|
+
labelDecorator,
|
|
36
|
+
localeText,
|
|
37
|
+
onBarClick,
|
|
38
|
+
others,
|
|
39
|
+
size,
|
|
40
|
+
barRole,
|
|
41
|
+
theme,
|
|
42
|
+
tooltipVariant,
|
|
43
|
+
...forwardedProps
|
|
44
|
+
} = props;
|
|
45
|
+
const [_id] = useId();
|
|
46
|
+
const id = propsId ?? _id;
|
|
47
|
+
|
|
48
|
+
if (propsData === undefined || propsData === null) {
|
|
49
|
+
return <LoadingBarChart id={id} {...forwardedProps} ref={ref} />;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (propsData.length === 0) {
|
|
53
|
+
return (
|
|
54
|
+
<EmptyBarChart
|
|
55
|
+
data={propsData}
|
|
56
|
+
emptyComponent={emptyComponent}
|
|
57
|
+
id={id}
|
|
58
|
+
localeText={localeText}
|
|
59
|
+
size={size}
|
|
60
|
+
{...forwardedProps}
|
|
61
|
+
ref={ref}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<RenderedBarChart
|
|
68
|
+
areXLabelsRotated={areXLabelsRotated}
|
|
69
|
+
barRole={barRole}
|
|
70
|
+
caping={caping}
|
|
71
|
+
chartRef={chartRef}
|
|
72
|
+
className={classNames(BarChart.className, className)}
|
|
73
|
+
data={propsData}
|
|
74
|
+
id={id}
|
|
75
|
+
isBarSelected={isBarSelected}
|
|
76
|
+
labelDecorator={labelDecorator}
|
|
77
|
+
onBarClick={onBarClick}
|
|
78
|
+
others={others}
|
|
79
|
+
size={size}
|
|
80
|
+
theme={theme}
|
|
81
|
+
tooltipVariant={tooltipVariant}
|
|
82
|
+
{...forwardedProps}
|
|
83
|
+
ref={ref}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
BarChart.className = CLASSNAME;
|
|
88
|
+
BarChart.defaultProps = DEFAULT_PROPS;
|
|
89
|
+
BarChart.displayName = COMPONENT_NAME;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React, { forwardRef, RefObject, useRef } from 'react';
|
|
2
|
+
import { scaleLinear as d3scaleLinear, ScaleLinear as d3ScaleLinear } from 'd3';
|
|
3
|
+
|
|
4
|
+
import { AnyScale } from '../../types';
|
|
5
|
+
import { BarChartProps } from './types';
|
|
6
|
+
import { StyledBarChart, StyledBarChartEmptyText } from './styles';
|
|
7
|
+
import { Axis } from '../Axis';
|
|
8
|
+
import { sizeToDimension } from './utils';
|
|
9
|
+
|
|
10
|
+
export const EmptyBarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
11
|
+
(props, ref) => {
|
|
12
|
+
const { className, emptyComponent, size, localeText, ...forwardedProps } =
|
|
13
|
+
props;
|
|
14
|
+
|
|
15
|
+
const chartDimensions = sizeToDimension(size!);
|
|
16
|
+
const width = chartDimensions.width;
|
|
17
|
+
const height = chartDimensions.height;
|
|
18
|
+
const margins = { top: 16, left: 16, right: 16, bottom: 32 };
|
|
19
|
+
const chartHeight = height - margins.top - margins.bottom;
|
|
20
|
+
const chartWidth = width - margins.left - margins.right;
|
|
21
|
+
|
|
22
|
+
const scaleRef = useRef<d3ScaleLinear<number, number>>();
|
|
23
|
+
if (!scaleRef.current) {
|
|
24
|
+
scaleRef.current = d3scaleLinear().domain([]).range([0, chartWidth]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<StyledBarChart
|
|
29
|
+
{...forwardedProps}
|
|
30
|
+
className={className}
|
|
31
|
+
ref={ref as RefObject<HTMLDivElement>}
|
|
32
|
+
>
|
|
33
|
+
{emptyComponent ?? (
|
|
34
|
+
<>
|
|
35
|
+
<StyledBarChartEmptyText
|
|
36
|
+
$maxWidth={width}
|
|
37
|
+
$textSize={chartDimensions.fontSize / 2}
|
|
38
|
+
>
|
|
39
|
+
<span>{localeText?.emptyChartText}</span>
|
|
40
|
+
</StyledBarChartEmptyText>
|
|
41
|
+
<svg width={width} height={height}>
|
|
42
|
+
<g transform={`translate(${margins.left},${margins.top})`}>
|
|
43
|
+
<Axis
|
|
44
|
+
position="bottom"
|
|
45
|
+
length={chartWidth}
|
|
46
|
+
scale={scaleRef.current as AnyScale}
|
|
47
|
+
x={0}
|
|
48
|
+
y={chartHeight}
|
|
49
|
+
tickValues={4}
|
|
50
|
+
/>
|
|
51
|
+
</g>
|
|
52
|
+
</svg>
|
|
53
|
+
</>
|
|
54
|
+
)}
|
|
55
|
+
</StyledBarChart>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React, { forwardRef, RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
import { BarChartProps } from './types';
|
|
4
|
+
import { StyledBarChart } from './styles';
|
|
5
|
+
|
|
6
|
+
export const LoadingBarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
7
|
+
(props, ref) => {
|
|
8
|
+
const { className, ...forwardedProps } = props;
|
|
9
|
+
|
|
10
|
+
return (
|
|
11
|
+
<StyledBarChart
|
|
12
|
+
{...forwardedProps}
|
|
13
|
+
className={className}
|
|
14
|
+
ref={ref as RefObject<HTMLDivElement>}
|
|
15
|
+
>
|
|
16
|
+
Loading...
|
|
17
|
+
</StyledBarChart>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
);
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React, { forwardRef, RefObject, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { scaleLinear as d3scaleLinear, ScaleLinear as d3ScaleLinear, sum as d3sum } from 'd3';
|
|
3
|
+
|
|
4
|
+
import { AnyScale, CategoryData } from '../../types';
|
|
5
|
+
import { BarChartProps } from './types';
|
|
6
|
+
import { StyledBarChart } from './styles';
|
|
7
|
+
import { Bar } from '../Bar';
|
|
8
|
+
import { Axis } from '../Axis';
|
|
9
|
+
import { useFormatCategoricalData } from '../../hooks';
|
|
10
|
+
import { sizeToDimension } from './utils';
|
|
11
|
+
|
|
12
|
+
interface RenderedBarChartProps extends Required<Pick<BarChartProps, 'data'>>, Omit<BarChartProps, 'data'> {}
|
|
13
|
+
|
|
14
|
+
export const RenderedBarChart = forwardRef<HTMLDivElement, RenderedBarChartProps>((props, ref) => {
|
|
15
|
+
const {
|
|
16
|
+
areXLabelsRotated,
|
|
17
|
+
barProps,
|
|
18
|
+
caping,
|
|
19
|
+
className,
|
|
20
|
+
data: propsData,
|
|
21
|
+
id,
|
|
22
|
+
isBarSelected,
|
|
23
|
+
labelDecorator,
|
|
24
|
+
onBarClick,
|
|
25
|
+
others,
|
|
26
|
+
size,
|
|
27
|
+
barRole,
|
|
28
|
+
theme,
|
|
29
|
+
tooltipVariant,
|
|
30
|
+
...forwardedProps
|
|
31
|
+
} = props;
|
|
32
|
+
|
|
33
|
+
const cache = useRef<CategoryData>();
|
|
34
|
+
|
|
35
|
+
const { data, colorScale } = useFormatCategoricalData({
|
|
36
|
+
data: propsData,
|
|
37
|
+
theme: theme!,
|
|
38
|
+
caping,
|
|
39
|
+
others,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
cache.current = data;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const chartDimensions = sizeToDimension(size!);
|
|
47
|
+
const width = chartDimensions.width;
|
|
48
|
+
const height = chartDimensions.height;
|
|
49
|
+
const margins = { top: 16, left: 16, right: 16, bottom: 32 };
|
|
50
|
+
const chartHeight = height - margins.top - margins.bottom;
|
|
51
|
+
const chartWidth = width - margins.left - margins.right;
|
|
52
|
+
const numberOfRows = data.filter((datum) => datum.value).length;
|
|
53
|
+
const gap = 5;
|
|
54
|
+
|
|
55
|
+
const scale = useMemo<d3ScaleLinear<number, number>>(
|
|
56
|
+
() =>
|
|
57
|
+
d3scaleLinear()
|
|
58
|
+
.domain([0, Math.max(...data.map(({ value }) => value))])
|
|
59
|
+
.range([0, chartWidth]),
|
|
60
|
+
[size]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const barHeight = (chartHeight - (numberOfRows + 1) * gap) / numberOfRows;
|
|
64
|
+
const total = d3sum(data, (d) => d.value);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<StyledBarChart {...forwardedProps} id={id} className={className} ref={ref as RefObject<HTMLDivElement>}>
|
|
68
|
+
<svg width={width} height={height}>
|
|
69
|
+
<g transform={`translate(${margins.left},${margins.top})`}>
|
|
70
|
+
<Axis
|
|
71
|
+
position="bottom"
|
|
72
|
+
length={chartWidth}
|
|
73
|
+
scale={scale as AnyScale}
|
|
74
|
+
x={0}
|
|
75
|
+
y={chartHeight}
|
|
76
|
+
tickValues={4}
|
|
77
|
+
tickRotation={areXLabelsRotated ? -45 : 0}
|
|
78
|
+
/>
|
|
79
|
+
{data
|
|
80
|
+
.filter((datum) => datum.value)
|
|
81
|
+
.map((datum, index) => {
|
|
82
|
+
const percent = datum.value / total;
|
|
83
|
+
const to = {
|
|
84
|
+
data: {
|
|
85
|
+
...datum,
|
|
86
|
+
percent,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
const from = cache.current ? { data: cache.current[index] } : undefined;
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<Bar
|
|
93
|
+
{...barProps}
|
|
94
|
+
color={colorScale?.(to.data.key)!}
|
|
95
|
+
data={to}
|
|
96
|
+
gap={gap}
|
|
97
|
+
height={barHeight}
|
|
98
|
+
id={`id${id}__bar-${index}`}
|
|
99
|
+
index={index}
|
|
100
|
+
isSelected={Boolean(isBarSelected!(to))}
|
|
101
|
+
key={`bar _${index}`}
|
|
102
|
+
labelDecorator={labelDecorator}
|
|
103
|
+
onClick={onBarClick}
|
|
104
|
+
previousData={from!}
|
|
105
|
+
role={barRole}
|
|
106
|
+
scale={scale!}
|
|
107
|
+
tooltipVariant={tooltipVariant}
|
|
108
|
+
/>
|
|
109
|
+
);
|
|
110
|
+
})}
|
|
111
|
+
</g>
|
|
112
|
+
</svg>
|
|
113
|
+
</StyledBarChart>
|
|
114
|
+
);
|
|
115
|
+
});
|