weixin_33744854 2015-06-30 11:15 采纳率: 0%
浏览 21

nvd3生活图表内存泄漏[重复]

This question already has an answer here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/24689157/nvd3-how-to-refresh-the-data-function-to-product-new-data-on-click" dir="ltr">NVD3 - How to refresh the data function to product new data on click</a>
                            <span class="question-originals-answer-count">
                                (1 answer)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-07-01 08:18:36Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

I try to create a living line chart. I always show a fixed number of points adding a new one means removing an old one. To do this I use an interval timer to redraw the chart.

This works quite nice until I run the profiler and have a look at the memory consumption. This chart consumes a lot of memory and more and more for every step. I cannot see an obvious reason because the data is shift() out of the array after a new value is push() in.

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);

What's wrong?

</div>
  • 写回答

1条回答 默认 最新

  • Memor.の 2015-07-01 06:00
    关注

    Thanks to @shabeer90 hint I found the solution. I just had to call the following method after the chart has been constructed.

    function update() {
        var data = getData();
    
        // Update the SVG with the new data and call chart
        chartData.datum(data).transition().duration(500).call(chart);
        nv.utils.windowResize(chart.update);
    };
    

    And that's it!

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器