Styling
Histogram bars take every bar option of Chart.js, scriptable as usual.
Colors and borders
ts
// Fill, border and corners: the usual bar options.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Adults',
data: heights,
binWidth: 4,
// Every bar option of Chart.js works.
backgroundColor: 'rgba(54, 162, 235, 0.35)',
borderColor: 'rgb(54, 162, 235)',
borderWidth: 2,
borderRadius: 3,
hoverBackgroundColor: 'rgba(54, 162, 235, 0.7)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Height (cm)'}},
y: {title: {display: true, text: 'Count'}}
}
}
};
export default config;Gap
gap leaves space between bars. The bins still touch; only the bars are narrower.
ts
// A gap of 3 pixels between bars; the bins themselves still touch.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [{label: 'gap: 3', data: heights, binWidth: 5, gap: 3}]
},
options: {
scales: {
x: {title: {display: true, text: 'Height (cm)'}},
y: {title: {display: true, text: 'Count'}}
}
}
};
export default config;Colors by bin
In scriptable options, context.raw is the bin, with its edges x0 and x1, its count and its index.
ts
// Colors by bin: heights of 180 cm and more stand out.
import type {ChartConfiguration} from 'chart.js';
import type {ScriptableContext} from 'chart.js';
import type {HistogramBin} from 'chartjs-chart-histogram';
import {heights} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Adults',
data: heights,
binWidth: 4,
// In scriptable options, `raw` is the bin: x0, x1, count and index.
backgroundColor(context: ScriptableContext<'histogram'>) {
const bin = context.raw as HistogramBin | undefined;
return bin && bin.x0 >= 180 ? 'rgb(255, 99, 132)' : 'rgb(201, 203, 207)';
}
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Height (cm)'}},
y: {title: {display: true, text: 'Count'}}
}
}
};
export default config;