Distribution / Beginner

Histogram

Show frequency distribution of continuous data.

Back to collection

Interactive Chart Preview

JAVASCRIPT
Live, interactive exampleChart.js

Chart.js Code Example

JAVASCRIPT
import Chart from 'chart.js/auto';

// Illustrative data used by the live preview.
const chartData = {
  "binHeaders": [
    "Bin Start",
    "Bin End",
    "Frequency"
  ],
  "bins": [
    {
      "start": 0,
      "end": 10
    },
    {
      "start": 10,
      "end": 20
    },
    {
      "start": 20,
      "end": 30
    },
    {
      "start": 30,
      "end": 40
    },
    {
      "start": 40,
      "end": 50
    },
    {
      "start": 50,
      "end": 60
    },
    {
      "start": 60,
      "end": 70
    },
    {
      "start": 70,
      "end": 80
    },
    {
      "start": 80,
      "end": 90
    },
    {
      "start": 90,
      "end": 100
    }
  ],
  "labels": [
    "0-10",
    "10-20",
    "20-30",
    "30-40",
    "40-50",
    "50-60",
    "60-70",
    "70-80",
    "80-90",
    "90-100"
  ],
  "datasets": [
    {
      "label": "Frequency",
      "data": [
        5,
        12,
        25,
        32,
        28,
        18,
        12,
        6,
        2,
        1
      ],
      "backgroundColor": "rgba(99, 102, 241, 0.7)",
      "borderColor": "rgb(99, 102, 241)",
      "borderWidth": 1,
      "barPercentage": 1,
      "categoryPercentage": 1
    }
  ]
};

function createConfig(chartData) {
    let chartType = 'bar';
    let options = {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'bottom',
            },
            tooltip: {
                mode: 'index',
                intersect: false,
            },
        },
    };
    chartType = 'bar';
    options.scales = {
        y: {
            beginAtZero: true,
            title: { display: true, text: chartData.datasets[0].label },
            grid: { color: 'rgba(0, 0, 0, 0.1)' },
        },
        x: {
            title: { display: true, text: (chartData.binHeaders ?? ['Bin Start', 'Bin End']).slice(0, 2).join(' / ') },
            grid: { display: false },
        },
    };
    return { type: chartType, data: structuredClone(chartData), options };
}

const chart = new Chart(document.getElementById('myChart'), createConfig(chartData));

Preview and code follow the same library selection.

Run This Example

Install the package below in a browser project such as Vite. Put the copied code in a client JavaScript module and add this container to the page before the module runs.

npm install chart.js@4.5.1
<div style="height:320px"><canvas id="myChart"></canvas></div>

The example includes its sample data. When removing the chart, destroy or dispose of its instance and disconnect any resize observer.

Reading This Example

The default Chart.js example contains 141 illustrative observations already grouped into ten equal-width bins. The 30-40 bin has the highest frequency, 32, followed by 40-50 with 28. The table contains bin counts, not the original individual scores.

All library tabs use these illustrative values. Rendering styles and library APIs differ, so use the table to compare exact numbers.

About this example & sample data
Sample values used in the chart
Score RangeFrequency
0-105
10-2012
20-3025
30-4032
40-5028
50-6018
60-7012
70-806
80-902
90-1001

READ THE CHART

Understand How Values Are Distributed

A histogram groups numeric observations into intervals and counts how many fall in each interval. It helps you inspect the center, spread, skew, and possible multiple peaks of a distribution.

01

Start With Measurements

Use a numeric variable such as a score, duration, or temperature. Define interval boundaries so each observation belongs to one bin. If you already have bin counts, plot those counts directly instead of binning them again.

02

Choose Bins Deliberately

Very wide bins can hide structure; very narrow bins can make random variation look important. Compare a few sensible widths. With unequal bin widths, use frequency density so bar area, rather than height alone, represents frequency.

03

Read Shape and Spread

Look for where observations concentrate, how far the values extend, and whether one tail is longer. A gap or a second peak can suggest different groups, but the histogram alone does not explain their cause.

04

Distinguish It From a Bar Chart

Histogram bins follow a numeric order and usually touch. Bar charts compare named categories and can be reordered. Use a box plot to compare compact distribution summaries across groups.

Edit Chart Data