Statistics
stat sets what the height of a bar shows. The bins are the same in each sample; only the scale changes.
Percent
ts
// Percentages: each bar is its share of all values.
import type {ChartConfiguration} from 'chart.js';
import {classA} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [{label: 'Students', data: classA, binWidth: 5, binStart: 30, stat: 'percent'}]
},
options: {
scales: {
x: {title: {display: true, text: 'Score'}},
y: {title: {display: true, text: 'Share of students (%)'}}
}
}
};
export default config;Relative frequency
ts
// Relative frequencies: the bars sum to 1.
import type {ChartConfiguration} from 'chart.js';
import {classA} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [{label: 'Students', data: classA, binWidth: 5, binStart: 30, stat: 'relative'}]
},
options: {
scales: {
x: {title: {display: true, text: 'Score'}},
y: {title: {display: true, text: 'Fraction of students'}}
}
}
};
export default config;Density
Density divides each bin's share by its width, so the total area of the bars is 1. It is the right choice for bins of different widths, and for comparing with a probability density curve.
ts
// Density on unequal bins: the bar areas sum to 1.
import type {ChartConfiguration} from 'chart.js';
import {classA} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [{label: 'Students', data: classA, bins: [0, 50, 60, 70, 80, 100], stat: 'density'}]
},
options: {
scales: {
x: {title: {display: true, text: 'Score'}},
y: {title: {display: true, text: 'Density'}}
}
}
};
export default config;