我正在使用react-chartjs-2爲我的應用程序創建折線圖。React Chart.js onClick自定義圖例
對於這個程序,我做了一個傳奇的定製,我可以使用這個生成它們:
// Chart component
<Line ref={ (chart) => chart ? this.insertLegends(chart) : null }
data={this.state.chart}
options={this.state.options}
/>
// Method which insert the html content
insertLegends(chart) {
this.refs.chartLegendContainerGlobal.innerHTML = chart.chart_instance.generateLegend();
}
首先,這是一個正確的做法? 我不得不在組件內部創建一個內聯條件來防止圖表爲空。
其次,我如何以及在哪裏可以爲每個圖例添加一個onClick事件?
我很迷茫,有沒有更好的方法來做這個傳奇定製?
謝謝!但我有一個問題,我怎樣才能調用我的方法insertLegends?用這種方法,我的這個在applyRef裏面是未定義的。 –
我弄清楚如何使用參考文獻來結合你的想法。我要發佈它。非常感謝! =) –
@ItaloBorges您需要在構造函數中綁定'applyRef'。 'this.applyRef = this.applyRef.bind(this);' –