D3 , what are ticks? and what is happening here

I understand this code (more or less) up until the simulation part in bold.

what is simulation .nodes(data) β€” so I am thinking a simulation is created (not entirely sure what that means) and then .nodes.() takes in data,
data is the array of objects but what does .nodes() do to it ? Then after I guess the function is run for each element

what are ticks? - it seems to be something like whenever the page is updated??


// set the dimensions and margins of the graph
var width = 450
var height = 450

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
    .attr("width", 450)
    .attr("height", 450)

// create dummy data -> just one element per circle
var data = [{ "name": "A" }, { "name": "B" }, { "name": "C" }, { "name": "D" }, { "name": "E" }, { "name": "F" }, { "name": "G" }, { "name": "H" }]

// Initialize the circle: all located at the center of the svg area
var node = svg.append("g")
    .attr("r", 25)
    .attr("cx", width / 2)
    .attr("cy", height / 2)
    .style("fill", "#69b3a2")
    .style("fill-opacity", 0.3)
    .attr("stroke", "#69a2b2")
    .style("stroke-width", 4)

// Features of the forces applied to the nodes:
var simulation = d3.forceSimulation()
    .force("center", d3.forceCenter().x(width / 2).y(height / 2)) // Attraction to the center of the svg area
    .force("charge", d3.forceManyBody().strength(0.5)) // Nodes are attracted one each other of value is > 0
    .force("collide", d3.forceCollide().strength(.01).radius(30).iterations(1)) // Force that avoids circle overlapping

// Apply these forces to the nodes and update their positions.
// Once the force algorithm is happy with positions ('alpha' value is low enough), simulations will stop.
**    .nodes(data)**
**    .on("tick", function(d){**
**      node**
**          .attr("cx", function(d){ return d.x; })**
**          .attr("cy", function(d){ return d.y; })**
**    });**


This is a force layout simulation. The layout is a graph, consisting of nodes and links (edges). This graph consists of only nodes. There are eight nodes (β€˜A’ to β€˜H’ from data). The nodes are displayed as circles in the graph. It might be slightly more meaningful if we use circles instead of node

var circles = svg.append("g")
     . . . 

The purpose of


is to specify, or associate, the nodes in the simulation graph. So we are specifying here that this force simulation graph has 8 nodes (β€˜A’ to β€˜H’). The simulation is carried out in a step-by-step iteration. At each step, a tick event occurs and the corresponding callback function is called. We want to update the location of circles, so we write


   .on("tick", function(){ //no argument necessary
     circles //renamed here from 'node'
        .attr("cx", function(d){ return d.x; })
        .attr("cy", function(d){ return d.y; })

circles is a variable for the β€˜g’ element in html that consists eight β€˜circle’ elements. The code

        .attr("cx", function(d){ return d.x; })
        .attr("cy", function(d){ return d.y; })

will update the respective positions of those 8 eight circles.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.