我试图重现此CollapisbleTree示例(http://bl.ocks.org/PBrockmann/0f22818096428b12ea23),但我尝试使用最新的v5而不是v3。我进行了一些更改以更新树API和对角线。当我运行它时,什么都没有显示,控制台日志上也没有错误。有任何想法吗?
http://plnkr.co/edit/BG2LfZDJQiMvFumkHL2Z?p=preview
<script>
//===============================================
function select2DataCollectName(d) {
if (d.children)
d.children.forEach(select2DataCollectName);
else if (d._children)
d._children.forEach(select2DataCollectName);
select2Data.push(d.name);
}
//===============================================
function searchTree(d) {
if (d.children)
d.children.forEach(searchTree);
else if (d._children)
d._children.forEach(searchTree);
var searchFieldValue = eval(searchField);
if (searchFieldValue && searchFieldValue.match(searchText)) {
// Walk parent chain
var ancestors = [];
var parent = d;
while (typeof(parent) !== "undefined") {
ancestors.push(parent);
//console.log(parent);
parent.class = "found";
parent = parent.parent;
}
//console.log(ancestors);
}
}
//===============================================
function clearAll(d) {
d.class = "";
if (d.children)
d.children.forEach(clearAll);
else if (d._children)
d._children.forEach(clearAll);
}
//===============================================
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
//===============================================
function collapseAllNotFound(d) {
if (d.children) {
if (d.class !== "found") {
d._children = d.children;
d._children.forEach(collapseAllNotFound);
d.children = null;
} else
d.children.forEach(collapseAllNotFound);
}
}
//===============================================
function expandAll(d) {
if (d._children) {
d.children = d._children;
d.children.forEach(expandAll);
d._children = null;
} else if (d.children)
d.children.forEach(expandAll);
}
//===============================================
// Toggle children on click.
function toggle(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
clearAll(root);
update(d);
$("#searchName").select2("val", "");
}
//===============================================
$("#searchName").on("select2-selecting", function(e) {
clearAll(root);
expandAll(root);
update(root);
searchField = "d.name";
searchText = e.object.text;
searchTree(root);
root.children.forEach(collapseAllNotFound);
update(root);
})
//===============================================
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
},
width = 960 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.tree()
.size([height, width]);
var diagonal = d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("flare.json", function(error, flare) {
root = flare;
root.x0 = height / 2;
root.y0 = 0;
select2Data = [];
select2DataCollectName(root);
select2DataObject = [];
select2Data.sort(function(a, b) {
if (a > b) return 1; // sort
if (a < b) return -1;
return 0;
})
.filter(function(item, i, ar) {
return ar.indexOf(item) === i;
}) // remove duplicate items
.filter(function(item, i, ar) {
select2DataObject.push({
"id": i,
"text": item
});
});
$("#searchName").select2({
data: select2DataObject,
containerCssClass: "search"
});
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(collapse);
update(root);
});
d3.select(self.frameElement).style("height", "800px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * 180;
});
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) {
return d.id || (d.id = ++i);
});
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + source.y0 + "," + source.x0 + ")";
})
.on("click", toggle);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -10 : 10;
})
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start";
})
.text(function(d) {
return d.name;
})
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) {
if (d.class === "found") {
return "#ff4136"; //red
} else if (d._children) {
return "lightsteelblue";
} else {
return "#fff";
}
})
.style("stroke", function(d) {
if (d.class === "found") {
return "#ff4136"; //red
}
});
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + source.y + "," + source.x + ")";
})
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) {
return d.target.id;
});
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {
x: source.x0,
y: source.y0
};
return diagonal({
source: o,
target: o
});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal)
.style("stroke", function(d) {
if (d.target.class === "found") {
return "#ff4136";
}
});
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {
x: source.x,
y: source.y
};
return diagonal({
source: o,
target: o
});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
</script>
控制台日志中没有错误消息,但没有任何显示