Histograms in JavaScript

How to make a D3.js-based histogram in JavaScript. Seven examples of colored, horizontal, and normal histogram bar charts.


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

var x = [];
for (var i = 0; i < 500; i ++) {
	x[i] = Math.random();
}

var trace = {
    x: x,
    type: 'histogram',
  };
var data = [trace];
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 x = [];\nfor (var i = 0; i < 500; i ++) {\n\tx[i] = Math.random();\n}\n\nvar trace = {\n x: x,\n type: 'histogram',\n };\nvar data = [trace];\nPlotly.newPlot('myDiv', data);\n"}">
var y = [];
for (var i = 0; i < 500; i ++) {
	y[i] = Math.random();
}

var data = [
  {
    y: y,
    type: 'histogram',
	marker: {
    color: 'pink',
	},
  }
];
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 y = [];\nfor (var i = 0; i < 500; i ++) {\n\ty[i] = Math.random();\n}\n\nvar data = [\n {\n y: y,\n type: 'histogram',\n\tmarker: {\n color: 'pink',\n\t},\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
var x1 = [];
var x2 = [];
for (var i = 1; i < 500; i++)
{
	k = Math.random();
	x1.push(Math.random() + 1);
	x2.push(Math.random() + 1.1);
}
var trace1 = {
  x: x1,
  type: "histogram",
  opacity: 0.5,
  marker: {
     color: 'green',
  },
};
var trace2 = {
  x: x2,
  type: "histogram",
  opacity: 0.6,
  marker: {
     color: 'red',
  },
};

var data = [trace1, trace2];
var layout = {barmode: "overlay"};
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 x1 = [];\nvar x2 = [];\nfor (var i = 1; i < 500; i++)\n{\n\tk = Math.random();\n\tx1.push(Math.random() + 1);\n\tx2.push(Math.random() + 1.1);\n}\nvar trace1 = {\n x: x1,\n type: \"histogram\",\n opacity: 0.5,\n marker: {\n color: 'green',\n },\n};\nvar trace2 = {\n x: x2,\n type: \"histogram\",\n opacity: 0.6,\n marker: {\n color: 'red',\n },\n};\n\nvar data = [trace1, trace2];\nvar layout = {barmode: \"overlay\"};\nPlotly.newPlot('myDiv', data, layout);\n"}">
var x1 = [];
var x2 = [];
for (var i = 0; i < 500; i ++) {
	x1[i] = Math.random();
	x2[i] = Math.random();
}

var trace1 = {
  x: x1,
  type: "histogram",
};
var trace2 = {
  x: x2,
  type: "histogram",
};
var data = [trace1, trace2];
var layout = {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 x1 = [];\nvar x2 = [];\nfor (var i = 0; i < 500; i ++) {\n\tx1[i] = Math.random();\n\tx2[i] = Math.random();\n}\n\nvar trace1 = {\n x: x1,\n type: \"histogram\",\n};\nvar trace2 = {\n x: x2,\n type: \"histogram\",\n};\nvar data = [trace1, trace2];\nvar layout = {barmode: \"stack\"};\nPlotly.newPlot('myDiv', data, layout);\n"}">
var x1 = [];
var x2 = [];
var y1 = [];
var y2 = [];
for (var i = 1; i < 500; i++)
{
  k = Math.random();
  x1.push(k*5);
  x2.push(k*10);
  y1.push(k);
  y2.push(k*2);
}
var trace1 = {
  x: x1,
  y: y1,
  name: 'control',
  autobinx: false,
  histnorm: "count",
  marker: {
    color: "rgba(255, 100, 102, 0.7)",
     line: {
      color:  "rgba(255, 100, 102, 1)",
      width: 1
    }
  },
  opacity: 0.5,
  type: "histogram",
  xbins: {
    end: 2.8,
    size: 0.06,
    start: .5
  }
};
var trace2 = {
  x: x2,
  y: y2,
  autobinx: false,
  marker: {
          color: "rgba(100, 200, 102, 0.7)",
           line: {
            color:  "rgba(100, 200, 102, 1)",
            width: 1
    }
       },
  name: "experimental",
  opacity: 0.75,
  type: "histogram",
  xbins: {
    end: 4,
    size: 0.06,
    start: -3.2

  }
};
var data = [trace1, trace2];
var layout = {
  bargap: 0.05,
  bargroupgap: 0.2,
  barmode: "overlay",
  title: {
    text: "Sampled Results"
  },
  xaxis: {
      title: {
          text: "Value"
      }
  },
  yaxis: {
      title: {
          text: "Count"
      }
  }
};
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 x1 = [];\nvar x2 = [];\nvar y1 = [];\nvar y2 = [];\nfor (var i = 1; i < 500; i++)\n{\n k = Math.random();\n x1.push(k*5);\n x2.push(k*10);\n y1.push(k);\n y2.push(k*2);\n}\nvar trace1 = {\n x: x1,\n y: y1,\n name: 'control',\n autobinx: false,\n histnorm: \"count\",\n marker: {\n color: \"rgba(255, 100, 102, 0.7)\",\n line: {\n color: \"rgba(255, 100, 102, 1)\",\n width: 1\n }\n },\n opacity: 0.5,\n type: \"histogram\",\n xbins: {\n end: 2.8,\n size: 0.06,\n start: .5\n }\n};\nvar trace2 = {\n x: x2,\n y: y2,\n autobinx: false,\n marker: {\n color: \"rgba(100, 200, 102, 0.7)\",\n line: {\n color: \"rgba(100, 200, 102, 1)\",\n width: 1\n }\n },\n name: \"experimental\",\n opacity: 0.75,\n type: \"histogram\",\n xbins: {\n end: 4,\n size: 0.06,\n start: -3.2\n\n }\n};\nvar data = [trace1, trace2];\nvar layout = {\n bargap: 0.05,\n bargroupgap: 0.2,\n barmode: \"overlay\",\n title: {\n text: \"Sampled Results\"\n },\n xaxis: {\n title: {\n text: \"Value\"\n }\n },\n yaxis: {\n title: {\n text: \"Count\"\n }\n }\n};\nPlotly.newPlot('myDiv', data, layout);\n"}">
var x = [];
for (var i = 0; i < 500; i ++) {
	x[i] = Math.random();
}

var trace = {
    x: x,
    type: 'histogram',
	cumulative: {enabled: true}
  };
var data = [trace];
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 x = [];\nfor (var i = 0; i < 500; i ++) {\n\tx[i] = Math.random();\n}\n\nvar trace = {\n x: x,\n type: 'histogram',\n\tcumulative: {enabled: true}\n };\nvar data = [trace];\nPlotly.newPlot('myDiv', data);\n"}">
var x = [];
for (var i = 0; i < 500; i ++) {
	x[i] = Math.random();
}

var data = [
  {
    x: x,
    type: 'histogram',
	histnorm: 'probability',
	marker: {
        color: 'rgb(255,255,100)',
     },
  }
];
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 x = [];\nfor (var i = 0; i < 500; i ++) {\n\tx[i] = Math.random();\n}\n\nvar data = [\n {\n x: x,\n type: 'histogram',\n\thistnorm: 'probability',\n\tmarker: {\n color: 'rgb(255,255,100)',\n },\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
var x = ["Apples","Apples","Apples","Oranges", "Bananas"]
var y = ["5","10","3","10","5"]

var data = [
  {
    histfunc: "count",
    y: y,
    x: x,
    type: "histogram",
    name: "count"
  },
  {
    histfunc: "sum",
    y: y,
    x: x,
    type: "histogram",
    name: "sum"
  }
]

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 x = [\"Apples\",\"Apples\",\"Apples\",\"Oranges\", \"Bananas\"]\nvar y = [\"5\",\"10\",\"3\",\"10\",\"5\"]\n\nvar data = [\n {\n histfunc: \"count\",\n y: y,\n x: x,\n type: \"histogram\",\n name: \"count\"\n },\n {\n histfunc: \"sum\",\n y: y,\n x: x,\n type: \"histogram\",\n name: \"sum\"\n }\n]\n\nPlotly.newPlot('myDiv', data)\n"}">