Google Charts - No data available - Able to display a blank chart?

↘锁芯ラ 提交于 2020-02-25 05:45:25

问题


How can I still have Google Charts still show a line chart if my data array looks like this? [['Date','Line']]

i.e. just the axis are defined. Most of my charts have data imported but some have nil on the data. I would like to display a blank chart. With data above I get an error message instead of a chart.

Here is code in my view

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

function drawChart() {
  var data = google.visualization.arrayToDataTable(#{@visiting_spread_movement_array});

  var options = {
                 title: 'Point Spread Movements for #{@event.visiting_team}'
                };


  var chart = new google.visualization.LineChart(document.getElementById('show-spread-visiting'));

  chart.draw(data, options);

}

回答1:


Well, to draw the chart you need at least one data point. To archieve this, you could use this workaround:

      var data = google.visualization.arrayToDataTable([
          [{
              f: 'Date',
              type: 'date' // wont work whithout this
          }, {
              f: 'Line',
              type: 'number' // wont work whithout this
          }], ]);

      var options = {
          title: 'Company Performance'
      };

      if (data.getNumberOfRows() == 0) { // if you have no data, add a data point and make the series transparent
          data.addRow([new Date(), 0])
          options.series = {
              0: {
                  color: 'transparent'
              }
          }
      }

Full fiddle: http://jsfiddle.net/qaLgh955/



来源:https://stackoverflow.com/questions/26425187/google-charts-no-data-available-able-to-display-a-blank-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!