D3.js动态更新树状图

4

我想通过新数据动态更新Treemap。 我已经阅读了这篇文章,但是我没有使它正常运行。这是我的代码:

function buildTreemap(jVals){
    //$("#treemap").empty();

    var data = jQuery.parseJSON(jVals);

    var margin = {top: 40, right: 10, bottom: 10, left: 10},
        width = 760 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    var treemap = d3.layout.treemap()
        .size([width, height])
        .sticky(true)
        .sort(function(a,b) { return a.anzahl - b.anzahl; })
        .round(true)
        .value(function(d) { return Math.log(d.anzahl); });

    var div = d3.select("#treemap").append("div")
        .style("position", "relative")
        .style("width", (width + margin.left + margin.right) + "px")
        .style("height", (height + margin.top + margin.bottom) + "px")
        .style("left", margin.left + "px")
        .style("top", margin.top + "px")
        .attr("id", "first");

    var node = div.datum(data).selectAll(".node")
        .data(treemap.nodes)
        .enter().append("div")
        .attr("class", "node")
        .style("left", function(d) { return d.x + "px"; })
        .style("top", function(d) { return d.y + "px"; })
        .style("width", function(d) {  return Math.max(0, d.dx - 1) + "px"; })
        .style("height", function(d) { return Math.max(0, d.dy - 1) + "px"; })
        .style("background", function(d) { return d.color ? d.color : "#ffffff";})
        .text(function(d) { return d.children ? "blue" : d.keytable + "(" + d.anzahl + "-" + Math.max(0, d.dx) + "-" + Math.max(0, d.dy) + ")"; })
        ;

};

我从另一个Js函数中调用了buildTreemap(),并传入了新数据。现在我想用过渡/持续时间重新绘制树状图。

请有人能帮帮我吗?非常感谢...

更新: 我已添加了以下内容:

d3.selectAll("input").on("change", function change() {
        var value = this.value === "count"
            ? function() { return 1; }
            : function(d) { return Math.log(d.anzahl); };

        node
            .data(treemap.value(value).nodes)
            .transition()
            .duration(1500)
            .call(position);
    });

这段代码可以让矩形树图在我点击“change”输入框时重新构建。但如果我使用以下代码:
d3.selectAll("#search").on("keyup", function change() {
        var value = function(d) { return Math.log(d.anzahl); };

        node
            .data(treemap.value(value).nodes)
            .transition()
            .duration(1500)
            .call(position);
    });

树状图保留,另一个带有新数据值的树状图会在我的第一个树状图下方打开。在键盘按键抬起时,另一个JavaScript函数会被触发并发送新数据到buildTreemap()。


你看过这个吗? - Lars Kotthoff
是的,但这只会重新调整具有相同数据的div的大小。我需要使用新数据进行重新调整大小。 - maker23
原则是相同的。仍然在更改时向treemap布局传递新数据。 - Lars Kotthoff
嗯,请看一下我的更新。 - maker23
你能把你的完整代码发布到某个地方吗,最好是在 jsfiddle 上?没有完整的画面很难看出发生了什么。 - Lars Kotthoff
好的,现在请看这个fiddle... http://jsfiddle.net/maker23/WB5jh/1/有两个数据对象。第一个我想首先加载,第二个我想在点击按钮后加载。 - maker23
2个回答

3

我稍微简化并修改了你的 jsfiddle,在这里显示点击按钮时的第二组数据。关键在于,在按钮被点击时调用的函数内,像初始化 treemap 时一样传入新数据,然后相应地处理 enter 和 update 选择,像这样:

var node = div.datum(data2).selectAll(".node")
          .data(treemap.nodes);
node.enter().append("div")
    .attr("class", "node");

node.transition().duration(1500).call(position)
    .style("background", function(d) { return d.color ? d.color : "#ffffff"; })
    .text(function(d) { return d.children ? "blue" : d.keytable + "(" + d.anzahl + "-" + Math.max(0, d.dx) + "-" + Math.max(0, d.dy) + ")"; });

请注意,输入选择会合并到更新选择中,因此仅需将节点附加到更新选择并设置属性即可。

好的非常好!但是如果我使用更复杂的数据,行为就会变得非常奇怪。看看这个链接: http://jsfiddle.net/maker23/WB5jh/4/ - maker23
我认为旧节点仍然留在树图中,而新节点被推入...类似这样?!? - maker23
只需在更改函数的末尾添加 node.exit().remove() 即可。 - Lars Kotthoff
针对您的另一个问题,看起来您正在传递格式不正确的数据。 - Lars Kotthoff

0

我看不到任何格式错误。

我的第一个数据是:

{
    "name": "items",
    "children": [
        {
            "id": "33447",
            "anzahl": "13",
            "color": "#ff8080",
            "keytable": "CCR",
            "bordercolor": "#B25959"
        },
        {
            "id": "33455",
            "anzahl": "4",
            "color": "#80ff80",
            "keytable": "COMMODITY",
            "bordercolor": "#59B259"
        },
        {
            "id": "2",
            "anzahl": "37",
            "color": "#80ffff",
            "keytable": "COUNTRY",
            "bordercolor": "#59B2B2"
        },
        {
            "id": "33465",
            "anzahl": "3",
            "color": "#ff0000",
            "keytable": "FUNDS",
            "bordercolor": "#B20000"
        },
        {
            "id": "67400",
            "anzahl": "22",
            "color": "#ffff00",
            "keytable": "GOVERNMENT",
            "bordercolor": "#B2B200"
        },
        {
            "id": "29",
            "anzahl": "183",
            "color": "#008080",
            "keytable": "INDEX",
            "bordercolor": "#005959"
        },
        {
            "id": "33449",
            "anzahl": "1",
            "color": "#8080ff",
            "keytable": "INTER",
            "bordercolor": "#5959B2"
        },
        {
            "id": "67422",
            "anzahl": "8",
            "color": "#800000",
            "keytable": "PARTY",
            "bordercolor": "#590000"
        },
        {
            "id": "33358",
            "anzahl": "4",
            "color": "#8000ff",
            "keytable": "PM",
            "bordercolor": "#5900B2"
        },
        {
            "id": "30",
            "anzahl": "1920",
            "color": "#f90631",
            "keytable": "SHARE",
            "bordercolor": "#AE0422"
        },
        {
            "id": "81",
            "anzahl": "70960",
            "color": "#11b1ee",
            "keytable": "WARRANT",
            "bordercolor": "#0B7BA6"
        },
        {
            "id": "1",
            "anzahl": "1",
            "color": "#004000",
            "keytable": "WORLD",
            "bordercolor": "#002C00"
        }
    ]
}

第二个数据是:

{
    "name": "items",
    "children": [
        {
            "id": "29",
            "anzahl": "12",
            "color": "#008080",
            "keytable": "INDEX",
            "bordercolor": "#005959"
        },
        {
            "id": "55645",
            "anzahl": "165",
            "color": "#11b1ee",
            "keytable": "WARRANT",
            "bordercolor": "#0B7BA6"
        }
    ]
}

在控制台中看起来不同...但为什么:

数据1:

Object {name: "items", children: Array[12]}
area: 333000
children: Array[12]
depth: 0
dx: 740
dy: 450
name: "items"
value: 39.15699051089851
x: 0
y: 0
__proto__: Object

数据2:

Object {name: "items", children: Array[2]}
children: Array[2]
name: "items"
__proto__: Object

但是孩子们也在那里...

更新:

我认为在data2上出现的错误“Uncaught TypeError: Cannot read property 'children' of undefined”意味着其中一个数据集丢失了。

在fiddle中,两个数据集都在函数内定义。但在我的实际示例中,数据集被传递给函数。像这样:

function buildTreemap(jVals_all, jVals_filtered, doRebuild){

if(doRebuild === false){
    var data1 = jQuery.parseJSON(jVals_all);
}
if(doRebuild === true){
    var data2 = jQuery.parseJSON(jVals_filtered);
}
....
}

那么,如果我发送data2,函数是否会错过data1的子项,因为data1是空的?

最终,我只想将一个对象传递给函数...

function buildTreemap(data){}

如果这样更容易解决,那我更倾向于采用这种方式。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接