Skip to main content
🌍 Public

Share visualization

Send this visualization to others.

Circles with D3

Public visualization

Viewing a saved revision (read-only)

e879e43f3aeb43e8b7df96386454e845

Circles with D3

3 stars445🌍 Public
CCurran Kelleher
Last edited Aug 4, 2025
Created on Jan 9, 2024

This example shows a set of seven colorful circles scattered across an SVG canvas, each with a distinct size, position, and fill color at 70% opacity. It demonstrates core D3.js v7 concepts including data binding, the General Update Pattern, and method chaining via `select`, `selectAll`, `data`, and `join` to render `circle` elements. The visualization uses static, data-driven coordinates and colors defined in a JavaScript array, rendered through an HTML import map loading D3 from a CDN. The code also showcases the `join` API for creating SVG circles within a container sized to the viewport.

AI-generated description

Creating circles with D3.

Here's an article formatted in Markdown based on the transcript and the provided code implementation:

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.

import { select } 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');
};

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');

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...
];

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

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