My code is based on protovis sample code for Force-Directed Layouts. I would like to add the ability to dynamically filter links with a slider based on their value. I already have a basic slider working. What I don't know is how to update the开发者_StackOverflow中文版 Network object so that only renders only the links that have a value greater than the value on the slider. Does anyone know how to do this?
The code to create the graph is
var minLinkValue = 2;
var vis = new pv.Panel()
.width(w)
.height(h)
.fillStyle("white")
.event("mousedown", pv.Behavior.pan())
.event("mousewheel", pv.Behavior.zoom());
var force = vis.add(pv.Layout.Force);
force.nodes(grafica.nodes);
force.links(grafica.links.filter(function(d) { return d.value > minLinkValue} ));
force.link.add(pv.Line);
force.node.add(pv.Dot)
.fillStyle(function(d) { return color(d) })
.strokeStyle(function() { return this.fillStyle().darker() })
.lineWidth(1)
.title(function(d) { return d.nodeName })
.visible(function(d) { return d.linkDegree > 0 })
.event("mousedown", pv.Behavior.drag());
vis.render();
I create the slider with html5
<input type="range" min="0" max="20" value="2" step="1" onchange="setAndShowNewValue(this.value)" />
<span id="range">2</span>
And set the new minimum value with
function setAndShowNewValue(newValue)
{
document.getElementById("range").innerHTML=newValue;
minLinkValue = newValue;
}
Thanks, Raul
Ok, the answer was in a thread at Protovis google group
So, what I did is to create the following function and call it after updating the minimum value allowed for a link:
function filterLinks() {
force.links(grafica.links.filter(function(d) { return d.value >
minLinkValue} ));
force.reset();
}
Then I fixed the code in protovis as suggested in the mentioned thread
精彩评论