Most basic area chart in d3.js

This post describes how to build a very basic area chart with d3.js. Only one time series is represented, to simplify the code as much as possible. You can see many other examples in the area chart section of the gallery. Learn more about the theory of boxplots in

Area chart section


  • The Html part of the code just creates a div that will be modified by d3 later on.

  • The first part of the javascript code set a svg area. It specify the chart size and its margin. Read more.

  • This graph shows the evolution of the bitcoin price, see this post for more info.

  • The area is built appending a path to the plot, and using the d3.area() helper function. See here to learn more about this kind of function.
<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>


// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 50},
    width = 460 - margin.left - margin.right,
    height = 400 - - margin.bottom;

// append the svg object to the body of the page
var svg ="#my_dataviz")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + + margin.bottom)
          "translate(" + margin.left + "," + + ")");

//Read the data

  // When reading the csv, I must format variables:
    return { date : d3.timeParse("%Y-%m-%d")(, value : d.value }

  // Now I can use this dataset:
  function(data) {

    // Add X axis --> it is a date format
    var x = d3.scaleTime()
      .domain(d3.extent(data, function(d) { return; }))
      .range([ 0, width ]);
      .attr("transform", "translate(0," + height + ")")

    // Add Y axis
    var y = d3.scaleLinear()
      .domain([0, d3.max(data, function(d) { return +d.value; })])
      .range([ height, 0 ]);

    // Add the area
      .attr("fill", "#cce5df")
      .attr("stroke", "#69b3a2")
      .attr("stroke-width", 1.5)
      .attr("d", d3.area()
        .x(function(d) { return x( })
        .y1(function(d) { return y(d.value) })



Related blocks →