-
-
Save Kamilczak020/3a0b2e420556614251303a0ecd8941b7 to your computer and use it in GitHub Desktop.
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
buildPieChartArcs(data, radius): JSX.Element[] { | |
const arc = d3Arc(); | |
// First, get total quantity | |
const domainMax = this.props.data | |
.map(dataPoint => dataPoint.value) | |
.reduce((previous, current) => previous + current); | |
let lastAngle = 0; | |
const arcPaths = data.map(dataPoint => { | |
const currentAngle = (dataPoint.value / domainMax) * 360; | |
const path = arc({ | |
innerRadius: 0, | |
outerRadius: radius, | |
startAngle: lastAngle * Math.PI / 180, | |
endAngle: (lastAngle + currentAngle) * Math.PI / 180, | |
padAngle: 0, | |
}); | |
const r = Math.floor(Math.random() * 256).toString(16); | |
const g = Math.floor(Math.random() * 256).toString(16); | |
const b = Math.floor(Math.random() * 256).toString(16); | |
const color = `#${r}${g}${b}`; | |
lastAngle = currentAngle + lastAngle; | |
return <path {...{d: path, fill: color, stroke: 'none'}} style={{margin: 0, padding: 0}} /> | |
}); | |
return arcPaths; | |
} | |
render() { | |
const transform = `translate(${this.props.radius}, ${this.props.radius})`; | |
return ( | |
<g {...{transform}}> | |
{this.buildPieChartArcs(this.props.data, this.props.radius)} | |
</g> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment