0

I need your help to learn how to dynamically implement data from a json file to Chartjs. The fields that I need to fill dynamically are labels and data.

JSON File <<<

[
   {
      "EFICAZ_TAB_ITEM_ID":1,
      "EFICAZ_PERCENTS":99
   },
   {
      "EFICAZ_TAB_ITEM_ID":2,
      "EFICAZ_PERCENTS":99
   },
   {
      "EFICAZ_TAB_ITEM_ID":3,
      "EFICAZ_PERCENTS":99
   }
]


CHARTJS <<<

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',

    // The data for our dataset
    data: {
        labels: [???],
        datasets: [{
            label: "My First dataset",
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [???],
        }]
    },

    // Configuration options go here
    options: {}
});
3
  • You need to get 2 arrays from your json: 1) Array of labels 2) Array of values Then you've to set that arrays as data.labels and data.datasets.data. If it's not initial values of your charts you have to update your charts with update() method called to your chart's instance. Commented Jun 11, 2018 at 12:45
  • And btw, your question is duplicate of this Commented Jun 11, 2018 at 12:47
  • @Brissy like this? labels: ArrayLabel data: ArrayData? Commented Jun 11, 2018 at 12:48

2 Answers 2

1

I have used $.ajax() to fetch JSON data, and save the data in two arrays, then you can use these arrays for your chart label and data. Hope this helps!

 var lbl = [];
    var dta = [];

    $.ajax({
      url: "test.json",
      dataType: 'json',
      async: false,
      success: function(data) {
        $.each(data, function(i, field){
             lbl.push(field.EFICAZ_TAB_ITEM_ID
    );
               dta.push(field.EFICAZ_PERCENTS);

           });
      }
    });


     $.getJSON("test.json", function(result){
           $.each(result, function(i, field){
             lbl.push(field.EFICAZ_TAB_ITEM_ID
    );
               dta.push(field.EFICAZ_PERCENTS);

           });
        });


    var ctx = document.getElementById("myCanvas").getContext('2d');



    var chart = new Chart(ctx, {
        // The type of chart we want to create
        type: 'line',

        // The data for our dataset
        data: {
            labels: lbl,
            datasets: [{
                label: "My First dataset",
                backgroundColor: 'rgb(255, 99, 132)',
                borderColor: 'rgb(255, 99, 132)',
                data: dta,
            }]
        },

        // Configuration options go here
        options: {}
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Here is example

document.addEventListener('DOMContentLoaded', function(){
  
  var chartData = [
   {
      "EFICAZ_TAB_ITEM_ID":1,
      "EFICAZ_PERCENTS":21
   },
   {
      "EFICAZ_TAB_ITEM_ID":2,
      "EFICAZ_PERCENTS":55
   },
   {
      "EFICAZ_TAB_ITEM_ID":3,
      "EFICAZ_PERCENTS":32
   }
]
 
 var labels = [];
 var values = [];
 chartData.forEach(function(el,key){
  labels.push(el.EFICAZ_TAB_ITEM_ID);
  values.push(el.EFICAZ_PERCENTS);
})
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',

    // The data for our dataset
    data: {
        labels: labels,
        datasets: [{
            label: "My First dataset",
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: values,
        }]
    },

    // Configuration options go here
    options: {}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.