-
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): Add text for center in donut charts (#389)
* feat(d3): add text for center in donut charts * add utils * fix * fix story * fix type
- Loading branch information
Showing
9 changed files
with
113 additions
and
1 deletion.
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,44 @@ | ||
import React from 'react'; | ||
import {groups} from 'd3'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitWidgetData} from '../../../../types'; | ||
import {ExampleWrapper} from '../ExampleWrapper'; | ||
import nintendoGames from '../nintendoGames'; | ||
import {CustomShapeRenderer} from '../../utils'; | ||
|
||
function prepareData() { | ||
const gamesByPlatform = groups(nintendoGames, (d) => d.esrb_rating || 'unknown'); | ||
return gamesByPlatform.map(([value, games]) => ({ | ||
name: value, | ||
value: games.length, | ||
})); | ||
} | ||
|
||
export const DonutWithTotals = () => { | ||
const data = prepareData(); | ||
const totals = data.reduce((sum, d) => sum + d.value, 0); | ||
|
||
const widgetData: ChartKitWidgetData = { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'pie', | ||
innerRadius: '50%', | ||
data: data, | ||
renderCustomShape: CustomShapeRenderer.pieCenterText(`${totals}`), | ||
}, | ||
], | ||
}, | ||
legend: {enabled: true}, | ||
title: { | ||
text: 'ESRB ratings', | ||
style: {fontSize: '12px', fontWeight: 'normal'}, | ||
}, | ||
}; | ||
|
||
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
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,5 @@ | ||
import {pieCenterText} from './pie-center-text'; | ||
|
||
export const CustomShapeRenderer = { | ||
pieCenterText, | ||
}; |
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,31 @@ | ||
import {create} from 'd3'; | ||
import get from 'lodash/get'; | ||
|
||
import {getLabelsSize} from '../renderer/utils'; | ||
|
||
const MAX_FONT_SIZE = 64; | ||
|
||
export function pieCenterText(text: string, options?: {padding?: number}) { | ||
if (!text) { | ||
return undefined; | ||
} | ||
|
||
const padding = get(options, 'padding', 12); | ||
|
||
return function (args: {series: {innerRadius: number}}) { | ||
let fontSize = MAX_FONT_SIZE; | ||
|
||
const textSize = getLabelsSize({labels: [text], style: {fontSize: `${fontSize}px`}}); | ||
fontSize = (fontSize * (args.series.innerRadius - padding) * 2) / textSize.maxWidth; | ||
|
||
const container = create('svg:g'); | ||
container | ||
.append('text') | ||
.text(text) | ||
.attr('text-anchor', 'middle') | ||
.attr('alignment-baseline', 'middle') | ||
.style('font-size', `${fontSize}px`); | ||
|
||
return container.node(); | ||
}; | ||
} |
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