gpt4 book ai didi

javascript - 在 d3.js 中更改某行的颜色

转载 作者:行者123 更新时间:2023-11-29 10:58:53 26 4
gpt4 key购买 nike

我已经在 javascript 中使用 d3.js 实现了一个网络,我想根据收到的 json 将链接的颜色从灰色更改为红色。 JSON 显示将连接的源节点和目标节点。节点架构的架构如下:

enter image description here

现在,当收到 JSON 时,我解析数据并使用以下代码附加一条连接源和目标节点到红色的新行:

       svg.selectAll()
.data(data.links)
.enter()
.append("line")
.attr("x1", function(d) { return nodes[source].x; })
.attr("y1", function(d) { return nodes[source].y; })
.attr("x2", function(d) { return nodes[target].x; })
.attr("y2", function(d) { return nodes[target].y; })
.attr("stroke","#b72b34")
.attr("stroke-width", 2)

然后,2 秒后,我使用相同的代码再次将线条的颜色更改为灰色。还有其他更简单的方法吗?

我现在的结果是: enter image description here

如您所见,用户能够看到过去添加的行,尽管这不是想要的图像。我只想更改现有链接的颜色(如模式 1 所示)。

提前谢谢你。

我的代码:

var nodes = [
{ x: width/2, y: height/4, id: 0, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-150, id: 1, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-150, id: 2, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-50, id: 3, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-50, id: 4, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Inkjet-Printer-icon.png"}
];


var links = [
{ source: 1, target: 2 },
{ source: 1, target: 3 },
{ source: 1, target: 4 },
{ source: 1, target: 0 },
{ source: 2, target: 3 },
{ source: 2, target: 1 },
{ source: 2, target: 4 },
{ source: 2, target: 0 },
{ source: 3, target: 4 },
{ source: 3, target: 1 },
{ source: 3, target: 2 },
{ source: 3, target: 0 },
{ source: 4, target: 1 },
{ source: 4, target: 2 },
{ source: 4, target: 3 },
{ source: 4, target: 0 },
{ source: 0, target: 1 },
{ source: 0, target: 2 },
{ source: 0, target: 3 },
{ source: 0, target: 4 },
];

svg.selectAll()
.data(links)
.enter()
.append("line")
.attr("x1", function(d) { return nodes[d.source].x; })
.attr("y1", function(d) { return nodes[d.source].y; })
.attr("x2", function(d) { return nodes[d.target].x; })
.attr("y2", function(d) { return nodes[d.target].y; })
.attr("stroke-width", function (d) { return Math.sqrt(d.value); })
.attr("stroke","#f6f6f6")
svg.selectAll()
.data(nodes)
.enter()
.append("image")
.attr("x", function(d) { return d.x - 30/2; })
.attr("y", function(d) { return d.y - 30/2; })
.attr("width", 45)
.attr("height", 45)
.attr("xlink:href",function(d) { return d.url; })

setInterval(function(test){
var url = "http://..."
d3.json(url, function(error, data) {
for (var i = 0; i < data.links.length; i++) {
source = findNode(data.links[i].source);
target = findNode(data.links[i].target);
svg.selectAll()
.data(data.links)
.enter()
.append("line")
.attr("x1", function(d) { return nodes[source].x; })
.attr("y1", function(d) { return nodes[source].y; })
.attr("x2", function(d) { return nodes[target].x; })
.attr("y2", function(d) { return nodes[target].y; })
.attr("stroke","#b72b34")
.attr("stroke-width", function (d) { return Math.sqrt(d.value);
})
;

}
});
}, 5000);

最佳答案

您目前每次更新图表时都在追加新行:

svg.selectAll()
.data(data.links)
.enter()
.append("line")

svg.selectAll() 是一个空选择,与svg.selectAll(null) 相同。因此,在 DOM 中为数据数组中的每个项目创建了一个项目,这些项目曾经更新过。这会在一段时间后减慢速度。

我们可以使用 svg.selectAll("circle") 选择圆圈以更新(或退出/输入旧/新行)但新数据数组将按顺序分配给现有行他们的索引,不是数据对。我们可以使用 .selectAll().data() ( the second argument for .data() ) 为每个元素设置一个键,但这必须是一个字符串。

与其使用键,我建议使用我们可以给每一行一个 id(当最初附加行时)代表它的源和目标:

 .attr("id", function(d) { return "link-"+d.source+"-"+d.target; })

然后,当我们处理 json 数据以更新图形时,我们会选择适当的行。选择后,我们应用样式。使用转换延迟,我们可以确保在两秒后完成转换回正常:

newData.links.forEach(function(d) {
d3.select("#link-"+d.source+"-"+d.target)
.attr("stroke","red")
.transition()
.delay(1500)
.duration(500)
.attr("stroke","#f6f6f6")
})

这部分代码适合您的超时函数:

var width = 600;
var height = 400;

var svg = d3.select("body").append("svg")
.attr("width",width)
.attr("height",height);

var nodes = [
{ x: width/2, y: height/4, id: 0, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-150, id: 1, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-150, id: 2, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-50, id: 3, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-50, id: 4, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Inkjet-Printer-icon.png"}
];


var links = [
{ source: 1, target: 2 },
{ source: 1, target: 3 },
{ source: 1, target: 4 },
{ source: 1, target: 0 },
{ source: 2, target: 3 },
{ source: 2, target: 1 },
{ source: 2, target: 4 },
{ source: 2, target: 0 },
{ source: 3, target: 4 },
{ source: 3, target: 1 },
{ source: 3, target: 2 },
{ source: 3, target: 0 },
{ source: 4, target: 1 },
{ source: 4, target: 2 },
{ source: 4, target: 3 },
{ source: 4, target: 0 },
{ source: 0, target: 1 },
{ source: 0, target: 2 },
{ source: 0, target: 3 },
{ source: 0, target: 4 },
];

svg.selectAll()
.data(links)
.enter()
.append("line")
.attr("x1", function(d) { return nodes[d.source].x; })
.attr("y1", function(d) { return nodes[d.source].y; })
.attr("x2", function(d) { return nodes[d.target].x; })
.attr("y2", function(d) { return nodes[d.target].y; })
.attr("stroke-width", function (d) { return Math.sqrt(d.value); })
.attr("id", function(d) { return "link-"+d.source+"-"+d.target; })
.attr("stroke","#f6f6f6")

svg.selectAll()
.data(nodes)
.enter()
.append("image")
.attr("x", function(d) { return d.x - 45/2; })
.attr("y", function(d) { return d.y - 45/2; })
.attr("width", 45)
.attr("height", 45)
.attr("xlink:href",function(d) { return d.url; })

setInterval(function(){
// randomly links instead of calling external file:
var data = links.filter(function(d) { if (Math.random() < 0.5) return d; })

data.forEach(function(d) {
d3.select("#link-"+d.source+"-"+d.target)
.attr("stroke","red")
.transition()
.delay(1500)
.duration(500)
.attr("stroke","#f6f6f6")
})

}, 3000); // sped up for demonstration
<script src="https://d3js.org/d3.v3.min.js"></script>

为了进行比较,这里提供了一个键作为替代方法提供给 .data()。对于它,我们将目标和源转换为字符串,就像我们对 id 所做的那样,但将其作为 .data() 中的键应用。这允许我们跳过 for each 循环。这是一种更规范的 d3 方法:

var width = 600;
var height = 400;

var svg = d3.select("body").append("svg")
.attr("width",width)
.attr("height",height);

var nodes = [
{ x: width/2, y: height/4, id: 0, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-150, id: 1, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-150, id: 2, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: 60, y: height-50, id: 3, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Computer-icon.png"},
{ x: width-100, y: height-50, id: 4, url: "http://icons.iconarchive.com/icons/icons-land/vista-hardware-devices/128/Inkjet-Printer-icon.png"}
];


var links = [
{ source: 1, target: 2 },
{ source: 1, target: 3 },
{ source: 1, target: 4 },
{ source: 1, target: 0 },
{ source: 2, target: 3 },
{ source: 2, target: 1 },
{ source: 2, target: 4 },
{ source: 2, target: 0 },
{ source: 3, target: 4 },
{ source: 3, target: 1 },
{ source: 3, target: 2 },
{ source: 3, target: 0 },
{ source: 4, target: 1 },
{ source: 4, target: 2 },
{ source: 4, target: 3 },
{ source: 4, target: 0 },
{ source: 0, target: 1 },
{ source: 0, target: 2 },
{ source: 0, target: 3 },
{ source: 0, target: 4 },
];

svg.selectAll()
.data(links, function(d) { return d.source+"-"+d.target })
.enter()
.append("line")
.attr("x1", function(d) { return nodes[d.source].x; })
.attr("y1", function(d) { return nodes[d.source].y; })
.attr("x2", function(d) { return nodes[d.target].x; })
.attr("y2", function(d) { return nodes[d.target].y; })
.attr("stroke-width", function (d) { return Math.sqrt(d.value); })
.attr("stroke","#f6f6f6")

svg.selectAll()
.data(nodes)
.enter()
.append("image")
.attr("x", function(d) { return d.x - 45/2; })
.attr("y", function(d) { return d.y - 45/2; })
.attr("width", 45)
.attr("height", 45)
.attr("xlink:href",function(d) { return d.url; })

setInterval(function(){
// randomly links instead of calling external file:
var data = links.filter(function(d) { if (Math.random() < 0.5) return d; })

d3.selectAll("line")
.data(data, function(d) { return d.source+"-"+d.target; })
.attr("stroke","red")
.transition()
.delay(1500)
.duration(500)
.attr("stroke","#f6f6f6")


}, 3000); // sped up for demonstration
<script src="https://d3js.org/d3.v3.min.js"></script>

关于javascript - 在 d3.js 中更改某行的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269251/

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