Google Chart不断重绘内存增加 [英] Google Chart Constant Redrawing Memory Increase

查看:102
本文介绍了Google Chart不断重绘内存增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每秒都会根据新数据重绘图表,它的工作原理和外观都很不错,但是我注意到它每秒增加了1 MB的内存.有任何解决这个问题的方法吗?我注意到如果我只有静态图表,则内存会稳定下来,但是一旦添加了常量重绘(以更新数据),内存使用就永远不会停止.

I'm redrawing the chart based on new data each second, and it works and looks great, but I notice it increase memory like 1 MB usage each second. Any way to fix this? I notice if I just have static charts, then the memory stabilizes, but once I add constant redrawing (in order to update data), the memory usage never stops.

起初我以为是因为每次都在创建图表的新实例,所以我更改了代码,因此每次都只重绘相同的实例,但这并没有太大帮助.

At first I thought it was because I was creating a new instance of the chart each time, so I change the code so it was only redrawing the same instance each time, but that didn't help much at all.

有人知道如何解决它吗?我是否需要先以某种方式转储旧图表?

Anyone know how to fix it? Do I need to dump the old chart first somehow?

google.setOnLoadCallback(test);

var chart;
var chartOptions;
var chartCreate;

function test() {
    chart = new google.visualization.DataTable();
    chart.addColumn('string', 'Lorem');
    chart.addColumn('number', 'Ipsum');
    chart.addRows([
            ['', 0]
    ]);
    chartOptions = {};
    chartCreate = new google.visualization.LineChart(document.getElementById('chartDiv'));
          chartCreate.draw(chart, chartOptions);
    ]);
}

function test2() {
    chart.removeRows(0, 5);
    for (var i = 0; i < dataSpaceArray.length; ++i) {
        chart.addRow([dataTimeArray[i], dataSpaceArray[i], dataSpeedArray[i]]);
    }
        chartCreate.draw(chart, chartOptions);
}

setTimeout(test2,1000)

推荐答案

我已经通过全局存储图表解决了这一问题.在绘制图表之前,您需要检查图表是否已实例化.如果不是,则创建新的图表对象,否则在绘制它之前调用clearChart()方法.像这样:

I've solved this by globaly store the chart. Before draw the chart, you need to check if chart has been instantiated. If not, create the new chart object else call clearChart() method before draw it. Like this:

//Store all chart objects in a global array to avoid memory leak
var charts = [];

function drawChart(chartName, element, data, chartOptions) {
   if (charts[chartName] === undefined || charts[chartName] === null) {
          charts[chartName] = new google.visualization.LineChart(group);
   } else {
          charts[chartName].clearChart();
   }
   charts[chartName].draw(dataTable, chartOptions);
}

这篇关于Google Chart不断重绘内存增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆