简体   繁体   English

无法在D3中过渡到新数据

[英]Failing to transition in D3 off new data

I can load and display data perfectly once, but any attempt to update seems to fail. 我可以一次完美地加载和显示数据,但是任何更新尝试似乎都失败了。

I'm trying to build a map that shows activity. 我正在尝试构建一个显示活动的地图。 In my data csv I have latitude and logitude and the hour of activity (0-23). 在我的数据csv中,我有纬度和经度以及活动时间(0-23)。 I am using this highly useful tutorial on updating D3 with new data. 我正在使用这个非常有用的教程 ,用新数据更新D3。 The idea being that I cycle through data by the hour. 我的想法是我按小时循环浏览数据。

I've more-or-less stolen the update function. 我或多或少偷了更新功能。 While the initial call works perfectly, any repeated calls manage to only recolor the already-present data, rather than remove it and add new data. 虽然初始调用可以正常工作,但是任何重复调用都只能为已经存在的数据重新着色,而不能删除它并添加新数据。 This leads me to believe I am not changing the incoming data. 这使我相信我不会更改传入的数据。

Is there anything obviously wrong with my update call? 我的更新电话明显有问题吗?

function update(data, x) {
    // DATA JOIN
    // Join new data to any existing data
    var circles = g.selectAll("circle")
        .data(data); //, function(d) { return d; });

    // UPDATE
    // Update old elements as needed.
    circles
        .style("fill", "green")
            .transition()
            .duration(750);

    // ENTER
    // Create new elements
    circles.enter().append("circle")
        .filter(function(d) { return d.hr == x; })
        .attr("cx", function(d) {
            return projection([d.lon, d.lat])[0];
        })
        .attr("cy", function(d) {
            return projection([d.lon, d.lat])[1];
        })
        .attr("r", 0)
        .style("opacity", 0.2)
        .style("fill", "blue")
        .transition()
        .duration(750)
        .ease("linear")
        .attr("r", function(d) {
            return d.radius/2;
        });


    // EXIT
    // Remove old elements
    circles.exit()
        .style("fill", "red")
        .transition()
        .duration(1500)
        .ease("linear")
        .attr("r", 0)
        .remove();
}

Run from the command line: 从命令行运行:

update(csv_data, 1)
update(csv_data, 5)

It seems like you're trying to change the data by the line 似乎您正在尝试按行更改数据

filter(function(d) { return d.hr == x; })

but that's only filtering the selection . 但这只是过滤选择

What you want to do is filter the data before passing it to your selection which you do with the line 您想要做的是在将数据传递到您选择的行之前对数据进行过滤

.data(data);

I suggest using Array.filter in the beginning of your update function: 我建议在更新功能的开头使用Array.filter

function update(data, x) {
    // DATA JOIN
    // Join new data to any existing data

    // filter the data as per x
    data = data.filter(function (d) { return d.hr == x; });

    var circles = g.selectAll("circle")
        .data(data);

    // rest of function
 }

Be sure to remove the line where you filter after the append. 确保在追加之后删除过滤的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM