Skip to content

Getting started ​

chartjs-chart-histogram adds a histogram chart type to Chart.js. You pass raw values; the plugin bins them and draws one bar per bin, from the bin's lower edge to its upper edge, on a linear axis.

It needs Chart.js 4.0.1 or later. For TypeScript, version 4.7 or later.

Install ​

sh
npm install chart.js chartjs-chart-histogram
sh
pnpm add chart.js chartjs-chart-histogram
sh
yarn add chart.js chartjs-chart-histogram

Register ​

Like every Chart.js chart type, the controller and its element must be registered once:

js
import {Chart, registerables} from 'chart.js';
import {HistogramController, HistogramElement} from 'chartjs-chart-histogram';

Chart.register(...registerables, HistogramController, HistogramElement);

If you register Chart.js components one by one to keep your bundle small, a histogram needs LinearScale. Add Tooltip, Legend and Title if you use them:

js
import {Chart, LinearScale, Tooltip, Legend} from 'chart.js';
import {HistogramController, HistogramElement} from 'chartjs-chart-histogram';

Chart.register(HistogramController, HistogramElement, LinearScale, Tooltip, Legend);

Without the registration, Chart.js throws "histogram" is not a registered controller. when the chart is created.

Density curves need HistogramDensityController, and Chart.js's LineElement and PointElement when you register components one by one. Selecting bins needs the HistogramSelection plugin. Both are left out of your bundle when you do not import them.

Your first histogram ​

js
new Chart(document.getElementById('chart'), {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'Response time (ms)',
        data: [120, 145, 98, 210, 175, 132, 160, 141, 199, 87]
      }
    ]
  }
});

There are no labels to set: the x axis is a linear scale that ends on the first and last bin edges. The bin edges come from the 'auto' rule, which numpy also uses by default. Values that are not finite numbers, such as null, NaN or 'abc', are left out.

ts
// Raw values in, bars out: the bins come from the 'auto' rule.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'Adults', data: heights}]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

Choose the bins ​

Set bins to a count, a rule name or the edges, or set binWidth:

js
{data: values, bins: 20}                  // 20 equal bins
{data: values, bins: 'fd'}                // the Freedman-Diaconis rule
{data: values, binWidth: 5, binStart: 0}  // bins of width 5 from 0
{data: values, bins: [0, 10, 50, 100]}    // these exact edges

Choosing bins explains each rule and when it suits your data. All options are listed in Options.

Use it from a CDN ​

Load Chart.js first, then the plugin. The plugin's UMD build registers the chart type on the global Chart by itself, and its exports are on the global ChartHistogram.

html
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-chart-histogram@1"></script>
<canvas id="chart"></canvas>
<script>
  new Chart(document.getElementById('chart'), {
    type: 'histogram',
    data: {datasets: [{label: 'Values', data: [1, 2, 2, 3, 3, 3, 4, 4, 5]}]}
  });
</script>

Bin without a chart ​

The binning is also available on its own, with no Chart.js and no DOM:

js
import {computeBins} from 'chartjs-chart-histogram/core';

const {edges, counts} = computeBins([1, 2, 2, 3, 3, 3, 4, 4, 5], {bins: 4});
// edges: [1, 2, 3, 4, 5], counts: [1, 2, 3, 3]

See Binning without a chart.

Released under the MIT License.