-
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): Stacked percentage charts (bar-x, bar-y, area) (#391)
* feat(D3 plugin): percent stacking * fix review(1)
- Loading branch information
Showing
19 changed files
with
343 additions
and
18 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
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,69 @@ | ||
import {groups} from 'd3'; | ||
import React from 'react'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData, AreaSeries, AreaSeriesData} from '../../../../types'; | ||
import {ExampleWrapper} from '../ExampleWrapper'; | ||
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 PercentStackingArea = () => { | ||
const {series} = prepareData(); | ||
|
||
const data = series.map((s) => { | ||
return { | ||
type: 'area', | ||
stacking: 'percent', | ||
name: s.name, | ||
data: s.data, | ||
} as AreaSeries; | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: data, | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
categories: years, | ||
}, | ||
}; | ||
|
||
return ( | ||
<ExampleWrapper> | ||
<ChartKit type="d3" data={widgetData} /> | ||
</ExampleWrapper> | ||
); | ||
}; |
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,61 @@ | ||
import React from 'react'; | ||
import {groups} from 'd3'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {BarXSeries, ChartKitWidgetData} from '../../../../types'; | ||
import {ExampleWrapper} from '../ExampleWrapper'; | ||
import nintendoGames from '../nintendoGames'; | ||
|
||
function prepareData() { | ||
const grouped = groups( | ||
nintendoGames, | ||
(d) => d.platform, | ||
(d) => (d.date ? new Date(d.date as number).getFullYear() : 'unknown'), | ||
); | ||
const categories: string[] = []; | ||
const series = grouped.map(([platform, years]) => { | ||
return { | ||
name: platform, | ||
data: years.map(([year, list]) => { | ||
categories.push(String(year)); | ||
|
||
return { | ||
x: String(year), | ||
y: list.length, | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
return {categories, series}; | ||
} | ||
|
||
export const PercentStackColumns = () => { | ||
const {series, categories} = prepareData(); | ||
const data = series.map((s) => { | ||
return { | ||
type: 'bar-x', | ||
stacking: 'percent', | ||
name: s.name, | ||
data: s.data, | ||
} as BarXSeries; | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: data, | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
categories: categories.sort(), | ||
title: { | ||
text: 'Release year', | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<ExampleWrapper> | ||
<ChartKit type="d3" data={widgetData} /> | ||
</ExampleWrapper> | ||
); | ||
}; |
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,63 @@ | ||
import React from 'react'; | ||
import {groups} from 'd3'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {BarYSeries, ChartKitWidgetData} from '../../../../types'; | ||
import {ExampleWrapper} from '../ExampleWrapper'; | ||
import nintendoGames from '../nintendoGames'; | ||
|
||
function prepareData() { | ||
const grouped = groups( | ||
nintendoGames, | ||
(d) => d.platform, | ||
(d) => (d.date ? new Date(d.date as number).getFullYear() : 'unknown'), | ||
); | ||
const categories: string[] = []; | ||
const series = grouped.map(([platform, years]) => { | ||
return { | ||
name: platform, | ||
data: years.map(([year, list]) => { | ||
categories.push(String(year)); | ||
|
||
return { | ||
y: String(year), | ||
x: list.length, | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
return {categories, series}; | ||
} | ||
|
||
export const PercentStackingBars = () => { | ||
const {series, categories} = prepareData(); | ||
const data = series.map((s) => { | ||
return { | ||
type: 'bar-y', | ||
stacking: 'percent', | ||
name: s.name, | ||
data: s.data, | ||
} as BarYSeries; | ||
}); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: data, | ||
}, | ||
yAxis: [ | ||
{ | ||
type: 'category', | ||
categories: categories.sort(), | ||
title: { | ||
text: 'Release year', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
return ( | ||
<ExampleWrapper> | ||
<ChartKit type="d3" data={widgetData} /> | ||
</ExampleWrapper> | ||
); | ||
}; |
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.