Skip to content

Commit

Permalink
tuned force layout #108
Browse files Browse the repository at this point in the history
solves the issues of graphs too compact or too wide according to number of links
* dynamic linkDistance based on links count
* dynamic X force strength based on links count
(naive implementation, may be enhanced in #131
  • Loading branch information
mef committed Aug 9, 2017
1 parent 5368535 commit c8232e5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dynamic/view/force.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ function ForceChart() {
, nodeMargin = 45

var textScale = d3.scaleLinear()
.range([1, 3])
.range([1, 3])
, color = d3.scaleOrdinal(d3.schemeCategory10)
, linkDistanceScale = d3.scaleLinear()
.domain([100, 1000])
.range([50, 120])
, xForceScale = d3.scaleLinear()
.domain([100, 1000])
.range([-.01, -.7])

/****************************************
*
Expand Down Expand Up @@ -287,19 +293,15 @@ function ForceChart() {

textScale.domain([data.nodes[data.nodes.length-1].count, data.nodes[0].count])

var linkDistance = d3.scaleLinear()
.domain([data.maxWeight, data.minWeight])
.range([50, 200])

self.weightScale = d3.scaleLog()
.domain(d3.extent(data.links, function (d) { return d.weight }))
.range([.1, 1])

self.simulation = d3.forceSimulation(data.nodes)
.force('link', d3.forceLink(data.links).distance(75).strength(function(d) {return self.weightScale(d.weight)}))
.force('link', d3.forceLink(data.links).distance(linkDistanceScale(data.links.length)).strength(function(d) {return self.weightScale(d.weight)}))
.force('charge', d3.forceManyBody().strength(-200))
.force('center', d3.forceCenter(self.width / 2, self.height / 2))
.force('x', d3.forceX().strength(-.25))
.force('x', d3.forceX().strength(xForceScale(data.links.length)))
.on('tick', ticked)
.on('end', ended)

Expand Down

0 comments on commit c8232e5

Please sign in to comment.