本文介绍了D3可折叠树不同的节点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 d3.js 中有一个可折叠的树.我的目标是通过类型"属性为节点着色.例如,type = "str" 的节点应填充为红色,而 type = "elem" 的节点应为绿色.我只是无法让它工作.有人可以帮我吗.
这是我的代码:
<html lang="zh-cn"><头><meta charset="utf-8"><title>树可折叠示例</title><风格>.节点{光标:指针;}.node圆{填充:#fff;笔画:钢蓝;描边宽度:2px;}.node 文本 {字体:10px 无衬线;}.关联 {填充:无;中风:#ccc;描边宽度:1.5px;}</风格><身体><!-- 加载 d3.js 库--><script src="http://d3js.org/d3.v3.min.js"></script><脚本>//************** 生成树状图 *****************var margin = {top: 20, right: 120, bottom: 20, left: 120},宽度 = 1000 - margin.right - margin.left,高度 = 500 - margin.top - margin.bottom;变量 i = 0,持续时间 = 750,根;var 树 = d3.layout.tree().size([高度, 宽度]);var 对角线 = d3.svg.diagonal().projection(function(d) { return [d.y, 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(dendrogram02.json",函数(错误,树数据){根 = 树数据 [0];root.x0 = 高度/2;根.y0 = 0;更新(根);});d3.select(self.frameElement).style("height", "500px");功能更新(来源){//计算新的树布局.var 节点 = tree.nodes(root).reverse(),链接 = 树.链接(节点);//标准化固定深度.node.forEach(function(d) { d.y = d.depth * 80; });//更新节点...var node = svg.selectAll("g.node").data(nodes, function(d) { return d.id || (d.id = ++i); });//在父节点之前的位置输入任何新节点.var nodeEnter = node.enter().append("g").attr(类",节点").attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }).on("点击", 点击);nodeEnter.append("圆圈").attr("r", 1e-6).style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });nodeEnter.append("文本").attr("x", function(d) { return d.children || d._children ? 20 : 13; }).attr("dy", function(d) { return d.children || d._children ? "-.8em" : ".35em"; }).attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }).text(function(d) { return d.name; }).style("填充不透明度", 1e-6);//将节点转移到新位置.var nodeUpdate = node.transition().duration(持续时间).attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });nodeUpdate.select("圆").attr("r", 4).style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });nodeUpdate.select("文本").style("填充不透明度", 1);//将退出节点转移到父节点的新位置.var nodeExit = node.exit().transition().duration(持续时间).attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; }).消除();nodeExit.select("圆").attr("r", 1e-6);nodeExit.select("文本").style("填充不透明度", 1e-6);//更新链接...var link = svg.selectAll("path.link").data(links, function(d) { return d.target.id; });//在父级之前的位置输入任何新链接.link.enter().insert("path", "g").attr(类",链接").attr(d",函数(d){var o = {x: source.x0, y: source.y0};返回对角线({源:o,目标:o});});//转换链接到他们的新位置.链接.transition().duration(持续时间).attr("d", 对角线);//将退出节点转移到父节点的新位置.link.exit().transition().duration(持续时间).attr(d",函数(d){var o = {x: source.x, y: source.y};返回对角线({源:o,目标:o});}).消除();//隐藏旧位置以进行转换.节点.forEach(函数(d){d.x0 = d.x;d.y0 = d.y;});}//点击时切换子项.功能点击(d){如果(d.儿童){d._children = d.children;d.children = null;} 别的 {d.children = d._children;d._children = null;}更新);}</html>
以及相应的.json:
[{"名称": "1",类型":来源",孩子们": [{"姓名": "0","type": "dfasdl",孩子们": [{"name": "公司",类型":序列",孩子们": [{"name": "行","类型": "元素",孩子们": [{"name": "id",类型":str"},{"name": "公司名称",类型":str"},{"name": "行业",类型":str"},{"name": "电话公司",类型":str"},{"name": "date_entered",类型":日期时间"}]}]},{"name": "联系人",类型":序列",孩子们": [{"name": "行","类型": "元素",孩子们": [{"name": "id",类型":str"},{"name": "title",类型":str"},{"name": "contactFirstName",类型":str"},{"name": "contactLastName",类型":str"},{"name": "电话美国",类型":str"}]}]},{"name": "员工",类型":序列",孩子们": [{"name": "行","类型": "元素",孩子们": [{"name": "id",类型":str"},{"name": "职位",类型":str"},{"name": "employeeFirstName",类型":str"},{"name": "员工姓氏",类型":str"},{"name": "电话美国",类型":str"}]}]},{"name": "供应商",类型":序列",孩子们": [{"name": "行","类型": "元素",孩子们": [{"name": "id",类型":str"},{"name": "类型",类型":str"},{"name": "供应商名字",类型":str"},{"name": "vendorLastName",类型":str"},{"name": "电话",类型":str"}]}]}]}]}]解决方案
你需要做的就是修改
nodeEnter.append("circle").attr("r", 1e-6).style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
根据您的要求:
nodeEnter.append("circle").attr("r", 1e-6).style(填充",函数(d){if(d.type == "str") 返回 "red";if(d.type == "elem") 返回 "green";});
当您设置两次填充颜色时,您还必须为更新选择执行此操作:
nodeUpdate.select("circle").attr("r", 4).style(填充",函数(d){if(d.type == "str") 返回 "red";if(d.type == "elem") 返回 "green";});
I've got a collapsible tree in d3.js. My goal is to color the nodes by the "type"-attribute. A node with type = "str" should be filled red for example, while the ones with type = "elem" should be colored green. I just can't get it to work. Can someone help me please.
Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tree Collapsible Example</title>
<style>
.node {
cursor: pointer;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 2px;
}
.node text {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// ************** Generate the tree diagram *****************
var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 1000 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, 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 + ")");
// load the external data
d3.json("dendrogram02.json", function(error, treeData) {
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
});
d3.select(self.frameElement).style("height", "500px");
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 * 80; });
// 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", click);
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 ? 20 : 13; })
.attr("dy", function(d) { return d.children || d._children ? "-.8em" : ".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)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
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);
// 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;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
</script>
</body>
</html>
And the corresponding .json:
[
{
"name": "1",
"type": "sources",
"children": [
{
"name": "0",
"type": "dfasdl",
"children": [
{
"name": "companies",
"type": "seq",
"children": [
{
"name": "row",
"type": "elem",
"children": [
{
"name": "id",
"type": "str"
},
{
"name": "companyName",
"type": "str"
},
{
"name": "industry",
"type": "str"
},
{
"name": "telephoneCompany",
"type": "str"
},
{
"name": "date_entered",
"type": "datetime"
}
]
}
]
},
{
"name": "contacts",
"type": "seq",
"children": [
{
"name": "row",
"type": "elem",
"children": [
{
"name": "id",
"type": "str"
},
{
"name": "title",
"type": "str"
},
{
"name": "contactFirstName",
"type": "str"
},
{
"name": "contactLastName",
"type": "str"
},
{
"name": "telephoneUS",
"type": "str"
}
]
}
]
},
{
"name": "employees",
"type": "seq",
"children": [
{
"name": "row",
"type": "elem",
"children": [
{
"name": "id",
"type": "str"
},
{
"name": "position",
"type": "str"
},
{
"name": "employeeFirstName",
"type": "str"
},
{
"name": "employeeLastName",
"type": "str"
},
{
"name": "telephoneUS",
"type": "str"
}
]
}
]
},
{
"name": "vendors",
"type": "seq",
"children": [
{
"name": "row",
"type": "elem",
"children": [
{
"name": "id",
"type": "str"
},
{
"name": "type",
"type": "str"
},
{
"name": "vendorFirstName",
"type": "str"
},
{
"name": "vendorLastName",
"type": "str"
},
{
"name": "telephone",
"type": "str"
}
]
}
]
}
]
}
]
}
]
解决方案
All you need to do is modify
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
according to your requirements:
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) {
if(d.type == "str") return "red";
if(d.type == "elem") return "green";
});
As you're setting the fill colour twice, you also have to do this for the update selection:
nodeUpdate.select("circle")
.attr("r", 4)
.style("fill", function(d) {
if(d.type == "str") return "red";
if(d.type == "elem") return "green";
});
这篇关于D3可折叠树不同的节点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!