O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

D3.js Quick Start Guide

Book Description

This book will help you build interactive graphs that are viewable in any web browser using JavaScript, D3.js, and SVG. You will learn how to make a scatter plot, a bar graph, a pie chart, a force directed graph, and a map.

Key Features

  • Takes you through the most common graphs you'll need
  • Add interactivity to your visualizations
  • Easy to follow builds

Book Description

D3.js is a JavaScript library that allows you to create graphs and data visualizations in the browser with HTML, SVG, and CSS. This book will take you from the basics of D3.js, so that you can create your own interactive visualizations, to creating the most common graphs that you will encounter as a developer, scientist, statistician, or data scientist.

The book begins with an overview of SVG, the basis for creating two-dimensional graphics in the browser. Once the reader has a firm understanding of SVG, we will tackle the basics of how to use D3.js to connect data to our SVG elements. We will start with a scatter plot that maps run data to circles on a graph, and expand our scatter plot to make it interactive. You will see how you can easily allow the users of your graph to create, edit, and delete run data by simply dragging and clicking the graph. Next, we will explore creating a bar graph, using external data from a mock API.

After that, we will explore animations and motion with a bar graph, and use various physics-based forces to create a force-directed graph. Finally, we will look at how to use GeoJSON data to create a map.

What you will learn

  • Build a scatter plot
  • Build a bar graph
  • Build a pie chart
  • Build a force-directed graph
  • Build a map
  • Build interactivity into your graphs

Who this book is for

This book is for web developers, interactive news developers, data scientists, and anyone interested in representing data through interactive visualizations on the Web with D3. Some basic knowledge of JavaScript is expected, but no prior experience with data visualization or D3 is required to follow this book.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. D3.js Quick Start Guide
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Started with D3.js
    1. What is SVG?
    2. What makes D3 so special?
    3. This book's approach to learning
    4. A preview of each build
      1. Using SVG to create images using code
      2. Building an interactive scatter plot
      3. Making a basic scatter plot interactive
      4. Creating a bar graph using a data file
      5. Animating SVG elements to create an interactive pie chart
      6. Using physics to create a force-directed graph
      7. Mapping
    5. Setting up
    6. Summary
  7. Using SVG to Create Images Using Code
    1. Base tag
    2. Basic elements
    3. Positioning an element
    4. Styling an element
    5. Important SVG elements
      1. Circle
      2. Line
      3. Rectangle
      4. Ellipse
      5. Polygon
      6. Polyline
      7. Text
      8. Group
      9. Bezier curves
        1. Cubic Bezier curves
        2. Smooth cubic Bezier curves
        3. Quadratic Bezier curve
        4. Smooth quadratic Bezier curve
      10. Drawing a path
        1. Arcs
      11. Documentation
    6. Summary
  8. Building an Interactive Scatter Plot
    1. Adding a link to the D3 library
    2. Adding an<svg>tag and sizing it with D3
    3. Creating some fake data for our app
    4. Adding SVG circles and styling them
    5. Creating a linear scale
    6. Attaching data to visual elements
    7. Use data attached to a visual element to affect its appearance
    8. Creating a time scale
    9. Parsing and formatting times
    10. Setting dynamic domains
    11. Dynamically generating SVG elements
    12. Creating axes
    13. Displaying data in a table
    14. Summary
  9. Making a Basic Scatter Plot Interactive
    1. Creating a click handler
    2. Removing data
    3. Dragging an element
    4. Updating data after a drag
    5. Creating a zoom behavior that scales elements
    6. Updating axes when zooming and panning
    7. Updating click points after a transform
    8. Avoiding redrawing the entire screen during rendering
    9. Hiding elements beyond an axis
    10. Summary
  10. Creating a Bar Graph Using a Data File
    1. Setting up our application
    2. Creating an external file to hold our data
    3. Making an AJAX request
      1. Writing the basic code
      2. Handling file access
    4. Using AJAX data to create SVG elements
    5. Adjusting the height and the width of the bars
    6. Adjusting the horizontal and the vertical placement of the bars
    7. Making the width of the bars dynamic
    8. Changing the color of the bar based on data
    9. Adding axes
    10. Summary
  11. Animating SVG Elements to Create an Interactive Pie Chart
    1. Setting up the application
    2. Create data/configuration variables
    3. Creating an ordinal scale
    4. Creating the color scale to map labels to colors
    5. Setting up the SVG
    6. Adding paths for each pie segment
    7. Generating an arc creating function
    8. Formatting the data for the arc
    9. Adjusting the position of the pie
    10. Making a donut graph
    11. Removing parts of the pie
    12. Summary
  12. Using Physics to Create a Force-Directed Graph
    1. What is a force-directed graph?
    2. How to set up a graph of relationships
      1. Display
      2. Physics
    3. Setting up the HTML
    4. Setting up styling for nodes and links
    5. Setting up the SVG
    6. Adding data for people
    7. Adding data for relationships
    8. Add circles to the SVG
    9. Adding lines to the SVG
    10. Creating a simulation
    11. Specifying how the simulation affects visual elements
    12. Creating forces
    13. Summary
  13. Mapping
    1. Defining GeoJSON
    2. Setting up the HTML
    3. Using a projection
    4. Generating a path using a projection and GeoJSON data
    5. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think