Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

d3 force simulation

var width = 600,
  height = 400;

var colorScale = ['orange', 'lightblue', '#B19CD9'];
var xCenter = [100, 300, 500]

var numNodes = 100;
var nodes = d3.range(numNodes).map(function(d, i) {
  return {
    radius: Math.random() * 25,
    category: i % 3
  }
});

var u = d3.select('svg g')
  .selectAll('circle')
  .data(nodes);

var enter = u.enter()
  .append('circle')
  .attr('r', function(d) {
    return d.radius;
  })
  .style('fill', function(d) {
    return colorScale[d.category];
  });

u = enter.merge(u);

u.exit().remove();

var simulation = d3.forceSimulation(nodes)
  .force('charge', d3.forceManyBody().strength(5))
  .force('x', d3.forceX().x(function(d) {
    return xCenter[d.category];
  }))
  .force('collision', d3.forceCollide().radius(function(d) {
    return d.radius;
  }))
  .on('tick', ticked);

function ticked() {
  u.attr('cx', function(d) {
      return d.x;
    })
    .attr('cy', function(d) {
      return d.y;
    })
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript foreach in object 
Javascript :: get datepicker value date 
Javascript :: angular input date pattern validation 
Javascript :: xml http request fetch 
Javascript :: angular create injectable 
Javascript :: csurf in express 
Javascript :: js 1 minute sleep 
Javascript :: how to update state in react 
Javascript :: jquery get name value method 
Javascript :: what does event emitter do in angular 
Javascript :: nodejs send download file from buffer 
Javascript :: TypeError: this.setState is not a function 
Javascript :: setimout 
Javascript :: how to create a component in angular using terminal 
Javascript :: _.escape underscore 
Javascript :: test each jest 
Javascript :: change text based on dropdown selection javascript 
Javascript :: if browser reactjs 
Javascript :: how to click on alret dialog with pupeteer 
Javascript :: hover javascript 
Javascript :: nextjs link image 
Javascript :: copy js object 
Javascript :: reactjs .net pass value to react 
Javascript :: mongodb js insertmany 
Javascript :: spread operator react 
Javascript :: lodash uniqby 
Javascript :: hash_hmac javascript 
Javascript :: appinsights trackException javascript 
Javascript :: how to create instance of class in javascript 
Javascript :: same date to string in javascript minus and days difference 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =