<tfoot id='Y8UJM'></tfoot>
  • <i id='Y8UJM'><tr id='Y8UJM'><dt id='Y8UJM'><q id='Y8UJM'><span id='Y8UJM'><b id='Y8UJM'><form id='Y8UJM'><ins id='Y8UJM'></ins><ul id='Y8UJM'></ul><sub id='Y8UJM'></sub></form><legend id='Y8UJM'></legend><bdo id='Y8UJM'><pre id='Y8UJM'><center id='Y8UJM'></center></pre></bdo></b><th id='Y8UJM'></th></span></q></dt></tr></i><div id='Y8UJM'><tfoot id='Y8UJM'></tfoot><dl id='Y8UJM'><fieldset id='Y8UJM'></fieldset></dl></div>
      <legend id='Y8UJM'><style id='Y8UJM'><dir id='Y8UJM'><q id='Y8UJM'></q></dir></style></legend>
        <bdo id='Y8UJM'></bdo><ul id='Y8UJM'></ul>

      1. <small id='Y8UJM'></small><noframes id='Y8UJM'>

      2. 将 JSON 文件与 chart.js 一起使用

        时间:2023-11-02
        1. <i id='snPzY'><tr id='snPzY'><dt id='snPzY'><q id='snPzY'><span id='snPzY'><b id='snPzY'><form id='snPzY'><ins id='snPzY'></ins><ul id='snPzY'></ul><sub id='snPzY'></sub></form><legend id='snPzY'></legend><bdo id='snPzY'><pre id='snPzY'><center id='snPzY'></center></pre></bdo></b><th id='snPzY'></th></span></q></dt></tr></i><div id='snPzY'><tfoot id='snPzY'></tfoot><dl id='snPzY'><fieldset id='snPzY'></fieldset></dl></div>

          <tfoot id='snPzY'></tfoot>

          <small id='snPzY'></small><noframes id='snPzY'>

              <tbody id='snPzY'></tbody>

              <bdo id='snPzY'></bdo><ul id='snPzY'></ul>
              <legend id='snPzY'><style id='snPzY'><dir id='snPzY'><q id='snPzY'></q></dir></style></legend>

                • 本文介绍了将 JSON 文件与 chart.js 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I've been looking all over chart.js-related questions, but not two developers seem to be giving the same answer on how to display a chart using chart.js + JSON.

                  I am trying to display a chart using a JSON file - specifically a list of "amounts" with their relative labels ("January 2017",...).

                  The chart canva display just fine, no console log error, but no chart itself. What am I missing?

                  Thanks!

                  Here my chart.js code:-

                  var labels = [];
                  var data = [];
                  
                  $.getJSON("https://jsonblob.com/api/jsonBlob/26078b70-6b6f-11e7-a38a-bf689f57642c"), function (data) {
                    $.each(data.customers.amounts, function(key, value){
                      var labels = json.map(function(item) {
                        labels.push(item.key);
                     });
                      var data = json.map(function(item) {
                        data.push(item.value);
                     });
                     });
                  }
                  
                  var ctx = document.getElementById('myChart').getContext('2d');
                  var chart = new Chart(ctx, {
                      type: 'line',
                      data: {
                          datasets: [{
                              labels: labels,
                              backgroundColor: 'rgb(129, 198, 2228)',
                              borderColor: 'rgb(0, 150, 215)',
                              data: data
                          }]
                      },
                      options: {
                        responsive: 'true',
                      }
                  });
                  

                  and here's my JSON file:-

                  {
                    "customers": [
                      {
                        "first_name": "John",
                        "last_name": "Doe",
                        "account": "123456",
                        "period": "13th July - 13th August",
                        "due_date": "14th September",
                        "amounts": [
                          ["January 2017", 121.23],
                          ["February 2017", 145.23],
                          ["March 2017", 55.12],
                          ["April 2017", 78.58],
                          ["May 2017", 89.13],
                          ["June 2017", 45.78],
                          ["July 2017", 90.22]
                        ]
                      }
                    ]
                  }
                  

                  解决方案

                  Couple of Issues :

                  • since $.getJSON() method is asynchronous, you should construct the chart inside it's callback function.
                  • you are looping through the response data incorrectly. could be as simple as :

                      var labels = data.customers[0].amounts.map(function(e) {
                         return e[0];
                      });
                  
                      var data = data.customers[0].amounts.map(function(e) {
                         return e[1];
                      });
                  

                  • you are adding labels array to your dataset, while it belogns to the data object.

                  Here is the revised version of your code :

                  $.getJSON("https://jsonblob.com/api/jsonBlob/26078b70-6b6f-11e7-a38a-bf689f57642c", function(data) {
                     var labels = data.customers[0].amounts.map(function(e) {
                        return e[0];
                     });
                     var data = data.customers[0].amounts.map(function(e) {
                        return e[1];
                     });
                  
                     var ctx = document.getElementById('myChart').getContext('2d');
                     var chart = new Chart(ctx, {
                        type: 'line',
                        data: {
                           labels: labels,
                           datasets: [{
                              backgroundColor: 'rgb(129, 198, 2228)',
                              borderColor: 'rgb(0, 150, 215)',
                              data: data
                           }]
                        },
                        options: {
                           responsive: 'true',
                        }
                     });
                  });

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
                  <canvas id="myChart"></canvas>

                  这篇关于将 JSON 文件与 chart.js 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Chart.js 中隐藏特定数据标签上的工具提示? 下一篇:如何使用 Chart.js 在标签中放置新行?

                  相关文章

                    <bdo id='EAz2Y'></bdo><ul id='EAz2Y'></ul>

                  <small id='EAz2Y'></small><noframes id='EAz2Y'>

                  1. <tfoot id='EAz2Y'></tfoot>
                      <legend id='EAz2Y'><style id='EAz2Y'><dir id='EAz2Y'><q id='EAz2Y'></q></dir></style></legend>

                    1. <i id='EAz2Y'><tr id='EAz2Y'><dt id='EAz2Y'><q id='EAz2Y'><span id='EAz2Y'><b id='EAz2Y'><form id='EAz2Y'><ins id='EAz2Y'></ins><ul id='EAz2Y'></ul><sub id='EAz2Y'></sub></form><legend id='EAz2Y'></legend><bdo id='EAz2Y'><pre id='EAz2Y'><center id='EAz2Y'></center></pre></bdo></b><th id='EAz2Y'></th></span></q></dt></tr></i><div id='EAz2Y'><tfoot id='EAz2Y'></tfoot><dl id='EAz2Y'><fieldset id='EAz2Y'></fieldset></dl></div>