Continuous Error Bars in JavaScript

How to add D3.js-based continuous error bars to a line, scatter, or bar chart.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

var trace1 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 
  y: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], 
  fill: "tozerox", 
  fillcolor: "rgba(0,100,80,0.2)", 
  line: {color: "transparent"}, 
  name: "Fair", 
  showlegend: false, 
  type: "scatter"
};
var trace2 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 
  y: [5.5, 3, 5.5, 8, 6, 3, 8, 5, 6, 5.5, 4.75, 5, 4, 7, 2, 4, 7, 4.4, 2, 4.5], 
  fill: "tozerox", 
  fillcolor: "rgba(0,176,246,0.2)", 
  line: {color: "transparent"}, 
  name: "Premium", 
  showlegend: false, 
  type: "scatter"
};
var trace3 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 
  y: [11, 9, 7, 5, 3, 1, 3, 5, 3, 1, -1, 1, 3, 1, -0.5, 1, 3, 5, 7, 9], 
  fill: "tozerox", 
  fillcolor: "rgba(231,107,243,0.2)", 
  line: {color: "transparent"}, 
  name: "Ideal", 
  showlegend: false, 
  type: "scatter"
};
var trace4 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
  y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
  line: {color: "rgb(0,100,80)"}, 
  mode: "lines", 
  name: "Fair", 
  type: "scatter"
};
var trace5 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
  y: [5, 2.5, 5, 7.5, 5, 2.5, 7.5, 4.5, 5.5, 5], 
  line: {color: "rgb(0,176,246)"}, 
  mode: "lines", 
  name: "Premium", 
  type: "scatter"
};
var trace6 = {
  x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
  y: [10, 8, 6, 4, 2, 0, 2, 4, 2, 0], 
  line: {color: "rgb(231,107,243)"}, 
  mode: "lines", 
  name: "Ideal", 
  type: "scatter"
};
var data = [trace1, trace2, trace3, trace4, trace5, trace6];
var layout = {
  paper_bgcolor: "rgb(255,255,255)", 
  plot_bgcolor: "rgb(229,229,229)", 
  xaxis: {
    gridcolor: "rgb(255,255,255)", 
    range: [1, 10], 
    showgrid: true, 
    showline: false, 
    showticklabels: true, 
    tickcolor: "rgb(127,127,127)", 
    ticks: "outside", 
    zeroline: false
  }, 
  yaxis: {
    gridcolor: "rgb(255,255,255)", 
    showgrid: true, 
    showline: false, 
    showticklabels: true, 
    tickcolor: "rgb(127,127,127)", 
    ticks: "outside", 
    zeroline: false
  }
};
Plotly.newPlot('myDiv', data, layout);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var trace1 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], \n y: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], \n fill: \"tozerox\", \n fillcolor: \"rgba(0,100,80,0.2)\", \n line: {color: \"transparent\"}, \n name: \"Fair\", \n showlegend: false, \n type: \"scatter\"\n};\nvar trace2 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], \n y: [5.5, 3, 5.5, 8, 6, 3, 8, 5, 6, 5.5, 4.75, 5, 4, 7, 2, 4, 7, 4.4, 2, 4.5], \n fill: \"tozerox\", \n fillcolor: \"rgba(0,176,246,0.2)\", \n line: {color: \"transparent\"}, \n name: \"Premium\", \n showlegend: false, \n type: \"scatter\"\n};\nvar trace3 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], \n y: [11, 9, 7, 5, 3, 1, 3, 5, 3, 1, -1, 1, 3, 1, -0.5, 1, 3, 5, 7, 9], \n fill: \"tozerox\", \n fillcolor: \"rgba(231,107,243,0.2)\", \n line: {color: \"transparent\"}, \n name: \"Ideal\", \n showlegend: false, \n type: \"scatter\"\n};\nvar trace4 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], \n y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], \n line: {color: \"rgb(0,100,80)\"}, \n mode: \"lines\", \n name: \"Fair\", \n type: \"scatter\"\n};\nvar trace5 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], \n y: [5, 2.5, 5, 7.5, 5, 2.5, 7.5, 4.5, 5.5, 5], \n line: {color: \"rgb(0,176,246)\"}, \n mode: \"lines\", \n name: \"Premium\", \n type: \"scatter\"\n};\nvar trace6 = {\n x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], \n y: [10, 8, 6, 4, 2, 0, 2, 4, 2, 0], \n line: {color: \"rgb(231,107,243)\"}, \n mode: \"lines\", \n name: \"Ideal\", \n type: \"scatter\"\n};\nvar data = [trace1, trace2, trace3, trace4, trace5, trace6];\nvar layout = {\n paper_bgcolor: \"rgb(255,255,255)\", \n plot_bgcolor: \"rgb(229,229,229)\", \n xaxis: {\n gridcolor: \"rgb(255,255,255)\", \n range: [1, 10], \n showgrid: true, \n showline: false, \n showticklabels: true, \n tickcolor: \"rgb(127,127,127)\", \n ticks: \"outside\", \n zeroline: false\n }, \n yaxis: {\n gridcolor: \"rgb(255,255,255)\", \n showgrid: true, \n showline: false, \n showticklabels: true, \n tickcolor: \"rgb(127,127,127)\", \n ticks: \"outside\", \n zeroline: false\n }\n};\nPlotly.newPlot('myDiv', data, layout);\n"}">
function random_date(start, end, mul)
  {
    return new Date(start.getTime() + mul * (end.getTime() - start.getTime()));
  }

function date_list(y1,m1,d1,y2,m2,d2,count)
  {
    var a =[];
    for(i=0;i<count;i++)
    {
      a.push(random_date(new Date(y1, m1, d1), new Date(y2,m2,d2),i));
    }
      return a;
  }

function random_number(num , mul)
  {
     var value = [ ];
     for(i=0;i<=num;i++)
      {
        var rand = Math.random() * mul;
        value.push(rand);
      }
     return value;
  }

var trace1 = {
  x: date_list(2001,01,01,2001,02,01,50),
  y: random_number(50,20),
  line: {width: 0},
  marker: {color: "444"},
  mode: "lines",
  name: "Lower Bound",
  type: "scatter"
};

var trace2 = {
  x: date_list(2001,01,01,2001,02,01,50),
  y: random_number(50,21),
  fill: "tonexty",
  fillcolor: "rgba(68, 68, 68, 0.3)",
  line: {color: "rgb(31, 119, 180)"},
  mode: "lines",
  name: "Measurement",
  type: "scatter"
};

var trace3 = {
  x: date_list(2001,01,01,2001,02,01,50),
  y: random_number(50,22),
  fill: "tonexty",
  fillcolor: "rgba(68, 68, 68, 0.3)",
  line: {width: 0},
  marker: {color: "444"},
  mode: "lines",
  name: "Upper Bound",
  type: "scatter"
}

var data = [trace1, trace2, trace3];
var layout = {
  showlegend: false,
  title: {
    text: "Continuous, variable value error bars<br>Notice the hover text!"
  },
  yaxis: {
    title: {
      text: "Wind speed (m/s)"
    }
  }
};
Plotly.newPlot('myDiv', data, layout);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"function random_date(start, end, mul)\n {\n return new Date(start.getTime() + mul * (end.getTime() - start.getTime()));\n }\n\nfunction date_list(y1,m1,d1,y2,m2,d2,count)\n {\n var a =[];\n for(i=0;i<count;i++)\n {\n a.push(random_date(new Date(y1, m1, d1), new Date(y2,m2,d2),i));\n }\n return a;\n }\n\nfunction random_number(num , mul)\n {\n var value = [ ];\n for(i=0;i<=num;i++)\n {\n var rand = Math.random() * mul;\n value.push(rand);\n }\n return value;\n }\n\nvar trace1 = {\n x: date_list(2001,01,01,2001,02,01,50),\n y: random_number(50,20),\n line: {width: 0},\n marker: {color: \"444\"},\n mode: \"lines\",\n name: \"Lower Bound\",\n type: \"scatter\"\n};\n\nvar trace2 = {\n x: date_list(2001,01,01,2001,02,01,50),\n y: random_number(50,21),\n fill: \"tonexty\",\n fillcolor: \"rgba(68, 68, 68, 0.3)\",\n line: {color: \"rgb(31, 119, 180)\"},\n mode: \"lines\",\n name: \"Measurement\",\n type: \"scatter\"\n};\n\nvar trace3 = {\n x: date_list(2001,01,01,2001,02,01,50),\n y: random_number(50,22),\n fill: \"tonexty\",\n fillcolor: \"rgba(68, 68, 68, 0.3)\",\n line: {width: 0},\n marker: {color: \"444\"},\n mode: \"lines\",\n name: \"Upper Bound\",\n type: \"scatter\"\n}\n\nvar data = [trace1, trace2, trace3];\nvar layout = {\n showlegend: false,\n title: {\n text: \"Continuous, variable value error bars<br>Notice the hover text!\"\n },\n yaxis: {\n title: {\n text: \"Wind speed (m/s)\"\n }\n }\n};\nPlotly.newPlot('myDiv', data, layout);"}">