简体   繁体   English

使用chartjs在折线图中显示数据标签(图例)

[英]Display data label (legend) in line-chart using chartjs

I am using chartjs to display a line chart. 我正在使用chartjs显示折线图。

The problem is that I want to display labels or a legend for both data sets below the chart. 问题是我想在图表下方显示两个数据集的标签或图例。 It should display the color and a textual description of the data set. 它应该显示数据集的颜色和文字说明。

 var ctx = document.getElementById("myChart").getContext("2d"); var data = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { label: "My First dataset", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,220,220,1)", pointColor: "rgba(220,220,220,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: [65, 59, 80, 81, 56, 55, 40] }, { label: "My Second dataset", fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(151,187,205,1)", pointColor: "rgba(151,187,205,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(151,187,205,1)", data: [28, 48, 40, 19, 86, 27, 90] } ] }; var myLineChart = new Chart(ctx).Line(data); 
 <script src="http://www.chartjs.org/assets/Chart.js"></script> <canvas id="myChart" width="400" height="400"></canvas> 

How can I do this? 我怎样才能做到这一点?

Use generateLegend to get the legend HTML and put in the HTML element of your choice. 使用generateLegend获取图例HTML并放入您选择的HTML元素。

document.getElementById("legend").innerHTML = myLineChart.generateLegend();

with HTML 与HTML

<div id="legend"></div>

You'd also want to style it 您还想为其设置样式

#legend ul {
    list-style: none;
    font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
    font-size: 12px;
}

#legend ul span {
    display: inline-block;
    height: 1em;
    width: 1em;
    margin-right: 0.5em;
}

Fiddle - http://jsfiddle.net/0hagvdas/ 小提琴-http: //jsfiddle.net/0hagvdas/


在此处输入图片说明

How to display data labels? 如何显示数据标签? I tried to use Index Labels. 我试图使用索引标签。 Check out: https://plnkr.co/edit/gdOEKze1QdzzinUwryCy?p=preview 检出: https : //plnkr.co/edit/gdOEKze1QdzzinUwryCy?p=preview

var dataThird = {
label: "s3",
data: {
indexLabel: 'dataPoints',
indexLabelPlacement: 'outside',  
indexLabelOrientation: 'horizontal',
dataPoints: [45220, 49450, 79880, 21690, 15112, 22452, 34400, 34227]
},
lineTension: 0.3,
fill: false,
borderColor: '#89da59',
backgroundColor: 'transparent',
pointBorderColor: '#89da59',
pointBackgroundColor: '#89da59',
pointRadius: 5,
pointHoverRadius: 7,
pointHitRadius: 30,
pointBorderWidth: 2,
pointStyle: 'circle'};

check my example where i added options: { legend: { display: true, position:'bottom' } 查看添加了选项的示例:{图例:{display:true,位置:'bottom'}

for legend below chart here is hyper link [ https://jsfiddle.net/hiren65/xer2qvph/4/][1] 图表下方的图例此处为超级链接[ https://jsfiddle.net/hiren65/xer2qvph/4/][1]

[1]: https://jsfiddle.net/hiren65/xer2qvph/4/ enter code here [1]: https//jsfiddle.net/hiren65/xer2qvph/4/ enter code here

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

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