API reference
The package has two entry points:
| Import | Contents |
|---|---|
chartjs-chart-histogram | The chart types, the element, the plugins, helpers, computeBins and computeDensity |
chartjs-chart-histogram/core | computeBins, computeDensity and their types only; no Chart.js |
Both ship as ES modules and CommonJS, with TypeScript declarations. A UMD build for script tags is at dist/chartjs-chart-histogram.umd.min.js; it registers the histogram and histogramDensity types when it loads and exposes the exports on the global ChartHistogram. The selection plugin stays off until you register ChartHistogram.HistogramSelection.
HistogramController
The histogram chart type. Register it with its element:
Chart.register(HistogramController, HistogramElement);It extends Chart.js's BarController. Its dataset options are listed in Options. Charts of type histogram get these defaults:
- The index axis (
x, orywithindexAxis: 'y') is alinearscale that ends on the outer bin edges, and the value axis is alinearscale from zero. interaction: {mode: 'bin', intersect: false}.- Tooltip callbacks that show each bin's range as the title and its value in the body, and prefix each line with its own range when the items' ranges differ.
HistogramElement
The bar of a histogram. It draws like a BarElement, takes the same options under options.elements.histogram, and is hit anywhere along its bin, at any height. An empty bin is never hit.
HistogramDensityController
The histogramDensity dataset type: a density curve over a histogram dataset. Register it beside the histogram:
Chart.register(HistogramController, HistogramElement, HistogramDensityController);It extends Chart.js's LineController, so its dataset takes every line option. Its own options are source, curve, bandwidth, kernel and points; see Density curves. Its data is ignored and may be []. Points are hidden by default (pointRadius: 0), and the curve is drawn with cubicInterpolationMode: 'monotone', which stays smooth at high pixel ratios and never dips below zero; set cubicInterpolationMode: 'default' for straight segments.
HistogramSelection
const HistogramSelection: Plugin<'histogram', HistogramSelectionOptions>;The plugin that selects bins with the pointer, with the id histogramSelection. It is opt-in: register it, or list it in a chart's plugins. Its options are under options.plugins.histogramSelection; see Selecting bins. The selection is passed to onSelect as a BinSelection, and each bar's scriptable context gets selected.
getBins
function getBins(chart: Chart, datasetIndex: number): BinResult | undefined;The result of the latest binning of a histogram dataset: see the result. undefined when the dataset is not a histogram or has not been drawn yet.
const {edges, counts, dropped} = getBins(chart, 0);computeBins
function computeBins(values: ArrayLike<number | null | undefined>, options?: BinOptions): BinResult;Bins values without a chart. See Binning without a chart.
computeDensity
function computeDensity(
values: ArrayLike<number | null | undefined>,
options?: DensityOptions
): DensityResult;A Gaussian kernel density estimate, or a normal curve, without a chart: {x, y, bandwidth}. See Density curves.
binEdgeTicks
function binEdgeTicks(scale: Scale): void;A scale callback that puts one tick on every bin edge of the first visible histogram dataset on that scale. Use it with explicit or unequal edges, where round-number ticks would fall inside bins:
options: {
scales: {
x: {
afterBuildTicks: binEdgeTicks;
}
}
}BinEdgeTicksPlugin
The same as a plugin, for several scales or several charts. Register it, then name the scales:
Chart.register(BinEdgeTicksPlugin);
options: {
plugins: {
binEdgeTicks: {
scales: ['x'];
}
}
}The bin interaction mode
Registering HistogramController adds the interaction mode 'bin': in each visible histogram dataset, the bin under the pointer along the index axis, at any height. It is the default of histogram charts. In a chart of another type that contains histogram datasets, set it yourself:
options: {interaction: {mode: 'bin', intersect: false}}Types
| Type | What it is |
|---|---|
HistogramDatasetOptions | Every option of a histogram dataset |
HistogramOptions | The binning options of a histogram dataset, without the bar options |
HistogramDataPoint | One entry of data: a number, a string, null, an object, an [x, y] pair, or a HistogramRecord |
HistogramRecord | A pre-binned record, {x0, x1, y} |
HistogramBin | A bin as Chart.js sees it: parsed in tooltips and raw in scriptable contexts. {x, y, x0, x1, count, index} |
BinsComputedEvent | What onBinsComputed receives: a BinResult plus chart and datasetIndex |
BinOptions | The options of computeBins |
BinResult | The result of a binning |
BinCountContext | What a bins function receives: {values, count, min, max} |
Rule, RuleName | The rule names bins accepts, and the short names results report |
Stat, Closed, Cumulative | The values of stat, closed and cumulative |
WarningCode, ChartWarningCode | The warning codes of computeBins, and of the chart |
BinEdgeTicksOptions | The options of BinEdgeTicksPlugin |
HistogramDensityDatasetOptions | Every option of a density dataset: the line options and HistogramDensityControllerOptions |
HistogramDensityControllerOptions | The options of a density dataset, without the line options |
DensityOptions, DensityResult | The options and the result of computeDensity |
DensityCurve, Bandwidth, Kernel | The values of curve, bandwidth and kernel |
HistogramSelectionOptions | The options of HistogramSelection |
BinSelection | What onSelect receives: {datasetIndex, start, end, bins, total} |
Importing the package also adds the histogram and histogramDensity types to Chart.js's TypeScript types, so ChartConfiguration<'histogram' | 'histogramDensity'> and ChartDataset<'histogram'> know every option. It adds an optional id to every dataset, selected to the scriptable context, and histogramSelection to the plugin options.