reactjs / react-chartjs

common react charting components using chart.js
MIT License
2.93k stars 301 forks source link

Question: With Advanced Usage #43

Closed JGSolutions closed 8 years ago

JGSolutions commented 8 years ago

How can I use this method?

From the chart.js site: var myLineChart = new Chart(ctx).Line(data); myLineChart.generateLegend();

Since react.js is rendering the pie chart.

Dallas62 commented 8 years ago

You can use .getChart() on the chart element then you have the myLineChart instance.

var ChartElement = (<LineChart data={data} />);

vart chart = ChartElement.getChart();

chart.generateLegend();
queimadus commented 8 years ago

@Dallas62, does your example still work in the latest react version?

Dallas62 commented 8 years ago

@queimadus I'm using react 0.13.3 and it seems to work. But the legend isn't a ReactElement.

queimadus commented 8 years ago

@Dallas62, my question specifically is if you can access the getChart() method before the ChartElement is rendered by the render method.

I managed to access the chart by rendering the ChartElement with a ref and then doing this.refs.chart.state.getChart() in the componentDidMount e.g..

How are you accessing the chart? Could you give an example?

Ideally, I'd like to render the ChartElement and the generateLegend() html right away in the render() function without having to use componentDidMount and using the approach described above.

Dallas62 commented 8 years ago

@queimadus I do not use generateLegend() in my case, I generate the legend myself.

I just answered to the question: How can I use this method?

queimadus commented 8 years ago

@Dallas62 so you don't access the underlying chart object from chart.js encapsulated in the ChartElement at all.

You generate the legend from the data you pass as props to the ChartElement. Is that it?

Dallas62 commented 8 years ago

Exactly