React-chart不会渲染PIE图表的图例

时间:2016-01-19 19:29:44

标签: javascript reactjs pie-chart chart.js

我正在使用react-chart在我们的应用程序中渲染饼图。我们想要显示饼图的图例,但无法找到关于如何做到这一点的任何选项,也无法帮助github回购。

请帮忙。

1 个答案:

答案 0 :(得分:1)

找到问题并发布相同的解决方案。

问题 - 该问题属于npm上可用的反应图表版本。可用的最新版本为0.6.0,而github repo为0.7.3。问题出在react-chart0.6.0中的代码段如下

classData[name] = function() {
   return this.state.chart[type].apply(this.state.chart, arguments);
};

请在获取最新版本后查看我用于显示图例的代码。

Component File

componentDidUpdate(){
   var pieChartLegend = this.refs.pieChart && this.refs.pieChart.generateLegend();
   if(this.state.chartLegend !== pieChartLegend){this.setState({chartLegend:pieChartLegend});}
}

render(){
   return(
    <div>
    <PieChart ref="pieChart" data={data} style={{height:'60%',width:'100%'}}
    options={
        {
            tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value + ' %' %>",
            legendTemplate:"<ul class=\"<%=name.toLowerCase()%>-legend\" style=\"list-style-type: none\"><% for (var i=0; i<segments.length; i++){%><li style=\"text-align: left;\"><span style=\"color:<%=segments[i].fillColor%>\"><%if(segments[i].label){%><%=segments[i].label%><%}%></span></li><%}%></ul>"
        }
    } />
    <div style={{position:'absolute',top:'100px',left:'370px'}} dangerouslySetInnerHTML={{__html: this.state.chartLegend}} />
    </div>
    )
}