@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.
- package/coverage/clover.xml +1070 -0
- package/coverage/coverage-final.json +60 -0
- package/coverage/lcov-report/ChartEmptyState/ChartEmptyState.tsx.html +679 -0
- package/coverage/lcov-report/ChartEmptyState/index.html +146 -0
- package/coverage/lcov-report/ChartEmptyState/index.ts.html +91 -0
- package/coverage/lcov-report/ChartEmptyState/styles.ts.html +184 -0
- package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChart.tsx.html +775 -0
- package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChartAxisBottom.tsx.html +259 -0
- package/coverage/lcov-report/HorizontalBarChart/HorizontalBarChartBar.tsx.html +421 -0
- package/coverage/lcov-report/HorizontalBarChart/index.html +191 -0
- package/coverage/lcov-report/HorizontalBarChart/index.ts.html +94 -0
- package/coverage/lcov-report/HorizontalBarChart/styles.ts.html +451 -0
- package/coverage/lcov-report/HorizontalBarChart/types.ts.html +433 -0
- package/coverage/lcov-report/PieChart/PieChart.tsx.html +1105 -0
- package/coverage/lcov-report/PieChart/PieChartArc.tsx.html +412 -0
- package/coverage/lcov-report/PieChart/index.html +176 -0
- package/coverage/lcov-report/PieChart/index.ts.html +94 -0
- package/coverage/lcov-report/PieChart/styles.ts.html +616 -0
- package/coverage/lcov-report/PieChart/types.ts.html +472 -0
- package/coverage/lcov-report/PieChart.tsx.html +1045 -0
- package/coverage/lcov-report/PieChartArc.tsx.html +271 -0
- package/coverage/lcov-report/StaticPieChart.tsx.html +286 -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 +427 -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 +355 -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 +496 -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/ChartContainer/ChartContainer.tsx.html +433 -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 +211 -0
- package/coverage/lcov-report/components/DataPoint/DataPoint.tsx.html +442 -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/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 +403 -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 +388 -0
- package/coverage/lcov-report/components/PieChart/RenderedPieChart.tsx.html +571 -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 +376 -0
- package/coverage/lcov-report/components/PieChart/types.ts.html +352 -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 +910 -0
- package/coverage/lcov-report/components/ScatterPlot/ScatterPlot.tsx.html +379 -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 +196 -0
- package/coverage/lcov-report/components/ScatterPlot/types.ts.html +376 -0
- package/coverage/lcov-report/components/ScatterPlot/utils.ts.html +388 -0
- package/coverage/lcov-report/favicon.png +0 -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/useZoom.tsx.html +235 -0
- package/coverage/lcov-report/index.html +311 -0
- package/coverage/lcov-report/index.ts.html +94 -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-report/styles.ts.html +631 -0
- package/coverage/lcov-report/types.ts.html +457 -0
- package/coverage/lcov.info +2272 -0
- package/dist/index.js +6326 -0
- package/dist/index.js.map +1 -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 +92 -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 +114 -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 +158 -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 +80 -0
- package/src/components/BarChart/BarChart.test.tsx +189 -0
- package/src/components/BarChart/BarChart.tsx +90 -0
- package/src/components/BarChart/EmptyBarChart.tsx +58 -0
- package/src/components/BarChart/LoadingBarChart.tsx +20 -0
- package/src/components/BarChart/RenderedBarChart.tsx +137 -0
- package/src/components/BarChart/__snapshots__/BarChart.test.tsx.snap +6932 -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 +75 -0
- package/src/components/ChartContainer/ChartContainer.tsx +116 -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 +42 -0
- package/src/components/ChartContainer/types.ts +24 -0
- package/src/components/DataPoint/DataPoint.tsx +119 -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 +106 -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 +91 -0
- package/src/components/PieChart/PieChart.test.tsx +201 -0
- package/src/components/PieChart/PieChart.tsx +101 -0
- package/src/components/PieChart/RenderedPieChart.tsx +162 -0
- package/src/components/PieChart/__snapshots__/PieChart.stories.storyshot +7843 -0
- package/src/components/PieChart/__snapshots__/PieChart.test.tsx.snap +20540 -0
- package/src/components/PieChart/index.ts +3 -0
- package/src/components/PieChart/styles.ts +97 -0
- package/src/components/PieChart/types.ts +89 -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 +275 -0
- package/src/components/ScatterPlot/ScatterPlot.stories.tsx +95 -0
- package/src/components/ScatterPlot/ScatterPlot.test.tsx +44 -0
- package/src/components/ScatterPlot/ScatterPlot.tsx +98 -0
- package/src/components/ScatterPlot/__snapshots__/ScatterPlot.test.tsx.snap +88352 -0
- package/src/components/ScatterPlot/index.ts +3 -0
- package/src/components/ScatterPlot/styles.ts +37 -0
- package/src/components/ScatterPlot/types.ts +97 -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 +221 -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/index.js +0 -2606
- package/index.js.map +0 -1
- /package/{CONTRIBUTING.md → dist/CONTRIBUTING.md} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ComponentProps } from 'react';
|
|
2
|
+
import { ArcProps } from '../Arc/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Component props.
|
|
6
|
+
*/
|
|
7
|
+
export interface ArcsProps
|
|
8
|
+
extends Omit<ComponentProps<'g'>, 'onClick' | 'role'>,
|
|
9
|
+
Pick<
|
|
10
|
+
ArcProps,
|
|
11
|
+
| 'id'
|
|
12
|
+
| 'labelDecorator'
|
|
13
|
+
| 'onClick'
|
|
14
|
+
| 'role'
|
|
15
|
+
| 'hasStroke'
|
|
16
|
+
| 'tooltipVariant'
|
|
17
|
+
> {
|
|
18
|
+
/** Arc props. */
|
|
19
|
+
arcs: Omit<ArcProps, 'ref'>[];
|
|
20
|
+
/** Whether arcs have internal labels or not. */
|
|
21
|
+
hasLabels?: boolean;
|
|
22
|
+
/** Native HTML props to forward to each slice. */
|
|
23
|
+
sliceProps?: Omit<ComponentProps<'g'>, 'onClick' | 'ref' | 'width'>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type StyledArcsProps = Omit<ArcsProps, 'arcs'> & {};
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import React, { RefObject, useRef } from 'react';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import type { StoryObj } from '@storybook/react';
|
|
4
|
+
import { scaleLinear as d3scaleLinear, scalePoint as d3scalePoint } from 'd3';
|
|
5
|
+
|
|
6
|
+
import { Axis, AxisPosition, AxisProps, AxisVariant } from '.';
|
|
7
|
+
import { Flexbox } from '@redsift/design-system';
|
|
8
|
+
import { useZoom } from '../../hooks';
|
|
9
|
+
import { AnyScale } from '../../types';
|
|
10
|
+
|
|
11
|
+
const meta = {
|
|
12
|
+
title: 'Charts/Core/Axis',
|
|
13
|
+
component: Axis,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default meta;
|
|
17
|
+
type Story = StoryObj<AxisProps>;
|
|
18
|
+
|
|
19
|
+
const scaleLinearX = d3scaleLinear().domain([0, 80]).range([0, 280]);
|
|
20
|
+
const scaleLinearY = d3scaleLinear().domain([0, 35]).range([160, 0]);
|
|
21
|
+
|
|
22
|
+
export const Linear: Story = {
|
|
23
|
+
render: () => (
|
|
24
|
+
<Flexbox gap="16px">
|
|
25
|
+
{[0, -45].map((tickRotation) => (
|
|
26
|
+
<svg
|
|
27
|
+
height={260}
|
|
28
|
+
style={{ border: '1px dashed black' }}
|
|
29
|
+
width={380}
|
|
30
|
+
key={`${tickRotation}`}
|
|
31
|
+
>
|
|
32
|
+
<g transform="translate(50,50)">
|
|
33
|
+
<Axis
|
|
34
|
+
scale={scaleLinearX as AnyScale}
|
|
35
|
+
position={AxisPosition.top}
|
|
36
|
+
x={0}
|
|
37
|
+
y={0}
|
|
38
|
+
length={280}
|
|
39
|
+
tickRotation={tickRotation}
|
|
40
|
+
/>
|
|
41
|
+
<Axis
|
|
42
|
+
scale={scaleLinearY as AnyScale}
|
|
43
|
+
position={AxisPosition.right}
|
|
44
|
+
x={280}
|
|
45
|
+
y={0}
|
|
46
|
+
length={160}
|
|
47
|
+
tickRotation={tickRotation}
|
|
48
|
+
/>
|
|
49
|
+
<Axis
|
|
50
|
+
scale={scaleLinearX as AnyScale}
|
|
51
|
+
position={AxisPosition.bottom}
|
|
52
|
+
x={0}
|
|
53
|
+
y={160}
|
|
54
|
+
length={280}
|
|
55
|
+
tickRotation={tickRotation}
|
|
56
|
+
/>
|
|
57
|
+
<Axis
|
|
58
|
+
scale={scaleLinearY as AnyScale}
|
|
59
|
+
position={AxisPosition.left}
|
|
60
|
+
x={0}
|
|
61
|
+
y={0}
|
|
62
|
+
length={160}
|
|
63
|
+
tickRotation={tickRotation}
|
|
64
|
+
/>
|
|
65
|
+
</g>
|
|
66
|
+
</svg>
|
|
67
|
+
))}
|
|
68
|
+
</Flexbox>
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const WithLegend: Story = {
|
|
73
|
+
render: () => (
|
|
74
|
+
<Flexbox gap="16px">
|
|
75
|
+
<svg height={260} style={{ border: '1px dashed black' }} width={380}>
|
|
76
|
+
<g transform="translate(50,50)">
|
|
77
|
+
<Axis
|
|
78
|
+
scale={scaleLinearX as AnyScale}
|
|
79
|
+
position={AxisPosition.top}
|
|
80
|
+
x={0}
|
|
81
|
+
y={32}
|
|
82
|
+
length={280}
|
|
83
|
+
legend="start"
|
|
84
|
+
legendPosition="start"
|
|
85
|
+
/>
|
|
86
|
+
<Axis
|
|
87
|
+
scale={scaleLinearX as AnyScale}
|
|
88
|
+
position={AxisPosition.top}
|
|
89
|
+
x={0}
|
|
90
|
+
y={96}
|
|
91
|
+
length={280}
|
|
92
|
+
legend="middle"
|
|
93
|
+
legendPosition="middle"
|
|
94
|
+
/>
|
|
95
|
+
<Axis
|
|
96
|
+
scale={scaleLinearX as AnyScale}
|
|
97
|
+
position={AxisPosition.top}
|
|
98
|
+
x={0}
|
|
99
|
+
y={160}
|
|
100
|
+
length={280}
|
|
101
|
+
legend="end"
|
|
102
|
+
legendPosition="end"
|
|
103
|
+
/>
|
|
104
|
+
</g>
|
|
105
|
+
</svg>
|
|
106
|
+
<svg height={260} style={{ border: '1px dashed black' }} width={380}>
|
|
107
|
+
<g transform="translate(50,50)">
|
|
108
|
+
<Axis
|
|
109
|
+
scale={scaleLinearY as AnyScale}
|
|
110
|
+
position={AxisPosition.left}
|
|
111
|
+
x={32}
|
|
112
|
+
y={0}
|
|
113
|
+
length={160}
|
|
114
|
+
legend="start"
|
|
115
|
+
legendPosition="start"
|
|
116
|
+
/>
|
|
117
|
+
<Axis
|
|
118
|
+
scale={scaleLinearY as AnyScale}
|
|
119
|
+
position={AxisPosition.left}
|
|
120
|
+
x={156}
|
|
121
|
+
y={0}
|
|
122
|
+
length={160}
|
|
123
|
+
legend="middle"
|
|
124
|
+
legendPosition="middle"
|
|
125
|
+
/>
|
|
126
|
+
<Axis
|
|
127
|
+
scale={scaleLinearY as AnyScale}
|
|
128
|
+
position={AxisPosition.left}
|
|
129
|
+
x={280}
|
|
130
|
+
y={0}
|
|
131
|
+
length={160}
|
|
132
|
+
legend="end"
|
|
133
|
+
legendPosition="end"
|
|
134
|
+
/>
|
|
135
|
+
</g>
|
|
136
|
+
</svg>
|
|
137
|
+
</Flexbox>
|
|
138
|
+
),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const AllVariants: Story = {
|
|
142
|
+
render: () => (
|
|
143
|
+
<Flexbox gap="16px">
|
|
144
|
+
{Object.keys(AxisVariant).map((variant) => (
|
|
145
|
+
<svg
|
|
146
|
+
height={260}
|
|
147
|
+
style={{ border: '1px dashed black' }}
|
|
148
|
+
width={380}
|
|
149
|
+
key={`${variant}`}
|
|
150
|
+
>
|
|
151
|
+
<g transform="translate(50,50)">
|
|
152
|
+
<Axis
|
|
153
|
+
scale={scaleLinearX as AnyScale}
|
|
154
|
+
position={AxisPosition.top}
|
|
155
|
+
x={0}
|
|
156
|
+
y={0}
|
|
157
|
+
length={280}
|
|
158
|
+
variant={variant as AxisVariant}
|
|
159
|
+
/>
|
|
160
|
+
<Axis
|
|
161
|
+
scale={scaleLinearY as AnyScale}
|
|
162
|
+
position={AxisPosition.right}
|
|
163
|
+
x={280}
|
|
164
|
+
y={0}
|
|
165
|
+
length={160}
|
|
166
|
+
variant={variant as AxisVariant}
|
|
167
|
+
/>
|
|
168
|
+
<Axis
|
|
169
|
+
scale={scaleLinearX as AnyScale}
|
|
170
|
+
position={AxisPosition.bottom}
|
|
171
|
+
x={0}
|
|
172
|
+
y={160}
|
|
173
|
+
length={280}
|
|
174
|
+
variant={variant as AxisVariant}
|
|
175
|
+
/>
|
|
176
|
+
<Axis
|
|
177
|
+
scale={scaleLinearY as AnyScale}
|
|
178
|
+
position={AxisPosition.left}
|
|
179
|
+
x={0}
|
|
180
|
+
y={0}
|
|
181
|
+
length={160}
|
|
182
|
+
variant={variant as AxisVariant}
|
|
183
|
+
/>
|
|
184
|
+
</g>
|
|
185
|
+
</svg>
|
|
186
|
+
))}
|
|
187
|
+
</Flexbox>
|
|
188
|
+
),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export const Zoomable: Story = {
|
|
192
|
+
render: () => {
|
|
193
|
+
const svgRef = useRef<SVGSVGElement>();
|
|
194
|
+
const { scaleX, scaleY } = useZoom({
|
|
195
|
+
svgRef,
|
|
196
|
+
scaleX: scaleLinearX,
|
|
197
|
+
scaleY: scaleLinearY,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<Flexbox gap="16px">
|
|
202
|
+
<svg
|
|
203
|
+
ref={svgRef as RefObject<SVGSVGElement>}
|
|
204
|
+
height={260}
|
|
205
|
+
style={{ border: '1px dashed black' }}
|
|
206
|
+
width={380}
|
|
207
|
+
>
|
|
208
|
+
<g transform="translate(50,50)">
|
|
209
|
+
<Axis
|
|
210
|
+
scale={scaleX as AnyScale}
|
|
211
|
+
position={AxisPosition.top}
|
|
212
|
+
x={0}
|
|
213
|
+
y={0}
|
|
214
|
+
length={280}
|
|
215
|
+
/>
|
|
216
|
+
<Axis
|
|
217
|
+
scale={scaleY as AnyScale}
|
|
218
|
+
position={AxisPosition.right}
|
|
219
|
+
x={280}
|
|
220
|
+
y={0}
|
|
221
|
+
length={160}
|
|
222
|
+
/>
|
|
223
|
+
<Axis
|
|
224
|
+
scale={scaleX as AnyScale}
|
|
225
|
+
position={AxisPosition.bottom}
|
|
226
|
+
x={0}
|
|
227
|
+
y={160}
|
|
228
|
+
length={280}
|
|
229
|
+
/>
|
|
230
|
+
<Axis
|
|
231
|
+
scale={scaleY as AnyScale}
|
|
232
|
+
position={AxisPosition.left}
|
|
233
|
+
x={0}
|
|
234
|
+
y={0}
|
|
235
|
+
length={160}
|
|
236
|
+
/>
|
|
237
|
+
</g>
|
|
238
|
+
</svg>
|
|
239
|
+
</Flexbox>
|
|
240
|
+
);
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const scalePointX = d3scalePoint()
|
|
245
|
+
.domain(['A', 'list', 'of', 'words', 'as', 'domain'])
|
|
246
|
+
.range([0, 280]);
|
|
247
|
+
const scalePointY = d3scalePoint().domain(['A', 'B', 'C']).range([160, 0]);
|
|
248
|
+
|
|
249
|
+
export const Point: Story = {
|
|
250
|
+
render: () => (
|
|
251
|
+
<Flexbox gap="16px">
|
|
252
|
+
{[0, -45].map((tickRotation) => (
|
|
253
|
+
<svg
|
|
254
|
+
height={260}
|
|
255
|
+
style={{ border: '1px dashed black' }}
|
|
256
|
+
width={380}
|
|
257
|
+
key={`${tickRotation}`}
|
|
258
|
+
>
|
|
259
|
+
<g transform="translate(50,50)">
|
|
260
|
+
<Axis
|
|
261
|
+
scale={scalePointX as AnyScale}
|
|
262
|
+
position={AxisPosition.top}
|
|
263
|
+
x={0}
|
|
264
|
+
y={0}
|
|
265
|
+
length={280}
|
|
266
|
+
tickRotation={tickRotation}
|
|
267
|
+
/>
|
|
268
|
+
<Axis
|
|
269
|
+
scale={scalePointY as AnyScale}
|
|
270
|
+
position={AxisPosition.right}
|
|
271
|
+
x={280}
|
|
272
|
+
y={0}
|
|
273
|
+
length={160}
|
|
274
|
+
tickRotation={tickRotation}
|
|
275
|
+
/>
|
|
276
|
+
<Axis
|
|
277
|
+
scale={scalePointX as AnyScale}
|
|
278
|
+
position={AxisPosition.bottom}
|
|
279
|
+
x={0}
|
|
280
|
+
y={160}
|
|
281
|
+
length={280}
|
|
282
|
+
tickRotation={tickRotation}
|
|
283
|
+
/>
|
|
284
|
+
<Axis
|
|
285
|
+
scale={scalePointY as AnyScale}
|
|
286
|
+
position={AxisPosition.left}
|
|
287
|
+
x={0}
|
|
288
|
+
y={0}
|
|
289
|
+
length={160}
|
|
290
|
+
tickRotation={tickRotation}
|
|
291
|
+
/>
|
|
292
|
+
</g>
|
|
293
|
+
</svg>
|
|
294
|
+
))}
|
|
295
|
+
</Flexbox>
|
|
296
|
+
),
|
|
297
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import React, { forwardRef, RefObject, useRef } from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
|
|
4
|
+
import { Comp } from '@redsift/design-system';
|
|
5
|
+
|
|
6
|
+
import { AxisProps, AxisPosition, AxisVariant } from './types';
|
|
7
|
+
import { StyledAxis } from './styles';
|
|
8
|
+
import { animated, useSpring, useTransition } from '@react-spring/web';
|
|
9
|
+
import { computeTicks } from './computeTicks';
|
|
10
|
+
import { config } from '../../config';
|
|
11
|
+
|
|
12
|
+
const COMPONENT_NAME = 'Axis';
|
|
13
|
+
const CLASSNAME = 'redsift-axis';
|
|
14
|
+
const DEFAULT_PROPS: Partial<AxisProps> = {
|
|
15
|
+
position: AxisPosition.bottom,
|
|
16
|
+
legendOffset: -32,
|
|
17
|
+
legendPosition: 'end',
|
|
18
|
+
tickPadding: 5,
|
|
19
|
+
tickRotation: 0,
|
|
20
|
+
tickSize: 5,
|
|
21
|
+
variant: AxisVariant.default,
|
|
22
|
+
x: 0,
|
|
23
|
+
y: 0,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getAxisType = (position: AxisPosition) =>
|
|
27
|
+
[AxisPosition.top, AxisPosition.bottom].includes(position) ? 'x' : 'y';
|
|
28
|
+
|
|
29
|
+
export const Axis: Comp<AxisProps, SVGGElement> = forwardRef((props, ref) => {
|
|
30
|
+
const {
|
|
31
|
+
className,
|
|
32
|
+
length,
|
|
33
|
+
legend,
|
|
34
|
+
legendOffset,
|
|
35
|
+
legendPosition,
|
|
36
|
+
position,
|
|
37
|
+
scale,
|
|
38
|
+
tickPadding,
|
|
39
|
+
tickRotation,
|
|
40
|
+
tickSize,
|
|
41
|
+
tickValues,
|
|
42
|
+
variant,
|
|
43
|
+
x,
|
|
44
|
+
y,
|
|
45
|
+
...forwardedProps
|
|
46
|
+
} = props;
|
|
47
|
+
const axis = getAxisType(position!);
|
|
48
|
+
|
|
49
|
+
const axisRef = (ref || useRef<SVGGElement>()) as RefObject<SVGGElement>;
|
|
50
|
+
|
|
51
|
+
const animatedProps = useSpring({
|
|
52
|
+
...config,
|
|
53
|
+
to: async (next: (props?: object) => Promise<void>) => {
|
|
54
|
+
await next({
|
|
55
|
+
lineX2: axis === 'x' ? length : 0,
|
|
56
|
+
lineY2: axis === 'x' ? 0 : length,
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
from: { lineX2: 0, lineY2: 0 },
|
|
60
|
+
reset: false,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
let legendNode = null;
|
|
64
|
+
if (legend !== undefined) {
|
|
65
|
+
let legendX = 0;
|
|
66
|
+
let legendY = 0;
|
|
67
|
+
let legendRotation = 0;
|
|
68
|
+
let textAnchor;
|
|
69
|
+
|
|
70
|
+
if (axis === 'y') {
|
|
71
|
+
legendRotation = -90;
|
|
72
|
+
legendX = legendOffset!;
|
|
73
|
+
if (legendPosition === 'start') {
|
|
74
|
+
textAnchor = 'start';
|
|
75
|
+
legendY = length!;
|
|
76
|
+
} else if (legendPosition === 'middle') {
|
|
77
|
+
textAnchor = 'middle';
|
|
78
|
+
legendY = length! / 2;
|
|
79
|
+
} else if (legendPosition === 'end') {
|
|
80
|
+
textAnchor = 'end';
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
legendY = legendOffset!;
|
|
84
|
+
if (legendPosition === 'start') {
|
|
85
|
+
textAnchor = 'start';
|
|
86
|
+
} else if (legendPosition === 'middle') {
|
|
87
|
+
textAnchor = 'middle';
|
|
88
|
+
legendX = length! / 2;
|
|
89
|
+
} else if (legendPosition === 'end') {
|
|
90
|
+
textAnchor = 'end';
|
|
91
|
+
legendX = length!;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
legendNode = (
|
|
96
|
+
<>
|
|
97
|
+
<text
|
|
98
|
+
transform={`translate(${legendX}, ${legendY}) rotate(${legendRotation})`}
|
|
99
|
+
textAnchor={textAnchor}
|
|
100
|
+
style={{
|
|
101
|
+
dominantBaseline: 'central',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{legend}
|
|
105
|
+
</text>
|
|
106
|
+
</>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { ticks, textAlign, textBaseline } = computeTicks({
|
|
111
|
+
axis,
|
|
112
|
+
scale,
|
|
113
|
+
ticksPosition: [AxisPosition.top, AxisPosition.left].includes(position!)
|
|
114
|
+
? 'before'
|
|
115
|
+
: 'after',
|
|
116
|
+
tickValues,
|
|
117
|
+
tickSize: tickSize!,
|
|
118
|
+
tickPadding: tickPadding!,
|
|
119
|
+
tickRotation: tickRotation!,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const transition = useTransition<
|
|
123
|
+
typeof ticks[0],
|
|
124
|
+
{ opacity: number; transform: string; textTransform: string }
|
|
125
|
+
>(ticks, {
|
|
126
|
+
...config,
|
|
127
|
+
keys: (tick) => tick.key,
|
|
128
|
+
initial: (tick) => ({
|
|
129
|
+
opacity: 1,
|
|
130
|
+
transform: `translate(${tick.x},${tick.y})`,
|
|
131
|
+
textTransform: `translate(${tick.textX},${tick.textY}) rotate(${tickRotation})`,
|
|
132
|
+
}),
|
|
133
|
+
from: (tick) => ({
|
|
134
|
+
opacity: 0,
|
|
135
|
+
transform: `translate(${tick.x},${tick.y})`,
|
|
136
|
+
textTransform: `translate(${tick.textX},${tick.textY}) rotate(${tickRotation})`,
|
|
137
|
+
}),
|
|
138
|
+
enter: (tick) => ({
|
|
139
|
+
opacity: 1,
|
|
140
|
+
transform: `translate(${tick.x},${tick.y})`,
|
|
141
|
+
textTransform: `translate(${tick.textX},${tick.textY}) rotate(${tickRotation})`,
|
|
142
|
+
}),
|
|
143
|
+
update: (tick) => ({
|
|
144
|
+
opacity: 1,
|
|
145
|
+
transform: `translate(${tick.x},${tick.y})`,
|
|
146
|
+
textTransform: `translate(${tick.textX},${tick.textY}) rotate(${tickRotation})`,
|
|
147
|
+
}),
|
|
148
|
+
leave: {
|
|
149
|
+
opacity: 0,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
<StyledAxis
|
|
155
|
+
className={classNames(Axis.className, className)}
|
|
156
|
+
{...forwardedProps}
|
|
157
|
+
transform={`translate(${x},${y})`}
|
|
158
|
+
ref={axisRef}
|
|
159
|
+
>
|
|
160
|
+
{variant === AxisVariant.default ||
|
|
161
|
+
variant === AxisVariant.tick ||
|
|
162
|
+
variant === AxisVariant.lineTick ||
|
|
163
|
+
variant === AxisVariant.tickValue
|
|
164
|
+
? transition((transitionProps, tick, _state, tickIndex) => {
|
|
165
|
+
return (
|
|
166
|
+
<animated.g
|
|
167
|
+
className={`${Axis.className}__tick`}
|
|
168
|
+
key={tickIndex}
|
|
169
|
+
transform={transitionProps.transform}
|
|
170
|
+
style={{ opacity: transitionProps.opacity }}
|
|
171
|
+
>
|
|
172
|
+
<line
|
|
173
|
+
className={`${Axis.className}-tick__line`}
|
|
174
|
+
x1={0}
|
|
175
|
+
x2={tick.lineX}
|
|
176
|
+
y1={0}
|
|
177
|
+
y2={tick.lineY}
|
|
178
|
+
/>
|
|
179
|
+
{variant === AxisVariant.default ||
|
|
180
|
+
variant === AxisVariant.tickValue ? (
|
|
181
|
+
<animated.text
|
|
182
|
+
dominantBaseline={textBaseline}
|
|
183
|
+
textAnchor={textAlign}
|
|
184
|
+
transform={transitionProps.textTransform}
|
|
185
|
+
>
|
|
186
|
+
{`${tick.value}`}
|
|
187
|
+
</animated.text>
|
|
188
|
+
) : null}
|
|
189
|
+
</animated.g>
|
|
190
|
+
);
|
|
191
|
+
})
|
|
192
|
+
: null}
|
|
193
|
+
|
|
194
|
+
{variant === AxisVariant.default ||
|
|
195
|
+
variant === AxisVariant.line ||
|
|
196
|
+
variant === AxisVariant.lineTick ? (
|
|
197
|
+
<animated.line
|
|
198
|
+
className={`${Axis.className}__line`}
|
|
199
|
+
x1={0}
|
|
200
|
+
x2={
|
|
201
|
+
config.immediate
|
|
202
|
+
? axis === 'x'
|
|
203
|
+
? length
|
|
204
|
+
: 0
|
|
205
|
+
: animatedProps.lineX2
|
|
206
|
+
}
|
|
207
|
+
y1={0}
|
|
208
|
+
y2={
|
|
209
|
+
config.immediate
|
|
210
|
+
? axis === 'x'
|
|
211
|
+
? 0
|
|
212
|
+
: length
|
|
213
|
+
: animatedProps.lineY2
|
|
214
|
+
}
|
|
215
|
+
/>
|
|
216
|
+
) : null}
|
|
217
|
+
{legendNode}
|
|
218
|
+
</StyledAxis>
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
Axis.className = CLASSNAME;
|
|
222
|
+
Axis.defaultProps = DEFAULT_PROPS;
|
|
223
|
+
Axis.displayName = COMPONENT_NAME;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AnyScale,
|
|
3
|
+
Coordinates,
|
|
4
|
+
ScaleValue,
|
|
5
|
+
ScaleWithBandwidth,
|
|
6
|
+
TicksSpec,
|
|
7
|
+
} from '../../types';
|
|
8
|
+
|
|
9
|
+
const getScaleTicks = (scale: AnyScale, spec?: TicksSpec) => {
|
|
10
|
+
// specific values
|
|
11
|
+
if (Array.isArray(spec)) {
|
|
12
|
+
return spec;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// continuous scales
|
|
16
|
+
if ('ticks' in scale) {
|
|
17
|
+
// default behaviour
|
|
18
|
+
if (spec === undefined) {
|
|
19
|
+
return scale.ticks();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// specific tick count
|
|
23
|
+
if (
|
|
24
|
+
typeof spec === 'number' &&
|
|
25
|
+
isFinite(spec) &&
|
|
26
|
+
Math.floor(spec) === spec
|
|
27
|
+
) {
|
|
28
|
+
return scale.ticks(spec);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// non linear scale default
|
|
33
|
+
return scale.domain();
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const centerScale = (scale: ScaleWithBandwidth) => {
|
|
37
|
+
const bandwidth = scale.bandwidth();
|
|
38
|
+
|
|
39
|
+
if (bandwidth === 0) return scale;
|
|
40
|
+
|
|
41
|
+
let offset = bandwidth / 2;
|
|
42
|
+
if (scale.round()) {
|
|
43
|
+
offset = Math.round(offset);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return <T extends ScaleValue>(d: T) => (scale(d) ?? 0) + offset;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const computeTicks = ({
|
|
50
|
+
axis,
|
|
51
|
+
scale,
|
|
52
|
+
ticksPosition,
|
|
53
|
+
tickValues,
|
|
54
|
+
tickSize,
|
|
55
|
+
tickPadding,
|
|
56
|
+
tickRotation,
|
|
57
|
+
}: {
|
|
58
|
+
axis: 'x' | 'y';
|
|
59
|
+
scale: AnyScale;
|
|
60
|
+
ticksPosition?: 'after' | 'before';
|
|
61
|
+
tickValues?: TicksSpec;
|
|
62
|
+
tickSize: number;
|
|
63
|
+
tickPadding: number;
|
|
64
|
+
tickRotation: number;
|
|
65
|
+
}) => {
|
|
66
|
+
const values = getScaleTicks(scale, tickValues);
|
|
67
|
+
|
|
68
|
+
const position = 'bandwidth' in scale ? centerScale(scale) : scale;
|
|
69
|
+
const line = { lineX: 0, lineY: 0 };
|
|
70
|
+
const text = { textX: 0, textY: 0 };
|
|
71
|
+
|
|
72
|
+
const isRTL = typeof document === 'object' ? document.dir === 'rtl' : false;
|
|
73
|
+
let translate: (value: ScaleValue) => Coordinates;
|
|
74
|
+
let textAlign = 'middle';
|
|
75
|
+
let textBaseline = 'central';
|
|
76
|
+
|
|
77
|
+
if (axis === 'x') {
|
|
78
|
+
translate = (d) => ({ x: position(d) ?? 0, y: 0 });
|
|
79
|
+
|
|
80
|
+
line.lineY = tickSize * (ticksPosition === 'after' ? 1 : -1);
|
|
81
|
+
text.textY =
|
|
82
|
+
(tickSize + tickPadding) * (ticksPosition === 'after' ? 1 : -1);
|
|
83
|
+
|
|
84
|
+
if (ticksPosition === 'after') {
|
|
85
|
+
textBaseline = 'text-before-edge';
|
|
86
|
+
} else {
|
|
87
|
+
textBaseline = 'alphabetic';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (tickRotation === 0) {
|
|
91
|
+
textAlign = 'middle';
|
|
92
|
+
} else if (
|
|
93
|
+
(ticksPosition === 'after' && tickRotation < 0) ||
|
|
94
|
+
(ticksPosition === 'before' && tickRotation > 0)
|
|
95
|
+
) {
|
|
96
|
+
textAlign = isRTL ? 'start' : 'end';
|
|
97
|
+
textBaseline = 'middle';
|
|
98
|
+
} else if (
|
|
99
|
+
(ticksPosition === 'after' && tickRotation > 0) ||
|
|
100
|
+
(ticksPosition === 'before' && tickRotation < 0)
|
|
101
|
+
) {
|
|
102
|
+
textAlign = isRTL ? 'end' : 'start';
|
|
103
|
+
textBaseline = 'middle';
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
translate = (d) => ({ x: 0, y: position(d) ?? 0 });
|
|
107
|
+
|
|
108
|
+
line.lineX = tickSize * (ticksPosition === 'after' ? 1 : -1);
|
|
109
|
+
text.textX =
|
|
110
|
+
(tickSize + tickPadding) * (ticksPosition === 'after' ? 1 : -1);
|
|
111
|
+
|
|
112
|
+
if (ticksPosition === 'after') {
|
|
113
|
+
textAlign = 'start';
|
|
114
|
+
} else {
|
|
115
|
+
textAlign = 'end';
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const ticks = values.map((value: ScaleValue) => ({
|
|
120
|
+
key: value instanceof Date ? `${value.valueOf()}` : `${value}`,
|
|
121
|
+
value,
|
|
122
|
+
...translate(value),
|
|
123
|
+
...line,
|
|
124
|
+
...text,
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
ticks,
|
|
129
|
+
textAlign,
|
|
130
|
+
textBaseline,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import styled from 'styled-components';
|
|
2
|
+
import { StyledAxisProps } from './types';
|
|
3
|
+
import { animated } from '@react-spring/web';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Component style.
|
|
7
|
+
*/
|
|
8
|
+
export const StyledAxis = styled(animated.g)<StyledAxisProps>`
|
|
9
|
+
.redsift-axis__line,
|
|
10
|
+
.redsift-axis-tick__line {
|
|
11
|
+
fill: none;
|
|
12
|
+
stroke: var(--redsift-color-neutral-black);
|
|
13
|
+
shape-rendering: crispEdges;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
text {
|
|
17
|
+
font-family: var(--redsift-typography-font-family-source-code-pro);
|
|
18
|
+
font-size: 10px;
|
|
19
|
+
user-select: none;
|
|
20
|
+
}
|
|
21
|
+
`;
|