-
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): add options for gradient legend
- Loading branch information
Showing
12 changed files
with
429 additions
and
101 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,76 @@ | ||
import React from 'react'; | ||
|
||
import type {StoryObj} from '@storybook/react'; | ||
import {groups} from 'd3'; | ||
|
||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import {Loader} from '../../../../components/Loader/Loader'; | ||
import {settings} from '../../../../libs'; | ||
import type {ChartKitWidgetData, PieSeriesData} from '../../../../types'; | ||
import {ExampleWrapper} from '../../examples/ExampleWrapper'; | ||
import nintendoGames from '../../examples/nintendoGames'; | ||
import {D3Plugin} from '../../index'; | ||
import {getContinuesColorFn} from '../../renderer/utils'; | ||
|
||
const PieWithGradientLegend = () => { | ||
const [loading, setLoading] = React.useState(true); | ||
|
||
React.useEffect(() => { | ||
settings.set({plugins: [D3Plugin]}); | ||
setLoading(false); | ||
}, []); | ||
|
||
if (loading) { | ||
return <Loader />; | ||
} | ||
|
||
const colors = ['rgb(255, 61, 100)', 'rgb(255, 198, 54)', 'rgb(84, 165, 32)']; | ||
const stops = [0, 0.5, 1]; | ||
|
||
const gamesByPlatform = groups(nintendoGames, (item) => item.platform); | ||
const data: PieSeriesData[] = gamesByPlatform.map(([platform, games]) => ({ | ||
name: platform, | ||
value: games.length, | ||
label: `${platform}(${games.length})`, | ||
})); | ||
const getColor = getContinuesColorFn({colors, stops, values: data.map((d) => d.value)}); | ||
data.forEach((d) => { | ||
d.color = getColor(d.value); | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'pie', | ||
data, | ||
}, | ||
], | ||
}, | ||
title: {text: 'Pie with gradient legend'}, | ||
legend: { | ||
enabled: true, | ||
type: 'continuous', | ||
title: {text: 'Games by platform'}, | ||
colorScale: { | ||
colors: colors, | ||
stops, | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<ExampleWrapper styles={{minHeight: '400px'}}> | ||
<ChartKit type="d3" data={widgetData} /> | ||
</ExampleWrapper> | ||
); | ||
}; | ||
|
||
export const PieWithGradientLegendStory: StoryObj<typeof PieWithGradientLegend> = { | ||
name: 'Gradient colored pie', | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Pie', | ||
component: PieWithGradientLegend, | ||
}; |
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.