如何通过单击力定向网络中的节点来更新外部条形图
我试图通过单击力导向网络中的节点来更新条形图的内容。目前,我正在尝试将主面板上的“mousemove”事件与“point”事件一起使用,该事件更新变量 activeNode,然后通知我希望访问哪一行。我遇到了麻烦,因为主面板中的点事件不会更新 activeNode 并且它始终设置为其默认值。尝试到处寻找解决方案,但我认为我错过了一些更基本的概念。
这是代码...
var w = document.body.clientWidth,
h = document.body.clientHeight,
colors = pv.Colors.category19(),
activeNode = 0;
var vis = new pv.Panel()
.width(w)
.height(h)
.fillStyle("white")
.event("mousemove", pv.Behavior.point(Infinity));
var force = vis.add(pv.Layout.Force)
.width(w-200)
.nodes(miserables.nodes)
.links(miserables.links);
force.link.add(pv.Line);
force.node.add(pv.Dot)
.def("o",-1)
.size(function(d) (d.linkDegree + 10) * Math.pow(this.scale, -1.5))
.fillStyle(function(d) d.fix ? "brown" : colors(d.group))
.strokeStyle(function() this.fillStyle().darker())
.lineWidth(1)
.title(function(d) this.index)
.event("mousedown", pv.Behavior.drag())
.event("drag", force)
.event("point", function() {activeNode = this.index; return vis;});
vis.add(pv.Label).top(20).left(w/2).text("activeNode = " + activeNode);
vis.add(pv.Bar)
.data(topw[activeNode].splice(0))
.top(function(d) this.index * 30)
.left(w-80)
.width(15)
.height(20)
.anchor("left").add(pv.Label)
.textAlign("right")
.text(function(d) d[0]);
vis.render();
I'm trying to update the contents of a bar chart by clicking on a node in a force directed network. Currently, I'm trying to use the "mousemove" event on the main panel with a "point" event that updates the variable activeNode which then informs which row I hope to access. I'm having trouble with the fact that my point event from the main panel doesn't update activeNode and its always set to its default value. Tried looking everywhere for a fix with this, but I think I'm missing some more fundamental concept.
Here is the code...
var w = document.body.clientWidth,
h = document.body.clientHeight,
colors = pv.Colors.category19(),
activeNode = 0;
var vis = new pv.Panel()
.width(w)
.height(h)
.fillStyle("white")
.event("mousemove", pv.Behavior.point(Infinity));
var force = vis.add(pv.Layout.Force)
.width(w-200)
.nodes(miserables.nodes)
.links(miserables.links);
force.link.add(pv.Line);
force.node.add(pv.Dot)
.def("o",-1)
.size(function(d) (d.linkDegree + 10) * Math.pow(this.scale, -1.5))
.fillStyle(function(d) d.fix ? "brown" : colors(d.group))
.strokeStyle(function() this.fillStyle().darker())
.lineWidth(1)
.title(function(d) this.index)
.event("mousedown", pv.Behavior.drag())
.event("drag", force)
.event("point", function() {activeNode = this.index; return vis;});
vis.add(pv.Label).top(20).left(w/2).text("activeNode = " + activeNode);
vis.add(pv.Bar)
.data(topw[activeNode].splice(0))
.top(function(d) this.index * 30)
.left(w-80)
.width(15)
.height(20)
.anchor("left").add(pv.Label)
.textAlign("right")
.text(function(d) d[0]);
vis.render();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题,但基本的问题是概念性的 - 当您在可视化中声明标记上的属性时,您可以使用值,例如:
或函数,例如:
区别在于第二个版本每次
render()
vis(或vis的相关部分)时都会重新评估。例如,如果您有:这只会在第一次渲染 vis 时进行评估。相反,您需要一个函数:
因此您更正后的代码可能如下所示(我对条形图进行了一些更改,因为我无权访问您引用的
topw
数据):There are a couple of issues here, but the basic one is conceptual - when you're declaring the properties on marks in your visualization, you can either use a value, like:
or a function, like:
The difference is that the second version will be re-evaluated each time you
render()
the vis (or the relevant part of the vis). So for example, where you have:this will only be evaluated the very first time the vis is rendered. Instead, you need a function:
So your corrected code might look like this (I changed the bar chart a bit, since I don't have access to the
topw
data you reference):