谷歌图表在浏览器中的高RAM

时间:2018-01-17 10:56:43

标签: javascript charts google-visualization

我看到使用谷歌图表会产生大量的RAM使用。

我必须说我的代码有些思考javascript,它允许浏览器每7秒加载一个新图表,它确实为Google Draw Chart函数提供了一个新文件的新数据,并且图表被绘制,但这似乎创建了在我的电脑上使用大量的RAM。有人知道如何避免这种伎俩吗?我想也许浏览器会将之前的所有数据都保存在缓存中,如果不这样做,那么RAM可能会在几分钟之后变得如此之高?因为它现在越来越高,只需几分钟即可达到100%并且浏览器停止工作。

这是我目前的代码:



function drawVisualization() 
{

//-- From this textfile the chart gets info which is the new symbol chart
var Textinhalt = $.ajax({ url: "Chartsettings.txt", contentType:"application/json", dataType:"json", async: false }).responseText;
var Setting = JSON.parse(Textinhalt);

 Symbol=Setting.Selection[0].symbol;
 Timeframe=Setting.Selection[0].timeframe;
 Broker=Setting.Selection[0].broker;

//--Now the new data is been getting from php response file
var fileurl = "getData.php?symbol="+Symbol+"&timeframe="+Timeframe+"&broker="+Broker;

var jsonData = $.ajax({
    url: fileurl,
    contentType:"application/json",
    dataType:"json",
    async: false
    }).responseText;
var array=$.parseJSON(jsonData);

//--Now new data have been saved into array and will be draw
var data =google.visualization.arrayToDataTable(array,true);

var options = { 
    legend: 'none'
    };

    var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
     chart.draw(data, options);

}


//--If this function is clicked the google charts get refreshed every 7 seconds and i did see that my browser creates more and more RAM usuage till it goes over 100% and stops working
function PlayCharts()
{
 drawVisualization();  
 setTimeout(PlaySignals, 7000);
}




1 个答案:

答案 0 :(得分:1)

问题的一部分可能是你每次都要创建一个新的图表 而不是使用新数据绘制相同的图表

建议稍微改变范围,
将图表变量移到drawVisualization之外 但你必须等到谷歌加载,直到创建
不确定你的页面加载功能是什么样的

此外,async: false上的$.ajax已弃用

建议类似以下设置...

// move declarations outside of drawVisualization
var chart;
var options = {
  legend: 'none'
};

// load google charts
google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  // create chart
  chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));

  // move other on page load functions here
  // google.charts.load can be used instead of --> $(document).ready
});

function drawVisualization() {
  //-- From this textfile the chart gets info which is the new symbol chart
  var Textinhalt = $.ajax({ url: "Chartsettings.txt", contentType:"application/json", dataType:"json", async: false }).responseText;
  var Setting = JSON.parse(Textinhalt);

  Symbol=Setting.Selection[0].symbol;
  Timeframe=Setting.Selection[0].timeframe;
  Broker=Setting.Selection[0].broker;

  //--Now the new data is been getting from php response file
  var fileurl = "getData.php?symbol="+Symbol+"&timeframe="+Timeframe+"&broker="+Broker;

  $.ajax({
    url: fileurl,
    contentType: 'application/json',
    dataType: 'json'
  }).done(function (jsonData) {
    var array = $.parseJSON(jsonData);

    //--Now new data have been saved into array and will be draw
    var data = google.visualization.arrayToDataTable(array, true);

    // draw same chart
    chart.draw(data, options);
  });
}

function PlayCharts() {
  drawVisualization();
  setTimeout(PlaySignals, 7000);
}