-
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): marker for line charts (#361)
* feat(D3 plugin): marker for line charts * add unit test for prepareLineSeries
- Loading branch information
Showing
12 changed files
with
377 additions
and
13 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
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,78 @@ | ||
import React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData, LineSeries, LineSeriesData} from '../../../../types'; | ||
import nintendoGames from '../nintendoGames'; | ||
import {dateTime} from '@gravity-ui/date-utils'; | ||
|
||
function prepareData() { | ||
const dataset = nintendoGames.filter( | ||
(d) => d.date && d.user_score && new Date(d.date) > new Date(2022, 0, 1), | ||
); | ||
const data = dataset.map((d) => ({ | ||
x: d.date || undefined, | ||
y: d.user_score || undefined, | ||
custom: d, | ||
})); | ||
|
||
return { | ||
series: [ | ||
{ | ||
data, | ||
name: 'Nintendo games', | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
export const LineWithMarkers = () => { | ||
const {series} = prepareData(); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: series.map<LineSeries>((s) => ({ | ||
type: 'line', | ||
data: s.data.filter((d) => d.x), | ||
name: s.name, | ||
marker: {enabled: true, symbol: 'square'}, | ||
})), | ||
}, | ||
yAxis: [ | ||
{ | ||
title: { | ||
text: 'User score', | ||
}, | ||
}, | ||
], | ||
xAxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Release dates', | ||
}, | ||
}, | ||
tooltip: { | ||
renderer: (d) => { | ||
const point = d.hovered[0]?.data as LineSeriesData; | ||
|
||
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} />; | ||
}; |
60 changes: 60 additions & 0 deletions
60
src/plugins/d3/renderer/hooks/useSeries/__tests__/prepare-line-series.test.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,60 @@ | ||
import {DEFAULT_MARKER, prepareLineSeries} from '../prepare-line-series'; | ||
import {scaleOrdinal} from 'd3'; | ||
import type {LineSeries} from '../../../../../../types'; | ||
import type {PreparedLegend} from '../types'; | ||
import {DEFAULT_PALETTE} from '../../../constants'; | ||
|
||
describe('prepareLineSeries', () => { | ||
describe('marker', () => { | ||
const commonArgs = { | ||
colorScale: scaleOrdinal([] as string[], DEFAULT_PALETTE), | ||
legend: {} as PreparedLegend, | ||
}; | ||
|
||
it('If the marker parameters are not specified, the default values should be applied', () => { | ||
const preparedSeries = prepareLineSeries({ | ||
...commonArgs, | ||
series: [{}] as LineSeries[], | ||
}); | ||
|
||
const actual = preparedSeries.map((s) => s.marker.states.normal); | ||
const expected = [DEFAULT_MARKER]; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('Normal state. The settings of a specific series should be prioritized over the seriesOptions', () => { | ||
const preparedSeries = prepareLineSeries({ | ||
...commonArgs, | ||
seriesOptions: { | ||
line: { | ||
marker: { | ||
enabled: true, | ||
radius: 100, | ||
symbol: 'square', | ||
}, | ||
}, | ||
}, | ||
series: [ | ||
{}, | ||
{ | ||
marker: { | ||
radius: 200, | ||
symbol: 'circle', | ||
}, | ||
}, | ||
{marker: {enabled: false}}, | ||
] as LineSeries[], | ||
}); | ||
|
||
const actual = preparedSeries.map((s) => s.marker.states.normal); | ||
const expected = [ | ||
{enabled: true, radius: 100, symbol: 'square', borderColor: '', borderWidth: 0}, | ||
{enabled: true, radius: 200, symbol: 'circle', borderColor: '', borderWidth: 0}, | ||
{enabled: false, radius: 100, symbol: 'square', borderColor: '', borderWidth: 0}, | ||
]; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
}); |
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.