Displaying JSON data in Chartjs

后端 未结 2 1801
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 01:38

I am trying to use Chart JS to create a table with dynamically generated data points coming from my JSON file. The logic of my code looks like so:

var datapa         


        
2条回答
  •  没有蜡笔的小新
    2020-12-13 02:17

    Your approach on constructing the chart is completely inappropriate. Here is the proper way, that you should follow :

    var jsonfile = {
       "jsonarray": [{
          "name": "Joe",
          "age": 12
       }, {
          "name": "Tom",
          "age": 14
       }]
    };
    
    var labels = jsonfile.jsonarray.map(function(e) {
       return e.name;
    });
    var data = jsonfile.jsonarray.map(function(e) {
       return e.age;
    });;
    
    var ctx = canvas.getContext('2d');
    var config = {
       type: 'line',
       data: {
          labels: labels,
          datasets: [{
             label: 'Graph Line',
             data: data,
             backgroundColor: 'rgba(0, 119, 204, 0.3)'
          }]
       }
    };
    
    var chart = new Chart(ctx, config);
    
    

提交回复
热议问题