Skip to content
Draft
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
"@babel/runtime-corejs3": "^7.11.2",
"@storybook/client-api": "^6.0.21",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this can also be a dev-dependency

"@togglecorp/fujs": "^1.9.2-beta.0",
"file-saver": "^2.0.2",
"d3-axis": "^2.0.0",
"d3-scale": "^3.2.3",
"d3-shape": "^2.0.0",
"file-saver": "^2.0.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File saver can be an optional dependency

"react-icons": "^3.11.0",
"resize-observer": "^1.0.0"
},
Expand Down Expand Up @@ -75,9 +76,9 @@
"@storybook/addon-links": "^6.0.21",
"@storybook/node-logger": "^6.0.21",
"@storybook/react": "^6.0.21",
"@types/file-saver": "^2.0.1",
"@types/d3-axis": "^2.0.0",
"@types/d3-scale": "^3.1.0",
"@types/file-saver": "^2.0.1",
"@types/node": "^14.11.2",
"@types/react": "^16.9.49",
"@types/react-dom": "^16.9.8",
Expand Down
90 changes: 90 additions & 0 deletions src/components/PieChart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { useMemo } from 'react';

import { pie, arc } from 'd3-shape';

import styles from './styles.css';

export interface PieChartProps<K> {
width: number;
height: number;
innerRadius: number;
data: K[];
valueSelector: (v: K) => number;
labelSelector: (v: K) => string;
colorSelector?: (v: K) => string;
showLabels?: boolean;
}

const colorAccent = getComputedStyle(document.documentElement)
.getPropertyValue('--tui-color-accent')
.trim();

const defaultColorSelector = () => colorAccent;

function PieChart<K>(props: PieChartProps<K>) {
const {
width,
height,
data,
valueSelector,
labelSelector,
colorSelector = defaultColorSelector,
innerRadius = 0,
showLabels = true,
} = props;

const pieGenerator = useMemo(
() => pie()
.value(valueSelector),
[valueSelector],
);

const arcGenerator = useMemo(
() => arc()
.innerRadius(innerRadius)
.outerRadius(Math.min(width, height) / 2 - 1),
[innerRadius, width, height],
);

const arcs = pieGenerator(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need to memoize this?


return (
<svg
width={width}
height={height}
>
<g
transform={`translate(${width / 2}, ${height / 2})`}
>
<g>
{arcs.map((d) => (
<path
key={labelSelector(d.data)}
d={arcGenerator(d)}
fill={colorSelector(d.data)}
>
<title>{labelSelector(d.data)}</title>
</path>
))}
</g>
{ showLabels && (
<g>
{arcs.map((d) => (
<text
className={styles.textLabel}
textAnchor="middle"
transform={`translate(${arcGenerator.centroid(d)})`}
>
<tspan>
{labelSelector(d.data)}
</tspan>
</text>
))}
</g>
)}
</g>
</svg>
);
}

export default PieChart;
4 changes: 4 additions & 0 deletions src/components/PieChart/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.text-label {
fill: var(--tui-color-text);
font-size: var(--tui-font-size-medium);
}
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ export { default as ToggleButton } from './components/ToggleButton';
export type { ToggleButtonProps } from './components/ToggleButton';
export { default as VisualFeedback } from './components/VisualFeedback';
export type { VisualFeedbackProps } from './components/VisualFeedback';
export { default as PieChart } from './components/PieChart';
export type { PieChartProps } from './components/PieChart';
38 changes: 38 additions & 0 deletions src/stories/PieChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { Story } from '@storybook/react/types-6-0';

import PieChart, { PieChartProps } from '#components/PieChart';

export default {
title: 'Visualization/PieChart',
component: PieChart,
argTypes: {},
};

interface Item {
label: string;
value: number;
}

const data = [
{ label: 'Jhapa', value: 20000, color: '#7fc97f' },
{ label: 'Illam', value: 10000, color: '#beaed4' },
{ label: 'Panchthar', value: 30000, color: '#fdc086' },
{ label: 'Taplejung', value: 50000, color: '#ffff99' },
];

const Template: Story<PieChartProps<Item>> = (props) => (
<PieChart
{...props}
width={600}
height={300}
data={data}
valueSelector={(d) => d.value}
labelSelector={(d) => d.label}
colorSelector={(d) => d.color}
/>
);

export const Default = Template.bind({});
Default.args = {
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5877,6 +5877,11 @@ d3-axis@^2.0.0:
dependencies:
d3-color "1 - 2"

"d3-path@1 - 2":
version "2.0.0"
resolved "https://registry.yarnpkg.com/d3-path/-/d3-path-2.0.0.tgz#55d86ac131a0548adae241eebfb56b4582dd09d8"
integrity sha512-ZwZQxKhBnv9yHaiWd6ZU4x5BtCQ7pXszEV9CU6kRgwIQVQGLMv1oiL4M+MK/n79sYzsj+gcgpPQSctJUsLN7fA==

d3-scale@^3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/d3-scale/-/d3-scale-3.2.3.tgz#be380f57f1f61d4ff2e6cbb65a40593a51649cfd"
Expand All @@ -5888,6 +5893,13 @@ d3-scale@^3.2.3:
d3-time "1 - 2"
d3-time-format "2 - 3"

d3-shape@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/d3-shape/-/d3-shape-2.0.0.tgz#2331b62fa784a2a1daac47a7233cfd69301381fd"
integrity sha512-djpGlA779ua+rImicYyyjnOjeubyhql1Jyn1HK0bTyawuH76UQRWXd+pftr67H6Fa8hSwetkgb/0id3agKWykw==
dependencies:
d3-path "1 - 2"

"d3-time-format@2 - 3":
version "3.0.0"
resolved "https://registry.yarnpkg.com/d3-time-format/-/d3-time-format-3.0.0.tgz#df8056c83659e01f20ac5da5fdeae7c08d5f1bb6"
Expand Down