-
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.
- Loading branch information
Showing
18 changed files
with
780 additions
and
10 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,40 @@ | ||
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'; | ||
|
||
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 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,85 @@ | ||
import React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData, AreaSeries, AreaSeriesData} from '../../../../types'; | ||
import nintendoGames from '../nintendoGames'; | ||
import {dateTime} from '@gravity-ui/date-utils'; | ||
|
||
function prepareData(): AreaSeries[] { | ||
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) => { | ||
return { | ||
x: d.date, | ||
y: d.user_score, | ||
label: `${d.title} (${d.user_score})`, | ||
custom: d, | ||
}; | ||
}) as AreaSeriesData[]; | ||
}; | ||
|
||
return [ | ||
{ | ||
name: 'Strategy', | ||
type: 'area', | ||
data: byGenre('Strategy'), | ||
}, | ||
{ | ||
name: 'Shooter', | ||
type: 'area', | ||
data: byGenre('Shooter'), | ||
}, | ||
]; | ||
} | ||
|
||
export const Basic = () => { | ||
const series = prepareData(); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: series, | ||
}, | ||
yAxis: [ | ||
{ | ||
title: { | ||
text: 'User score', | ||
}, | ||
}, | ||
], | ||
xAxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Release dates', | ||
}, | ||
}, | ||
tooltip: { | ||
renderer: (d) => { | ||
const point = d.hovered[0]?.data as AreaSeriesData; | ||
|
||
if (!point) { | ||
return null; | ||
} | ||
|
||
const title = point.custom.title; | ||
const score = point.custom.user_score; | ||
const date = dateTime({input: point.custom.date}).format('DD MMM YYYY'); | ||
|
||
return ( | ||
<React.Fragment> | ||
<b>{title}</b> | ||
<br /> | ||
Release date: {date} | ||
<br /> | ||
User score: {score} | ||
</React.Fragment> | ||
); | ||
}, | ||
}, | ||
}; | ||
|
||
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
89 changes: 89 additions & 0 deletions
89
src/plugins/d3/renderer/hooks/useSeries/prepare-area-series.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,89 @@ | ||
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} from './constants'; | ||
import {getRandomCKId} from '../../../../../utils'; | ||
import {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: { | ||
enabled: true, | ||
opacity: 0.25, | ||
radius: 10, | ||
}, | ||
}; | ||
|
||
return { | ||
states: { | ||
normal: markerNormalState, | ||
hover: merge(hoveredMarkerDefaultOptions, seriesHoverState?.marker), | ||
}, | ||
}; | ||
} | ||
|
||
export function prepareAreaSeries(args: PrepareAreaSeriesArgs) { | ||
const {colorScale, series: seriesList, seriesOptions, legend} = args; | ||
const defaultAreaWidth = get(seriesOptions, 'area.lineWidth', DEFAULT_LINE_WIDTH); | ||
|
||
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, | ||
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, | ||
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
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.