简体   繁体   English

如何使用Chart.js v1将彩色图例框添加到饼图中?

[英]How to add a coloured legend box to a pie chart with Chart.js v1?

I am trying to add a legend box to a pie chart with Chart.js v1. 我正在尝试使用Chart.js v1将图例框添加到饼图中。

This is my HTML code: 这是我的HTML代码:

<div id="TheLegendOfDeviceChart"></div>
<canvas id="deviceChart" style="height:250px"></canvas>

And this is my Javascript code: 这是我的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();

This is what I get: 这是我得到的:

A pie chart with a monochromatic legend 带有单色图例的饼图

But I would like to have a coloured legend box like this: http://www.chartjs.org/docs/#doughnut-pie-chart-introduction 但是我想有一个这样的彩色图例框: http : //www.chartjs.org/docs/#doughnut-pie-chart-introduction

How can I modify my legendTemplate to get this? 如何修改我的legendTemplate以获得此信息?

You will have to use Chart.js 2.0 to get the legend that you desire. 您将必须使用Chart.js 2.0来获取所需的图例。 What is the reason you are wanting to use 1.0? 您要使用1.0的原因是什么? It is no longer supported and the last release was Apr 5, 2016. 不再受支持,最新版本为2016年4月5日。

I highly recommend switching to 2.0 and then you can easily take advantage of the new legends. 我强烈建议切换到2.0,然后您就可以轻松利用新的图例。

If for whatever reason you cannot use 2.0 and must stick to 1.0, then you need to correct your legendTemplate property. 如果出于任何原因您不能使用2.0,而必须坚持使用1.0,则需要更正legendTemplate属性。 Based on the fiddle you provided yours is: 根据您提供的小提琴,您可以:

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>"

It's hard to tell since its a long string, but this HTML template results in an empty <span> which should otherwise contain your dataset label. 由于字符串很长,因此很难分辨,但是此HTML模板会导致空的<span> ,否则应包含您的数据集标签。 Remember, an empty span will not render anything even if it has a background color defined. 请记住,空白范围即使定义了background色也不会呈现任何内容。 Here is the resulting legend HTML once its rendered to demonstrate what I mean. 这是生成的图例HTML,一旦生成,将演示我的意思。

在此处输入图片说明

Per the Chart.js v1 docs , the correct legendTemplate string should be. 根据Chart.js v1文档 ,正确的legendTemplate字符串应该是。

legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"><%if(segments[i].label){%><%=segments[i].label%><%}%></span></li><%}%></ul>"

The difference being that the <span> contains the dataset label (and therefore displays correctly with colored background). 区别在于<span>包含数据集标签(因此可以正确显示彩色背景)。 However, even this default looks pretty bad. 但是,即使这个默认值看起来也很糟糕。

Fortunately, this is just standard HTML and CSS so you can style your legend however you see fit. 幸运的是,这只是标准的HTML和CSS,因此您可以按自己的喜好设置图例的样式。 I would recommend you create a legend in HTML that you like, them convert it to a string and add in the necessary chart.js template expressions (eg <% for (var i=0; i<segments.length; i++){% , <%=segments[i].fillColor%> , etc). 我建议您在HTML中创建自己喜欢的图例,将其转换为字符串并添加必要的chart.js模板表达式(例如<% for (var i=0; i<segments.length; i++){%<%=segments[i].fillColor%>等)。

Here is a codepen example using a quick legend template that I came up with that looks pretty close to the Chart.js v2 template. 这是一个使用我提供的快速图例模板的Codepen示例 ,看起来非常接近Chart.js v2模板。 For completeness, I've also provided the key parts below. 为了完整起见,我还提供了以下关键部分。

css: CSS:

.indicator_box {
  width: 55px;
  height: 5px;
  padding: 5px;
  margin: 5px 10px 5px 10px;
  padding-left: 5px;
  display: block;
  float: left;
}

Template String: 模板字符串:

"<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li style=\"float: left; clear: both;\"><div class=\"indicator_box\" style=\"background-color:<%=segments[i].fillColor%>\"></div><span style=\"font-size: 20px;\"><%if(segments[i].label){%><%=segments[i].label%><%}%></span></li><%}%></ul>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM