Most basic parallel coordinates chart in d3.js

This post describes how to build a very basic parallel coordinates chart with d3.js. You can see many other examples in the dedicated section of the gallery. Learn more about the theory this kind of chart in

Parallel coordinates section


  • The tricky part of parallel coordinate charts is to automate the creation of the Y axis.

  • One axis must be created per variable. The code beside create one scale per column in the .csv input file and store it in a y object.

  • For instance, y["Sepal_width"] is the scale of the variable called Sepal_width.

  • Note: the axis are drawn after the lines: they are thus on top of the chart, making them readable

  • See the parallel coordinate chart section for improvement of this plot.
<!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: 30, right: 10, bottom: 10, left: 0},
  width = 500 - 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 + "," + + ")");

// Parse the Data
d3.csv("", function(data) {

  // Extract the list of dimensions we want to keep in the plot. Here I keep all except the column called Species
  dimensions = d3.keys(data[0]).filter(function(d) { return d != "Species" })

  // For each dimension, I build a linear scale. I store all in a y object
  var y = {}
  for (i in dimensions) {
    name = dimensions[i]
    y[name] = d3.scaleLinear()
      .domain( d3.extent(data, function(d) { return +d[name]; }) )
      .range([height, 0])

  // Build the X scale -> it find the best position for each Y axis
  x = d3.scalePoint()
    .range([0, width])

  // The path function take a row of the csv as input, and return x and y coordinates of the line to draw for this raw.
  function path(d) {
      return d3.line()( { return [x(p), y[p](d[p])]; }));

  // Draw the lines
    .attr("d",  path)
    .style("fill", "none")
    .style("stroke", "#69b3a2")
    .style("opacity", 0.5)

  // Draw the axis:
    // For each dimension of the dataset I add a 'g' element:
    // I translate this element to its right position on the x axis
    .attr("transform", function(d) { return "translate(" + x(d) + ")"; })
    // And I build the axis with the call function
    .each(function(d) {[d])); })
    // Add axis title
      .style("text-anchor", "middle")
      .attr("y", -9)
      .text(function(d) { return d; })
      .style("fill", "black")



Related blocks →