Combine 2 force layouts

41 views
Skip to first unread message

s_mb

unread,
Jan 16, 2012, 6:24:22 AM1/16/12
to d3-js
Hi,

I want to add two diagramms on my page. Each of them should have a
force layout with different parameters (gravity, charge, etc). If I
create two javascript files with two different force layouts, one set
of settings overwrites the other set.

Is there a solution?

Thx
Sebastian

Ian Johnson

unread,
Jan 16, 2012, 4:28:06 PM1/16/12
to d3...@googlegroups.com
This sounds like a javascript scope issue, so one thing you could do is use different variable names when making your force layouts (if you have the same variable name in two different files, only the last one to be loaded will be used). You may want to do some more reading on javascript to get the fundamentals down, otherwise things may seem a lot harder than they are :)
--
Ian Johnson

Mike Bostock

unread,
Jan 16, 2012, 4:35:22 PM1/16/12
to d3...@googlegroups.com
I often wrap code in a self-executing function to avoid these sorts of
collisions. For example, compare this:

<script>(function() {

var foo = 1;
setTimeout(function() {
alert(foo);
}, 2000);

})()</script>
<script>(function() {

var foo = 2;

})()</script>

To this:

<script>

var foo = 1;
setTimeout(function() {
alert(foo);
}, 2000);

</script>
<script>

var foo = 2;

</script>

Mike

Reply all
Reply to author
Forward
0 new messages