Filled Area Plots in JavaScript

How to make a D3.js-based filled area plot in javascript. An area chart displays a solid color between the traces of a graph.


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

var trace1 = {
  x: [1, 2, 3, 4],
  y: [0, 2, 3, 5],
  fill: 'tozeroy',
  type: 'scatter'
};

var trace2 = {
  x: [1, 2, 3, 4],
  y: [3, 5, 1, 7],
  fill: 'tonexty',
  type: 'scatter'
};

var data = [trace1, trace2];

Plotly.newPlot('myDiv', data);
Click to copy
11.522.533.5401234567
trace 1trace 0
var trace1 = {
  x: [1, 2, 3, 4],
  y: [0, 2, 3, 5],
  fill: 'tozeroy',
  type: 'scatter',
  mode: 'none'
};

var trace2 = {
  x: [1, 2, 3, 4],
  y: [3, 5, 1, 7],
  fill: 'tonexty',
  type: 'scatter',
  mode: 'none'
};

var layout = {
  title: {
    text: 'Overlaid Chart Without Boundary Lines'
  }
};

var data = [trace1, trace2];

Plotly.newPlot('myDiv', data, layout);
Click to copy
11.522.533.5401234567
trace 1trace 0Overlaid Chart Without Boundary Lines
var plotDiv = document.getElementById('plot');
var traces = [
	{x: [1,2,3], y: [2,1,4], stackgroup: 'one'},
	{x: [1,2,3], y: [1,1,2], stackgroup: 'one'},
	{x: [1,2,3], y: [3,0,2], stackgroup: 'one'}
];

Plotly.newPlot('myDiv', traces, {title: {text: 'stacked and filled line chart'}});
Click to copy
11.522.5302468
trace 2trace 1trace 0stacked and filled line chart
var plotDiv = document.getElementById('plot');
var traces = [
	{x: [1,2,3], y: [2,1,4], stackgroup: 'one', groupnorm:'percent'},
	{x: [1,2,3], y: [1,1,2], stackgroup: 'one'},
	{x: [1,2,3], y: [3,0,2], stackgroup: 'one'}
];

Plotly.newPlot('myDiv', traces, {title: {text: 'Normalized stacked and filled line chart'}});

Click to copy
11.522.53020406080100
trace 2trace 1trace 0Normalized stacked and filled line chart
var data = [
  {
    x: [0,0.5,1,1.5,2],
    y: [0,1,2,1,0],
    fill: 'toself',
    fillcolor: '#ab63fa',
    hoveron: 'points+fills',
    line: {
      color: '#ab63fa'
    },
    text: "Points + Fills",
    hoverinfo: 'text'
  },
  {
    x: [3,3.5,4,4.5,5],
    y: [0,1,2,1,0],
    fill: 'toself',
    fillcolor: '#e763fa',
    hoveron: 'points',
    line: {
      color: '#e763fa'
    },
    text: "Points only",
    hoverinfo: 'text'
  }]

var layout = {
  title: {
    text: 'Hover on <i>points</i> or <i>fill</i>'
  },
  xaxis: {
    range: [0,5]
  },
  yaxis: {
    range: [0,3]
  }
}

Plotly.newPlot('myDiv', data, layout)
Click to copy
01234500.511.522.53
trace 0trace 1Hover on points or fill