L
L
lev892021-03-15 15:36:25
JavaScript
lev89, 2021-03-15 15:36:25

How to use array values ​​in chart.js data and label field?

I want to pass array values ​​to data fields. I am fetching json data and storing it in an array.

var data = JSON.parse('<?=$json;?>');
    // Labels
    data.forEach(function (entry) {
        console.log(entry.month + ' ' + entry.year);
    });
    // Data
    data.forEach(function (entry) {
        console.log(entry.cnt);
    });

Now I want to use these values ​​to build a chart
var myChart = new Chart(ctx, {
        type: 'line',
        data: {
          labels: [], // ???
          type: 'line',
          defaultFontFamily: 'Poppins',
          datasets: [{
            data: [], // ???
            label: "Количество заявок",
            backgroundColor: 'rgba(0,103,255,.15)',
            borderColor: 'rgba(0,103,255,0.5)',
            borderWidth: 3.5,
            pointStyle: 'circle',
            pointRadius: 5,
            pointBorderColor: 'transparent',
            pointBackgroundColor: 'rgba(0,103,255,0.5)',
          },]
        },

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question