Google图表不断绘制

时间:2019-03-21 14:56:42

标签: javascript google-visualization

所以我有此图表:

        google.charts.load('44', {
  callback: drawAxisTickColors,
  packages: ['controls', 'corechart']
});


 function drawAxisTickColors() {
   var data = new google.visualization.DataTable();
   data.addColumn('datetime', 'Date');
   data.addColumn('number', 'value');

   var dateArr2 = (<?php echo json_encode($dateArr); ?>);
   console.log(dateArr2);

   var bitcoinArr = (<?php echo json_encode($numbers); ?>);
   console.log(bitcoinArr);

   bitcoinArr = bitcoinArr.map(Number);

  var array = [];

  var length = Math.min(dateArr2.length, bitcoinArr.length);
  for (var i = 0; i < length; ++i) {
     array.push([ new Date(dateArr2[i]), bitcoinArr[i] ]);
  }

   data.addRows(array);

  var control = new google.visualization.ControlWrapper({
  controlType: 'ChartRangeFilter',
  containerId: 'control_div',
  options: {
      filterColumnIndex: 0,
      ui: {
          chartOptions: {
              height: 50,
              width: 600,
              chartArea: {
                  width: '80%'
              }
          }
      }
  }
  });

  var chart = new google.visualization.ChartWrapper({
  chartType: 'LineChart',
  containerId: 'chart_div',
  options: {
    width: 620,
    chartArea: {
      width: '80%'
    },
    hAxis: {
      format: 'hh:ss',
      slantedText: false,
      maxAlternation: 1
    }
  }
  });

  function setOptions() {
  var firstDate;
  var lastDate;
  var v = control.getState();

  if (v.range) {
    document.getElementById('dbgchart').innerHTML = v.range.start + ' to ' + v.range.end;
    firstDate = new Date(v.range.start.getTime() + 1);
    lastDate = new Date(v.range.end.getTime() - 1);
    data.setValue(v.range.start.getMonth(), 0, firstDate);
    data.setValue(v.range.end.getMonth(), 0, lastDate);
  } else {
    firstDate = data.getValue(0, 0);
    lastDate = data.getValue(data.getNumberOfRows() - 1, 0);
  }

  var ticks = [];
  for (var i = firstDate.getMonth(); i <= lastDate.getMonth(); i++) {
    ticks.push(data.getValue(i, 0));
  }

  chart.setOption('hAxis.ticks', ticks);
  chart.setOption('hAxis.viewWindow.min', firstDate);
  chart.setOption('hAxis.viewWindow.max', lastDate);
  if (dash) {
    chart.draw();
  }
  }

  setOptions();

  google.visualization.events.addListener(control, 'statechange', setOptions);

   var dash = new google.visualization.Dashboard(document.getElementById('dashboard'));
   dash.bind([control], [chart]);
  dash.draw(data);
}

我使用此图表显示每个用户的导入,但是每次绘制图表时,都会发生这种情况:

enter image description here

您可以看到图表保留了旧数据,这使我的网站变慢了。我尝试了clearChart(),但我的图表却显示错误或错误:

clearchart is not a function

我也尝试过:

Google Chart Constant Redrawing Memory Increase

但这似乎不适用于我。

我在做什么错? 请帮忙。

1 个答案:

答案 0 :(得分:1)

我解决了

不要调用Google API 100次