js树形图:将新旧属性绑定到SVG元素
d3.js dendrogram : binding old and new properties to the svg element
我对d3的理解目前相当有限,我目前正在摆弄d3 .js的树形图示例。它可以在几个地方找到:例如:
当我实现它时,一切都很好,直到你尝试更新一个属性,比如节点的圆直径。如果我这样做(使用像AngularJS这样的交互式框架来观察参数的变化):节点的大小会发生变化。所以还没有问题。但是,如果我随后单击其中一个节点,则大小将重置为初始化大小,而不是新大小。
当一个节点被单击时,它遵循click函数:
var nodeEnter = node.enter().append("g")
.attr("class", "dendrogramnode2")
.on("click", click);
click函数调用更新函数。
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
然后更新树突图并打开或关闭必要的节点。
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root),
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 "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("x", 10)
.attr("dy", ".35em")
.attr("text-anchor", "start")
//.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length * 8.5) + ")"; })
.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 "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1)
.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length + 50) + ")"; });
// TODO: appropriate transform
var nodeExit = node.exit().transition()
.duration(duration)
//.attr("transform", function(d) { return "diagonal(" + 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;
});
}
这个更新函数因此被称为:
初始化->没问题(例如:圆r = 5)
当一个参数得到更新->没有问题,参数更新。(例如:圆r = 10)
当你点击一个节点->问题->图形接受初始参数。(例如:圆r = 5)
所有这些可能有很大的范围和javascript如何处理数据绑定,但我不知道如何正确地做到这一点。我要么需要能够访问新的属性,而不是旧的。
有没有办法
调整代码以便采用新参数而不是旧参数?
将参数绑定到svg组作为对象(可能不那么有效?)所以我可以手动达到它从任何范围使用d3选择器?
我已经为DOM元素设置了一个属性,这些属性在每个数据中不不同。
function dograph(p){
//paramater object is passed and it's name is "p"
d3.select(elementid).selectAll("svg")[0][0]["p"] = p
//at some point update function gets called as part of an event and p is no longer available.
//someSvgElement
.on("mouseover"){update())}
function update(source) {
var p = d3.select(elementid).selectAll("svg")[0][0]["p"];
//stuff happens
}
}
可能不是完美的解决方案,但它适合我。
相关文章:
- Ember.js/HHandlebars:使用{{view}}助手渲染的视图不绑定属性
- 聚合物 - 在重复模板中获取数据绑定属性值
- 如果指令包含在另一个指令中,我如何在隔离范围内添加双向数据绑定属性
- AngularJS1.2 Directive双向绑定属性未反映到View
- 为什么AngularJS在页面加载时没有自动绑定属性?包括一个非常简单的例子
- 如何在AngularJS指令中绑定属性
- 更改铁列表中现有项的绑定属性的值
- 如何使用 angularjs 中的表达式绑定属性名称
- Angular 指令内部 HTML 重新编译,动态创建绑定属性
- 标识事件中的绑定属性
- AngularJS 指令未正确绑定属性
- 在显示绑定属性之前在 JavaScript 中格式化绑定属性
- 从数据绑定属性迭代键值对
- 剑道绑定属性与字符串和变量
- 如何在一个元素上具有多个数据绑定属性
- 无法识别余烬组件 - 绑定属性渗入控制器
- 挖空添加我自己的数据绑定属性,如 With
- Emberjs如何绑定属性和id
- Ember绑定属性没有更新,尽管控制器属性更新得很好
- 如何在Knockout中选择性地绑定属性