-
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.
- Loading branch information
Showing
37 changed files
with
1,318 additions
and
92 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
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,48 @@ | ||
import React from 'react'; | ||
import {StoryObj} from '@storybook/react'; | ||
import {withKnobs} from '@storybook/addon-knobs'; | ||
import {Button} from '@gravity-ui/uikit'; | ||
import {settings} from '../../../../libs'; | ||
import {D3Plugin} from '../..'; | ||
import {Basic} from '../../examples/area/Basic'; | ||
import {StackedArea} from '../../examples/area/StackedArea'; | ||
|
||
const ChartStory = ({Chart}: {Chart: React.FC}) => { | ||
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%', | ||
}} | ||
> | ||
<Chart /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const BasicAreaChartStory: StoryObj<typeof ChartStory> = { | ||
name: 'Basic', | ||
args: { | ||
Chart: Basic, | ||
}, | ||
}; | ||
|
||
export const StackedAreaChartStory: StoryObj<typeof ChartStory> = { | ||
name: 'Stacked', | ||
args: { | ||
Chart: StackedArea, | ||
}, | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Area', | ||
decorators: [withKnobs], | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData, AreaSeries, AreaSeriesData} from '../../../../types'; | ||
import nintendoGames from '../nintendoGames'; | ||
|
||
function prepareData(): AreaSeries[] { | ||
const games = nintendoGames.filter((d) => { | ||
return d.date && d.user_score && d.genres.includes('Puzzle'); | ||
}); | ||
|
||
return [ | ||
{ | ||
name: 'User score', | ||
type: 'area', | ||
data: games.map<AreaSeriesData>((d) => { | ||
return { | ||
x: Number(d.date), | ||
y: Number(d.user_score), | ||
}; | ||
}), | ||
}, | ||
]; | ||
} | ||
|
||
export const Basic = () => { | ||
const series = prepareData(); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
title: { | ||
text: 'User score (puzzle genre)', | ||
}, | ||
series: { | ||
data: series, | ||
}, | ||
xAxis: { | ||
type: 'datetime', | ||
}, | ||
}; | ||
|
||
return <ChartKit type="d3" data={widgetData} />; | ||
}; |
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,67 @@ | ||
import {groups} from 'd3'; | ||
import React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData, AreaSeries, AreaSeriesData} from '../../../../types'; | ||
import nintendoGames from '../nintendoGames'; | ||
|
||
const years = Array.from( | ||
new Set( | ||
nintendoGames.map((d) => | ||
d.date ? String(new Date(d.date as number).getFullYear()) : 'unknown', | ||
), | ||
), | ||
).sort(); | ||
|
||
function prepareData() { | ||
const grouped = groups( | ||
nintendoGames, | ||
(d) => d.platform, | ||
(d) => (d.date ? String(new Date(d.date as number).getFullYear()) : 'unknown'), | ||
); | ||
const series = grouped.map(([platform, gamesByYear]) => { | ||
const platformGames = Object.fromEntries(gamesByYear) || {}; | ||
return { | ||
name: platform, | ||
data: years.reduce<AreaSeriesData[]>((acc, year) => { | ||
if (year in platformGames) { | ||
acc.push({ | ||
x: year, | ||
y: platformGames[year].length, | ||
}); | ||
} | ||
|
||
return acc; | ||
}, []), | ||
}; | ||
}); | ||
|
||
return {series}; | ||
} | ||
|
||
export const StackedArea = () => { | ||
const {series} = prepareData(); | ||
|
||
const data = series.map((s) => { | ||
return { | ||
type: 'area', | ||
stacking: 'normal', | ||
name: s.name, | ||
data: s.data, | ||
} as AreaSeries; | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: data, | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
categories: years, | ||
title: { | ||
text: 'Release year', | ||
}, | ||
}, | ||
}; | ||
|
||
return <ChartKit type="d3" data={widgetData} />; | ||
}; |
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
Oops, something went wrong.