D3v4 force layout transition from clustered layout to one node












0












$begingroup$


I was wondering if I could get some input on how to make my code more concise, since I think in particular with the force simulation sections I have some repetition. And any other input to make my code more readable would be great. I have a mixing of jQuery and D3 but that is just a quick demo for putting some working code together since there is a lot more to the final visualization, and it doesn't actually use jQuery. Thank you!



This code basically creates a force simulation using D3, reads in data (on sharks), and at first the nodes are sized based on the size of the shark and the nodes are clustered based on shark families. If the user clicks on the "Greenland shark" button then only the node with the common_name property "Greenland Shark" would be visible. And if the user clicks on the button "Sharks by family" then all the nodes would appear again clustered by family. Below is my code.



  var margin = {
top: 50,
right: 50,
bottom: 20,
left: 50
};



var width = 1200 - 300 - margin.left - margin.right;

var heightInitial = 700;
var height = heightInitial - margin.top - margin.bottom;


var svg = d3.select("#graph")
.style("width", width + margin.left + margin.right + "px")
.append("svg")
.style("width", width + margin.left + margin.right + "px")
.style("height", height + margin.top + margin.bottom + "px")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom).attr("class", "svg")

var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");


var forceStrength = 0.03;

var colorScale = d3.scaleOrdinal()
.range(['#450303', '#6e0505', '#951f1f', '#d09b9b', '#ad5151', '#e7cdcd',
'#b30000', '#e34a33'
])

var familyXScale = d3.scaleOrdinal()
.range([width * 0.2,
width * 0.4,
width * 0.55,
width * 0.7,
width * 0.8,
width * 0.9
]);

var familyYScale = d3.scaleOrdinal()
.range([height * 0.2,
height * 0.4,
height * 0.55,
height * 0.7,
height * 0.8,
height * 0.9
]);

var yPositionScale = d3.scaleLinear()
.domain([1, 50])
.range([height / 2 - 50, height / 2 + 50])

var center = {
x: (width + margin.left + margin.right) / 2,
y: (height + margin.top + margin.bottom) / 2
};

var radiusScale = d3.scaleSqrt()
.range([0, 1])

d3.queue()
.defer(d3.csv, "./data2.csv")
.await(ready)

function ready(error, nodes) {

var families = d3.map(nodes, function(d) {
return d.family
}).keys();

var sharks = nodes.map(function(d) {
return d.name
});

var indexarray = [...Array(families.length).keys()];

var mapIndex = d3.scaleOrdinal().domain(families).range(indexarray);

colorScale
.domain(families)

familyXScale
.domain(families)

var simulation = d3.forceSimulation();

simulation.force('x', d3.forceX(function(d) {
var i = mapIndex(d.family);
return xposition(i)
}).strength(0.03))
.force('y', d3.forceY(function(d) {
var i = mapIndex(d.family);
return yposition(i)
}).strength((0.03)))
.force('collide', d3.forceCollide(function(d) {
return radiusScale(+d.size)
})).velocityDecay(0.1).alphaDecay(0.001);


var circles = g.selectAll(".sharks")
.data(nodes)
.enter().append("circle")
.attr("class", "sharks")
.attr("r", function(d) {
return radiusScale(+d.size)
})
.attr("fill", function(d) {
return colorScale(d.family)
})
.attr('stroke', '')


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


nodes.forEach(function(d) {
d.x = familyXScale(d.family)
d.y = yPositionScale(sharks.indexOf(d.name))
})


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



function charge(d) {
return -Math.pow(d.radius, 2.0) * forceStrength;
}

$("#greenlandshark").on('click', function() {
greenlandShark();
})

$("#sharksbyfamily").on('click', function() {
sharksByFamilyRev();
})



function sharksByFamilyRev() {

circles = g.selectAll(".sharks").data(nodes);

circles.exit().transition().duration(750)
.attr("r", 0)
.remove();

circles.transition().duration(750)
.attr("fill", function(d) {
return colorScale(d.family)
}).attr("r", function(d) {
return radiusScale(+d.size);
})

circles = circles.enter().append("circle").attr("class", "sharks")
.attr("fill", function(d) {
return colorScale(d.family)
}).attr("r", function(d) {
return radiusScale(+d.size);
})
.attr('stroke', '')
.merge(circles);

simulation.force('x', d3.forceX(function(d) {
var i = mapIndex(d.family);
return xposition(i)
}).strength(0.03))
.force('y', d3.forceY(function(d) {
var i = mapIndex(d.family);
return yposition(i)
}).strength((0.03)))
.force('collide', d3.forceCollide(function(d) {
return radiusScale(+d.size)
})).velocityDecay(0.1).alphaDecay(0.001);

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

simulation.alpha(1).restart();

}

function greenlandShark() {

var newNodes = filterNodes('common_name', 'Greenland shark');

circles = g.selectAll(".sharks").data(newNodes);

circles.exit()
.transition()
.duration(1000)
.attr("r", 0)
.remove();

circles
.attr('r', function(d) {
return radiusScale(+d.size)
})
.attr('fill', function(d) {
return colorScale(d.family)
});

simulation.nodes(newNodes)
.on('tick', ticked);

simulation.force('x', d3.forceX().strength(0.03).x(center.x))
.force('y', d3.forceY(function(d) {
return height / 2
}).strength((0.03)));

simulation.alpha(1).restart();

}

function filterNodes(key, group) {
var newnodes = nodes.filter(function(d) {
return d[key] == group;
});
return newnodes;
}


}


function xposition(i) {
if (i % 6 == 0) {
return width * 0.2
} else if (i % 6 == 1) {
return width * 0.4
} else if (i % 6 == 2) {
return width * 0.55
} else if (i % 6 == 3) {
return width * 0.7
} else if (i % 6 == 4) {
return width * 0.8
} else {
return width * 0.9
}
}

function yposition(i) {
if (i >= 0 && i <= 5) {
return width * 0.2
} else if (i >= 6 && i <= 11) {
return width * 0.4
} else if (i >= 12 && i <= 17) {
return width * 0.55
} else if (i >= 18 && i <= 23) {
return width * 0.7
} else if (i >= 24 && i <= 29) {
return width * 0.8
} else {
return width * 0.9
}
}


https://plnkr.co/edit/pBPJUhEKQIPnB0ammGDd?p=info










share|improve this question









$endgroup$

















    0












    $begingroup$


    I was wondering if I could get some input on how to make my code more concise, since I think in particular with the force simulation sections I have some repetition. And any other input to make my code more readable would be great. I have a mixing of jQuery and D3 but that is just a quick demo for putting some working code together since there is a lot more to the final visualization, and it doesn't actually use jQuery. Thank you!



    This code basically creates a force simulation using D3, reads in data (on sharks), and at first the nodes are sized based on the size of the shark and the nodes are clustered based on shark families. If the user clicks on the "Greenland shark" button then only the node with the common_name property "Greenland Shark" would be visible. And if the user clicks on the button "Sharks by family" then all the nodes would appear again clustered by family. Below is my code.



      var margin = {
    top: 50,
    right: 50,
    bottom: 20,
    left: 50
    };



    var width = 1200 - 300 - margin.left - margin.right;

    var heightInitial = 700;
    var height = heightInitial - margin.top - margin.bottom;


    var svg = d3.select("#graph")
    .style("width", width + margin.left + margin.right + "px")
    .append("svg")
    .style("width", width + margin.left + margin.right + "px")
    .style("height", height + margin.top + margin.bottom + "px")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom).attr("class", "svg")

    var g = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


    var forceStrength = 0.03;

    var colorScale = d3.scaleOrdinal()
    .range(['#450303', '#6e0505', '#951f1f', '#d09b9b', '#ad5151', '#e7cdcd',
    '#b30000', '#e34a33'
    ])

    var familyXScale = d3.scaleOrdinal()
    .range([width * 0.2,
    width * 0.4,
    width * 0.55,
    width * 0.7,
    width * 0.8,
    width * 0.9
    ]);

    var familyYScale = d3.scaleOrdinal()
    .range([height * 0.2,
    height * 0.4,
    height * 0.55,
    height * 0.7,
    height * 0.8,
    height * 0.9
    ]);

    var yPositionScale = d3.scaleLinear()
    .domain([1, 50])
    .range([height / 2 - 50, height / 2 + 50])

    var center = {
    x: (width + margin.left + margin.right) / 2,
    y: (height + margin.top + margin.bottom) / 2
    };

    var radiusScale = d3.scaleSqrt()
    .range([0, 1])

    d3.queue()
    .defer(d3.csv, "./data2.csv")
    .await(ready)

    function ready(error, nodes) {

    var families = d3.map(nodes, function(d) {
    return d.family
    }).keys();

    var sharks = nodes.map(function(d) {
    return d.name
    });

    var indexarray = [...Array(families.length).keys()];

    var mapIndex = d3.scaleOrdinal().domain(families).range(indexarray);

    colorScale
    .domain(families)

    familyXScale
    .domain(families)

    var simulation = d3.forceSimulation();

    simulation.force('x', d3.forceX(function(d) {
    var i = mapIndex(d.family);
    return xposition(i)
    }).strength(0.03))
    .force('y', d3.forceY(function(d) {
    var i = mapIndex(d.family);
    return yposition(i)
    }).strength((0.03)))
    .force('collide', d3.forceCollide(function(d) {
    return radiusScale(+d.size)
    })).velocityDecay(0.1).alphaDecay(0.001);


    var circles = g.selectAll(".sharks")
    .data(nodes)
    .enter().append("circle")
    .attr("class", "sharks")
    .attr("r", function(d) {
    return radiusScale(+d.size)
    })
    .attr("fill", function(d) {
    return colorScale(d.family)
    })
    .attr('stroke', '')


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


    nodes.forEach(function(d) {
    d.x = familyXScale(d.family)
    d.y = yPositionScale(sharks.indexOf(d.name))
    })


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



    function charge(d) {
    return -Math.pow(d.radius, 2.0) * forceStrength;
    }

    $("#greenlandshark").on('click', function() {
    greenlandShark();
    })

    $("#sharksbyfamily").on('click', function() {
    sharksByFamilyRev();
    })



    function sharksByFamilyRev() {

    circles = g.selectAll(".sharks").data(nodes);

    circles.exit().transition().duration(750)
    .attr("r", 0)
    .remove();

    circles.transition().duration(750)
    .attr("fill", function(d) {
    return colorScale(d.family)
    }).attr("r", function(d) {
    return radiusScale(+d.size);
    })

    circles = circles.enter().append("circle").attr("class", "sharks")
    .attr("fill", function(d) {
    return colorScale(d.family)
    }).attr("r", function(d) {
    return radiusScale(+d.size);
    })
    .attr('stroke', '')
    .merge(circles);

    simulation.force('x', d3.forceX(function(d) {
    var i = mapIndex(d.family);
    return xposition(i)
    }).strength(0.03))
    .force('y', d3.forceY(function(d) {
    var i = mapIndex(d.family);
    return yposition(i)
    }).strength((0.03)))
    .force('collide', d3.forceCollide(function(d) {
    return radiusScale(+d.size)
    })).velocityDecay(0.1).alphaDecay(0.001);

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

    simulation.alpha(1).restart();

    }

    function greenlandShark() {

    var newNodes = filterNodes('common_name', 'Greenland shark');

    circles = g.selectAll(".sharks").data(newNodes);

    circles.exit()
    .transition()
    .duration(1000)
    .attr("r", 0)
    .remove();

    circles
    .attr('r', function(d) {
    return radiusScale(+d.size)
    })
    .attr('fill', function(d) {
    return colorScale(d.family)
    });

    simulation.nodes(newNodes)
    .on('tick', ticked);

    simulation.force('x', d3.forceX().strength(0.03).x(center.x))
    .force('y', d3.forceY(function(d) {
    return height / 2
    }).strength((0.03)));

    simulation.alpha(1).restart();

    }

    function filterNodes(key, group) {
    var newnodes = nodes.filter(function(d) {
    return d[key] == group;
    });
    return newnodes;
    }


    }


    function xposition(i) {
    if (i % 6 == 0) {
    return width * 0.2
    } else if (i % 6 == 1) {
    return width * 0.4
    } else if (i % 6 == 2) {
    return width * 0.55
    } else if (i % 6 == 3) {
    return width * 0.7
    } else if (i % 6 == 4) {
    return width * 0.8
    } else {
    return width * 0.9
    }
    }

    function yposition(i) {
    if (i >= 0 && i <= 5) {
    return width * 0.2
    } else if (i >= 6 && i <= 11) {
    return width * 0.4
    } else if (i >= 12 && i <= 17) {
    return width * 0.55
    } else if (i >= 18 && i <= 23) {
    return width * 0.7
    } else if (i >= 24 && i <= 29) {
    return width * 0.8
    } else {
    return width * 0.9
    }
    }


    https://plnkr.co/edit/pBPJUhEKQIPnB0ammGDd?p=info










    share|improve this question









    $endgroup$















      0












      0








      0





      $begingroup$


      I was wondering if I could get some input on how to make my code more concise, since I think in particular with the force simulation sections I have some repetition. And any other input to make my code more readable would be great. I have a mixing of jQuery and D3 but that is just a quick demo for putting some working code together since there is a lot more to the final visualization, and it doesn't actually use jQuery. Thank you!



      This code basically creates a force simulation using D3, reads in data (on sharks), and at first the nodes are sized based on the size of the shark and the nodes are clustered based on shark families. If the user clicks on the "Greenland shark" button then only the node with the common_name property "Greenland Shark" would be visible. And if the user clicks on the button "Sharks by family" then all the nodes would appear again clustered by family. Below is my code.



        var margin = {
      top: 50,
      right: 50,
      bottom: 20,
      left: 50
      };



      var width = 1200 - 300 - margin.left - margin.right;

      var heightInitial = 700;
      var height = heightInitial - margin.top - margin.bottom;


      var svg = d3.select("#graph")
      .style("width", width + margin.left + margin.right + "px")
      .append("svg")
      .style("width", width + margin.left + margin.right + "px")
      .style("height", height + margin.top + margin.bottom + "px")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom).attr("class", "svg")

      var g = svg.append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


      var forceStrength = 0.03;

      var colorScale = d3.scaleOrdinal()
      .range(['#450303', '#6e0505', '#951f1f', '#d09b9b', '#ad5151', '#e7cdcd',
      '#b30000', '#e34a33'
      ])

      var familyXScale = d3.scaleOrdinal()
      .range([width * 0.2,
      width * 0.4,
      width * 0.55,
      width * 0.7,
      width * 0.8,
      width * 0.9
      ]);

      var familyYScale = d3.scaleOrdinal()
      .range([height * 0.2,
      height * 0.4,
      height * 0.55,
      height * 0.7,
      height * 0.8,
      height * 0.9
      ]);

      var yPositionScale = d3.scaleLinear()
      .domain([1, 50])
      .range([height / 2 - 50, height / 2 + 50])

      var center = {
      x: (width + margin.left + margin.right) / 2,
      y: (height + margin.top + margin.bottom) / 2
      };

      var radiusScale = d3.scaleSqrt()
      .range([0, 1])

      d3.queue()
      .defer(d3.csv, "./data2.csv")
      .await(ready)

      function ready(error, nodes) {

      var families = d3.map(nodes, function(d) {
      return d.family
      }).keys();

      var sharks = nodes.map(function(d) {
      return d.name
      });

      var indexarray = [...Array(families.length).keys()];

      var mapIndex = d3.scaleOrdinal().domain(families).range(indexarray);

      colorScale
      .domain(families)

      familyXScale
      .domain(families)

      var simulation = d3.forceSimulation();

      simulation.force('x', d3.forceX(function(d) {
      var i = mapIndex(d.family);
      return xposition(i)
      }).strength(0.03))
      .force('y', d3.forceY(function(d) {
      var i = mapIndex(d.family);
      return yposition(i)
      }).strength((0.03)))
      .force('collide', d3.forceCollide(function(d) {
      return radiusScale(+d.size)
      })).velocityDecay(0.1).alphaDecay(0.001);


      var circles = g.selectAll(".sharks")
      .data(nodes)
      .enter().append("circle")
      .attr("class", "sharks")
      .attr("r", function(d) {
      return radiusScale(+d.size)
      })
      .attr("fill", function(d) {
      return colorScale(d.family)
      })
      .attr('stroke', '')


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


      nodes.forEach(function(d) {
      d.x = familyXScale(d.family)
      d.y = yPositionScale(sharks.indexOf(d.name))
      })


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



      function charge(d) {
      return -Math.pow(d.radius, 2.0) * forceStrength;
      }

      $("#greenlandshark").on('click', function() {
      greenlandShark();
      })

      $("#sharksbyfamily").on('click', function() {
      sharksByFamilyRev();
      })



      function sharksByFamilyRev() {

      circles = g.selectAll(".sharks").data(nodes);

      circles.exit().transition().duration(750)
      .attr("r", 0)
      .remove();

      circles.transition().duration(750)
      .attr("fill", function(d) {
      return colorScale(d.family)
      }).attr("r", function(d) {
      return radiusScale(+d.size);
      })

      circles = circles.enter().append("circle").attr("class", "sharks")
      .attr("fill", function(d) {
      return colorScale(d.family)
      }).attr("r", function(d) {
      return radiusScale(+d.size);
      })
      .attr('stroke', '')
      .merge(circles);

      simulation.force('x', d3.forceX(function(d) {
      var i = mapIndex(d.family);
      return xposition(i)
      }).strength(0.03))
      .force('y', d3.forceY(function(d) {
      var i = mapIndex(d.family);
      return yposition(i)
      }).strength((0.03)))
      .force('collide', d3.forceCollide(function(d) {
      return radiusScale(+d.size)
      })).velocityDecay(0.1).alphaDecay(0.001);

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

      simulation.alpha(1).restart();

      }

      function greenlandShark() {

      var newNodes = filterNodes('common_name', 'Greenland shark');

      circles = g.selectAll(".sharks").data(newNodes);

      circles.exit()
      .transition()
      .duration(1000)
      .attr("r", 0)
      .remove();

      circles
      .attr('r', function(d) {
      return radiusScale(+d.size)
      })
      .attr('fill', function(d) {
      return colorScale(d.family)
      });

      simulation.nodes(newNodes)
      .on('tick', ticked);

      simulation.force('x', d3.forceX().strength(0.03).x(center.x))
      .force('y', d3.forceY(function(d) {
      return height / 2
      }).strength((0.03)));

      simulation.alpha(1).restart();

      }

      function filterNodes(key, group) {
      var newnodes = nodes.filter(function(d) {
      return d[key] == group;
      });
      return newnodes;
      }


      }


      function xposition(i) {
      if (i % 6 == 0) {
      return width * 0.2
      } else if (i % 6 == 1) {
      return width * 0.4
      } else if (i % 6 == 2) {
      return width * 0.55
      } else if (i % 6 == 3) {
      return width * 0.7
      } else if (i % 6 == 4) {
      return width * 0.8
      } else {
      return width * 0.9
      }
      }

      function yposition(i) {
      if (i >= 0 && i <= 5) {
      return width * 0.2
      } else if (i >= 6 && i <= 11) {
      return width * 0.4
      } else if (i >= 12 && i <= 17) {
      return width * 0.55
      } else if (i >= 18 && i <= 23) {
      return width * 0.7
      } else if (i >= 24 && i <= 29) {
      return width * 0.8
      } else {
      return width * 0.9
      }
      }


      https://plnkr.co/edit/pBPJUhEKQIPnB0ammGDd?p=info










      share|improve this question









      $endgroup$




      I was wondering if I could get some input on how to make my code more concise, since I think in particular with the force simulation sections I have some repetition. And any other input to make my code more readable would be great. I have a mixing of jQuery and D3 but that is just a quick demo for putting some working code together since there is a lot more to the final visualization, and it doesn't actually use jQuery. Thank you!



      This code basically creates a force simulation using D3, reads in data (on sharks), and at first the nodes are sized based on the size of the shark and the nodes are clustered based on shark families. If the user clicks on the "Greenland shark" button then only the node with the common_name property "Greenland Shark" would be visible. And if the user clicks on the button "Sharks by family" then all the nodes would appear again clustered by family. Below is my code.



        var margin = {
      top: 50,
      right: 50,
      bottom: 20,
      left: 50
      };



      var width = 1200 - 300 - margin.left - margin.right;

      var heightInitial = 700;
      var height = heightInitial - margin.top - margin.bottom;


      var svg = d3.select("#graph")
      .style("width", width + margin.left + margin.right + "px")
      .append("svg")
      .style("width", width + margin.left + margin.right + "px")
      .style("height", height + margin.top + margin.bottom + "px")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom).attr("class", "svg")

      var g = svg.append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


      var forceStrength = 0.03;

      var colorScale = d3.scaleOrdinal()
      .range(['#450303', '#6e0505', '#951f1f', '#d09b9b', '#ad5151', '#e7cdcd',
      '#b30000', '#e34a33'
      ])

      var familyXScale = d3.scaleOrdinal()
      .range([width * 0.2,
      width * 0.4,
      width * 0.55,
      width * 0.7,
      width * 0.8,
      width * 0.9
      ]);

      var familyYScale = d3.scaleOrdinal()
      .range([height * 0.2,
      height * 0.4,
      height * 0.55,
      height * 0.7,
      height * 0.8,
      height * 0.9
      ]);

      var yPositionScale = d3.scaleLinear()
      .domain([1, 50])
      .range([height / 2 - 50, height / 2 + 50])

      var center = {
      x: (width + margin.left + margin.right) / 2,
      y: (height + margin.top + margin.bottom) / 2
      };

      var radiusScale = d3.scaleSqrt()
      .range([0, 1])

      d3.queue()
      .defer(d3.csv, "./data2.csv")
      .await(ready)

      function ready(error, nodes) {

      var families = d3.map(nodes, function(d) {
      return d.family
      }).keys();

      var sharks = nodes.map(function(d) {
      return d.name
      });

      var indexarray = [...Array(families.length).keys()];

      var mapIndex = d3.scaleOrdinal().domain(families).range(indexarray);

      colorScale
      .domain(families)

      familyXScale
      .domain(families)

      var simulation = d3.forceSimulation();

      simulation.force('x', d3.forceX(function(d) {
      var i = mapIndex(d.family);
      return xposition(i)
      }).strength(0.03))
      .force('y', d3.forceY(function(d) {
      var i = mapIndex(d.family);
      return yposition(i)
      }).strength((0.03)))
      .force('collide', d3.forceCollide(function(d) {
      return radiusScale(+d.size)
      })).velocityDecay(0.1).alphaDecay(0.001);


      var circles = g.selectAll(".sharks")
      .data(nodes)
      .enter().append("circle")
      .attr("class", "sharks")
      .attr("r", function(d) {
      return radiusScale(+d.size)
      })
      .attr("fill", function(d) {
      return colorScale(d.family)
      })
      .attr('stroke', '')


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


      nodes.forEach(function(d) {
      d.x = familyXScale(d.family)
      d.y = yPositionScale(sharks.indexOf(d.name))
      })


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



      function charge(d) {
      return -Math.pow(d.radius, 2.0) * forceStrength;
      }

      $("#greenlandshark").on('click', function() {
      greenlandShark();
      })

      $("#sharksbyfamily").on('click', function() {
      sharksByFamilyRev();
      })



      function sharksByFamilyRev() {

      circles = g.selectAll(".sharks").data(nodes);

      circles.exit().transition().duration(750)
      .attr("r", 0)
      .remove();

      circles.transition().duration(750)
      .attr("fill", function(d) {
      return colorScale(d.family)
      }).attr("r", function(d) {
      return radiusScale(+d.size);
      })

      circles = circles.enter().append("circle").attr("class", "sharks")
      .attr("fill", function(d) {
      return colorScale(d.family)
      }).attr("r", function(d) {
      return radiusScale(+d.size);
      })
      .attr('stroke', '')
      .merge(circles);

      simulation.force('x', d3.forceX(function(d) {
      var i = mapIndex(d.family);
      return xposition(i)
      }).strength(0.03))
      .force('y', d3.forceY(function(d) {
      var i = mapIndex(d.family);
      return yposition(i)
      }).strength((0.03)))
      .force('collide', d3.forceCollide(function(d) {
      return radiusScale(+d.size)
      })).velocityDecay(0.1).alphaDecay(0.001);

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

      simulation.alpha(1).restart();

      }

      function greenlandShark() {

      var newNodes = filterNodes('common_name', 'Greenland shark');

      circles = g.selectAll(".sharks").data(newNodes);

      circles.exit()
      .transition()
      .duration(1000)
      .attr("r", 0)
      .remove();

      circles
      .attr('r', function(d) {
      return radiusScale(+d.size)
      })
      .attr('fill', function(d) {
      return colorScale(d.family)
      });

      simulation.nodes(newNodes)
      .on('tick', ticked);

      simulation.force('x', d3.forceX().strength(0.03).x(center.x))
      .force('y', d3.forceY(function(d) {
      return height / 2
      }).strength((0.03)));

      simulation.alpha(1).restart();

      }

      function filterNodes(key, group) {
      var newnodes = nodes.filter(function(d) {
      return d[key] == group;
      });
      return newnodes;
      }


      }


      function xposition(i) {
      if (i % 6 == 0) {
      return width * 0.2
      } else if (i % 6 == 1) {
      return width * 0.4
      } else if (i % 6 == 2) {
      return width * 0.55
      } else if (i % 6 == 3) {
      return width * 0.7
      } else if (i % 6 == 4) {
      return width * 0.8
      } else {
      return width * 0.9
      }
      }

      function yposition(i) {
      if (i >= 0 && i <= 5) {
      return width * 0.2
      } else if (i >= 6 && i <= 11) {
      return width * 0.4
      } else if (i >= 12 && i <= 17) {
      return width * 0.55
      } else if (i >= 18 && i <= 23) {
      return width * 0.7
      } else if (i >= 24 && i <= 29) {
      return width * 0.8
      } else {
      return width * 0.9
      }
      }


      https://plnkr.co/edit/pBPJUhEKQIPnB0ammGDd?p=info







      d3.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 12 mins ago









      jhjanickijhjanicki

      233




      233






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211795%2fd3v4-force-layout-transition-from-clustered-layout-to-one-node%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211795%2fd3v4-force-layout-transition-from-clustered-layout-to-one-node%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀