-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6f5b60
commit 57432a5
Showing
18 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import React from 'react'; | ||
import { RadialBar } from '../RadialBar'; | ||
import { RadialBarCircle } from '../RadialBarCircle'; | ||
import { RadialBarScale } from '../RadialBarScale'; | ||
import { RadialBarSweep } from '../RadialBarSweep'; | ||
import { Chart } from './Chart'; | ||
|
||
const meta: Meta<typeof Chart> = { | ||
title: 'Chart', | ||
component: Chart, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Chart>; | ||
|
||
export const FullCircles: Story = { | ||
args: { | ||
width: 252 + 8 + 8, | ||
height: 252 + 8 + 8, | ||
marginBottom: 8, | ||
marginLeft: 8, | ||
marginRight: 8, | ||
marginTop: 8, | ||
children: ( | ||
<g transform="translate(126,126)"> | ||
<RadialBarScale | ||
angleMin={0} | ||
angleMax={2 * Math.PI} | ||
valueMin={0} | ||
valueMax={120} | ||
> | ||
<RadialBar radius={126} ratio={0.77} cornerRadius="50%"> | ||
<RadialBarSweep to={120} fill="lightgrey" /> | ||
<RadialBarSweep to={105} /> | ||
</RadialBar> | ||
<RadialBar radius={93} ratio={0.87} cornerRadius="50%"> | ||
<RadialBarSweep to={103} fill="grey" /> | ||
</RadialBar> | ||
</RadialBarScale> | ||
</g> | ||
), | ||
}, | ||
}; | ||
|
||
export const HalfCircles: Story = { | ||
args: { | ||
width: 544 + 8 + 8, | ||
height: 162 + 8 + 8, | ||
marginBottom: 8, | ||
marginLeft: 8, | ||
marginRight: 8, | ||
marginTop: 8, | ||
children: ( | ||
<g transform="translate(126,126)"> | ||
<RadialBarScale | ||
angleMin={(-1 * Math.PI) / 2} | ||
angleMax={Math.PI / 2} | ||
valueMin={1} | ||
valueMax={3} | ||
> | ||
<RadialBar radius={126} ratio={0.77} cornerRadius="50%"> | ||
<RadialBarSweep to={3} fill="lightgrey" /> | ||
<RadialBarCircle at={2} /> | ||
</RadialBar> | ||
<RadialBar radius={93} ratio={0.87} cornerRadius="50%"> | ||
<RadialBarSweep to={3} fill="lightgrey" /> | ||
<RadialBarCircle at={3} /> | ||
</RadialBar> | ||
</RadialBarScale> | ||
</g> | ||
), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import { ChartElement, ChartProps } from './ChartProps'; | ||
import { useChart } from './useChart'; | ||
|
||
export const Chart = React.forwardRef<ChartElement, ChartProps>(function Chart( | ||
props, | ||
ref | ||
) { | ||
const { svgProps, gProps } = useChart(props); | ||
|
||
return ( | ||
<svg ref={ref} {...svgProps}> | ||
<g {...gProps} /> | ||
</svg> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react'; | ||
import { ChartParams } from './useChart'; | ||
|
||
export interface ChartElement extends SVGSVGElement {} | ||
|
||
export interface ChartProps | ||
extends ChartParams, | ||
Omit<React.SVGProps<ChartElement>, 'width' | 'height'> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './Chart'; | ||
export * from './ChartProps'; | ||
export * from './useChart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { SVGProps } from 'react'; | ||
import { ChartProps } from './ChartProps'; | ||
import { | ||
evalMargin, | ||
makeDimensions, | ||
toStandardViewBox, | ||
toViewBoxAttr, | ||
} from '../utils'; | ||
|
||
export type ChartParams = { | ||
/** | ||
* The width of the container. | ||
*/ | ||
width: number; | ||
/** | ||
* The height of the container. | ||
*/ | ||
height: number; | ||
/** | ||
* The space between the bottom edge of the container and its surroundings. | ||
*/ | ||
marginBottom?: number; | ||
/** | ||
* The space between the left edge of the container and its surroundings. | ||
*/ | ||
marginLeft?: number; | ||
/** | ||
* The space between the right edge of the container and its surroundings. | ||
*/ | ||
marginRight?: number; | ||
/** | ||
* The space between the top edge of the container and its surroundings. | ||
*/ | ||
marginTop?: number; | ||
}; | ||
|
||
export interface ChartAPI { | ||
svgProps: SVGProps<SVGSVGElement>; | ||
gProps: SVGProps<SVGGElement>; | ||
} | ||
|
||
export function useChart(props: ChartProps): ChartAPI { | ||
const { | ||
width, | ||
height, | ||
marginBottom, | ||
marginLeft, | ||
marginRight, | ||
marginTop, | ||
children, | ||
...other | ||
} = props; | ||
|
||
const margin = evalMargin({ | ||
bottom: marginBottom, | ||
left: marginLeft, | ||
right: marginRight, | ||
top: marginTop, | ||
}); | ||
|
||
const dimensions = makeDimensions({ | ||
width, | ||
height, | ||
margin, | ||
}); | ||
|
||
const viewBox = toStandardViewBox({ dimensions }); | ||
|
||
const transform = `translate(${dimensions.margin.left}, ${dimensions.margin.top})`; | ||
|
||
return { | ||
svgProps: { | ||
viewBox: toViewBoxAttr({ viewBox }), | ||
width: dimensions.width, | ||
height: dimensions.height, | ||
...other, | ||
style: { maxWidth: '100%', height: 'auto', ...other?.style }, | ||
}, | ||
gProps: { | ||
transform, | ||
children, | ||
}, | ||
}; | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/dataviz/src/ResponsiveChart/ResponsiveChart.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import React from 'react'; | ||
import { RadialBar } from '../RadialBar'; | ||
import { RadialBarCircle } from '../RadialBarCircle'; | ||
import { RadialBarScale } from '../RadialBarScale'; | ||
import { RadialBarSweep } from '../RadialBarSweep'; | ||
import { ResponsiveChart } from './ResponsiveChart'; | ||
|
||
const meta: Meta<typeof ResponsiveChart> = { | ||
title: 'ResponsiveChart', | ||
component: ResponsiveChart, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ResponsiveChart>; | ||
|
||
export const FullCircles: Story = { | ||
args: { | ||
innerWidth: 252, | ||
height: 252 + 8 + 8, | ||
marginBottom: 8, | ||
marginLeft: 8, | ||
marginRight: 8, | ||
marginTop: 8, | ||
children: ( | ||
<g transform="translate(126,126)"> | ||
<RadialBarScale | ||
angleMin={0} | ||
angleMax={2 * Math.PI} | ||
valueMin={0} | ||
valueMax={120} | ||
> | ||
<RadialBar radius={126} ratio={0.77} cornerRadius="50%"> | ||
<RadialBarSweep to={120} fill="lightgrey" /> | ||
<RadialBarSweep to={105} /> | ||
</RadialBar> | ||
<RadialBar radius={93} ratio={0.87} cornerRadius="50%"> | ||
<RadialBarSweep to={103} fill="grey" /> | ||
</RadialBar> | ||
</RadialBarScale> | ||
</g> | ||
), | ||
}, | ||
}; | ||
|
||
export const HalfCircles: Story = { | ||
args: { | ||
innerWidth: 544, | ||
height: 162 + 8 + 8, | ||
marginBottom: 8, | ||
marginLeft: 8, | ||
marginRight: 8, | ||
marginTop: 8, | ||
children: ( | ||
<g transform="translate(126,126)"> | ||
<RadialBarScale | ||
angleMin={(-1 * Math.PI) / 2} | ||
angleMax={Math.PI / 2} | ||
valueMin={1} | ||
valueMax={3} | ||
> | ||
<RadialBar radius={126} ratio={0.77} cornerRadius="50%"> | ||
<RadialBarSweep to={3} fill="lightgrey" /> | ||
<RadialBarCircle at={2} /> | ||
</RadialBar> | ||
<RadialBar radius={93} ratio={0.87} cornerRadius="50%"> | ||
<RadialBarSweep to={3} fill="lightgrey" /> | ||
<RadialBarCircle at={3} /> | ||
</RadialBar> | ||
</RadialBarScale> | ||
</g> | ||
), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { | ||
ResponsiveChartElement, | ||
ResponsiveChartProps, | ||
} from './ResponsiveChartProps'; | ||
import { useResponsiveChart } from './useResponsiveChart'; | ||
|
||
export const ResponsiveChart = React.forwardRef< | ||
ResponsiveChartElement, | ||
ResponsiveChartProps | ||
>(function ResponsiveChart(props, ref) { | ||
const { | ||
width, | ||
height, | ||
innerWidth, | ||
innerHeight, | ||
marginBottom, | ||
marginLeft, | ||
marginRight, | ||
marginTop, | ||
children, | ||
...other | ||
} = props; | ||
|
||
const { | ||
container: { props: containerProps, ref: containerRef }, | ||
svg: { props: svgProps }, | ||
inner: { props: innerProps }, | ||
} = useResponsiveChart({ | ||
width, | ||
height, | ||
innerWidth, | ||
innerHeight, | ||
marginBottom, | ||
marginLeft, | ||
marginRight, | ||
marginTop, | ||
}); | ||
|
||
return ( | ||
<div ref={containerRef} {...containerProps}> | ||
<svg ref={ref} {...svgProps} {...other}> | ||
<g {...innerProps}>{children}</g> | ||
</svg> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ResponsiveChartParams } from './useResponsiveChart'; | ||
|
||
export interface ResponsiveChartElement extends SVGSVGElement {} | ||
|
||
export interface ResponsiveChartProps | ||
extends ResponsiveChartParams, | ||
Omit<React.SVGProps<ResponsiveChartElement>, 'width' | 'height'> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './ResponsiveChart'; | ||
export * from './ResponsiveChartProps'; | ||
export * from './useResponsiveChart'; |
Oops, something went wrong.