简体   繁体   English

如何使用ajax和jquery向Google饼图动态添加行

[英]how to dynamically add rows to google pie chart using ajax and jquery

how do I dynamically add rows to the google pie chart on each time the data is pulled by ajax call? 每次通过Ajax调用提取数据时,如何动态向Google饼图添加行?

html html

<div id="piechart" style="width: 900px; height: 500px;"></div>

google pie chart 谷歌饼图

google.charts.load("current", {packages: ["corechart"]});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Task', 'Hours per Day'],
        ['Work', 11],
        ['Eat', 2],
        ['Commute', 2],
        ['Watch TV', 2],
        ['Sleep', 7]
    ]);

    var options = {
        'width': 900,
        'height': 500
        // pieHole: 0.4,
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));
    chart.draw(data, options);
}

now instead of I want dynamic adding of rows as soon as page is loaded a should add new values fetched from ajax query like this: 现在,而不是我希望在页面加载后立即动态添加行,应该添加从ajax查询中获取的新值,如下所示:

ajax call 阿贾克斯电话

function getPieChartData() {
    $.ajax({
        url: pieChartData,
        headers: {
            token: token,
            params: JSON.stringify({
                "ads":[2,3],"start_date":"2018-01-01"
            })
        },
        type: "GET"
    }).then((response)=> {
        r = JSON.parse(response);
        let data = r.data
        if(data.length >0){
        }
    })
}

by default, google.charts.load will wait for the page to load, 默认情况下, google.charts.load将等待页面加载,
we can use this in place of $(document).ready or similar functions. 我们可以用它代替$(document).ready或类似的函数。

once google charts has loaded, go ahead and create the data table, chart, options, etc., Google图表加载后,继续创建数据表,图表,选项等,
then get the data. 然后获取数据。

assuming the data is an array of arrays, similar to the example you provided, 假设数据是一个数组数组,类似于您提供的示例,
you can simply use data table method addRows to add the data to the data table. 您可以简单地使用数据表方法addRows将数据添加到数据表中。
eg 例如

data.addRows([
  ['Work', 11],
  ['Eat', 2],
  ['Commute', 2],
  ['Watch TV', 2],
  ['Sleep', 7]
]);

or... 要么...

data.addRows(r.data);

if it is a single array or one row, use addRow 如果是单个数组或一行,请使用addRow

data.addRow(['Work', 11]);

the full snippet might look something like this... 完整的代码段可能看起来像这样...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Category');
  data.addColumn('number', 'Value');

  var options = {
    width: 900,
    height: 500
  };

  var chart = new google.visualization.PieChart(document.getElementById('piechart'));

  getPieChartData();

  function getPieChartData() {
    var pieChartData = 'some url';
    var token = 'some token';

    $.ajax({
      url: pieChartData,
      headers: {
        token: token,
        params: JSON.stringify({
          "ads":[2,3],"start_date":"2018-01-01"
        })
      },
      type: "GET"
    }).then((response)=> {
      r = JSON.parse(response);
      if (r.data.length > 0){
        data.addRows(r.data);
        chart.draw(data, options);
      }
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM