Shared bins
Datasets with the same binGroup share their bin edges, computed from all their values, so the bars line up and can be compared bin by bin. The first dataset of the group sets the binning options; each dataset keeps its own stat. Hiding a dataset with the legend does not move the others' bars.
Two releases compared
ts
// Two releases compared on the same bins.
import type {ChartConfiguration} from 'chart.js';
import {releaseNew, releaseOld} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Release 1.4',
data: releaseOld,
// Datasets with the same binGroup share their edges. The first one sets
// the binning options for the group.
binGroup: 'releases',
bins: 'fd',
stat: 'percent',
backgroundColor: 'rgba(201, 203, 207, 0.7)'
},
{
label: 'Release 1.5',
data: releaseNew,
binGroup: 'releases',
stat: 'percent',
backgroundColor: 'rgba(54, 162, 235, 0.6)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Page load time (ms)'}},
y: {title: {display: true, text: 'Page loads (%)'}}
}
}
};
export default config;Stacked
Because shared bins line up, their bars can be stacked with Chart.js's stacked scale option.
ts
// Stacked bars on shared bins.
import type {ChartConfiguration} from 'chart.js';
import {classA, classB} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{label: 'Class A', data: classA, binGroup: 'scores', binWidth: 5, binStart: 30},
{label: 'Class B', data: classB, binGroup: 'scores'}
]
},
options: {
// Shared bins line up, so the bars of the two classes can stack.
scales: {
x: {stacked: true, title: {display: true, text: 'Score'}},
y: {stacked: true, title: {display: true, text: 'Students'}}
}
}
};
export default config;