javascript - 如何将新数据添加到 D3js 循环打包?
问题描述
我想在圆形包装力模拟中一点一点地添加新的圆圈。基于这个例子,我使用一个现有的数据数组做了一个循环包装力模拟:https ://www.d3-graph-gallery.com/graph/circularpacking_template.html
如何在保持模拟活动的同时在圆形包装中添加新数据(新圆圈)?
let dataset = [{ data: 3997, type: "file_1" },{ data: 39879, type: "file_2" }];
var width = 450;
var height = 370;
var svg = d3
.select("#my_dataviz")
.append("svg")
.attr("width", width)
.attr("height", height);
var max = d3.max(dataset, function (d) {
return d.data;
});
var size = d3.scaleSqrt().domain([0, max]).range([2, 35]);
var node = svg
.append("g")
.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("r", function (d) {
return size(d.data);
});
var simulation = d3
.forceSimulation(node)
.force("charge", d3.forceManyBody().strength(2))
.force("center", d3.forceCenter(width / 2, height / 2))
.force(
"collision",
d3.forceCollide().radius(function (d) {
return size(d.data) + 2;
})
);
simulation.nodes(dataset).on("tick", function (d) {
node
.attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
});
解决方案
推荐阅读
- macos - VsCode包含路径错误无法解决
- base64 - Taurus 用户如何传入 Content-Type 和 base64 值?
- angular - Visual Studio 2019 Angular 模板不起作用?
- asp.net - ASP.NET MVC - 将 JSON 对象返回到视图并“格式化为表格”
- sql - 基于列删除 SQL 中的重复项
- swift - 检测 iOS 暗模式的变化
- powershell - 从 jenkins 管道获取 powershell 脚本的输出值
- twitter - Tweet 对象中的坐标字段
- r - 使用 dplyr 从字符串列中提取特定单词后面的日期
- python - 并非所有参数都在 SQL 语句中使用 - Python、MySQL