-
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): basic area chart (#363)
* feat(D3 plugin): basic area chart * fix calculating axis domain for stacking series * stacked area * fix yAxis min value for area series * review fixes
- Loading branch information
Showing
27 changed files
with
951 additions
and
52 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
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
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,93 @@ | ||
import {ScaleOrdinal} from 'd3'; | ||
import get from 'lodash/get'; | ||
import merge from 'lodash/merge'; | ||
|
||
import {ChartKitWidgetSeriesOptions, AreaSeries} from '../../../../../types'; | ||
import {PreparedAreaSeries, PreparedLegend} from './types'; | ||
|
||
import { | ||
DEFAULT_DATALABELS_PADDING, | ||
DEFAULT_DATALABELS_STYLE, | ||
DEFAULT_HALO_OPTIONS, | ||
} from './constants'; | ||
import {getRandomCKId} from '../../../../../utils'; | ||
import {getSeriesStackId, prepareLegendSymbol} from './utils'; | ||
|
||
export const DEFAULT_LINE_WIDTH = 1; | ||
|
||
export const DEFAULT_MARKER = { | ||
enabled: false, | ||
symbol: 'circle', | ||
radius: 4, | ||
borderWidth: 0, | ||
borderColor: '', | ||
}; | ||
|
||
type PrepareAreaSeriesArgs = { | ||
colorScale: ScaleOrdinal<string, string>; | ||
series: AreaSeries[]; | ||
seriesOptions?: ChartKitWidgetSeriesOptions; | ||
legend: PreparedLegend; | ||
}; | ||
|
||
function prepareMarker(series: AreaSeries, seriesOptions?: ChartKitWidgetSeriesOptions) { | ||
const seriesHoverState = get(seriesOptions, 'area.states.hover'); | ||
const markerNormalState = Object.assign( | ||
{}, | ||
DEFAULT_MARKER, | ||
seriesOptions?.area?.marker, | ||
series.marker, | ||
); | ||
const hoveredMarkerDefaultOptions = { | ||
enabled: true, | ||
radius: markerNormalState.radius, | ||
borderWidth: 1, | ||
borderColor: '#ffffff', | ||
halo: DEFAULT_HALO_OPTIONS, | ||
}; | ||
|
||
return { | ||
states: { | ||
normal: markerNormalState, | ||
hover: merge(hoveredMarkerDefaultOptions, seriesHoverState?.marker), | ||
}, | ||
}; | ||
} | ||
|
||
export function prepareArea(args: PrepareAreaSeriesArgs) { | ||
const {colorScale, series: seriesList, seriesOptions, legend} = args; | ||
const defaultAreaWidth = get(seriesOptions, 'area.lineWidth', DEFAULT_LINE_WIDTH); | ||
const defaultOpacity = get(seriesOptions, 'area.opacity', 0.75); | ||
|
||
return seriesList.map<PreparedAreaSeries>((series) => { | ||
const id = getRandomCKId(); | ||
const name = series.name || ''; | ||
const color = series.color || colorScale(name); | ||
|
||
const prepared: PreparedAreaSeries = { | ||
type: series.type, | ||
color, | ||
opacity: get(series, 'opacity', defaultOpacity), | ||
lineWidth: get(series, 'lineWidth', defaultAreaWidth), | ||
name, | ||
id, | ||
visible: get(series, 'visible', true), | ||
legend: { | ||
enabled: get(series, 'legend.enabled', legend.enabled), | ||
symbol: prepareLegendSymbol(series), | ||
}, | ||
data: series.data, | ||
stacking: series.stacking, | ||
stackId: getSeriesStackId(series), | ||
dataLabels: { | ||
enabled: series.dataLabels?.enabled || false, | ||
style: Object.assign({}, DEFAULT_DATALABELS_STYLE, series.dataLabels?.style), | ||
padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING), | ||
allowOverlap: get(series, 'dataLabels.allowOverlap', false), | ||
}, | ||
marker: prepareMarker(series, seriesOptions), | ||
}; | ||
|
||
return prepared; | ||
}, []); | ||
} |
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.