Mitchell Hillman

Simple chart using D3.js

This post outlines the basics of how to use the d3 javascript library to make a simple chart.


Finished Example

First, import the d3 library into your project. This example will use version 7.6.1 of the d3 library hosted from a cdn.

<script src="" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

SVG dimensions

Create an SVG element and set its size.

<svg id="my-chart"></svg>
const element = document.getElementById("my-chart");

const width = 400;
const height = 200;
const padding = {
  bottom: 50,
  left: 65,
  right: 0,
  top: 10,

const svg = d3
  .attr("width", width + padding.left + padding.right)
  .attr("height", height + + padding.bottom);

Define X and Y Axis

Set the range and domain of the x and y axis of your data using one of the d3 scale types.

const xScale = d3
  // d3.scaleBand() requires an actual Date object to work properly.
  .domain({ date }) => new Date(date)))
  .range([0, width]);

const yScale = d3
  // d3.max function is helpful for determining the maximum value from a data set.
  .domain([0, d3.max({ amount }) => amount))])
  .range([height, 0]);

domain is the total amount of data this chart will display on that axis. range is the physical space in pixels that amount of data will be spread across. Take note that width is the second element of the array for the yScale, but height is the first element for the xScale. Depending on your design those may need to be reversed.

Draw the axis labels

Create a g group that will position all the future graphic elements

const graph = svg
  .attr("id", "graph")
  .attr("transform", `translate(${padding.left}, ${})`);

Use d3.axisLeft and d3.axisRight respectively to make an axis generator based on the scales just created

const axisLeftGenerator = d3

const axisBottomGenerator = d3

Append the left axis to the graph and configure the text and general appearance

const axisLeft = graph
  .attr("id", "axisLeft")

axisLeft.selectAll(".tick text").attr("transform", "translate(-11, 0)");

  .attr("style", "font-size: 12px; text-transform: uppercase")
  .attr("fill", gridColor)
  .attr("width", width)
  .attr("height", 1);

Do the same for the bottom axis.

  .attr("id", "axisBottom")
  .attr("transform", `translate(0, ${height})`)
  .selectAll(".tick text")
  .attr("transform", "translate(0 , 13)");

In this example some styles apply to both x and y axis.

graph.selectAll(".domain").attr("stroke", gridColor);

  .selectAll(".tick text")
  .attr("fill", textColor)
  .attr("style", "font-size: 12px;");

Draw dots for each point

  .attr("fill", blue)
  .attr("id", "dots")
  .attr("r", 5)
  .attr("transform", ({ amount, date }) => {
    const x = xScale(new Date(date)) + xScale.bandwidth() / 2;
    const y = yScale(amount);
    return `translate(${x}, ${y})`;

Draw a line connecting the dots

  .attr("id", "my-path")
  .attr("fill", "none")
  .attr("stroke", blue)
  .attr("stroke-width", 1)
      .x(({ date }) => xScale(new Date(date)) + xScale.bandwidth() / 2)
      .y(({ amount }) => yScale(amount))