Selecting bins
The HistogramSelection plugin selects bins with the pointer. It is a separate, opt-in plugin: charts that do not use it do not load it. Register it for every chart, or list it in the plugins of one chart:
import {HistogramSelection} from 'chartjs-chart-histogram';
Chart.register(HistogramSelection);
// or: new Chart(canvas, {type: 'histogram', data, plugins: [HistogramSelection]});The plugin changes no colour by itself. Each bar's scriptable context has selected, so a scriptable option styles the selection: backgroundColor: (ctx) => (ctx.selected ? a : b). onSelect receives the selection after each change:
{datasetIndex: 0, start: 160, end: 175, bins: [4, 5, 6], total: 212}start and end are the outer edges of the selected bins, bins their indices and total their summed count (or weight). It receives null when the selection is cleared, also when an update changes the bin edges, since the old indices would point at other ranges.
Click to select
In the default mode, 'bin', a click selects the bin under the pointer, at any height above or below its bar. Shift-click adds a bin or removes it. A click outside every bin clears the selection.
// Click a bar to select its bin, shift-click to add or remove bins, click outside the bars
// to clear.
import type {ChartConfiguration, ScriptableContext} from 'chart.js';
import {HistogramSelection} from 'chartjs-chart-histogram';
import {heights} from './data/index.js';
const color = (context: ScriptableContext<'histogram'>) =>
context.selected ? 'rgb(255, 99, 132)' : 'rgba(54, 162, 235, 0.5)';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Adults',
data: heights,
binWidth: 5,
binStart: 140,
backgroundColor: color,
hoverBackgroundColor: color
}
]
},
// Only this chart selects bins. Chart.register(HistogramSelection) turns it on for all.
plugins: [HistogramSelection],
options: {
plugins: {
title: {display: true, text: 'Click a bar'},
histogramSelection: {
onSelect(selection, chart) {
const title = chart.options.plugins?.title;
if (title) {
title.text = selection
? `${String(selection.start)} to ${String(selection.end)} cm: ${String(selection.total)} adults in ${String(selection.bins.length)} bins`
: 'Click a bar';
}
}
}
},
scales: {
x: {title: {display: true, text: 'Height (cm)'}},
y: {title: {display: true, text: 'Count'}}
}
}
};
export default config;Drag a range
With mode: 'range', a drag along the bin axis selects every bin it touches, snapped outward to their edges, and shades the band behind the bars; a click clears it. modifierKey makes only drags with that key held select, which leaves plain drags to another plugin. Here a plain drag zooms with chartjs-plugin-zoom, and a shift-drag selects.
// Shift-drag to select a range of bins; a plain drag zooms (chartjs-plugin-zoom), and a
// double click resets the zoom.
import type {ChartConfiguration, ScriptableContext} from 'chart.js';
import {HistogramSelection} from 'chartjs-chart-histogram';
import {responseTimes} from './data/index.js';
const config: ChartConfiguration<'histogram'> = {
type: 'histogram',
data: {
datasets: [
{
label: 'Requests',
data: responseTimes,
binWidth: 25,
binStart: 0,
backgroundColor: (context: ScriptableContext<'histogram'>) =>
context.selected ? 'rgb(75, 192, 192)' : 'rgb(201, 203, 207)'
}
]
},
plugins: [HistogramSelection],
options: {
plugins: {
title: {display: true, text: 'Shift-drag to select, drag to zoom'},
histogramSelection: {
mode: 'range',
modifierKey: 'shift',
backgroundColor: 'rgba(75, 192, 192, 0.15)',
onSelect(selection, chart) {
const title = chart.options.plugins?.title;
if (title) {
title.text = selection
? `${String(selection.total)} requests from ${String(selection.start)} to ${String(selection.end)} ms`
: 'Shift-drag to select, drag to zoom';
}
}
},
zoom: {zoom: {drag: {enabled: true}, mode: 'x'}}
},
onClick(event, _elements, chart) {
if ((event.native as MouseEvent | null)?.detail === 2) {
(chart as unknown as {resetZoom: () => void}).resetZoom();
}
},
scales: {
x: {title: {display: true, text: 'Response time (ms)'}},
y: {title: {display: true, text: 'Requests'}}
}
}
};
export default config;Options
Under options.plugins.histogramSelection:
| Option | Default | Description |
|---|---|---|
mode | 'bin' | 'bin': click and shift-click select bins. 'range': a drag selects a range |
modifierKey | null | In 'range' mode, 'shift', 'ctrl', 'alt' or 'meta': only drags with it held select |
backgroundColor | 'rgba(128, 128, 128, 0.2)' | The colour of the band behind the selected bins in 'range' mode |
onSelect | null | (selection, chart) => void, called after each change |
histogramSelection: false turns the plugin off for one chart. The selection belongs to one histogram dataset: the one drawn on top that has a bar under the pointer.