我正在使用chartjs来显示折线图。
问题是我想在图表下面显示两个数据集的标签或图例。它应该显示数据集的颜色和文本描述。
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>
&#13;
我该怎么做?
答案 0 :(得分:0)
使用generateLegend
获取图例HTML并输入您选择的HTML元素。
document.getElementById("legend").innerHTML = myLineChart.generateLegend();
使用HTML
<div id="legend"></div>
你也想要设计风格
#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;
}
答案 1 :(得分:0)
如何显示数据标签?我试图使用索引标签。查看: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'};
答案 2 :(得分:0)
在添加示例的示例中进行检查:{ 图例:{display:true, 位置:“底部” }
对于图表下方的图例 这是超级链接[https://jsfiddle.net/hiren65/xer2qvph/4/][1]
[1]:https://jsfiddle.net/hiren65/xer2qvph/4/ enter code here