Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions src/components/DonutDiagram/DonutDiagram.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import { Box } from '../Box/Box';
import { defaultTheme } from '../../themes/default';
import { ThemeProvider } from 'emotion-theming';
import { DonutDiagram } from './DonutDiagram';

const stories = storiesOf('Donut Diagram', module);

stories.add('simple', () => {
const [value, setValue] = useState(0);

setTimeout(() => {
setValue(0.5);
}, 3000);

return (
<ThemeProvider theme={defaultTheme}>
<Box>
<DonutDiagram
data={[
{
value: 0.25,
color: 'primary.$300',
id: 'id1',
},
{
value: 0.4,
color: 'primary.$900',
id: 'id6',
},
]}
baseColor="#80c726"
border={12}
size={112}
/>
<DonutDiagram
data={[
{
value,
color: 'primary.$300',
id: 'id2',
},
]}
easing={'ease-out'}
animationTime={500}
baseColor="danger.$300"
border={40}
size={112}
/>
<DonutDiagram
data={[
{
value: 0.5,
color: 'success.$500',
id: 'id3',
},
]}
baseColor="transparent"
border={20}
size={150}
/>
<DonutDiagram
data={[
{
value: 0.1,
color: 'primary.$300',
id: 'id4',
},
]}
baseColor="danger.$300"
size={112}
border={2}
/>
<DonutDiagram
data={[
{
value: 0.75,
color: 'primary.$300',
id: 'id5',
},
]}
baseColor="danger.$300"
size={300}
border="none"
/>
<DonutDiagram
data={[
{ value: 0.25, color: 'primary.$300', id: 'value1' },
{ value: 0.29, color: 'danger.$300', id: 'value2' },
{ value: 0.2, color: 'green.$500', id: 'value3' },
{ value: 0.15, color: 'success.$300', id: 'value4' },
{ value: 0.1, color: 'warning.$500', id: 'value5' },
]}
baseColor="main.$100"
size={300}
border="none"
/>
<DonutDiagram
data={[
{
value: 0.25,
color: 'primary.$300',
id: 'id1',
},
{
value: 0.4,
color: 'primary.$900',
id: 'id6',
},
]}
baseColor="#80c726"
border={12}
size={112}
gaps={{ color: '#fff', value: 0.05 }}
/>
<DonutDiagram
data={[
{
value: 0.25,
color: 'primary.$300',
id: 'id1',
},
{
value: 0.4,
color: 'primary.$900',
id: 'id6',
},
{
value: 0.35,
color: 'main.$300',
id: 'id6.1',
},
]}
baseColor="#80c726"
border={12}
size={112}
gaps={{ color: '#fff' }}
/>
</Box>
</ThemeProvider>
);
});
135 changes: 135 additions & 0 deletions src/components/DonutDiagram/DonutDiagram.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import React, { HTMLAttributes, SVGAttributes } from 'react';
import { Box, BoxAsElement, BoxProps } from '../..';
import styled from '@emotion/styled';

interface IDiagramData {
id: string;
value: number;
color: string;
}

interface IGaps {
color: string;
value?: number;
}

interface IProps {
data: Array<IDiagramData>;
baseColor: string;
border: number | 'none';
size: number;
animationTime?: number;
easing?: string;
gaps?: IGaps;
}

const defaultProps = {
animationTime: 300,
easing: 'linear',
};

type SVGBoxProps = BoxProps<SVGElement, HTMLAttributes<SVGElement>> &
SVGAttributes<SVGElement>;

const Svg = styled(Box as BoxAsElement<'svg', SVGBoxProps>)({
flexShrink: 0,
backfaceVisibility: 'hidden',
'&:not(:root)': {
overflow: 'hidden',
},
});

const DEFAULT_GAP = 0.01;

export const DonutDiagram: React.FC<BoxProps & IProps> = ({
data,
baseColor,
border: borderProp,
size,
animationTime,
easing,
gaps,
...rest
}) => {
const border: number =
borderProp === 'none' ? size / 2 : (borderProp as number);
const circumference = Math.PI * (size - border);
const baseData = gaps
? [
{
value: circumference,
color: gaps.color,
id: 'gap-base',
},
{
value: circumference * (1 - (gaps?.value || DEFAULT_GAP)),
color: baseColor,
id: 'base',
},
]
: [{ value: circumference, color: baseColor, id: 'base' }];
const sectorsData = data
.slice()
.sort(({ value: valueA }, { value: valueB }) => valueA - valueB)
.reduce((acc, { value: amount, color, id }) => {
if (!gaps) {
const sum =
amount * circumference + (acc[acc.length - 1]?.value || 0);

return [...acc, { value: sum, color, id }];
} else {
const { color: gapColor, value = DEFAULT_GAP } = gaps;

const sum =
(amount - value) * circumference +
(acc[acc.length - 1]?.value || 0);

const gap = value * circumference + sum;

return [
...acc,
{ value: sum, color, id },
{ value: gap, color: gapColor, id: `gap-${id}` },
];
}
}, [] as Array<IDiagramData>);

const circlesData = [...baseData, ...sectorsData.reverse()];

return (
<Box
size={size}
sx={{
transform: 'rotate(-90deg)',
willChange: 'transform',
}}
{...rest}
>
<Svg
as="svg"
viewBox={`0 0 ${size} ${size}`}
size={size}
xmlns="http://www.w3.org/2000/svg"
>
{circlesData.map(({ value, color, id }) => (
<Svg
key={id}
as="circle"
cx={size / 2}
cy={size / 2}
r={size / 2 - border / 2}
sx={{
stroke: color,
strokeWidth: border,
strokeDasharray: `${value}, ${circumference}`,
fill: 'none',
transition: `${animationTime! / 100}s ${easing}`,
}}
/>
))}
</Svg>
</Box>
);
};

DonutDiagram.defaultProps = defaultProps;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export * from './components/Accordion/AccordionItem';
export * from './components/Accordion/AccordionHeader';
export * from './components/Accordion/AccordionPanel';
export * from './components/DotLoader/DotLoader';
export * from './components/DonutDiagram/DonutDiagram';
export * from './components/WxLoader/WxLoader';
export * from './components/CircleCounter/CircleCounter';
export * from './themes/default';
Expand Down
2 changes: 2 additions & 0 deletions src/themes/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export const darkThemeColors = {
negstroke: colors.red.$300,
alertbg: 'rgba(255, 197, 48, 0.1)',
negativebg: 'rgba(255, 72, 94, 0.1)',
positivebg: 'rgba(42, 198, 132, 0.1)',
togglebg: colors.mediumGrey.$850,
};

Expand Down Expand Up @@ -207,6 +208,7 @@ export const lightThemeColors = {
negstroke: colors.red.$300,
alertbg: 'rgba(255, 197, 48, 0.15)',
negativebg: 'rgba(255, 72, 94, 0.15)',
positivebg: 'rgba(42, 198, 132, 0.15)',
togglebg: colors.lightGrey.$400,
};

Expand Down