Book Image

Data Visualization with D3 4.x Cookbook - Second Edition

By : Nick Zhu
Book Image

Data Visualization with D3 4.x Cookbook - Second Edition

By: Nick Zhu

Overview of this book

Master D3.js and create amazing visualizations with the Data Visualization with D3 4.x Cookbook. Written by professional data engineer Nick Zhu, this D3.js cookbook features over 65 recipes. ? Solve real-world visualization problems using D3.js practical recipes ? Understand D3 fundamentals ? Includes illustrations, ready-to-go code samples and pre-built chart recipes
Table of Contents (21 chapters)
Data Visualization with D3 4.x Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Setting the link constraint


So far, we have covered some important aspects of the force layout, such as gravity, charge, friction, and velocity. In this recipe, we will discuss another critical functionality: links. As we have mentioned in the introduction section, D3 force simulation supports a scalable simple graph constraint, and in this recipe, we will demonstrate how link constraint can be leveraged in conjunction with other forces.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter11/link-constraint.html .

How to do it...

In this recipe, whenever the user clicks on the mouse, we will generate a force-directed ring of particles constrained by links between nodes. Here is how it is implemented:

<script type="text/javascript"> 
    var w = 1280, h = 800, 
            r = 4.5, nodes = [], links = []; 
 
    var force = d3.forceSimulation() 
                    .velocityDecay...