-
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): add allowOverlap dataLabels option (#347)
* feat(D3 plugin): add allowOverlap dataLabels option * fix review(1)
- Loading branch information
Showing
23 changed files
with
574 additions
and
126 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
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 React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {BarXSeries, BarXSeriesData, ChartKitWidgetData} from '../../../../types'; | ||
import nintendoGames from '../nintendoGames'; | ||
import {groups, sort} from 'd3'; | ||
|
||
const years = Array.from( | ||
new Set( | ||
nintendoGames.map((g) => | ||
g.date ? new Date(g.date as number).getFullYear().toString() : 'unknown', | ||
), | ||
), | ||
); | ||
|
||
function prepareData(): BarXSeries[] { | ||
const games = sort( | ||
nintendoGames.filter((d) => { | ||
return d.date && d.user_score; | ||
}), | ||
(d) => d.date, | ||
); | ||
|
||
const groupByYear = (d: any) => (d.date ? new Date(d.date as number).getFullYear() : 'unknown'); | ||
|
||
const byGenre = (genre: string): BarXSeries => { | ||
const data = groups( | ||
games.filter((d) => d.genres.includes(genre)), | ||
groupByYear, | ||
).map<BarXSeriesData>(([year, items]) => { | ||
return { | ||
x: years.indexOf(String(year)), | ||
y: items.length, | ||
}; | ||
}); | ||
|
||
return { | ||
type: 'bar-x', | ||
name: genre, | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
stacking: 'normal', | ||
data, | ||
}; | ||
}; | ||
|
||
return [byGenre('Strategy'), byGenre('Shooter'), byGenre('Puzzle'), byGenre('Action')]; | ||
} | ||
|
||
export const DataLabels = () => { | ||
const series = prepareData(); | ||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: series, | ||
}, | ||
xAxis: { | ||
categories: years, | ||
type: 'category', | ||
title: { | ||
text: 'Release year', | ||
}, | ||
ticks: {pixelInterval: 200}, | ||
}, | ||
}; | ||
|
||
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,99 @@ | ||
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(): 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) => { | ||
return { | ||
x: d.date, | ||
y: d.user_score, | ||
label: `${d.title} (${d.user_score})`, | ||
custom: d, | ||
}; | ||
}) as LineSeriesData[]; | ||
}; | ||
|
||
return [ | ||
{ | ||
name: 'Strategy', | ||
type: 'line', | ||
data: byGenre('Strategy'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Shooter', | ||
type: 'line', | ||
data: byGenre('Shooter'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
export const DataLabels = () => { | ||
const series = prepareData(); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: series.map<LineSeries>((s) => ({ | ||
type: 'line', | ||
data: s.data.filter((d) => d.x), | ||
name: s.name, | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
})), | ||
}, | ||
yAxis: [ | ||
{ | ||
title: { | ||
text: 'User score', | ||
}, | ||
}, | ||
], | ||
xAxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Release dates', | ||
}, | ||
ticks: {pixelInterval: 200}, | ||
}, | ||
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} />; | ||
}; |
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
Oops, something went wrong.