坚持在
找到可折叠树示例的样式D3代码:https://bl.ocks.org/mbostock/4339083
JSON:https://bl.ocks.org/mbostock/4339083 (但不是 size ,使用 number 设置为某个单个数字整数)
到目前为止,我已经修改了index.html:
nodeEnter.append("circle")
.attr("r", 1e-6)
*.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });*
要
.style("stroke", function(d) { return d.status > 0 ? "green" : "red";})
我希望节点在数字数据等于大于1的情况下填充红色,如果为零,我希望为绿色。
除此之外,如果某种灵魂也可以
我已经完全放弃了我的所有在线资源,所以我们非常感谢任何想法。
答案 0 :(得分:1)
我不确定你真正需要什么,但我认为你为你的json对象添加了一个属性status
,根据它的值,你给对应的元素赋予了不同的圆圈颜色。
- 接受第三个条件(即负数)
醇>
在添加您的圈子时,您可以将每个圈子的样式设置如下:
如果项目的状态大于1,则将stroke
proprety设置为红色,否则等于零,而不是绿色,否则(如果是负数),而不是橙色,例如。
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("stroke",
function(d) {
/*return color according to the current data's status value */
/*it is > 1 than red else if < 0 than orange else(if 0)than red */
return d.status >= 1 ?
"red"
:( d.status < 0 ? "orange" : "green")
}) ;
- 指导我如何根据子节点填充祖先节点上的颜色
醇>
我认为您希望将具有子项的每个圈子的fill
属性设置为与stroke
proprety相同的颜色。
因此,在循环创建的圆圈时,您必须检查当前项目是否包含子项,如果是这种情况,请将fill
proprety设置为stroke
proprety的相同颜色,如下所示:
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill",
function(d) {
/*check if children proprety exists for the current if yes
get its stoke color and set fill proprety it this value */
return d._children ? d3.select(this).style('stroke') : "" ;
}) ;
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.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 + ")");
var flare ={
"name": "positive with children",
"status":3,
"children": [
{
"name": "zero with children",
"status":0,
"children": [
{
"name": "positive with children",
"status":1,
"children": [
{"name": "positive with no children", "size": 3938,"status":3},
{"name": "negative with no children", "size": 3812,"status":-1},
{"name": "positive with no children", "size": 6714,"status":1}
]
},
{
"name": "negative with children",
"status":-2,
"children": [
{"name": "positive with no children", "size": 3534,"status":1},
{"name": "positive with no children", "size": 5731,"status":6}
]
}
]
},
{
"name":"postive with children",
"status":1,
"children": [
{"name": "negative with no children", "size": 599,"status":-3},
{"name": "zero with no children", "size": 386,"status":0},
{"name": "positive with no children", "size": 323,"status":3},
{"name": "zero with no children", "size": 307,"status":0}
]
},
{
"name":"negative with children",
"status":-3,
"children": [
{"name": "positive with no children", "size": 335,"status":1},
{"name": "negative with no children", "size": 383,"status":-1},
{"name": "zero with no children", "size": 874,"status":0},
{
"name": "positive with children",
"status":1,
"children": [
{"name": "positive with no children", "size": 3165,"status":1},
{"name": "zero with no children", "size": 2815,"status":0},
{"name": "negative with no children", "size": 3366,"status":-2}
]
}
]
}
]
};
root = flare;
root.x0 = height / 2;
root.y0 = 0;
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", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("stroke", function(d) { return d.status >= 1 ? "red" : ( d.status < 0 ? "orange" : "green") }) ;
//.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) { return d._children ? "lightsteelblue" : "#fff"; });
.style("fill", function(d) { return d._children ? d3.select(this).style('stroke') : "";}) ;
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);
}
.node {
cursor: pointer;
}
.node circle {
fill: #fff;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.0/d3.min.js"></script>