@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,3 @@
1
+ export * from './PieChart';
2
+ export * from './types';
3
+ export * from './styles';
@@ -0,0 +1,97 @@
1
+ import styled, { css } from 'styled-components';
2
+ import { StyledPieChartProps } from './types';
3
+ import { ChartContainer } from '../ChartContainer';
4
+
5
+ /**
6
+ * Component style.
7
+ */
8
+ export const StyledPieChart = styled(ChartContainer)<StyledPieChartProps>`
9
+ .redsift-piechart__chart-wrapper {
10
+ position: relative;
11
+ }
12
+ `;
13
+
14
+ export const StyledPieChartCenterText = styled.div<{
15
+ $maxWidth: number;
16
+ $textSize: number;
17
+ $smallTextSize: number;
18
+ }>`
19
+ position: absolute;
20
+ top: 0;
21
+ left: 0;
22
+ height: 100%;
23
+ width: 100%;
24
+ display: flex;
25
+ flex-direction: column;
26
+ justify-content: center;
27
+ align-items: center;
28
+ pointer-events: none;
29
+
30
+ > * {
31
+ max-width: ${({ $maxWidth }) => $maxWidth}px;
32
+ }
33
+
34
+ > b {
35
+ font-family: var(--redsift-typography-font-family-source-code-pro);
36
+ color: var(--redsift-color-neutral-black);
37
+ font-weight: var(--redsift-typography-font-weight-medium);
38
+ font-size: ${({ $textSize }) => $textSize}px;
39
+ line-height: ${({ $textSize }) => $textSize}px;
40
+
41
+ &:nth-child(2) {
42
+ font-weight: normal;
43
+ margin-top: 8px;
44
+ font-family: var(--redsift-typography-font-family-raleway);
45
+ font-size: ${({ $smallTextSize }) => $smallTextSize}px;
46
+ line-height: ${({ $smallTextSize }) => $smallTextSize}px;
47
+ }
48
+ }
49
+ > span {
50
+ color: var(--redsift-color-neutral-black);
51
+ font-size: ${({ $textSize }) => $textSize}px;
52
+ line-height: ${({ $textSize }) => $textSize}px;
53
+ &:nth-child(3) {
54
+ margin-top: 4px;
55
+ font-size: ${({ $smallTextSize }) => $smallTextSize}px;
56
+ line-height: ${({ $smallTextSize }) => $smallTextSize}px;
57
+ }
58
+ }
59
+ > b + span {
60
+ font-size: var(--redsift-typography-body-font-size);
61
+ font-family: var(--redsift-typography-font-family-raleway);
62
+ line-height: var(--redsift-typography-badge-line-height);
63
+ color: rgba(0, 0, 0, 0.6);
64
+ text-align: center;
65
+ }
66
+ `;
67
+
68
+ export const StyledPieChartEmptyText = styled.div<{
69
+ $maxWidth: number;
70
+ $textSize: number;
71
+ $isDonut: boolean;
72
+ }>`
73
+ position: absolute;
74
+ top: 0;
75
+ left: 0;
76
+ height: 100%;
77
+ width: 100%;
78
+ display: flex;
79
+ flex-direction: column;
80
+ justify-content: center;
81
+ align-items: center;
82
+ pointer-events: none;
83
+
84
+ > * {
85
+ max-width: ${({ $maxWidth }) => $maxWidth}px;
86
+ }
87
+
88
+ > span {
89
+ font-family: var(--redsift-typography-font-family-raleway);
90
+ color: ${({ $isDonut }) =>
91
+ $isDonut
92
+ ? css`var(--redsift-color-neutral-midgrey)`
93
+ : css`var(--redsift-color-neutral-black)`};
94
+ font-size: ${({ $textSize }) => $textSize}px;
95
+ line-height: ${({ $textSize }) => $textSize}px;
96
+ }
97
+ `;
@@ -0,0 +1,89 @@
1
+ import {
2
+ ChartDimensions,
3
+ ChartSize,
4
+ ChartTheme,
5
+ ArcDatum,
6
+ TooltipVariant,
7
+ CategoryData,
8
+ } from '../../types';
9
+ import { ValueOf } from '@redsift/design-system';
10
+ import { ArcProps } from '../Arc';
11
+ import { ChartContainerProps } from '../ChartContainer';
12
+ import { ComponentProps, ReactNode } from 'react';
13
+
14
+ /**
15
+ * Component variant.
16
+ */
17
+ export const PieChartVariant = {
18
+ plain: 'plain',
19
+ spaced: 'spaced',
20
+ donut: 'donut',
21
+ spacedDonut: 'spacedDonut',
22
+ } as const;
23
+ export type PieChartVariant = ValueOf<typeof PieChartVariant>;
24
+
25
+ /**
26
+ * Component's labels variant.
27
+ */
28
+ export const PieChartLabelVariant = {
29
+ none: 'none',
30
+ internal: 'internal',
31
+ externalLabel: 'externalLabel',
32
+ externalLabelValue: 'externalLabelValue',
33
+ externalLabelPercent: 'externalLabelPercent',
34
+ } as const;
35
+ export type PieChartLabelVariant = ValueOf<typeof PieChartLabelVariant>;
36
+
37
+ interface LocaleText {
38
+ emptyChartText: string;
39
+ }
40
+
41
+ export type PieChartDimensions = ChartDimensions & {
42
+ smallFontSize: number;
43
+ fontSize: number;
44
+ innerRadius: number;
45
+ };
46
+
47
+ /**
48
+ * Component props.
49
+ */
50
+ export interface PieChartProps extends ChartContainerProps {
51
+ /** Number of categories to use, the rest being put into a new category called "Others". */
52
+ caping?: number;
53
+ /** Dataset to use to generate the chart, should be a list of objects containing a key and a value. */
54
+ data?: CategoryData;
55
+ /** Component to use if the chart is empty (replacing the default one). */
56
+ emptyComponent?: ReactNode;
57
+ /** Method to determine whether a slice is selected or not. */
58
+ isSliceSelected?: (datum: ArcDatum) => boolean | undefined;
59
+ /** Method to override the data labels. */
60
+ labelDecorator?: (datum: ArcDatum) => string;
61
+ /** Define whether the labels should be displayed inside or outside the charts and if they should contain raw or percentage values. */
62
+ labelVariant?: PieChartLabelVariant;
63
+ /** Labels and texts. */
64
+ localeText?: LocaleText;
65
+ /** Method to be called on a click on a slice. */
66
+ onSliceClick?: (datum: ArcDatum) => void;
67
+ /** Whether a category should be displayed for categories that has been removed by the caping option. Optionaly, this prop can be used to change the label of this category. */
68
+ others?: boolean | string;
69
+ /** PieChart size. */
70
+ size?: ChartSize | PieChartDimensions;
71
+ /** Native HTML props to forward to each slice. */
72
+ sliceProps?: Omit<ComponentProps<'g'>, 'onClick' | 'ref' | 'width'>;
73
+ /** Slice role. Used to indicate that slices are to be considered buttons or links. If an onClick is provided, the slices will have the role `button` unless specifically set to `link` with this prop. */
74
+ sliceRole?: ArcProps['role'];
75
+ /** Secondary text to be displayed in the middle of the chart, between the main text and subtext. Recommended to be used with donut variants only. */
76
+ middleText?: string;
77
+ /** Secondary text to be displayed in the middle of the chart, above the main text. Recommended to be used with `donut` variants only. */
78
+ subtext?: string;
79
+ /** Main text to be displayed in the middle of the chart. Recommended to be used with `donut` variants only. */
80
+ text?: string;
81
+ /** Color palette to use. You can choose among the list of available color palette or also choose to use the success/warning/danger theme for which you have to specify which key corresponds to which status. */
82
+ theme?: ChartTheme;
83
+ /** PieChart variant. */
84
+ variant?: PieChartVariant;
85
+ /** Tooltip variant. */
86
+ tooltipVariant?: TooltipVariant;
87
+ }
88
+
89
+ export type StyledPieChartProps = PieChartProps & {};
@@ -0,0 +1,38 @@
1
+ import { ChartSize } from '../../types';
2
+ import { PieChartDimensions } from './types';
3
+
4
+ export const sizeToDimension = (
5
+ size: ChartSize | PieChartDimensions
6
+ ): PieChartDimensions => {
7
+ if (typeof size !== 'string') {
8
+ return size;
9
+ }
10
+
11
+ switch (size) {
12
+ case ChartSize.small:
13
+ return {
14
+ width: 200,
15
+ height: 200,
16
+ smallFontSize: 13,
17
+ fontSize: 30,
18
+ innerRadius: 55,
19
+ };
20
+ case ChartSize.large:
21
+ return {
22
+ width: 300,
23
+ height: 300,
24
+ smallFontSize: 18,
25
+ fontSize: 38,
26
+ innerRadius: 80,
27
+ };
28
+ case ChartSize.medium:
29
+ default:
30
+ return {
31
+ width: 240,
32
+ height: 240,
33
+ smallFontSize: 14,
34
+ fontSize: 34,
35
+ innerRadius: 65,
36
+ };
37
+ }
38
+ };
@@ -0,0 +1,70 @@
1
+ import React, { forwardRef, RefObject, useEffect, useRef } from 'react';
2
+ import { scaleLinear as d3scaleLinear } from 'd3';
3
+
4
+ import { AnyScale, JSONArray } from '../../types';
5
+ import { ScatterPlotProps } from './types';
6
+ import { StyledScatterPlot, StyledScatterPlotEmptyText } from './styles';
7
+ import { Axis } from '../Axis';
8
+ import { sizeToDimension } from './utils';
9
+
10
+ export const EmptyScatterPlot = forwardRef<HTMLDivElement, ScatterPlotProps>(
11
+ (props, ref) => {
12
+ const {
13
+ className,
14
+ data: propsData,
15
+ emptyComponent,
16
+ localeText,
17
+ size,
18
+ ...forwardedProps
19
+ } = props;
20
+
21
+ const cache = useRef<JSONArray>();
22
+
23
+ const data = propsData;
24
+
25
+ useEffect(() => {
26
+ cache.current = data;
27
+ });
28
+
29
+ const chartDimensions = sizeToDimension(size!);
30
+ const width = chartDimensions.width;
31
+ const height = chartDimensions.height;
32
+ const margins = { top: 16, left: 48, right: 16, bottom: 48 };
33
+ const chartWidth = width - margins.left - margins.right;
34
+
35
+ return (
36
+ <StyledScatterPlot
37
+ {...forwardedProps}
38
+ className={className}
39
+ ref={ref as RefObject<HTMLDivElement>}
40
+ >
41
+ {emptyComponent ?? (
42
+ <>
43
+ <StyledScatterPlotEmptyText
44
+ $maxWidth={width}
45
+ $textSize={chartDimensions.fontSize / 2}
46
+ >
47
+ <span>{localeText?.emptyChartText}</span>
48
+ </StyledScatterPlotEmptyText>
49
+ <svg width={width} height={height}>
50
+ <g transform={`translate(${margins.left},${margins.top})`}>
51
+ <Axis
52
+ position="bottom"
53
+ length={width - 32}
54
+ scale={
55
+ d3scaleLinear()
56
+ .domain([])
57
+ .range([0, chartWidth]) as AnyScale
58
+ }
59
+ x={0}
60
+ y={height}
61
+ tickValues={4}
62
+ />
63
+ </g>
64
+ </svg>
65
+ </>
66
+ )}
67
+ </StyledScatterPlot>
68
+ );
69
+ }
70
+ );
@@ -0,0 +1,20 @@
1
+ import React, { forwardRef, RefObject } from 'react';
2
+
3
+ import { ScatterPlotProps } from './types';
4
+ import { StyledScatterPlot } from './styles';
5
+
6
+ export const LoadingScatterPlot = forwardRef<HTMLDivElement, ScatterPlotProps>(
7
+ (props, ref) => {
8
+ const { className, ...forwardedProps } = props;
9
+
10
+ return (
11
+ <StyledScatterPlot
12
+ {...forwardedProps}
13
+ className={className}
14
+ ref={ref as RefObject<HTMLDivElement>}
15
+ >
16
+ Loading...
17
+ </StyledScatterPlot>
18
+ );
19
+ }
20
+ );
@@ -0,0 +1,275 @@
1
+ import React, { forwardRef, RefObject, useEffect, useRef } from 'react';
2
+ import {
3
+ BrushBehavior as d3BrushBehavior,
4
+ descending as d3descending,
5
+ extent as d3extent,
6
+ max as d3max,
7
+ min as d3min,
8
+ scaleLinear as d3scaleLinear,
9
+ scaleSqrt as d3scaleSqrt,
10
+ select as d3select,
11
+ } from 'd3';
12
+
13
+ import { AnyScale, JSONArray, LabelVariant } from '../../types';
14
+ import {
15
+ ScatterPlotLabelVariant,
16
+ ScatterPlotProps,
17
+ ScatterPlotVariant,
18
+ } from './types';
19
+ import { StyledScatterPlot } from './styles';
20
+ import { useZoom, useColor, useBrush } from '../../hooks';
21
+ import { Axis } from '../Axis';
22
+ import { Legend } from '../Legend';
23
+ import { Dot } from '../Dot';
24
+ import { BASE_NUMBER_OF_TICKS, countBy, group, sizeToDimension } from './utils';
25
+
26
+ interface RenderedScatterPlotProps
27
+ extends Required<Pick<ScatterPlotProps, 'data'>>,
28
+ Omit<ScatterPlotProps, 'data'> {}
29
+
30
+ export const RenderedScatterPlot = forwardRef<
31
+ HTMLDivElement,
32
+ RenderedScatterPlotProps
33
+ >((props, ref) => {
34
+ const {
35
+ isBrushable,
36
+ className,
37
+ data: propsData,
38
+ dotRole,
39
+ id,
40
+ isDotSelected,
41
+ labelDecorator,
42
+ labelVariant,
43
+ legendProps,
44
+ onBrush,
45
+ onBrushEnd,
46
+ onDotClick,
47
+ size,
48
+ theme,
49
+ tooltipVariant,
50
+ variant,
51
+ xAxisVariant,
52
+ yAxisVariant,
53
+ ...forwardedProps
54
+ } = props;
55
+
56
+ const cache = useRef<JSONArray>();
57
+
58
+ const data = propsData.filter(
59
+ (datum) =>
60
+ datum.value &&
61
+ typeof datum.key[0] === 'number' &&
62
+ !Number.isNaN(datum.key[0]) &&
63
+ typeof datum.key[1] === 'number' &&
64
+ !Number.isNaN(datum.key[1])
65
+ );
66
+
67
+ useEffect(() => {
68
+ cache.current = data;
69
+ });
70
+
71
+ const chartDimensions = sizeToDimension(size!);
72
+ const width = chartDimensions.width;
73
+ const height = chartDimensions.height;
74
+ const margins = { top: 16, left: 48, right: 16, bottom: 48 };
75
+ const chartHeight = height - margins.top - margins.bottom;
76
+ const chartWidth = width - margins.left - margins.right;
77
+ const minSize = d3min([chartWidth, chartHeight]) as number;
78
+
79
+ const extendedScaleX = (() => {
80
+ const domain = d3extent(data, (d) => d.key[0]) as [number, number];
81
+ const gap = domain[1] - domain[0];
82
+ domain[0] -= gap * 0.1;
83
+ domain[1] += gap * 0.1;
84
+ return d3scaleLinear().domain(domain).range([0, chartWidth]).nice();
85
+ })();
86
+ const extendedScaleY = (() => {
87
+ const domain = d3extent(data, (d) => d.key[1]) as [number, number];
88
+ const gap = domain[1] - domain[0];
89
+ domain[0] -= gap * 0.1;
90
+ domain[1] += gap * 0.1;
91
+ return d3scaleLinear().domain(domain).range([chartHeight, 0]).nice();
92
+ })();
93
+
94
+ const hasCategory =
95
+ data[0] &&
96
+ data[0].key.length >= 3 &&
97
+ data[0].key[2] !== null &&
98
+ data[0].key[2] !== undefined;
99
+ const countsByCategory = hasCategory ? countBy(data) : undefined;
100
+ const colorScale = useColor({
101
+ data: countsByCategory || data,
102
+ theme: theme!,
103
+ category: 'key',
104
+ });
105
+
106
+ const svgRef = useRef<SVGSVGElement>();
107
+ const brushRef = useRef<d3BrushBehavior<unknown>>();
108
+ const svgBrushRef = useRef<SVGGElement>();
109
+ const { scaleX, scaleY, transform } = useZoom({
110
+ svgRef,
111
+ scaleX: extendedScaleX,
112
+ scaleY: extendedScaleY,
113
+ size,
114
+ onZoom: () => {
115
+ if (brushRef.current && svgBrushRef.current) {
116
+ brushRef.current.clear(d3select(svgBrushRef.current));
117
+
118
+ onBrushEnd?.(null);
119
+ }
120
+ },
121
+ });
122
+ useBrush({
123
+ brushRef,
124
+ svgRef: svgBrushRef,
125
+ extent: [
126
+ [0, 0],
127
+ [chartWidth, chartHeight],
128
+ ],
129
+ scaleX,
130
+ scaleY,
131
+ isBrushable,
132
+ isGridded: variant === ScatterPlotVariant.gridded,
133
+ onBrush,
134
+ onBrushEnd,
135
+ });
136
+
137
+ const circles =
138
+ variant === ScatterPlotVariant.gridded
139
+ ? group(data, scaleX, scaleY).sort((a, b) =>
140
+ d3descending(a.points.length, b.points.length)
141
+ )
142
+ : data.map((d) => ({
143
+ category: d.key[2],
144
+ x: d.key[0],
145
+ y: d.key[1],
146
+ data: {
147
+ key: d.key,
148
+ value: 1,
149
+ },
150
+ points: [d],
151
+ }));
152
+ const globalSize =
153
+ variant === ScatterPlotVariant.gridded
154
+ ? d3scaleSqrt()
155
+ .domain([1, Math.max(d3max(circles, (d) => d.points.length)!, 1)])
156
+ .range([3, minSize / BASE_NUMBER_OF_TICKS / 2])
157
+ : undefined;
158
+ const legendWidth = useRef<string>();
159
+ if (countsByCategory && !legendWidth.current) {
160
+ legendWidth.current = `${
161
+ Math.max(
162
+ ...countsByCategory.map((d) => d.key.length + String(d.value).length)
163
+ ) *
164
+ 8 +
165
+ 32
166
+ }px`;
167
+ }
168
+
169
+ return (
170
+ <StyledScatterPlot
171
+ {...forwardedProps}
172
+ id={id}
173
+ className={className}
174
+ ref={ref as RefObject<HTMLDivElement>}
175
+ >
176
+ <svg
177
+ ref={svgRef as RefObject<SVGSVGElement>}
178
+ width={width}
179
+ height={height}
180
+ >
181
+ <g
182
+ ref={svgBrushRef as RefObject<SVGGElement>}
183
+ transform={`translate(${margins.left},${margins.top})`}
184
+ />
185
+ <g transform={`translate(${margins.left},${margins.top})`}>
186
+ <g>
187
+ {circles.map((circle, index) => {
188
+ const scaleXDomain = scaleX.domain();
189
+ const scaleYDomain = scaleY.domain();
190
+
191
+ if (
192
+ circle.x! < scaleXDomain[0] ||
193
+ circle.x! > scaleXDomain[1] ||
194
+ circle.y! < scaleYDomain[0] ||
195
+ circle.y! > scaleYDomain[1]
196
+ ) {
197
+ return null;
198
+ }
199
+
200
+ const to = {
201
+ ...circle,
202
+ data: {
203
+ ...circle.data,
204
+ percent: circle.category
205
+ ? circle.data.value /
206
+ countsByCategory?.find(
207
+ ({ key }) => key === circle.category
208
+ )?.value!
209
+ : undefined,
210
+ },
211
+ r:
212
+ variant === ScatterPlotVariant.gridded
213
+ ? globalSize!(circle.points.length) /
214
+ (transform.k < 1 ? transform.k : 1)
215
+ : 4,
216
+ };
217
+
218
+ return (
219
+ <Dot
220
+ color={colorScale?.(circle.category!)}
221
+ data={to}
222
+ isSelected={Boolean(isDotSelected?.(to))}
223
+ key={`circle _${index}`}
224
+ labelDecorator={labelDecorator}
225
+ onClick={onDotClick}
226
+ role={dotRole}
227
+ scaleX={scaleX}
228
+ scaleY={scaleY}
229
+ tooltipVariant={tooltipVariant}
230
+ />
231
+ );
232
+ })}
233
+ </g>
234
+ </g>
235
+ <g transform={`translate(${margins.left},${margins.top})`}>
236
+ <Axis
237
+ position="left"
238
+ length={chartHeight}
239
+ scale={scaleY as AnyScale}
240
+ x={0}
241
+ y={0}
242
+ tickValues={8}
243
+ variant={xAxisVariant}
244
+ />
245
+ <Axis
246
+ position="bottom"
247
+ length={chartWidth}
248
+ scale={scaleX as AnyScale}
249
+ x={0}
250
+ y={chartHeight}
251
+ tickValues={8}
252
+ variant={yAxisVariant}
253
+ />
254
+ </g>
255
+ </svg>
256
+ {hasCategory && labelVariant !== ScatterPlotLabelVariant.none ? (
257
+ <Legend
258
+ data={countsByCategory!.map((d) => ({
259
+ ...d,
260
+ color: colorScale?.(d.key)!,
261
+ }))}
262
+ variant={
263
+ labelVariant === ScatterPlotLabelVariant.externalLabelValue
264
+ ? LabelVariant.value
265
+ : labelVariant === ScatterPlotLabelVariant.externalLabelPercent
266
+ ? LabelVariant.percent
267
+ : LabelVariant.label
268
+ }
269
+ width={legendWidth.current}
270
+ {...legendProps}
271
+ />
272
+ ) : null}
273
+ </StyledScatterPlot>
274
+ );
275
+ });
@@ -0,0 +1,95 @@
1
+ import { Meta, StoryObj } from '@storybook/react';
2
+
3
+ import mock from '../../../../../.storybook/static/mocks/bakery-trimmed.json';
4
+
5
+ import { DataVizColorPalette } from '@redsift/design-system';
6
+ import { ScatterPlot } from '.';
7
+ import { ScatterPlotLabelVariant, ScatterPlotVariant } from './types';
8
+ import {
9
+ ChartSize,
10
+ ColorTheme,
11
+ CoordinatesCategoryData,
12
+ CoordinatesCategoryDim,
13
+ TooltipVariant,
14
+ } from '../../types';
15
+ import { AxisVariant } from '../Axis';
16
+
17
+ export 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: DataVizColorPalette.purple,
32
+ Bread: DataVizColorPalette.pink,
33
+ Tea: DataVizColorPalette.aqua,
34
+ },
35
+ };
36
+
37
+ const meta: Meta<typeof ScatterPlot> = {
38
+ title: 'Charts/ScatterPlot',
39
+ component: ScatterPlot as any,
40
+ argTypes: {
41
+ variant: {
42
+ options: Object.values(ScatterPlotVariant),
43
+ control: { type: 'select' },
44
+ },
45
+ labelVariant: {
46
+ options: Object.values(ScatterPlotLabelVariant),
47
+ control: { type: 'select' },
48
+ },
49
+ tooltipVariant: {
50
+ options: Object.values(TooltipVariant),
51
+ control: { type: 'select' },
52
+ },
53
+ size: {
54
+ options: Object.values(ChartSize),
55
+ control: { type: 'select' },
56
+ },
57
+ theme: {
58
+ options: Object.keys(themes),
59
+ mapping: themes,
60
+ control: {
61
+ type: 'select',
62
+ },
63
+ },
64
+ xAxisVariant: {
65
+ options: Object.values(AxisVariant),
66
+ control: { type: 'select' },
67
+ },
68
+ yAxisVariant: {
69
+ options: Object.values(AxisVariant),
70
+ control: { type: 'select' },
71
+ },
72
+ },
73
+ };
74
+ export default meta;
75
+ type Story = StoryObj<typeof ScatterPlot>;
76
+
77
+ const data: CoordinatesCategoryData = mock.map((d) => ({
78
+ key: [+d.x!, +d.y!, d.Items] as CoordinatesCategoryDim,
79
+ value: 1,
80
+ }));
81
+
82
+ export const Controls: Story = {
83
+ args: {
84
+ title: 'Scatter Plot',
85
+ variant: 'gridded',
86
+ labelVariant: 'externalLabelValue',
87
+ tooltipVariant: 'value',
88
+ size: 'medium',
89
+ theme: 'default',
90
+ isBrushable: true,
91
+ xAxisVariant: 'default',
92
+ yAxisVariant: 'default',
93
+ data: data,
94
+ },
95
+ };