Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(d3 plugin): add basic pie chart #244

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
66 changes: 66 additions & 0 deletions src/plugins/d3/__stories__/pie/BasicDonut.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {withKnobs, object} from '@storybook/addon-knobs';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {ChartKit} from '../../../../components/ChartKit';
import type {ChartKitRef} from '../../../../types';
import type {ChartKitWidgetData} from '../../../../types/widget-data';
import {D3Plugin} from '../..';

const Template: Story = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();
const data: ChartKitWidgetData = {
series: {
data: [
{
type: 'pie',
innerRadius: '50%',
data: [
{
name: 'One',
value: 50,
},
{
name: 'Two',
value: 20,
},
{
name: 'Three',
value: 90,
},
],
},
],
},
title: {text: 'Basic donut'},
legend: {enabled: false},
tooltip: {enabled: false},
};

if (!shown) {
settings.set({plugins: [D3Plugin]});
return <Button onClick={() => setShown(true)}>Show chart</Button>;
}

return (
<div
style={{
height: '300px',
width: '100%',
}}
>
<ChartKit ref={chartkitRef} type="d3" data={object<ChartKitWidgetData>('data', data)} />
</div>
);
};

export const BasicDonut = Template.bind({});

const meta: Meta = {
title: 'Plugins/D3/Pie',
decorators: [withKnobs],
};

export default meta;
65 changes: 65 additions & 0 deletions src/plugins/d3/__stories__/pie/BasicPie.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {withKnobs, object} from '@storybook/addon-knobs';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {ChartKit} from '../../../../components/ChartKit';
import type {ChartKitRef} from '../../../../types';
import type {ChartKitWidgetData} from '../../../../types/widget-data';
import {D3Plugin} from '../..';

const Template: Story = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();
const data: ChartKitWidgetData = {
series: {
data: [
{
type: 'pie',
data: [
{
name: 'One',
value: 50,
},
{
name: 'Two',
value: 20,
},
{
name: 'Three',
value: 90,
},
],
},
],
},
title: {text: 'Basic pie'},
legend: {enabled: false},
tooltip: {enabled: false},
};

if (!shown) {
settings.set({plugins: [D3Plugin]});
return <Button onClick={() => setShown(true)}>Show chart</Button>;
}

return (
<div
style={{
height: '300px',
width: '100%',
}}
>
<ChartKit ref={chartkitRef} type="d3" data={object<ChartKitWidgetData>('data', data)} />
</div>
);
};

export const BasicPie = Template.bind({});

const meta: Meta = {
title: 'Plugins/D3/Pie',
decorators: [withKnobs],
};

export default meta;
93 changes: 93 additions & 0 deletions src/plugins/d3/__stories__/pie/Styled.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {withKnobs, object} from '@storybook/addon-knobs';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {ChartKit} from '../../../../components/ChartKit';
import type {ChartKitRef} from '../../../../types';
import type {ChartKitWidgetData} from '../../../../types/widget-data';
import {D3Plugin} from '../..';

const Template: Story = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();
const data: ChartKitWidgetData = {
series: {
data: [
{
type: 'pie',
borderRadius: 5,
borderWidth: 3,
center: ['25%', null],
name: 'Pie',
radius: '75%',
data: [
{
name: 'One',
value: 50,
},
{
name: 'Two',
value: 20,
},
{
name: 'Three',
value: 90,
},
],
},
{
type: 'pie',
borderRadius: 5,
borderWidth: 3,
center: ['75%', null],
innerRadius: '50%',
name: 'Donut',
radius: '75%',
data: [
{
name: 'One',
value: 50,
},
{
name: 'Two',
value: 20,
},
{
name: 'Three',
value: 90,
},
],
},
],
},
title: {text: 'Styled pies'},
legend: {enabled: false},
tooltip: {enabled: false},
};

if (!shown) {
settings.set({plugins: [D3Plugin]});
return <Button onClick={() => setShown(true)}>Show chart</Button>;
}

return (
<div
style={{
height: '300px',
width: '100%',
}}
>
<ChartKit ref={chartkitRef} type="d3" data={object<ChartKitWidgetData>('data', data)} />
</div>
);
};

export const Styled = Template.bind({});

const meta: Meta = {
title: 'Plugins/D3/Pie',
decorators: [withKnobs],
};

export default meta;
7 changes: 6 additions & 1 deletion src/plugins/d3/renderer/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const Chart = ({width, height, data}: Props) => {
const svgRef = React.createRef<SVGSVGElement>();
const hasAxisRelatedSeries = series.data.some(isAxisRelatedSeries);
const {chartHovered, handleMouseEnter, handleMouseLeave} = useChartEvents();
const {chart, legend, title, tooltip, xAxis, yAxis} = useChartOptions(data);
const {chart, legend, title, tooltip, xAxis, yAxis} = useChartOptions({
...data,
hasAxisRelatedSeries,
kuzmadom marked this conversation as resolved.
Show resolved Hide resolved
});
const {boundsWidth, boundsHeight, legendHeight} = useChartDimensions({
width,
height,
Expand All @@ -59,6 +62,8 @@ export const Chart = ({width, height, data}: Props) => {
tooltip,
});
const {shapes} = useShapes({
boundsWidth,
boundsHeight,
series: chartSeries,
xAxis,
xScale,
Expand Down
Loading