Export Image
Export Code
Connected

Fork of Circles with D3

Priyank Singh

Sunday, May 18, 2025: 0 viewsMonday, May 19, 2025: 0 viewsTuesday, May 20, 2025: 0 viewsWednesday, May 21, 2025: 0 viewsThursday, May 22, 2025: 0 viewsFriday, May 23, 2025: 0 viewsSaturday, May 24, 2025: 0 viewsSunday, May 25, 2025: 0 viewsMonday, May 26, 2025: 0 viewsTuesday, May 27, 2025: 0 viewsWednesday, May 28, 2025: 0 viewsThursday, May 29, 2025: 0 viewsFriday, May 30, 2025: 0 viewsSaturday, May 31, 2025: 0 viewsSunday, June 1, 2025: 0 viewsMonday, June 2, 2025: 0 viewsTuesday, June 3, 2025: 0 viewsWednesday, June 4, 2025: 0 viewsThursday, June 5, 2025: 0 viewsFriday, June 6, 2025: 0 viewsSaturday, June 7, 2025: 0 viewsSunday, June 8, 2025: 0 viewsMonday, June 9, 2025: 0 viewsTuesday, June 10, 2025: 0 viewsWednesday, June 11, 2025: 0 viewsThursday, June 12, 2025: 0 viewsFriday, June 13, 2025: 0 viewsSaturday, June 14, 2025: 0 viewsSunday, June 15, 2025: 0 viewsMonday, June 16, 2025: 0 viewsTuesday, June 17, 2025: 0 viewsWednesday, June 18, 2025: 0 viewsThursday, June 19, 2025: 0 viewsFriday, June 20, 2025: 1 viewSaturday, June 21, 2025: 0 viewsSunday, June 22, 2025: 0 viewsMonday, June 23, 2025: 0 viewsTuesday, June 24, 2025: 0 viewsWednesday, June 25, 2025: 0 viewsThursday, June 26, 2025: 0 viewsFriday, June 27, 2025: 0 viewsSaturday, June 28, 2025: 0 viewsSunday, June 29, 2025: 0 viewsMonday, June 30, 2025: 0 viewsTuesday, July 1, 2025: 0 viewsWednesday, July 2, 2025: 0 viewsThursday, July 3, 2025: 0 viewsFriday, July 4, 2025: 0 viewsSaturday, July 5, 2025: 0 viewsSunday, July 6, 2025: 0 viewsMonday, July 7, 2025: 0 viewsTuesday, July 8, 2025: 0 viewsWednesday, July 9, 2025: 0 viewsThursday, July 10, 2025: 0 viewsFriday, July 11, 2025: 0 viewsSaturday, July 12, 2025: 0 viewsSunday, July 13, 2025: 0 viewsMonday, July 14, 2025: 0 viewsTuesday, July 15, 2025: 0 viewsWednesday, July 16, 2025: 0 viewsThursday, July 17, 2025: 0 viewsFriday, July 18, 2025: 0 viewsSaturday, July 19, 2025: 0 viewsSunday, July 20, 2025: 0 viewsMonday, July 21, 2025: 0 viewsTuesday, July 22, 2025: 0 viewsWednesday, July 23, 2025: 0 viewsThursday, July 24, 2025: 0 viewsFriday, July 25, 2025: 0 viewsSaturday, July 26, 2025: 0 viewsSunday, July 27, 2025: 1 viewMonday, July 28, 2025: 0 viewsTuesday, July 29, 2025: 0 viewsWednesday, July 30, 2025: 0 viewsThursday, July 31, 2025: 0 viewsFriday, August 1, 2025: 0 viewsSaturday, August 2, 2025: 0 viewsSunday, August 3, 2025: 0 viewsMonday, August 4, 2025: 0 viewsTuesday, August 5, 2025: 0 viewsWednesday, August 6, 2025: 0 viewsThursday, August 7, 2025: 0 viewsFriday, August 8, 2025: 0 viewsSaturday, August 9, 2025: 0 viewsSunday, August 10, 2025: 0 viewsMonday, August 11, 2025: 0 viewsTuesday, August 12, 2025: 0 viewsWednesday, August 13, 2025: 0 viewsThursday, August 14, 2025: 0 views
21 views in last 90 days
Last edited Apr 22, 2024
Created on Apr 22, 2024
Forked from Circles with D3

Creating circles with D3.

Creating Circles with D3

In this tutorial, we will learn how to create circles using D3.js, a powerful JavaScript library for manipulating documents based on data. We will cover various concepts including the D3 General update pattern, method chaining, and more, all in the context of hot reloading for instant visual feedback.

Setting Up the Environment

First, we need to set up our environment with an index.js file where we will write our D3 code. We will also have a package.json file specifying D3 as a dependency.

{
  "dependencies": {
    "d3": "7.8.5"
  },
  "vizhub": {
    "libraries": {
      "d3": {
        "global": "d3",
        "path": "/dist/d3.min.js"
      }
    }
  }
}

This is the format of package.json, which includes some VizHub-specific configuration to tell VizHub how to pull in the library from a CDN (Content Distribution Network) and which browser global to look for. Besides the vizhub field, this package.json format is compatible with NPM, so you can export the code and run it locally. To lean more on running the code locally, see vite-export-template.

import { select } from 'd3';

Once package.json is there, we can import things from D3!

Selecting DOM Elements with D3

We start by importing the select function from D3, which allows us to select DOM elements. We will use this to create an SVG element within our container.

export const main = (container) => {
  const svg = select(container)
    .selectAll('svg')
    .data([null])
    .join('svg');
};

This pattern allows the code to run multiple times without creating multiple SVG elements.

Setting the Dimensions of the SVG Element

To set the dimensions of our SVG element, we use the attr method. We will set the width and height based on the container's dimensions.

const width = container.clientWidth;
const height = container.clientHeight;

svg
  .attr('width', width)
  .attr('height', height)
  .style('background', '#F0FFF4');

We can use container.clientWidth and container.clientHeight to measure the container DOM element at page load time. This works because of an assumption that the parent DOM element has a defined width and height.

Defining the Data for Our Circles

Next, we define the data that will drive our circles. Each object in our data array represents a circle with its coordinates (x, y), radius (r), and fill color.

const data = [
  { x: 155, y: 386, r: 20, fill: '#0000FF' },
  { x: 340, y: 238, r: 52, fill: '#FF0AAE' },
  // Add more circle data here...
];

This is a "data-driven approach", where we decouple the data from the rendering logic responsible for transforming it on the screen. This is a stepping stone towards loading in data from CSV or JSON files.

Drawing Circles with D3

We use D3's data join pattern to bind our data to the circles we will create. For each data object, we set the circle's attributes (center coordinates, radius, and fill color).

svg
  .selectAll('circle')
  .data(data)
  .join('circle')
  .attr('cx', (d) => d.x)
  .attr('cy', (d) => d.y)
  .attr('r', (d) => d.r)
  .attr('fill', (d) => d.fill)
  .attr('opacity', 0.708); // Optional: Set opacity for overlap effect

This is a typical example of D3's "method chaining" API, wherein the selection is returned from the .attr method, which defines values for an attribute of the DOM elements.

Conclusion

Now you have a basic understanding of how to create and manipulate circles with D3.js. You can experiment with different data and styles to create your own visualizations. Happy coding!

Feel free to modify or add more sections to the article as needed.

Challenge

  • Fork this example
  • Modify it
  • Maybe make it Halloween themed
  • Maybe make it Christmas themed
  • Be creative and have fun!
MIT Licensed