-
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 waterfall chart (#475)
* feat(D3 plugin): basic waterfall chart * fix label position * Fix negative bar position * fix types * fix
- Loading branch information
Showing
21 changed files
with
803 additions
and
29 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
116 changes: 116 additions & 0 deletions
116
src/plugins/d3/__stories__/waterfall/Playground.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,116 @@ | ||
import React from 'react'; | ||
|
||
import {Button} from '@gravity-ui/uikit'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {StoryObj} from '@storybook/react'; | ||
|
||
import {D3Plugin} from '../..'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import {settings} from '../../../../libs'; | ||
import {ChartKitWidgetData} from '../../../../types'; | ||
|
||
function prepareData() { | ||
const result: ChartKitWidgetData = { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'waterfall', | ||
data: [ | ||
{y: 100, x: 0}, | ||
{y: -20, x: 1}, | ||
{y: -15, x: 2}, | ||
{y: 30, x: 3}, | ||
{y: 45, x: 4}, | ||
{y: 10, x: 5}, | ||
{y: -120, x: 6}, | ||
{y: 30, x: 7}, | ||
{y: 10, x: 8}, | ||
{y: -20, x: 9}, | ||
{y: -5, x: 10}, | ||
{y: 35, x: 11}, | ||
{total: true, x: 12}, | ||
], | ||
name: 'Profit', | ||
dataLabels: {enabled: true}, | ||
}, | ||
], | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
categories: [ | ||
'Jan', | ||
'Feb', | ||
'Mar', | ||
'Apr', | ||
'May', | ||
'Jun', | ||
'Jul', | ||
'Aug', | ||
'Sep', | ||
'Oct', | ||
'Nov', | ||
'Dec', | ||
'Totals', | ||
], | ||
labels: { | ||
enabled: true, | ||
rotation: 30, | ||
}, | ||
}, | ||
yAxis: [ | ||
{ | ||
labels: { | ||
enabled: true, | ||
rotation: -90, | ||
}, | ||
ticks: { | ||
pixelInterval: 120, | ||
}, | ||
}, | ||
], | ||
chart: { | ||
events: { | ||
click: action('chart.events.click'), | ||
}, | ||
}, | ||
}; | ||
|
||
return result; | ||
} | ||
|
||
const ChartStory = ({data}: {data: ChartKitWidgetData}) => { | ||
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%', | ||
}} | ||
> | ||
<ChartKit type="d3" data={data} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const PlaygroundWaterfallChartStory: StoryObj<typeof ChartStory> = { | ||
name: 'Playground', | ||
args: { | ||
data: prepareData(), | ||
}, | ||
argTypes: { | ||
data: { | ||
control: 'object', | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Waterfall', | ||
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
49 changes: 49 additions & 0 deletions
49
src/plugins/d3/renderer/hooks/useSeries/prepare-waterfall.ts
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,49 @@ | ||
import type {ScaleOrdinal} from 'd3'; | ||
import get from 'lodash/get'; | ||
|
||
import type {WaterfallSeries} from '../../../../../types'; | ||
import {getRandomCKId} from '../../../../../utils'; | ||
import {DEFAULT_PALETTE} from '../../constants'; | ||
|
||
import {DEFAULT_DATALABELS_PADDING, DEFAULT_DATALABELS_STYLE} from './constants'; | ||
import type {PreparedLegend, PreparedSeries, PreparedWaterfallSeries} from './types'; | ||
import {prepareLegendSymbol} from './utils'; | ||
|
||
type PrepareWaterfallSeriesArgs = { | ||
colorScale: ScaleOrdinal<string, string>; | ||
series: WaterfallSeries[]; | ||
legend: PreparedLegend; | ||
}; | ||
|
||
export function prepareWaterfallSeries(args: PrepareWaterfallSeriesArgs): PreparedSeries[] { | ||
const {colorScale, series: seriesList, legend} = args; | ||
const [, negativeColor, positiveColor] = DEFAULT_PALETTE; | ||
|
||
return seriesList.map<PreparedWaterfallSeries>((series) => { | ||
const name = series.name || ''; | ||
const color = series.color || colorScale(name); | ||
|
||
const prepared: PreparedWaterfallSeries = { | ||
type: series.type, | ||
color, | ||
positiveColor: positiveColor, | ||
negativeColor: negativeColor, | ||
name, | ||
id: getRandomCKId(), | ||
visible: get(series, 'visible', true), | ||
legend: { | ||
enabled: get(series, 'legend.enabled', legend.enabled), | ||
symbol: prepareLegendSymbol(series), | ||
}, | ||
data: series.data, | ||
dataLabels: { | ||
enabled: series.dataLabels?.enabled || false, | ||
style: Object.assign({}, DEFAULT_DATALABELS_STYLE, series.dataLabels?.style), | ||
allowOverlap: series.dataLabels?.allowOverlap || false, | ||
padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING), | ||
}, | ||
cursor: get(series, 'cursor', null), | ||
}; | ||
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
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.