Skip to content

Instantly share code, notes, and snippets.

@Kamilczak020
Created December 13, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kamilczak020/3a0b2e420556614251303a0ecd8941b7 to your computer and use it in GitHub Desktop.
Save Kamilczak020/3a0b2e420556614251303a0ecd8941b7 to your computer and use it in GitHub Desktop.
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