【发布时间】:2017-07-24 12:52:54
【问题描述】:
我正在尝试使用 Chart.js v1 将图例框添加到饼图中。
这是我的 HTML 代码:
<div id="TheLegendOfDeviceChart"></div>
<canvas id="deviceChart" style="height:250px"></canvas>
这是我的 Javascript 代码:
var pieChartCanvas = $("#deviceChart").get(0).getContext("2d");
var pieChart = new Chart(pieChartCanvas);
var PieData = [
{
value: 700,
color: "#f56954",
highlight: "#f56954",
label: "Chrome"
},
{
value: 500,
color: "#00a65a",
highlight: "#00a65a",
label: "IE"
},
// other values
];
var pieOptions = {
// some options
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>",
};
var pie = pieChart.Pie(PieData, pieOptions);
document.getElementById('TheLegendOfDeviceChart').innerHTML = pie.generateLegend();
这是我得到的:
A pie chart with a monochromatic legend
但我想要一个这样的彩色图例框: http://www.chartjs.org/docs/#doughnut-pie-chart-introduction
如何修改我的 legendTemplate 来获得这个?
【问题讨论】:
标签: javascript html charts chart.js legend