Skip to content

API reference ​

The package has two entry points:

ImportContents
chartjs-chart-histogramThe chart types, the element, the plugins, helpers, computeBins and computeDensity
chartjs-chart-histogram/corecomputeBins, 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:

js
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, or y with indexAxis: 'y') is a linear scale that ends on the outer bin edges, and the value axis is a linear scale 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:

js
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 ​

ts
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 ​

ts
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.

js
const {edges, counts, dropped} = getBins(chart, 0);

computeBins ​

ts
function computeBins(values: ArrayLike<number | null | undefined>, options?: BinOptions): BinResult;

Bins values without a chart. See Binning without a chart.

computeDensity ​

ts
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 ​

ts
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:

js
options: {
  scales: {
    x: {
      afterBuildTicks: binEdgeTicks;
    }
  }
}

BinEdgeTicksPlugin ​

The same as a plugin, for several scales or several charts. Register it, then name the scales:

js
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:

js
options: {interaction: {mode: 'bin', intersect: false}}

Types ​

TypeWhat it is
HistogramDatasetOptionsEvery option of a histogram dataset
HistogramOptionsThe binning options of a histogram dataset, without the bar options
HistogramDataPointOne entry of data: a number, a string, null, an object, an [x, y] pair, or a HistogramRecord
HistogramRecordA pre-binned record, {x0, x1, y}
HistogramBinA bin as Chart.js sees it: parsed in tooltips and raw in scriptable contexts. {x, y, x0, x1, count, index}
BinsComputedEventWhat onBinsComputed receives: a BinResult plus chart and datasetIndex
BinOptionsThe options of computeBins
BinResultThe result of a binning
BinCountContextWhat a bins function receives: {values, count, min, max}
Rule, RuleNameThe rule names bins accepts, and the short names results report
Stat, Closed, CumulativeThe values of stat, closed and cumulative
WarningCode, ChartWarningCodeThe warning codes of computeBins, and of the chart
BinEdgeTicksOptionsThe options of BinEdgeTicksPlugin
HistogramDensityDatasetOptionsEvery option of a density dataset: the line options and HistogramDensityControllerOptions
HistogramDensityControllerOptionsThe options of a density dataset, without the line options
DensityOptions, DensityResultThe options and the result of computeDensity
DensityCurve, Bandwidth, KernelThe values of curve, bandwidth and kernel
HistogramSelectionOptionsThe options of HistogramSelection
BinSelectionWhat 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.

Released under the MIT License.