nvd3生活图表内存泄漏

时间:2015-06-30 11:15:45

标签: ajax nvd3.js

我尝试创建一个生命线图表。我总是显示固定数量的点,添加一个新点意味着删除旧点。为此,我使用间隔计时器重绘图表。

这非常好用,直到我运行探查器并查看内存消耗。此图表消耗大量内存,并且每个步骤都会越来越多。我看不出一个明显的原因,因为在新值为shift()之后,数据是push()

var data = [{
    "key" : "Long",
    "values" : getData()
}];
var chart;

function redraw() {

    nv.addGraph(function() {
        var chart = nv.models.lineChart().margin({
            left : 100
        })
        //Adjust chart margins to give the x-axis some breathing room.
        .useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
        //.transitionDuration(350) //how fast do you want the lines to transition?
        .showLegend(true) //Show the legend, allowing users to turn on/off line series.
        .showYAxis(true) //Show the y-axis
        .showXAxis(true);

        //Show the x-axis
        chart.xAxis.tickFormat(function(d) {
            return d3.time.format('%x')(new Date(d))
        });

        chart.yAxis.tickFormat(d3.format(',.1%'));

        d3.select('#chart svg').datum(data)
        //.transition().duration(500)
        .call(chart);

        nv.utils.windowResize(chart.update);
        return chart;
    });
}

function getData() {
    var arr = [];
    var theDate = new Date(2012, 01, 01, 0, 0, 0, 0);
    for (var x = 0; x < 30; x++) {
        arr.push({
            x : new Date(theDate.getTime()),
            y : Math.random() * 100
        });
        theDate.setDate(theDate.getDate() + 1);
    }
    return arr;
}

setInterval(function() {
    var long = data[0].values;
    var next = new Date(long[long.length - 1].x);
    next.setDate(next.getDate() + 1)
    long.shift();
    long.push({
        x : next.getTime(),
        y : Math.random() * 100
    });
    redraw();
}, 1500);

怎么了?

1 个答案:

答案 0 :(得分:0)

感谢@ shabeer90提示我找到了解决方案。在构建图表之后,我只需要调用以下方法。

ParseClient.Initialize("XXXXXXXXX", "XXXXXXXXXXXXXXX");               

            var push = new ParsePush();
            if (!string.IsNullOrWhiteSpace(Id))
            {
                push.Query = from install in ParseInstallation.Query
                             where install.Get<string>("NO") == Id
                             select install;
            }                
            push.Data = new Dictionary<string, object> {
              {"title", TitleMeg},
              {"alert", AlertMeg}
            };
            await push.SendAsync();

那就是它!