gpt4 book ai didi

javascript - 基于数据填充和设计 D3 树节点

转载 作者:行者123 更新时间:2023-12-03 05:57:27 25 4
gpt4 key购买 nike

坚持对可折叠树示例进行样式设计,位于

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的情况下填充红色,如果为零则填充绿色。

除此之外,如果有善良的灵魂也可以

  1. 满足第三个条件(即负数)
  2. 指导我如何根据子节点填充祖先节点的颜色

我已经彻底搜索了所有在线资源,因此非常感谢任何想法。

最佳答案

我不确定你真正需要什么,但我认为你向 json 对象添加了一个属性 status ,并根据它的值,你为相应的元素提供了不同的圆圈颜色。

  1. entertain a third condition (i.e. a negative number)

在附加圈子时,您可以设置每个圈子的样式,如下所示:

如果项目的状态大于 1,则将 lines 属性设置为红色,否则如果它等于 0,则设置为绿色,否则(如果它是负数)设置为橙色。

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")
}) ;
  1. direct me how to fill the colors on the ancestor nodes based off of the children nodes

我认为您想要将每个具有子项的圆的 fill 属性设置为与 lines 属性相同的颜色。

因此,在循环创建的圆圈时,您必须检查当前项目是否有子项(如果有),将 fill 属性设置为与 lines 属性相同的颜色,如下所示:

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>

关于javascript - 基于数据填充和设计 D3 树节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39867460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com