Connected scatter plot with multiple series

This post describes how to build a connected scatter plot with multiple data series in d3.js. It follows the previous basic connected scatter plot and also describes how to add a legend in d3.js.

Connected scatter section


  • First, you need to understand how to build a basic connected scatterplot.

  • The first tricky step is to reformat the data. Here, we start with a wide or untidy format: each series is stored in a different column. We need to reformat it in a format where each group is a key.

  • Adding the elements is tricky: first we have to enter() the reformatted dataset. Once in a group, we have to enter() the list of x and y coordinates.

  • Last step is to add the labels at the end of each line. Need to enter the data and then use datum() to extract the position of the last data point.
<!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>

<!-- Color Scale -->
<script src=""></script>


// set the dimensions and margins of the graph
var margin = {top: 10, right: 100, bottom: 30, left: 30},
    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
d3.csv("", function(data) {

    // List of groups (here I have one group per column)
    var allGroup = ["valueA", "valueB", "valueC"]

    // Reformat the data: we need an array of arrays of {x, y} tuples
    var dataReady = function(grpName) { // .map allows to do something for each element of the list
      return {
        name: grpName,
        values: {
          return {time: d.time, value: +d[grpName]};
    // I strongly advise to have a look to dataReady with
    // console.log(dataReady)

    // A color scale: one color for each group
    var myColor = d3.scaleOrdinal()

    // Add X axis --> it is a date format
    var x = d3.scaleLinear()
      .range([ 0, width ]);
      .attr("transform", "translate(0," + height + ")")

    // Add Y axis
    var y = d3.scaleLinear()
      .domain( [0,20])
      .range([ height, 0 ]);

    // Add the lines
    var line = d3.line()
      .x(function(d) { return x(+d.time) })
      .y(function(d) { return y(+d.value) })
        .attr("d", function(d){ return line(d.values) } )
        .attr("stroke", function(d){ return myColor( })
        .style("stroke-width", 4)
        .style("fill", "none")

    // Add the points
      // First we need to enter in a group
        .style("fill", function(d){ return myColor( })
      // Second we need to enter in the 'values' part of this group
      .data(function(d){ return d.values })
        .attr("cx", function(d) { return x(d.time) } )
        .attr("cy", function(d) { return y(d.value) } )
        .attr("r", 5)
        .attr("stroke", "white")

    // Add a legend at the end of each line
          .datum(function(d) { return {name:, value: d.values[d.values.length - 1]}; }) // keep only the last value of each time series
          .attr("transform", function(d) { return "translate(" + x(d.value.time) + "," + y(d.value.value) + ")"; }) // Put the text at the position of the last point
          .attr("x", 12) // shift the text a bit more right
          .text(function(d) { return; })
          .style("fill", function(d){ return myColor( })
          .style("font-size", 15)



Related blocks →