I am attempting to create a d3 pie chart based off of this resource.
However, I get the following error:
Uncaught Type Error - Cannot read property 'pie' of undefined
My code:
class PieChart extends React.Component {
constructor() {
super();
// - This is where the error is occuring!
this.pie = d3.layout.pie().value((d) => d.value);
this.colors = d3.scale.category10();
}
arcGenerator(d, i) {
return (
<LabeledArc key = {`arc-${i}`}
data = {d}
innerRadius = {this.props.innerRadius}
outerRadius = { this.props.outerRadius }
color = {this.colors(i)} />
);
}
render() {
console.log('Render Method Fires');
let pie = this.pie(this.props.data),
translate = `translate(${this.props.x}, ${this.props.y})`;
return (
<g transform={translate}>
{pie.map((d, i) => this.arcGenerator(d, i))}
</g>
);
}
}
I think I have everything setup correctly. Im using react-rails gem as well as the d3-rails. I had to download the d3.js and put it directly in my js folder to get rid of the 'cannot find d3'.
Can anyone point me in the right direction, maybe you have a better resource for adding d3 + react functionality in rails?
d3.scale.category10()
. Please have a look at my answer for more information. – Hydrastinine