Horizontal Bar Charts in JavaScript

How to make a D3.js-based hortizontal bar chart in JavaScript.


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

var data = [{
  type: 'bar',
  x: [20, 14, 23],
  y: ['giraffes', 'orangutans', 'monkeys'],
  orientation: 'h'
}];

Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [{\n type: 'bar',\n x: [20, 14, 23],\n y: ['giraffes', 'orangutans', 'monkeys'],\n orientation: 'h'\n}];\n\nPlotly.newPlot('myDiv', data);\n"}">
var trace1 = {
  x: [20, 14, 23],
  y: ['giraffes', 'orangutans', 'monkeys'],
  name: 'SF Zoo',
  orientation: 'h',
  marker: {
    color: 'rgba(55,128,191,0.6)',
    width: 1
  },
  type: 'bar'
};

var trace2 = {
  x: [12, 18, 29],
  y: ['giraffes', 'orangutans', 'monkeys'],
  name: 'LA Zoo',
  orientation: 'h',
  type: 'bar',
  marker: {
    color: 'rgba(255,153,51,0.6)',
    width: 1
  }
};

var data = [trace1, trace2];

var layout = {
  title: {
    text: 'Colored Bar Chart'
  },
  barmode: 'stack'
};

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: [20, 14, 23],\n y: ['giraffes', 'orangutans', 'monkeys'],\n name: 'SF Zoo',\n orientation: 'h',\n marker: {\n color: 'rgba(55,128,191,0.6)',\n width: 1\n },\n type: 'bar'\n};\n\nvar trace2 = {\n x: [12, 18, 29],\n y: ['giraffes', 'orangutans', 'monkeys'],\n name: 'LA Zoo',\n orientation: 'h',\n type: 'bar',\n marker: {\n color: 'rgba(255,153,51,0.6)',\n width: 1\n }\n};\n\nvar data = [trace1, trace2];\n\nvar layout = {\n title: {\n text: 'Colored Bar Chart'\n },\n barmode: 'stack'\n};\n\nPlotly.newPlot('myDiv', data, layout);\n"}">
var xSavings = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
  7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998
];

var xNetworth = [93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999, 141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3];

var ySavings = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'];

var yNetworth = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'];

var trace1 = {
  x: xSavings,
  y: ySavings,
  xaxis: 'x1',
  yaxis: 'y1',
  type: 'bar',
  marker: {
    color: 'rgba(50,171,96,0.6)',
    line: {
      color: 'rgba(50,171,96,1.0)',
      width: 1
    }
  },
  name: 'Household savings, percentage of household disposable income',
  orientation: 'h'
};

var trace2 = {
  x: xNetworth,
  y: yNetworth,
  xaxis: 'x2',
  yaxis: 'y1',
  mode: 'lines+markers',
  line: {
    color: 'rgb(128,0,128)'
  },
  name: 'Household net worth, Million USD/capita'
};

var data = [trace1, trace2];

var layout = {
  title: {
    text: 'Household Savings & Net Worth for Eight OECD Countries'
  },
  xaxis1: {
    range: [0, 20],
    domain: [0, 0.5],
    zeroline: false,
    showline: false,
    showticklabels: true,
    showgrid: true
  },
  xaxis2: {
    range: [25000, 150000],
    domain: [0.5, 1],
    zeroline: false,
    showline: false,
    showticklabels: true,
    showgrid: true,
    side: 'top',
    dtick: 25000
  },
  legend: {
    x: 0.029,
    y: 1.238,
    font: {
      size: 10
    }
  },
  margin: {
    l: 100,
    r: 20,
    t: 200,
    b: 70
  },
  width: 600,
  height: 600,
  paper_bgcolor: 'rgb(248,248,255)',
  plot_bgcolor: 'rgb(248,248,255)',
  annotations: [
    {
      xref: 'paper',
      yref: 'paper',
      x: -0.2,
      y: -0.109,
      text: 'OECD ' + '(2015), Household savings (indicator), ' + 'Household net worth (indicator). doi: ' + '10.1787/cfc6f499-en (Accessed on 05 June 2015)',
      showarrow: false,
      font:{
        family: 'Arial',
        size: 10,
        color: 'rgb(150,150,150)'
      }
    }
  ]
};

for ( var i = 0 ; i < xSavings.length ; i++ ) {
  var result = {
    xref: 'x1',
    yref: 'y1',
    x: xSavings[i]+2.3,
    y: ySavings[i],
    text: xSavings[i] + '%',
    font: {
      family: 'Arial',
      size: 12,
      color: 'rgb(50, 171, 96)'
    },
     showarrow: false,
  };
  var result2 = {
    xref: 'x2',
    yref: 'y1',
    x: xNetworth[i] - 20000,
    y: yNetworth[i],
    text: xNetworth[i] + ' M',
    font: {
      family: 'Arial',
      size: 12,
      color: 'rgb(128, 0, 128)'
    },
     showarrow: false
  };
  layout.annotations.push(result, result2);
}

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 xSavings = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,\n 7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998\n];\n\nvar xNetworth = [93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999, 141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3];\n\nvar ySavings = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'];\n\nvar yNetworth = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'];\n\nvar trace1 = {\n x: xSavings,\n y: ySavings,\n xaxis: 'x1',\n yaxis: 'y1',\n type: 'bar',\n marker: {\n color: 'rgba(50,171,96,0.6)',\n line: {\n color: 'rgba(50,171,96,1.0)',\n width: 1\n }\n },\n name: 'Household savings, percentage of household disposable income',\n orientation: 'h'\n};\n\nvar trace2 = {\n x: xNetworth,\n y: yNetworth,\n xaxis: 'x2',\n yaxis: 'y1',\n mode: 'lines+markers',\n line: {\n color: 'rgb(128,0,128)'\n },\n name: 'Household net worth, Million USD/capita'\n};\n\nvar data = [trace1, trace2];\n\nvar layout = {\n title: {\n text: 'Household Savings & Net Worth for Eight OECD Countries'\n },\n xaxis1: {\n range: [0, 20],\n domain: [0, 0.5],\n zeroline: false,\n showline: false,\n showticklabels: true,\n showgrid: true\n },\n xaxis2: {\n range: [25000, 150000],\n domain: [0.5, 1],\n zeroline: false,\n showline: false,\n showticklabels: true,\n showgrid: true,\n side: 'top',\n dtick: 25000\n },\n legend: {\n x: 0.029,\n y: 1.238,\n font: {\n size: 10\n }\n },\n margin: {\n l: 100,\n r: 20,\n t: 200,\n b: 70\n },\n width: 600,\n height: 600,\n paper_bgcolor: 'rgb(248,248,255)',\n plot_bgcolor: 'rgb(248,248,255)',\n annotations: [\n {\n xref: 'paper',\n yref: 'paper',\n x: -0.2,\n y: -0.109,\n text: 'OECD ' + '(2015), Household savings (indicator), ' + 'Household net worth (indicator). doi: ' + '10.1787/cfc6f499-en (Accessed on 05 June 2015)',\n showarrow: false,\n font:{\n family: 'Arial',\n size: 10,\n color: 'rgb(150,150,150)'\n }\n }\n ]\n};\n\nfor ( var i = 0 ; i < xSavings.length ; i++ ) {\n var result = {\n xref: 'x1',\n yref: 'y1',\n x: xSavings[i]+2.3,\n y: ySavings[i],\n text: xSavings[i] + '%',\n font: {\n family: 'Arial',\n size: 12,\n color: 'rgb(50, 171, 96)'\n },\n showarrow: false,\n };\n var result2 = {\n xref: 'x2',\n yref: 'y1',\n x: xNetworth[i] - 20000,\n y: yNetworth[i],\n text: xNetworth[i] + ' M',\n font: {\n family: 'Arial',\n size: 12,\n color: 'rgb(128, 0, 128)'\n },\n showarrow: false\n };\n layout.annotations.push(result, result2);\n}\n\nPlotly.newPlot('myDiv', data, layout);\n"}">