-
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): split charts with same X axis (#486)
* feat(D3 plugin): split charts with same X axis * fix types * Add plot titles * fix import * fix typings and story
- Loading branch information
Showing
22 changed files
with
463 additions
and
64 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,121 @@ | ||
import React from 'react'; | ||
|
||
import {action} from '@storybook/addon-actions'; | ||
import type {StoryObj} from '@storybook/react'; | ||
|
||
import {D3Plugin} from '../..'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import {Loader} from '../../../../components/Loader/Loader'; | ||
import {settings} from '../../../../libs'; | ||
import type {ChartKitRef, ChartKitWidgetData, LineSeries, LineSeriesData} from '../../../../types'; | ||
import nintendoGames from '../../examples/nintendoGames'; | ||
|
||
function prepareData(): LineSeries[] { | ||
const games = nintendoGames.filter((d) => { | ||
return d.date && d.user_score; | ||
}); | ||
|
||
const byGenre = (genre: string) => { | ||
return games | ||
.filter((d) => d.genres.includes(genre)) | ||
.map((d) => { | ||
const releaseDate = new Date(d.date as number); | ||
return { | ||
x: releaseDate.getFullYear(), | ||
y: d.user_score, | ||
label: `${d.title} (${d.user_score})`, | ||
custom: d, | ||
}; | ||
}) as LineSeriesData[]; | ||
}; | ||
|
||
return [ | ||
{ | ||
name: 'Strategy', | ||
type: 'line', | ||
data: byGenre('Strategy'), | ||
yAxis: 0, | ||
}, | ||
{ | ||
name: 'Shooter', | ||
type: 'line', | ||
data: byGenre('Shooter'), | ||
yAxis: 1, | ||
}, | ||
{ | ||
name: 'Puzzle', | ||
type: 'line', | ||
data: byGenre('Puzzle'), | ||
yAxis: 1, | ||
}, | ||
]; | ||
} | ||
|
||
const ChartStory = () => { | ||
const [loading, setLoading] = React.useState(true); | ||
const chartkitRef = React.useRef<ChartKitRef>(); | ||
|
||
React.useEffect(() => { | ||
settings.set({plugins: [D3Plugin]}); | ||
setLoading(false); | ||
}, []); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
title: { | ||
text: 'Chart title', | ||
}, | ||
series: { | ||
data: prepareData(), | ||
}, | ||
split: { | ||
enable: true, | ||
layout: 'vertical', | ||
gap: '40px', | ||
plots: [{title: {text: 'Strategy'}}, {title: {text: 'Shooter & Puzzle'}}], | ||
}, | ||
yAxis: [ | ||
{ | ||
title: {text: '1'}, | ||
plotIndex: 0, | ||
}, | ||
{ | ||
title: {text: '2'}, | ||
plotIndex: 1, | ||
}, | ||
], | ||
xAxis: { | ||
type: 'linear', | ||
labels: { | ||
numberFormat: { | ||
showRankDelimiter: false, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
if (loading) { | ||
return <Loader />; | ||
} | ||
|
||
return ( | ||
<div style={{height: '90vh', width: '100%'}}> | ||
<ChartKit | ||
ref={chartkitRef} | ||
type="d3" | ||
data={widgetData} | ||
onRender={action('onRender')} | ||
onLoad={action('onLoad')} | ||
onChartLoad={action('onChartLoad')} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Split: StoryObj<typeof ChartStory> = { | ||
name: 'Split', | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Line', | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
|
||
import {block} from '../../../../utils/cn'; | ||
import type {PreparedPlotTitle} from '../hooks/useSplit/types'; | ||
|
||
const b = block('d3-plot-title'); | ||
|
||
type Props = { | ||
title?: PreparedPlotTitle; | ||
}; | ||
|
||
export const PlotTitle = (props: Props) => { | ||
const {title} = props; | ||
|
||
if (!title) { | ||
return null; | ||
} | ||
|
||
const {x, y, text, style, height} = title; | ||
|
||
return ( | ||
<text | ||
className={b()} | ||
dx={x} | ||
dy={y} | ||
dominantBaseline="middle" | ||
textAnchor="middle" | ||
style={{ | ||
lineHeight: `${height}px`, | ||
...style, | ||
}} | ||
> | ||
<tspan>{text}</tspan> | ||
</text> | ||
); | ||
}; |
Oops, something went wrong.