Basic
Pass raw values; the bins are chosen by the 'auto' rule. These samples show three common shapes of data.
Normal data
Heights of 500 adults: one peak, symmetric.
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;Skewed data
Response times bunch up on the left and have a long tail on the right. Doane's rule adds bins for skewed data.
ts
// Skewed data: response times with a long right tail.
import type {ChartConfiguration} from 'chart.js';
import {responseTimes} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Requests',
data: responseTimes,
// Doane's rule adapts to skewed data.
bins: 'doane',
backgroundColor: 'rgba(255, 159, 64, 0.7)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Response time (ms)'}},
y: {title: {display: true, text: 'Requests'}}
}
}
};
export default config;Two clusters
With bins narrow enough, both peaks show. Too wide, and they merge into one.
ts
// Two clusters: narrow bins show both peaks.
import type {ChartConfiguration} from 'chart.js';
import {waitingTimes} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Eruptions',
data: waitingTimes,
binWidth: 2.5,
backgroundColor: 'rgba(75, 192, 192, 0.7)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Minutes since the previous eruption'}},
y: {title: {display: true, text: 'Eruptions'}}
}
}
};
export default config;Empty bins
A bin with no values has no bar but keeps its place, so gaps in the data show as gaps in the chart.
ts
// Empty bins keep their place on the axis; the pointer selects nothing there.
import type {ChartConfiguration} from 'chart.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [{label: 'Values', data: [1, 1.5, 2, 2.2, 7.5, 8, 8.1, 9], binWidth: 1, binStart: 0}]
},
options: {
scales: {
x: {title: {display: true, text: 'Value'}},
y: {title: {display: true, text: 'Count'}}
}
}
};
export default config;