I’m creating a data driven display using force graph from D3.
I supply the nodes and links from server data via ajax.
There is a function in D3 Force Layout model which allows you to set the link distance, via a function. I’d like to store this function(s) in a database or have them generated server side at the very least.
Here is a simple example.
force
.gravity(json_data.gravity || 0.15)
.charge(-150 + json_data.nodes.length)
.nodes(json_data.nodes)
.links(json_data.links)
.linkDistance(function (link, index) {
if (index % 2)
return 30;
return 50;
})
.start();
For fields like charge and linkDistance I’d really like the heavy lifting to be done server side.
Whats the “best” way of achieving this? I understand there is something Eval might be able to solve but, most posts say avoid it..
Thoughts?
EDIT:
this is what I am trying to store
"function (link, index) { if (index % 2) return 30; return 50;"
"function (link, index) { if (index % 3 ==1) return 30; if (index % 3==2) return 40; return 50;"
Here’s where I took it.
ServerSide :
public JsonGraphDTO(int Id, UnitRepository repository) {
gravity = 0.15;
linkDistance = "return 40;";
...
Client Side Processing :
var fn = new Function('lnk','index',json_data.linkDistance);
force
.gravity(json_data.gravity || 0.15)
.charge(-150 + json_data.nodes.length)
.nodes(json_data.nodes)
.links(json_data.links)
.linkDistance(fn)
.start();
You want to be able to link to different functions, name them.
and reference them
You can do it as an array
or as an object
You said you want to load them dynamically, so load them via a script tag.
And on the server you would return the functions back with the call back
I used an Array, you can send back an object or just a function if you want.