@redsift/charts 8.0.0 → 8.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.
Files changed (218) hide show
  1. package/coverage/clover.xml +1070 -0
  2. package/coverage/coverage-final.json +60 -0
  3. package/coverage/lcov-report/ChartEmptyState/ChartEmptyState.tsx.html +679 -0
  4. package/coverage/lcov-report/ChartEmptyState/index.html +146 -0
  5. package/coverage/lcov-report/ChartEmptyState/index.ts.html +91 -0
  6. package/coverage/lcov-report/ChartEmptyState/styles.ts.html +184 -0
  7. package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChart.tsx.html +775 -0
  8. package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChartAxisBottom.tsx.html +259 -0
  9. package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChartBar.tsx.html +421 -0
  10. package/coverage/lcov-report/HorizontalBarChart/index.html +191 -0
  11. package/coverage/lcov-report/HorizontalBarChart/index.ts.html +94 -0
  12. package/coverage/lcov-report/HorizontalBarChart/styles.ts.html +451 -0
  13. package/coverage/lcov-report/HorizontalBarChart/types.ts.html +433 -0
  14. package/coverage/lcov-report/PieChart/PieChart.tsx.html +1105 -0
  15. package/coverage/lcov-report/PieChart/PieChartArc.tsx.html +412 -0
  16. package/coverage/lcov-report/PieChart/index.html +176 -0
  17. package/coverage/lcov-report/PieChart/index.ts.html +94 -0
  18. package/coverage/lcov-report/PieChart/styles.ts.html +616 -0
  19. package/coverage/lcov-report/PieChart/types.ts.html +472 -0
  20. package/coverage/lcov-report/PieChart.tsx.html +1045 -0
  21. package/coverage/lcov-report/PieChartArc.tsx.html +271 -0
  22. package/coverage/lcov-report/StaticPieChart.tsx.html +286 -0
  23. package/coverage/lcov-report/base.css +224 -0
  24. package/coverage/lcov-report/block-navigation.js +87 -0
  25. package/coverage/lcov-report/components/Arc/Arc.tsx.html +304 -0
  26. package/coverage/lcov-report/components/Arc/index.html +146 -0
  27. package/coverage/lcov-report/components/Arc/index.ts.html +94 -0
  28. package/coverage/lcov-report/components/Arc/styles.ts.html +208 -0
  29. package/coverage/lcov-report/components/Arcs/Arcs.tsx.html +427 -0
  30. package/coverage/lcov-report/components/Arcs/index.html +146 -0
  31. package/coverage/lcov-report/components/Arcs/index.ts.html +94 -0
  32. package/coverage/lcov-report/components/Arcs/styles.ts.html +106 -0
  33. package/coverage/lcov-report/components/Axis/Axis.tsx.html +754 -0
  34. package/coverage/lcov-report/components/Axis/computeTicks.ts.html +481 -0
  35. package/coverage/lcov-report/components/Axis/index.html +176 -0
  36. package/coverage/lcov-report/components/Axis/index.ts.html +94 -0
  37. package/coverage/lcov-report/components/Axis/styles.ts.html +148 -0
  38. package/coverage/lcov-report/components/Axis/types.ts.html +253 -0
  39. package/coverage/lcov-report/components/Bar/Bar.tsx.html +421 -0
  40. package/coverage/lcov-report/components/Bar/index.html +161 -0
  41. package/coverage/lcov-report/components/Bar/index.ts.html +94 -0
  42. package/coverage/lcov-report/components/Bar/styles.ts.html +247 -0
  43. package/coverage/lcov-report/components/Bar/types.ts.html +178 -0
  44. package/coverage/lcov-report/components/BarChart/BarChart.tsx.html +355 -0
  45. package/coverage/lcov-report/components/BarChart/EmptyBarChart.tsx.html +259 -0
  46. package/coverage/lcov-report/components/BarChart/LoadingBarChart.tsx.html +145 -0
  47. package/coverage/lcov-report/components/BarChart/RenderedBarChart.tsx.html +496 -0
  48. package/coverage/lcov-report/components/BarChart/index.html +206 -0
  49. package/coverage/lcov-report/components/BarChart/index.ts.html +94 -0
  50. package/coverage/lcov-report/components/BarChart/styles.ts.html +190 -0
  51. package/coverage/lcov-report/components/BarChart/utils.ts.html +145 -0
  52. package/coverage/lcov-report/components/ChartContainer/ChartContainer.tsx.html +433 -0
  53. package/coverage/lcov-report/components/ChartContainer/index.html +146 -0
  54. package/coverage/lcov-report/components/ChartContainer/index.ts.html +94 -0
  55. package/coverage/lcov-report/components/ChartContainer/intl/index.html +116 -0
  56. package/coverage/lcov-report/components/ChartContainer/intl/index.ts.html +106 -0
  57. package/coverage/lcov-report/components/ChartContainer/styles.ts.html +211 -0
  58. package/coverage/lcov-report/components/DataPoint/DataPoint.tsx.html +442 -0
  59. package/coverage/lcov-report/components/DataPoint/index.html +146 -0
  60. package/coverage/lcov-report/components/DataPoint/index.ts.html +94 -0
  61. package/coverage/lcov-report/components/DataPoint/styles.ts.html +109 -0
  62. package/coverage/lcov-report/components/Dot/Dot.tsx.html +232 -0
  63. package/coverage/lcov-report/components/Dot/index.html +146 -0
  64. package/coverage/lcov-report/components/Dot/index.ts.html +94 -0
  65. package/coverage/lcov-report/components/Dot/styles.ts.html +184 -0
  66. package/coverage/lcov-report/components/Legend/Legend.tsx.html +268 -0
  67. package/coverage/lcov-report/components/Legend/index.html +146 -0
  68. package/coverage/lcov-report/components/Legend/index.ts.html +94 -0
  69. package/coverage/lcov-report/components/Legend/styles.ts.html +130 -0
  70. package/coverage/lcov-report/components/LegendItem/LegendItem.tsx.html +403 -0
  71. package/coverage/lcov-report/components/LegendItem/index.html +146 -0
  72. package/coverage/lcov-report/components/LegendItem/index.ts.html +94 -0
  73. package/coverage/lcov-report/components/LegendItem/styles.ts.html +205 -0
  74. package/coverage/lcov-report/components/PieChart/EmptyPieChart.tsx.html +343 -0
  75. package/coverage/lcov-report/components/PieChart/LoadingPieChart.tsx.html +145 -0
  76. package/coverage/lcov-report/components/PieChart/PieChart.tsx.html +388 -0
  77. package/coverage/lcov-report/components/PieChart/RenderedPieChart.tsx.html +571 -0
  78. package/coverage/lcov-report/components/PieChart/index.html +221 -0
  79. package/coverage/lcov-report/components/PieChart/index.ts.html +94 -0
  80. package/coverage/lcov-report/components/PieChart/styles.ts.html +376 -0
  81. package/coverage/lcov-report/components/PieChart/types.ts.html +352 -0
  82. package/coverage/lcov-report/components/PieChart/utils.ts.html +199 -0
  83. package/coverage/lcov-report/components/ScatterPlot/EmptyScatterPlot.tsx.html +295 -0
  84. package/coverage/lcov-report/components/ScatterPlot/LoadingScatterPlot.tsx.html +145 -0
  85. package/coverage/lcov-report/components/ScatterPlot/RenderedScatterPlot.tsx.html +910 -0
  86. package/coverage/lcov-report/components/ScatterPlot/ScatterPlot.tsx.html +379 -0
  87. package/coverage/lcov-report/components/ScatterPlot/index.html +221 -0
  88. package/coverage/lcov-report/components/ScatterPlot/index.ts.html +94 -0
  89. package/coverage/lcov-report/components/ScatterPlot/styles.ts.html +196 -0
  90. package/coverage/lcov-report/components/ScatterPlot/types.ts.html +376 -0
  91. package/coverage/lcov-report/components/ScatterPlot/utils.ts.html +388 -0
  92. package/coverage/lcov-report/favicon.png +0 -0
  93. package/coverage/lcov-report/hooks/index.html +176 -0
  94. package/coverage/lcov-report/hooks/index.ts.html +97 -0
  95. package/coverage/lcov-report/hooks/useBrush.tsx.html +430 -0
  96. package/coverage/lcov-report/hooks/useColor.tsx.html +163 -0
  97. package/coverage/lcov-report/hooks/useFormatCategoricalData.tsx.html +289 -0
  98. package/coverage/lcov-report/hooks/useZoom.tsx.html +235 -0
  99. package/coverage/lcov-report/index.html +311 -0
  100. package/coverage/lcov-report/index.ts.html +94 -0
  101. package/coverage/lcov-report/prettify.css +1 -0
  102. package/coverage/lcov-report/prettify.js +2 -0
  103. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  104. package/coverage/lcov-report/sorter.js +196 -0
  105. package/coverage/lcov-report/styles.ts.html +631 -0
  106. package/coverage/lcov-report/types.ts.html +457 -0
  107. package/coverage/lcov.info +2272 -0
  108. package/dist/index.js +6326 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/package.json +98 -0
  111. package/index.ts +1 -0
  112. package/jest.config.js +3 -0
  113. package/package.json +2 -3
  114. package/rollup.config.js +13 -0
  115. package/src/components/Arc/Arc.test.tsx +92 -0
  116. package/src/components/Arc/Arc.tsx +73 -0
  117. package/src/components/Arc/index.ts +3 -0
  118. package/src/components/Arc/styles.ts +41 -0
  119. package/src/components/Arc/types.ts +18 -0
  120. package/src/components/Arcs/Arcs.stories.tsx +177 -0
  121. package/src/components/Arcs/Arcs.tsx +114 -0
  122. package/src/components/Arcs/index.ts +3 -0
  123. package/src/components/Arcs/styles.ts +7 -0
  124. package/src/components/Arcs/types.ts +26 -0
  125. package/src/components/Axis/Axis.stories.tsx +297 -0
  126. package/src/components/Axis/Axis.tsx +223 -0
  127. package/src/components/Axis/computeTicks.ts +132 -0
  128. package/src/components/Axis/index.ts +3 -0
  129. package/src/components/Axis/styles.ts +21 -0
  130. package/src/components/Axis/types.ts +56 -0
  131. package/src/components/Bar/Bar.stories.tsx +152 -0
  132. package/src/components/Bar/Bar.test.tsx +158 -0
  133. package/src/components/Bar/Bar.tsx +112 -0
  134. package/src/components/Bar/index.ts +3 -0
  135. package/src/components/Bar/styles.ts +54 -0
  136. package/src/components/Bar/types.ts +31 -0
  137. package/src/components/BarChart/BarChart.stories.tsx +80 -0
  138. package/src/components/BarChart/BarChart.test.tsx +189 -0
  139. package/src/components/BarChart/BarChart.tsx +90 -0
  140. package/src/components/BarChart/EmptyBarChart.tsx +58 -0
  141. package/src/components/BarChart/LoadingBarChart.tsx +20 -0
  142. package/src/components/BarChart/RenderedBarChart.tsx +137 -0
  143. package/src/components/BarChart/__snapshots__/BarChart.test.tsx.snap +6932 -0
  144. package/src/components/BarChart/index.ts +3 -0
  145. package/src/components/BarChart/styles.ts +35 -0
  146. package/src/components/BarChart/types.ts +58 -0
  147. package/src/components/BarChart/utils.ts +20 -0
  148. package/src/components/ChartContainer/ChartContainer.stories.tsx +81 -0
  149. package/src/components/ChartContainer/ChartContainer.test.tsx +75 -0
  150. package/src/components/ChartContainer/ChartContainer.tsx +116 -0
  151. package/src/components/ChartContainer/index.ts +3 -0
  152. package/src/components/ChartContainer/intl/en-US.json +3 -0
  153. package/src/components/ChartContainer/intl/fr-FR.json +3 -0
  154. package/src/components/ChartContainer/intl/index.ts +7 -0
  155. package/src/components/ChartContainer/styles.ts +42 -0
  156. package/src/components/ChartContainer/types.ts +24 -0
  157. package/src/components/DataPoint/DataPoint.tsx +119 -0
  158. package/src/components/DataPoint/index.ts +3 -0
  159. package/src/components/DataPoint/styles.ts +8 -0
  160. package/src/components/DataPoint/types.ts +33 -0
  161. package/src/components/Dot/Dot.stories.tsx +157 -0
  162. package/src/components/Dot/Dot.test.tsx +136 -0
  163. package/src/components/Dot/Dot.tsx +49 -0
  164. package/src/components/Dot/index.ts +3 -0
  165. package/src/components/Dot/styles.ts +33 -0
  166. package/src/components/Dot/types.ts +16 -0
  167. package/src/components/Legend/Legend.stories.tsx +108 -0
  168. package/src/components/Legend/Legend.tsx +61 -0
  169. package/src/components/Legend/index.ts +3 -0
  170. package/src/components/Legend/styles.ts +15 -0
  171. package/src/components/Legend/types.ts +27 -0
  172. package/src/components/LegendItem/LegendItem.test.tsx +69 -0
  173. package/src/components/LegendItem/LegendItem.tsx +106 -0
  174. package/src/components/LegendItem/index.ts +3 -0
  175. package/src/components/LegendItem/styles.ts +40 -0
  176. package/src/components/LegendItem/types.ts +30 -0
  177. package/src/components/PieChart/EmptyPieChart.tsx +86 -0
  178. package/src/components/PieChart/LoadingPieChart.tsx +20 -0
  179. package/src/components/PieChart/PieChart.stories.tsx +91 -0
  180. package/src/components/PieChart/PieChart.test.tsx +201 -0
  181. package/src/components/PieChart/PieChart.tsx +101 -0
  182. package/src/components/PieChart/RenderedPieChart.tsx +162 -0
  183. package/src/components/PieChart/__snapshots__/PieChart.stories.storyshot +7843 -0
  184. package/src/components/PieChart/__snapshots__/PieChart.test.tsx.snap +20540 -0
  185. package/src/components/PieChart/index.ts +3 -0
  186. package/src/components/PieChart/styles.ts +97 -0
  187. package/src/components/PieChart/types.ts +89 -0
  188. package/src/components/PieChart/utils.ts +38 -0
  189. package/src/components/ScatterPlot/EmptyScatterPlot.tsx +70 -0
  190. package/src/components/ScatterPlot/LoadingScatterPlot.tsx +20 -0
  191. package/src/components/ScatterPlot/RenderedScatterPlot.tsx +275 -0
  192. package/src/components/ScatterPlot/ScatterPlot.stories.tsx +95 -0
  193. package/src/components/ScatterPlot/ScatterPlot.test.tsx +44 -0
  194. package/src/components/ScatterPlot/ScatterPlot.tsx +98 -0
  195. package/src/components/ScatterPlot/__snapshots__/ScatterPlot.test.tsx.snap +88352 -0
  196. package/src/components/ScatterPlot/index.ts +3 -0
  197. package/src/components/ScatterPlot/styles.ts +37 -0
  198. package/src/components/ScatterPlot/types.ts +97 -0
  199. package/src/components/ScatterPlot/utils.ts +101 -0
  200. package/src/config.ts +10 -0
  201. package/src/hooks/index.ts +4 -0
  202. package/src/hooks/useBrush.tsx +115 -0
  203. package/src/hooks/useColor.tsx +26 -0
  204. package/src/hooks/useFormatCategoricalData.tsx +68 -0
  205. package/src/hooks/useZoom.tsx +50 -0
  206. package/src/index.ts +15 -0
  207. package/src/scheme.ts +221 -0
  208. package/src/types/data.ts +52 -0
  209. package/src/types/index.ts +5 -0
  210. package/src/types/legend.ts +19 -0
  211. package/src/types/scale.ts +79 -0
  212. package/src/types/size.ts +17 -0
  213. package/src/types/theme.ts +27 -0
  214. package/tsconfig.json +3 -0
  215. package/index.js +0 -2606
  216. package/index.js.map +0 -1
  217. /package/{CONTRIBUTING.md → dist/CONTRIBUTING.md} +0 -0
  218. /package/{index.d.ts → dist/index.d.ts} +0 -0
@@ -0,0 +1,56 @@
1
+ import { ComponentProps } from 'react';
2
+ import { ValueOf } from '@redsift/design-system';
3
+ import { AnyScale, TicksSpec } from '../../types';
4
+
5
+ /**
6
+ * Component variant.
7
+ */
8
+ export const AxisVariant = {
9
+ none: 'none',
10
+ line: 'line',
11
+ lineTick: 'lineTick',
12
+ tick: 'tick',
13
+ tickValue: 'tickValue',
14
+ default: 'default',
15
+ } as const;
16
+ export type AxisVariant = ValueOf<typeof AxisVariant>;
17
+
18
+ export const AxisPosition = {
19
+ top: 'top',
20
+ right: 'right',
21
+ bottom: 'bottom',
22
+ left: 'left',
23
+ };
24
+ export type AxisPosition = ValueOf<typeof AxisPosition>;
25
+
26
+ /**
27
+ * Component props.
28
+ */
29
+ export interface AxisProps extends Omit<ComponentProps<'g'>, 'scale'> {
30
+ areLabelsRotated?: boolean;
31
+ format?: string;
32
+ legend?: string;
33
+ legendOffset?: number;
34
+ legendPosition?: 'start' | 'middle' | 'end';
35
+ /** Length of the axis. */
36
+ length?: number;
37
+ /** Position of axis. top|bottom means this is an x axis, right|left means this is an y axis. */
38
+ position?: AxisPosition;
39
+ /** Scale (d3.js) used to generate the axis. */
40
+ scale: AnyScale;
41
+ tickPadding?: number;
42
+ tickRotation?: number;
43
+ tickSize?: number;
44
+ tickValues?: TicksSpec;
45
+ /** Variant. */
46
+ variant?: AxisVariant;
47
+ /** X position. */
48
+ x?: number;
49
+ /** Y position. */
50
+ y?: number;
51
+ }
52
+
53
+ export type StyledAxisProps = Omit<
54
+ AxisProps,
55
+ 'areXLabelsRotated' | 'size' | 'scale'
56
+ > & {};
@@ -0,0 +1,152 @@
1
+ import React from 'react';
2
+ // @ts-ignore
3
+ import type { StoryObj } from '@storybook/react';
4
+ import { scaleLinear as d3scaleLinear } from 'd3';
5
+
6
+ import { Bar, BarOrientation, BarProps } from '.';
7
+ import { BarDatum, TooltipVariant } from '../../types';
8
+
9
+ const meta = {
10
+ title: 'Charts/Core/Bar',
11
+ component: Bar,
12
+ };
13
+
14
+ export default meta;
15
+ type Story = StoryObj<BarProps>;
16
+
17
+ const scale = d3scaleLinear().domain([0, 1000]).range([0, 500]);
18
+
19
+ export const Simple: Story = {
20
+ render: () => (
21
+ <>
22
+ {Object.keys(BarOrientation).map((orientation) => (
23
+ <svg width={500} height={500} key={orientation}>
24
+ <Bar
25
+ orientation={orientation}
26
+ data={{
27
+ data: {
28
+ key: 'Croissant',
29
+ value: 333,
30
+ },
31
+ }}
32
+ scale={scale}
33
+ />
34
+ </svg>
35
+ ))}
36
+ </>
37
+ ),
38
+ };
39
+
40
+ export const Clickable: Story = {
41
+ render: () => (
42
+ <>
43
+ {Object.keys(BarOrientation).map((orientation) => (
44
+ <svg width={500} height={500} key={orientation}>
45
+ <Bar
46
+ index={0}
47
+ isSelected={false}
48
+ onClick={(datum) => console.log(datum)}
49
+ orientation={orientation}
50
+ data={{
51
+ data: {
52
+ key: 'Button',
53
+ value: 150,
54
+ },
55
+ }}
56
+ scale={scale}
57
+ />
58
+ <Bar
59
+ index={1}
60
+ isSelected
61
+ onClick={(datum) => {
62
+ window.open(
63
+ `https://en.wikipedia.org/wiki/${datum.data.key}`,
64
+ '_blank'
65
+ );
66
+ }}
67
+ orientation={orientation}
68
+ data={{
69
+ data: {
70
+ key: 'Link',
71
+ value: 333,
72
+ },
73
+ }}
74
+ role="link"
75
+ scale={scale}
76
+ />
77
+ </svg>
78
+ ))}
79
+ </>
80
+ ),
81
+ };
82
+
83
+ export const Selectable: Story = {
84
+ render: () => (
85
+ <>
86
+ {Object.keys(BarOrientation).map((orientation) => (
87
+ <svg width={500} height={500} key={orientation}>
88
+ <Bar
89
+ index={0}
90
+ isSelected={false}
91
+ orientation={orientation}
92
+ data={{
93
+ data: {
94
+ key: 'Deselected',
95
+ value: 150,
96
+ },
97
+ }}
98
+ role="option"
99
+ scale={scale}
100
+ />
101
+ <Bar
102
+ index={1}
103
+ isSelected
104
+ orientation={orientation}
105
+ data={{
106
+ data: {
107
+ key: 'Selected',
108
+ value: 333,
109
+ },
110
+ }}
111
+ role="option"
112
+ scale={scale}
113
+ />
114
+ </svg>
115
+ ))}
116
+ </>
117
+ ),
118
+ };
119
+
120
+ export const WithTooltip: Story = {
121
+ render: () => (
122
+ <>
123
+ {Object.keys(BarOrientation).map((orientation) => (
124
+ <svg width={500} height={500} key={orientation}>
125
+ {Object.keys(TooltipVariant).map((tooltipVariant, index) => (
126
+ <Bar
127
+ index={index}
128
+ isSelected
129
+ key={tooltipVariant}
130
+ labelDecorator={(datum: BarDatum) =>
131
+ `${datum.data.key
132
+ .charAt(0)
133
+ .toUpperCase()}${datum.data.key.slice(1)}`
134
+ }
135
+ orientation={orientation}
136
+ data={{
137
+ data: {
138
+ key: tooltipVariant,
139
+ value: 333,
140
+ percent: 0.05,
141
+ },
142
+ }}
143
+ tooltipVariant={tooltipVariant as TooltipVariant}
144
+ role="option"
145
+ scale={scale}
146
+ />
147
+ ))}
148
+ </svg>
149
+ ))}
150
+ </>
151
+ ),
152
+ };
@@ -0,0 +1,158 @@
1
+ import React from 'react';
2
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { scaleLinear as d3scaleLinear } from 'd3';
5
+
6
+ import { Bar } from './Bar';
7
+ import { TooltipVariant } from '../../types';
8
+
9
+ jest.mock('react', () => ({
10
+ ...jest.requireActual('react'),
11
+ useId: () => '123',
12
+ }));
13
+
14
+ const scale = d3scaleLinear().domain([0, 1000]).range([0, 500]);
15
+
16
+ describe('Bar', () => {
17
+ const onClickSpy = jest.fn();
18
+
19
+ afterEach(() => {
20
+ onClickSpy.mockClear();
21
+ });
22
+
23
+ it('supports click', async () => {
24
+ const { getByRole } = render(
25
+ <svg width={500} height={500}>
26
+ <Bar
27
+ id="123"
28
+ index={0}
29
+ data={{
30
+ data: {
31
+ key: 'Croissant',
32
+ value: 333,
33
+ },
34
+ }}
35
+ scale={scale}
36
+ onClick={onClickSpy}
37
+ />
38
+ </svg>
39
+ );
40
+
41
+ const bar = getByRole('button');
42
+ fireEvent.click(bar);
43
+ expect(onClickSpy).toHaveBeenCalledWith({
44
+ data: { key: 'Croissant', value: 333 },
45
+ });
46
+ });
47
+
48
+ it('supports keydown', async () => {
49
+ const { getByRole } = render(
50
+ <svg width={500} height={500}>
51
+ <Bar
52
+ id="123"
53
+ index={0}
54
+ data={{
55
+ data: {
56
+ key: 'Croissant',
57
+ value: 333,
58
+ },
59
+ }}
60
+ scale={scale}
61
+ onClick={onClickSpy}
62
+ orientation="vertical"
63
+ />
64
+ </svg>
65
+ );
66
+
67
+ const bar = getByRole('button');
68
+ fireEvent.keyDown(bar, { key: 'Enter', code: 'Enter', charCode: 13 });
69
+ expect(onClickSpy).toHaveBeenCalledWith({
70
+ data: { key: 'Croissant', value: 333 },
71
+ });
72
+ });
73
+
74
+ it('shows a tooltip on hover', async () => {
75
+ const user = userEvent.setup();
76
+ const { getByRole } = render(
77
+ <svg width={500} height={500}>
78
+ <Bar
79
+ id="123"
80
+ index={0}
81
+ // The onClick gives the bar the role "button" which is then used to access the bars.
82
+ onClick={onClickSpy}
83
+ data={{
84
+ data: {
85
+ key: 'Croissant',
86
+ value: 333,
87
+ },
88
+ }}
89
+ scale={scale}
90
+ tooltipVariant={TooltipVariant.label}
91
+ />
92
+ </svg>
93
+ );
94
+
95
+ const bar = getByRole('button');
96
+ await user.hover(bar);
97
+ await waitFor(() => {
98
+ expect(screen.getByRole('tooltip', { exact: true })).toBeInTheDocument();
99
+ });
100
+ const tooltip = screen.getByRole('tooltip');
101
+ expect(tooltip.textContent).toContain('Croissant');
102
+ });
103
+
104
+ it.each`
105
+ Type | props | result
106
+ ${'with label'} | ${{ data: { data: { key: 'Croissant', value: 333 } }, tooltipVariant: TooltipVariant.label }} | ${'Croissant'}
107
+ ${'with value'} | ${{ data: { data: { key: 'Croissant', value: 333 } }, tooltipVariant: TooltipVariant.value }} | ${'Croissant - 333'}
108
+ ${'with percent'} | ${{ data: { data: { key: 'Croissant', value: 333, percent: 0.05 } }, tooltipVariant: TooltipVariant.percent }} | ${'Croissant - 5%'}
109
+ ${'with empty value if there is a label'} | ${{ data: { data: { key: 'Croissant', value: 0 } }, tooltipVariant: TooltipVariant.value }} | ${'Croissant'}
110
+ `('it shows a tooltip on hover $Type', async function ({ props, result }) {
111
+ const user = userEvent.setup();
112
+ const { getByRole } = render(
113
+ <svg width={500} height={500}>
114
+ <Bar
115
+ id="123"
116
+ index={0}
117
+ // The onClick gives the bar the role "button" which is then used to access the bars.
118
+ onClick={onClickSpy}
119
+ scale={scale}
120
+ {...props}
121
+ />
122
+ </svg>
123
+ );
124
+
125
+ const bar = getByRole('button');
126
+ await user.hover(bar);
127
+ await waitFor(() => {
128
+ expect(screen.getByRole('tooltip', { exact: true })).toBeInTheDocument();
129
+ });
130
+ const tooltip = screen.getByRole('tooltip');
131
+ expect(tooltip.textContent).toBe(result);
132
+ });
133
+
134
+ it.each`
135
+ Type | props
136
+ ${'with variant label when label is empty'} | ${{ data: { data: { key: '', value: 333 } }, tooltipVariant: TooltipVariant.label }}
137
+ `('it does not show a tooltip on hover $Type', async function ({ props }) {
138
+ const user = userEvent.setup();
139
+ const { getByRole } = render(
140
+ <svg width={500} height={500}>
141
+ <Bar
142
+ id="123"
143
+ index={0}
144
+ // The onClick gives the bar the role "button" which is then used to access the bars.
145
+ onClick={onClickSpy}
146
+ scale={scale}
147
+ {...props}
148
+ />
149
+ </svg>
150
+ );
151
+
152
+ const bar = getByRole('button');
153
+ await user.hover(bar);
154
+ expect(() => screen.getByRole('tooltip')).toThrow(
155
+ 'Unable to find an accessible element with the role "tooltip"'
156
+ );
157
+ });
158
+ });
@@ -0,0 +1,112 @@
1
+ import React, { forwardRef } from 'react';
2
+ import classNames from 'classnames';
3
+ import { animated, useSpring } from '@react-spring/web';
4
+ import { interpolate as d3interpolate } from 'd3';
5
+
6
+ import { Comp } from '@redsift/design-system';
7
+
8
+ import { BarOrientation, BarProps } from './types';
9
+ import { StyledBar } from './styles';
10
+ import { monochrome } from '../../scheme';
11
+ import { config } from '../../config';
12
+
13
+ const COMPONENT_NAME = 'Bar';
14
+ const CLASSNAME = 'redsift-bar';
15
+ const DEFAULT_PROPS: Partial<BarProps> = {
16
+ color: monochrome,
17
+ index: 0,
18
+ gap: 5,
19
+ height: 40,
20
+ orientation: BarOrientation.horizontal,
21
+ previousData: {
22
+ data: {
23
+ key: '',
24
+ value: 0,
25
+ },
26
+ },
27
+ width: 40,
28
+ };
29
+
30
+ export const Bar: Comp<BarProps, SVGGElement> = forwardRef((props, ref) => {
31
+ const {
32
+ color,
33
+ data,
34
+ index,
35
+ isSelected: propsIsSelected,
36
+ labelDecorator,
37
+ onClick,
38
+ role,
39
+ } = props;
40
+
41
+ const {
42
+ className,
43
+ gap,
44
+ height,
45
+ orientation,
46
+ previousData,
47
+ scale,
48
+ width,
49
+ ...forwardedProps
50
+ } = props;
51
+
52
+ const interpolator = d3interpolate(
53
+ previousData!.data?.value || 0,
54
+ data.data.value
55
+ );
56
+
57
+ const text = labelDecorator ? labelDecorator(data) : data.data.key;
58
+ const isSelectable = role === 'option';
59
+ const isDeselected = isSelectable && propsIsSelected === false;
60
+
61
+ const animatedProps = useSpring({
62
+ ...config,
63
+ to: async (next: (props?: object) => Promise<void>) => {
64
+ await next({ t: 1 });
65
+ },
66
+ from: { t: 0 },
67
+ });
68
+
69
+ return (
70
+ <StyledBar
71
+ {...forwardedProps}
72
+ ref={ref}
73
+ className={classNames(Bar.className, className)}
74
+ transform={
75
+ orientation === BarOrientation.horizontal
76
+ ? `translate(0, ${gap! * (index! + 1) + height! * index!})`
77
+ : `translate(${gap! * (index! + 1) + width! * index!}, 0)`
78
+ }
79
+ $clickable={Boolean(onClick)}
80
+ >
81
+ {orientation === BarOrientation.horizontal ? (
82
+ <animated.rect
83
+ height={height}
84
+ fill={isDeselected ? 'var(--redsift-color-neutral-lightgrey)' : color}
85
+ width={
86
+ config.immediate
87
+ ? scale(data.data.value)
88
+ : animatedProps.t.to((t) => scale(interpolator(t)))
89
+ }
90
+ />
91
+ ) : (
92
+ <animated.rect
93
+ height={
94
+ config.immediate
95
+ ? scale(data.data.value)
96
+ : animatedProps.t.to((t) => scale(interpolator(t)))
97
+ }
98
+ fill={isDeselected ? 'var(--redsift-color-neutral-lightgrey)' : color}
99
+ width={width}
100
+ />
101
+ )}
102
+ {orientation === BarOrientation.horizontal ? (
103
+ <text x="10" y={height! / 2} dy="0.35em" aria-hidden={true}>
104
+ {text}
105
+ </text>
106
+ ) : null}
107
+ </StyledBar>
108
+ );
109
+ });
110
+ Bar.className = CLASSNAME;
111
+ Bar.defaultProps = DEFAULT_PROPS;
112
+ Bar.displayName = COMPONENT_NAME;
@@ -0,0 +1,3 @@
1
+ export * from './Bar';
2
+ export * from './types';
3
+ export * from './styles';
@@ -0,0 +1,54 @@
1
+ import styled, { css } from 'styled-components';
2
+ import { StyledBarProps } from './types';
3
+ import { DataPoint } from '../DataPoint';
4
+
5
+ /**
6
+ * Component style.
7
+ */
8
+ export const StyledBar = styled(DataPoint)<StyledBarProps>`
9
+ rect {
10
+ fill-opacity: 0.9;
11
+ }
12
+
13
+ ${({ $clickable }) =>
14
+ $clickable
15
+ ? css`
16
+ cursor: pointer;
17
+
18
+ &:hover,
19
+ &:focus,
20
+ &.focused {
21
+ outline: none;
22
+ rect {
23
+ fill-opacity: 0.7;
24
+ }
25
+ }
26
+
27
+ // &:focus-visible {
28
+ // rect {
29
+ // stroke: var(--redsift-color-default-primary);
30
+ // stroke-width: 4px;
31
+ // paint-order: stroke;
32
+ // }
33
+ // }
34
+
35
+ @media (prefers-reduced-motion: no-preference) {
36
+ &:focus-visible,
37
+ &.focused {
38
+ outline: 2px solid var(--redsift-color-default-primary);
39
+ transition: outline-offset 75ms ease-out;
40
+ }
41
+
42
+ &:not(&:active)&:focus-visible,
43
+ &:not(&:active)&.focused {
44
+ transition-duration: 0.25s;
45
+ }
46
+ }
47
+
48
+ &:not(&:active)&:focus-visible,
49
+ &:not(&:active)&.focused {
50
+ outline-offset: 0.2rem;
51
+ }
52
+ `
53
+ : ''}}
54
+ `;
@@ -0,0 +1,31 @@
1
+ import { ScaleLinear as d3ScaleLinear } from 'd3';
2
+ import { ValueOf } from '@redsift/design-system';
3
+ import { BarDatum } from '../../types';
4
+ import { DataPointProps, StyledDataPointProps } from '../DataPoint';
5
+
6
+ /**
7
+ * Component variant.
8
+ */
9
+ export const BarOrientation = {
10
+ horizontal: 'horizontal',
11
+ vertical: 'vertical',
12
+ };
13
+ export type BarOrientation = ValueOf<typeof BarOrientation>;
14
+
15
+ /**
16
+ * Component props.
17
+ */
18
+ export interface BarProps extends DataPointProps<BarDatum> {
19
+ /** Gap between two siblings. */
20
+ gap?: number;
21
+ /** Height of the bar in horizontal orientation. */
22
+ height?: number;
23
+ /** Orientation of the bar. */
24
+ orientation?: BarOrientation;
25
+ /** A linear continuous scale defined over a numeric domain used to determine the width or height of the bar (depending on the orientation). */
26
+ scale: d3ScaleLinear<number, number, never>;
27
+ /** Width of the bar in vertical orientation. */
28
+ width?: number;
29
+ }
30
+
31
+ export type StyledBarProps = StyledDataPointProps & Omit<BarProps, 'scale'>;
@@ -0,0 +1,80 @@
1
+ import { Meta, StoryObj } from '@storybook/react';
2
+
3
+ import { DataVizColorPalette } from '@redsift/design-system';
4
+ import { BarChart } from '.';
5
+ import {
6
+ CategoryData,
7
+ ChartSize,
8
+ ChartTheme,
9
+ ColorTheme,
10
+ TooltipVariant,
11
+ } from '../../types';
12
+
13
+ const themes = {
14
+ ...ColorTheme,
15
+ 'success-warning-danger': {
16
+ success: 'Bread',
17
+ warning: 'Tea',
18
+ danger: 'Coffee',
19
+ },
20
+ 'success-warning-danger-neutral': {
21
+ success: 'Bread',
22
+ warning: 'Tea',
23
+ danger: 'Coffee',
24
+ neutral: 'Pastry',
25
+ },
26
+ custom: {
27
+ Coffee: DataVizColorPalette.purple,
28
+ Bread: DataVizColorPalette.pink,
29
+ Tea: DataVizColorPalette.aqua,
30
+ },
31
+ };
32
+
33
+ const meta: Meta<typeof BarChart> = {
34
+ title: 'Charts/BarChart',
35
+ component: BarChart as any,
36
+ argTypes: {
37
+ tooltipVariant: {
38
+ options: Object.values(TooltipVariant),
39
+ control: { type: 'select' },
40
+ },
41
+ size: {
42
+ options: Object.values(ChartSize),
43
+ control: { type: 'select' },
44
+ },
45
+ theme: {
46
+ options: Object.keys(themes),
47
+ mapping: themes,
48
+ control: {
49
+ type: 'select',
50
+ },
51
+ },
52
+ },
53
+ };
54
+ export default meta;
55
+ type Story = StoryObj<typeof BarChart>;
56
+
57
+ const data: CategoryData = [
58
+ { key: 'Cookies', value: 540 },
59
+ { key: 'Coffee', value: 5471 },
60
+ { key: 'Bread', value: 3325 },
61
+ { key: 'Tea', value: 1435 },
62
+ { key: 'Pastry', value: 856 },
63
+ { key: 'Sandwich', value: 771 },
64
+ { key: 'Croissant', value: 771 },
65
+ { key: 'Hot chocolate', value: 588 },
66
+ { key: 'Toast', value: 318 },
67
+ ];
68
+
69
+ export const Controls: Story = {
70
+ args: {
71
+ title: 'Bar Chart',
72
+ tooltipVariant: 'value',
73
+ size: 'medium',
74
+ theme: 'success-warning-danger-neutral' as ChartTheme,
75
+ caping: 4,
76
+ others: true,
77
+ onBarClick: (datum) => console.log(datum),
78
+ data: data,
79
+ },
80
+ };