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

👍:0

Use generateLegend to get the legend HTML and put in the HTML element of your choice.

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

with 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/


enter image description here

👍:0

How to display data labels? I tried to use Index Labels. Check out: 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'};

👍:0

check my example where i added options: {
legend: { display: true,
position:’bottom’
}

for legend below chart
here is hyper link [https://jsfiddle.net/hiren65/xer2qvph/4/][1]

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

Leave a comment