How to add caption to a chart using Chartjs?

Asked

Viewed 552 times

1

I created the following chart:

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red",
        subtitle: "texto"
    },
    {
        value: 590,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 190,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    }
];

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx).Doughnut(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="myChart" width="600" height="400"></canvas>

Also, I would like to add caption on the chart as well:

inserir a de

But I’m not finding out how. Can anyone help me?

1 answer

4


I just changed the end of your code:

Includes the generateLegend() to generate a legend in the new div that I added (legendDiv).

Here you can read about the documentation for Chart JS V1.

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red",
        subtitle: "texto"
    },
    {
        value: 590,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 190,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    }
];

var ctx = document.getElementById("myChart").getContext("2d");
var grafico = new Chart(ctx).Doughnut(data);
document.getElementById("legendDiv").innerHTML = grafico .generateLegend();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="myChart" width="600" height="400"></canvas>
<div id="legendDiv"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.