[英]how to add legend in pie chart
I use php codeigniter and I create pie chart using javascript. 我使用php codeigniter,并使用javascript创建饼图。 and database data pass using json encode.
和数据库数据使用json编码传递。 My problem is how I use legend.
我的问题是我如何使用图例。 for pie chart and title.
饼图和标题。
javascript JavaScript的
var pieData = [
{
value: sales_data[i].ans,
color:"#FF0000"
},
{
value : sales_data[i].noans,
color : "#006400"
},
{
value : sales_data[i].reans,
color : "#191970"
}
,
{
value : sales_data[i].noreans,
color : "#FFEA88"
}
];
// get pie chart canvas
var countries=document.getElementById("countries").getContext("2d");
// draw pie chart
new Chart(countries).Pie(pieData, pieOptions);
}
}
});
Give this a shot - 试一下-
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>"
Include this as part of your pieOptions. 将此作为您的pieOptions的一部分。
Refer this documentation of chart.js for more details on how to use chart.js. 有关如何使用chart.js的更多详细信息,请参考此chart.js文档。 For legends there dosent seem to be a simple way but you have an option to give label to each segment in your piechart which will act as your legend.
对于图例,似乎有一种简单的方法,但是您可以选择在饼图中的每个片段上加上标签,以作为您的图例。
var myPieChart = new Chart(ctx[0]).Pie(data,options);
var data = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red",
labelColor : 'white',
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green",
labelColor : 'white',
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow",
labelColor : 'white',
}
]
In this the label acts as your map legend and it looks like the example fiddle piechart using chart.js 在此标签用作您的地图图例,看起来像使用chart.js的示例小提琴饼图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.