-
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 options for gradient legend (#529)
* feat(D3): add options for gradient legend * fix * add story for bar-x series * fix
- Loading branch information
Showing
13 changed files
with
550 additions
and
103 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/plugins/d3/__stories__/bar-x/ContinuousLegend.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 {BarXSeriesData, ChartKitWidgetData} from '../../../../types'; | ||
import {ExampleWrapper} from '../../examples/ExampleWrapper'; | ||
import nintendoGames from '../../examples/nintendoGames'; | ||
import {D3Plugin} from '../../index'; | ||
import {getContinuesColorFn} from '../../renderer/utils'; | ||
|
||
const BarXWithContinuousLegend = () => { | ||
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 categories = gamesByPlatform.map(([platform, _games]) => platform); | ||
const data: BarXSeriesData[] = gamesByPlatform.map(([platform, games], index) => ({ | ||
x: index, | ||
y: games.length, | ||
label: `${platform}(${games.length})`, | ||
})); | ||
const getColor = getContinuesColorFn({colors, stops, values: data.map((d) => Number(d.y))}); | ||
data.forEach((d) => { | ||
d.color = getColor(Number(d.y)); | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'bar-x', | ||
name: 'Series 1', | ||
data, | ||
}, | ||
], | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
categories, | ||
}, | ||
title: {text: 'Bar-x with continues color'}, | ||
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 BarXWithContinuousLegendStory: StoryObj<typeof BarXWithContinuousLegend> = { | ||
name: 'Continuous legend', | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Bar-x', | ||
component: BarXWithContinuousLegend, | ||
}; |
76 changes: 76 additions & 0 deletions
76
src/plugins/d3/__stories__/pie/ContinuousLegend.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
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 PieWithContinuousLegend = () => { | ||
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 continues color'}, | ||
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 PieWithContinuousLegendStory: StoryObj<typeof PieWithContinuousLegend> = { | ||
name: 'Pie with continuous color', | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Pie', | ||
component: PieWithContinuousLegend, | ||
}; |
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.