-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(D3 plugin): add chart click event (#423)
- Loading branch information
Showing
14 changed files
with
337 additions
and
54 deletions.
There are no files selected for viewing
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,95 @@ | ||
import React from 'react'; | ||
|
||
import {Button} from '@gravity-ui/uikit'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {StoryObj} from '@storybook/react'; | ||
|
||
import {D3Plugin} from '../..'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import {settings} from '../../../../libs'; | ||
import {ChartKitWidgetData} from '../../../../types'; | ||
import {HighchartsPlugin} from '../../../highcharts'; | ||
|
||
function prepareData(): ChartKitWidgetData { | ||
return { | ||
series: { | ||
options: { | ||
line: { | ||
lineWidth: 2, | ||
}, | ||
}, | ||
data: [ | ||
{ | ||
name: 'A', | ||
type: 'area', | ||
data: [ | ||
{x: 1, y: 200}, | ||
{x: 2, y: 220}, | ||
{x: 3, y: 180}, | ||
], | ||
stacking: 'normal', | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
{ | ||
name: 'B', | ||
type: 'area', | ||
data: [ | ||
{x: 1, y: 30}, | ||
{x: 2, y: 25}, | ||
{x: 3, y: 45}, | ||
], | ||
stacking: 'normal', | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
chart: { | ||
events: { | ||
click: action('chart.events.click'), | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
const ChartStory = ({data}: {data: ChartKitWidgetData}) => { | ||
const [shown, setShown] = React.useState(false); | ||
|
||
if (!shown) { | ||
settings.set({plugins: [D3Plugin, HighchartsPlugin]}); | ||
return <Button onClick={() => setShown(true)}>Show chart</Button>; | ||
} | ||
|
||
return ( | ||
<> | ||
<div | ||
style={{ | ||
height: '80vh', | ||
width: '100%', | ||
}} | ||
> | ||
<ChartKit type="d3" data={data} /> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export const PlaygroundLineChartStory: StoryObj<typeof ChartStory> = { | ||
name: 'Playground', | ||
args: { | ||
data: prepareData(), | ||
}, | ||
argTypes: { | ||
data: { | ||
control: 'object', | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Area', | ||
component: ChartStory, | ||
}; |
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
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,89 @@ | ||
import React from 'react'; | ||
|
||
import {Button} from '@gravity-ui/uikit'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {StoryObj} from '@storybook/react'; | ||
|
||
import {D3Plugin} from '../..'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import {settings} from '../../../../libs'; | ||
import {ChartKitWidgetData} from '../../../../types'; | ||
import nintendoGames from '../../examples/nintendoGames'; | ||
|
||
function prepareData() { | ||
const dataset = nintendoGames.filter((d) => d.date && d.user_score); | ||
const data = dataset.map((d) => ({ | ||
x: d.date || undefined, | ||
y: d.user_score || undefined, | ||
custom: d, | ||
})); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'scatter', | ||
data, | ||
name: 'Scatter series', | ||
}, | ||
], | ||
}, | ||
yAxis: [ | ||
{ | ||
title: { | ||
text: 'User score', | ||
}, | ||
}, | ||
], | ||
xAxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Release dates', | ||
}, | ||
}, | ||
chart: { | ||
events: { | ||
click: action('chart.events.click'), | ||
}, | ||
}, | ||
}; | ||
|
||
return widgetData; | ||
} | ||
|
||
const ChartStory = ({data}: {data: ChartKitWidgetData}) => { | ||
const [shown, setShown] = React.useState(false); | ||
|
||
if (!shown) { | ||
settings.set({plugins: [D3Plugin]}); | ||
return <Button onClick={() => setShown(true)}>Show chart</Button>; | ||
} | ||
|
||
return ( | ||
<div | ||
style={{ | ||
height: '80vh', | ||
width: '100%', | ||
}} | ||
> | ||
<ChartKit type="d3" data={data} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const PlaygroundBarYChartStory: StoryObj<typeof ChartStory> = { | ||
name: 'Playground', | ||
args: { | ||
data: prepareData(), | ||
}, | ||
argTypes: { | ||
data: { | ||
control: 'object', | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Scatter', | ||
component: ChartStory, | ||
}; |
58 changes: 32 additions & 26 deletions
58
src/plugins/d3/__stories__/treemap/Playground.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
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
Oops, something went wrong.