Skip to content

Instantly share code, notes, and snippets.

@petebrowne
Last active June 11, 2016 17:58
Show Gist options
  • Save petebrowne/59fdbb9efa7416e56ca23b33d3b98561 to your computer and use it in GitHub Desktop.
Save petebrowne/59fdbb9efa7416e56ca23b33d3b98561 to your computer and use it in GitHub Desktop.
class Pie extends React.Component {
constructor(props) {
super(props);
// https://github.com/d3/d3/wiki/Ordinal-Scales#category10
this.colorScale = d3.scale.category10();
this.renderSlice = this.renderSlice.bind(this);
}
render() {
let {x, y, data} = this.props;
// https://github.com/d3/d3/wiki/Pie-Layout
let pie = d3.layout.pie();
return (
<g transform={`translate(${x}, ${y})`}>
{/* Render a slice for each data point */}
{pie(data).map(this.renderSlice)}
</g>
);
}
renderSlice(value, i) {
// We'll create this component in a minute
return (
<Slice key={i}
outerRadius={this.props.radius}
value={value}
fill={this.colorScale(i)} />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment