1๐
โ
Not sure if this is helpful (not used react-faux-dom) but I do the following; render a canvas element using react then when the component mounts use that moment to create the graph
import React from 'react';
import Chart from 'chart.js';
import ReactDOM from 'react-dom';
class ChartComponent extends React.Component {
componentDidMount() {
this.initializeChart(this.props.config);
}
initializeChart(options) {
let el = ReactDOM.findDOMNode(this.refs.chart);
let ctx = el.getContext("2d");
let chart = new Chart(ctx, options);
}
render() {
return (<canvas ref="chart" height="400px" />);
}
}
export default ChartComponent;
2๐
Also You can use React.createRef()
from React16
class Chart extends Component {
constructor(props) {
super(props);
this.chartRef = React.createRef();
}
componentDidMount() {
const ctx = this.chartRef.current.getContext('2d');
new ChartJS(ctx, options); // eslint-disable-line no-new
}
render() {
return (
<canvas ref={this.chartRef} />
</div>
);
}
}
1๐
import React,{Component} from 'react';
import Chart from "chart.js";
โ
โ
class GraphChart extends Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
โ
}
โ
componentDidMount() {
const ctx = this.ctx;
new Chart(ctx, {
type: "bar",
data: {
labels: ["Red", "Blue", "Yellow"],
datasets: [
{
label: "# of Likes",
data: [12, 19, 3],
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)"
]
},
{
label: "# of Likes",
data: [-12, -19, -3],
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)"
]
}
]
}
});
}
render() {
return (
<div>
<canvas width='800' height='300' ref={ctx => (this.ctx = ctx)}/>
</div>
)
}
}
export default GraphChart;
Source:stackexchange.com