Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
D3.js Quick Start Guide

You're reading from   D3.js Quick Start Guide Create amazing, interactive visualizations in the browser with JavaScript

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781789342383
Length 180 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Matthew Huntington Matthew Huntington
Author Profile Icon Matthew Huntington
Matthew Huntington
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started with D3.js 2. Using SVG to Create Images Using Code FREE CHAPTER 3. Building an Interactive Scatter Plot 4. Making a Basic Scatter Plot Interactive 5. Creating a Bar Graph Using a Data File 6. Animating SVG Elements to Create an Interactive Pie Chart 7. Using Physics to Create a Force-Directed Graph 8. Mapping 9. Other Books You May Enjoy

Dragging an element

We want to be able to update the data for a run by dragging the associated circle. To do this, we'll use a behavior, which you can think of as a combination of multiple event handlers. For a drag behavior, there are three callbacks:

  • When the user starts to drag
  • Each time the user moves the cursor before releasing the mouse button
  • When the user releases the mouse button

There are two steps whenever we create a behavior:

  1. Create the behavior
  2. Attach the behavior to one or more elements

Put the following code at the bottom of the render() function declaration:

//put this code at the end of the render function
var drag = function(datum){
    var x = d3.event.x;
    var y = d3.event.y;
    d3.select(this).attr('cx', x);
    d3.select(this).attr('cy', y);
}
var dragBehavior = d3.drag()    
    .on('drag', drag);
d3.selectAll...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime