Home > Software engineering >  Clustered bubbles with links joining the nodes
Clustered bubbles with links joining the nodes

Time:10-22

I need to have clustered bubbles with links joining the nodes.

Cluster are being formed at two levels:

  • split horizontally based on the prop, so that there are three diff stacks
  • within the each stack combine nodes of similar type

To achieve the same, i am combining the solution from two sample of observablehq:

  1. To split nodes into horizontally (stacks)
  2. to combine nodes with-in the stack

Following is solution:

jsFiddle link: jsFiddle Solution

const m = 10 // number of groups;
const l = 3
const [width, height] = [1000, 600];
const color = d3.scaleOrdinal(d3.range(m), d3.schemeCategory10);

function renderChart(dataSource) {
  const svg = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

    const pack = () => d3.pack()
        .size([width, height])
        .padding(1)
        (d3.hierarchy(dataSource.nodes)
            .sum(d => d.value));

  const nodes = pack().leaves();

  const simulation = d3.forceSimulation(nodes)
    .force("x", d3.forceX(d => {
      if (d.data.level === 1) {
        return width / 3 - 100; // width/5 - 50 - 400;
      }
      return width / 3 * d.data.level; // width/5 * d.group - 400;
    }).strength(0.95))
    .force("y", d3.forceY(height / 2).strength(0.01))
    .force("center", d3.forceCenter(width / 2, height / 2))
    .force("cluster", forceCluster())
    .force("collide", forceCollide());


  const node = svg.append("g")
    .selectAll("circle")
    .data(nodes)
    .join("circle")
    .attr("cx", d => d.x)
    .attr("cy", d => d.y)
    .attr("fill", d => color(d.data.group));

  node.transition()
    .attrTween("r", d => {
      const i = d3.interpolate(0, d.r);
      return t => d.r = i(t);
    });

  // links
  const links = dataSource.links.map(d => Object.create(d));
  const link = svg.append("g")
    .selectAll("line")
    .data(links)
    .join("line")
    .classed('link', true)
    .style('stroke', '#999')
    .style("stroke-opacity", 0.75);

  function ticked() {
    link
      .attr("x1", function(d) {
        return d.source.x;
      })
      .attr("y1", function(d) {
        return d.source.y;
      })
      .attr("x2", function(d) {
        return d.target.x;
      })
      .attr("y2", function(d) {
        return d.target.y;
      });

    node
      .attr("cx", d => d.x)
      .attr("cy", d => d.y);
  }

  simulation
    .nodes(nodes)
    .on("tick", ticked);

    simulation
        .force("link", d3.forceLink().id(function (d) { return d.data.id; }).strength(0.3))
        .force("link").links(links);
}

d3.json("https://gist.githubusercontent.com/ravengao/a548cc4a7dd38f1afe0fe4b31ba8901b/raw/fd86a4e94e31ca2497ee78e1006dbed798f62e67/sample_data_supp%3E0.4_new.json").then((d) => {
  const dataSource = {
    links: d.links,
    nodes: d.nodes.map(n => {
      n.group = Math.random() * m | 0;
      n.value = -Math.log(Math.random())
      return n;
    })
  };

  dataSource.nodes = ({
    children: Array.from(
      d3.group(
        dataSource.nodes,
        d => d.group
      ),
      ([, children]) => ({
        children
      })
    )
  });

  renderChart(dataSource);
});


function forceCluster() {
  const strength = 0.2;
  let nodes;

  function force(alpha) {
    const centroids = d3.rollup(nodes, centroid, d => d.data.group);
    const l = alpha * strength;
    for (const d of nodes) {
      const {
        x: cx,
        y: cy
      } = centroids.get(d.data.group);
      d.vx -= (d.x - cx) * l;
      d.vy -= (d.y - cy) * l;
    }
  }

  force.initialize = _ => nodes = _;

  return force;
}

function forceCollide() {
  const alpha = 0.4; // fixed for greater rigidity!
  const padding1 = 2; // separation between same-color nodes
  const padding2 = 6; // separation between different-color nodes
  let nodes;
  let maxRadius;

  function force() {
    const quadtree = d3.quadtree(nodes, d => d.x, d => d.y);
    for (const d of nodes) {
      const r = d.r   maxRadius;
      const nx1 = d.x - r,
        ny1 = d.y - r;
      const nx2 = d.x   r,
        ny2 = d.y   r;
      quadtree.visit((q, x1, y1, x2, y2) => {
        if (!q.length)
          do {
            if (q.data !== d) {
              const r = d.r   q.data.r   (d.data.group === q.data.data.group ? padding1 : padding2);
              let x = d.x - q.data.x,
                y = d.y - q.data.y,
                l = Math.hypot(x, y);
              if (l < r) {
                l = (l - r) / l * alpha;
                d.x -= x *= l, d.y -= y *= l;
                q.data.x  = x, q.data.y  = y;
              }
            }
          } while (q = q.next);
        return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
      });
    }
  }

  force.initialize = _ => maxRadius = d3.max(nodes = _, d => d.r)   Math.max(padding1, padding2);

  return force;
}

function centroid(nodes) {
  let x = 0;
  let y = 0;
  let z = 0;
  for (const d of nodes) {
    let k = d.r ** 2;
    x  = d.x * k;
    y  = d.y * k;
    z  = k;
  }
  return {
    x: x / z,
    y: y / z
  };
}

function generateUUID() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d   Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>

Problem: Until the links are added, everything works perfect with both type of grouping/clusters. But as soon as try to add links b/w. nodes, grouping fails.

Try commenting the line no 81, 82 & 83 and grouping works fine. but when enabled, grouping is lost.

Looking forward to help on the same.

Thanks, Manish

CodePudding user response:

You have an expected result: there are competing forces and the end result is a compromise between a force(s) that groups nodes and a force that links nodes. The forces pull nodes in competing directions, the end result is neither optimzed for groups nor for links but some combination of the two.

The simple solution is to remove any strength associated with the link force. This will result in no force applied to the linked nodes, leaving them in place to be positioned by the forces that group them:

 .force("link").links(links).strength(0);

A solution that requires more work to restructure your code but would be more efficient, would be to remove the link force altogether and just add lines that connect points, then update those lines every tick.

The first solution is presented below.

const m = 10 // number of groups;
const l = 3
const [width, height] = [1000, 600];
const color = d3.scaleOrdinal(d3.range(m), d3.schemeCategory10);

function renderChart(dataSource) {
  const svg = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

    const pack = () => d3.pack()
        .size([width, height])
        .padding(1)
        (d3.hierarchy(dataSource.nodes)
            .sum(d => d.value));

  const nodes = pack().leaves();

  const simulation = d3.forceSimulation(nodes)
    .force("x", d3.forceX(d => {
      if (d.data.level === 1) {
        return width / 3 - 100; // width/5 - 50 - 400;
      }
      return width / 3 * d.data.level; // width/5 * d.group - 400;
    }).strength(0.95))
    .force("y", d3.forceY(height / 2).strength(0.01))
    .force("center", d3.forceCenter(width / 2, height / 2))
    .force("cluster", forceCluster())
    .force("collide", forceCollide());


  const node = svg.append("g")
    .selectAll("circle")
    .data(nodes)
    .join("circle")
    .attr("cx", d => d.x)
    .attr("cy", d => d.y)
    .attr("fill", d => color(d.data.group));

  node.transition()
    .attrTween("r", d => {
      const i = d3.interpolate(0, d.r);
      return t => d.r = i(t);
    });

  // links
  const links = dataSource.links.map(d => Object.create(d));
  const link = svg.append("g")
    .selectAll("line")
    .data(links)
    .join("line")
    .classed('link', true)
    .style('stroke', '#999')
    .style("stroke-opacity", 0.75);

  function ticked() {
    link
      .attr("x1", function(d) {
        return d.source.x;
      })
      .attr("y1", function(d) {
        return d.source.y;
      })
      .attr("x2", function(d) {
        return d.target.x;
      })
      .attr("y2", function(d) {
        return d.target.y;
      });

    node
      .attr("cx", d => d.x)
      .attr("cy", d => d.y);
  }

  simulation
    .nodes(nodes)
    .on("tick", ticked);

    simulation
        .force("link", d3.forceLink().id(function (d) { return d.data.id; }).strength(0.3))
        .force("link").links(links).strength(0);
}

d3.json("https://gist.githubusercontent.com/ravengao/a548cc4a7dd38f1afe0fe4b31ba8901b/raw/fd86a4e94e31ca2497ee78e1006dbed798f62e67/sample_data_supp%3E0.4_new.json").then((d) => {
  const dataSource = {
    links: d.links,
    nodes: d.nodes.map(n => {
      n.group = Math.random() * m | 0;
      n.value = -Math.log(Math.random())
      return n;
    })
  };

  dataSource.nodes = ({
    children: Array.from(
      d3.group(
        dataSource.nodes,
        d => d.group
      ),
      ([, children]) => ({
        children
      })
    )
  });

  renderChart(dataSource);
});


function forceCluster() {
  const strength = 0.2;
  let nodes;

  function force(alpha) {
    const centroids = d3.rollup(nodes, centroid, d => d.data.group);
    const l = alpha * strength;
    for (const d of nodes) {
      const {
        x: cx,
        y: cy
      } = centroids.get(d.data.group);
      d.vx -= (d.x - cx) * l;
      d.vy -= (d.y - cy) * l;
    }
  }

  force.initialize = _ => nodes = _;

  return force;
}

function forceCollide() {
  const alpha = 0.4; // fixed for greater rigidity!
  const padding1 = 2; // separation between same-color nodes
  const padding2 = 6; // separation between different-color nodes
  let nodes;
  let maxRadius;

  function force() {
    const quadtree = d3.quadtree(nodes, d => d.x, d => d.y);
    for (const d of nodes) {
      const r = d.r   maxRadius;
      const nx1 = d.x - r,
        ny1 = d.y - r;
      const nx2 = d.x   r,
        ny2 = d.y   r;
      quadtree.visit((q, x1, y1, x2, y2) => {
        if (!q.length)
          do {
            if (q.data !== d) {
              const r = d.r   q.data.r   (d.data.group === q.data.data.group ? padding1 : padding2);
              let x = d.x - q.data.x,
                y = d.y - q.data.y,
                l = Math.hypot(x, y);
              if (l < r) {
                l = (l - r) / l * alpha;
                d.x -= x *= l, d.y -= y *= l;
                q.data.x  = x, q.data.y  = y;
              }
            }
          } while (q = q.next);
        return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
      });
    }
  }

  force.initialize = _ => maxRadius = d3.max(nodes = _, d => d.r)   Math.max(padding1, padding2);

  return force;
}

function centroid(nodes) {
  let x = 0;
  let y = 0;
  let z = 0;
  for (const d of nodes) {
    let k = d.r ** 2;
    x  = d.x * k;
    y  = d.y * k;
    z  = k;
  }
  return {
    x: x / z,
    y: y / z
  };
}

function generateUUID() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d   Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>

To do the second approach, we remove the link force, but we need to replicate one part of it: the force layout's initialization converts each line's source and target properties from an ID to an actual item in the nodes array, we need to do this ourselves:

  // Map for ease:
  const map = new Map(nodes.map(n=>[n.data.id,n]));

  const link = svg.append("g") 
    ...
    .each(function(d) {
      // replace source and target ID with actual nodes:
      d.source = map.get(d.source);
      d.target = map.get(d.target);
    })

Note, we leave the tick function alone as we still need to update the links rendering each tick, but now we don't have the overhead of the force layout calculating the pull exercised by each link on its source/target nodes:

const m = 10 // number of groups;
const l = 3
const [width, height] = [1000, 600];
const color = d3.scaleOrdinal(d3.range(m), d3.schemeCategory10);

function renderChart(dataSource) {
  const svg = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

    const pack = () => d3.pack()
        .size([width, height])
        .padding(1)
        (d3.hierarchy(dataSource.nodes)
            .sum(d => d.value));

  const nodes = pack().leaves();

  const simulation = d3.forceSimulation(nodes)
    .force("x", d3.forceX(d => {
      if (d.data.level === 1) {
        return width / 3 - 100; // width/5 - 50 - 400;
      }
      return width / 3 * d.data.level; // width/5 * d.group - 400;
    }).strength(0.95))
    .force("y", d3.forceY(height / 2).strength(0.01))
    .force("center", d3.forceCenter(width / 2, height / 2))
    .force("cluster", forceCluster())
    .force("collide", forceCollide());


  const node = svg.append("g")
    .selectAll("circle")
    .data(nodes)
    .join("circle")
    .attr("cx", d => d.x)
    .attr("cy", d => d.y)
    .attr("fill", d => color(d.data.group));

  node.transition()
    .attrTween("r", d => {
      const i = d3.interpolate(0, d.r);
      return t => d.r = i(t);
    });
    
  // Map for ease later:
  const map = new Map(nodes.map(n=>[n.data.id,n]));

  // links
  const links = dataSource.links.map(d => Object.create(d));
  const link = svg.append("g") 
    .lower()
    .selectAll("line")
    .data(links)
    .join("line")
    .classed('link', true)
    .style('stroke', '#999')
    .style("stroke-opacity", 0.75)
    .each(function(d) {
      // replace source and target ID with actual nodes:
      d.source = map.get(d.source);
      d.target = map.get(d.target);
    })
   

  function ticked() {
    link
      .attr("x1", function(d) {
        return d.source.x;
      })
      .attr("y1", function(d) {
        return d.source.y;
      })
      .attr("x2", function(d) {
        return d.target.x;
      })
      .attr("y2", function(d) {
        return d.target.y;
      });

    node
      .attr("cx", d => d.x)
      .attr("cy", d => d.y);
  }

  simulation
    .nodes(nodes)
    .on("tick", ticked);

 //   simulation
 //       .force("link", d3.forceLink().id(function (d) { return d.data.id; }).strength(0.3))
 //       .force("link").links(links).strength(0);
}

d3.json("https://gist.githubusercontent.com/ravengao/a548cc4a7dd38f1afe0fe4b31ba8901b/raw/fd86a4e94e31ca2497ee78e1006dbed798f62e67/sample_data_supp%3E0.4_new.json").then((d) => {
  const dataSource = {
    links: d.links,
    nodes: d.nodes.map(n => {
      n.group = Math.random() * m | 0;
      n.value = -Math.log(Math.random())
      return n;
    })
  };

  dataSource.nodes = ({
    children: Array.from(
      d3.group(
        dataSource.nodes,
        d => d.group
      ),
      ([, children]) => ({
        children
      })
    )
  });

  renderChart(dataSource);
});


function forceCluster() {
  const strength = 0.2;
  let nodes;

  function force(alpha) {
    const centroids = d3.rollup(nodes, centroid, d => d.data.group);
    const l = alpha * strength;
    for (const d of nodes) {
      const {
        x: cx,
        y: cy
      } = centroids.get(d.data.group);
      d.vx -= (d.x - cx) * l;
      d.vy -= (d.y - cy) * l;
    }
  }

  force.initialize = _ => nodes = _;

  return force;
}

function forceCollide() {
  const alpha = 0.4; // fixed for greater rigidity!
  const padding1 = 2; // separation between same-color nodes
  const padding2 = 6; // separation between different-color nodes
  let nodes;
  let maxRadius;

  function force() {
    const quadtree = d3.quadtree(nodes, d => d.x, d => d.y);
    for (const d of nodes) {
      const r = d.r   maxRadius;
      const nx1 = d.x - r,
        ny1 = d.y - r;
      const nx2 = d.x   r,
        ny2 = d.y   r;
      quadtree.visit((q, x1, y1, x2, y2) => {
        if (!q.length)
          do {
            if (q.data !== d) {
              const r = d.r   q.data.r   (d.data.group === q.data.data.group ? padding1 : padding2);
              let x = d.x - q.data.x,
                y = d.y - q.data.y,
                l = Math.hypot(x, y);
              if (l < r) {
                l = (l - r) / l * alpha;
                d.x -= x *= l, d.y -= y *= l;
                q.data.x  = x, q.data.y  = y;
              }
            }
          } while (q = q.next);
        return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
      });
    }
  }

  force.initialize = _ => maxRadius = d3.max(nodes = _, d => d.r)   Math.max(padding1, padding2);

  return force;
}

function centroid(nodes) {
  let x = 0;
  let y = 0;
  let z = 0;
  for (const d of nodes) {
    let k = d.r ** 2;
    x  = d.x * k;
    y  = d.y * k;
    z  = k;
  }
  return {
    x: x / z,
    y: y / z
  };
}

function generateUUID() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d   Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>

  • Related