Skip to content

Binning without a chart ​

computeBins is the binning that the chart type uses, available on its own. It has no dependency on Chart.js and needs no DOM, so it runs in Node, in a worker, or on a server. Import it from the core entry to keep Chart.js out of your bundle:

js
import {computeBins} from 'chartjs-chart-histogram/core';

const result = computeBins([1, 2, 2, 3, 3, 3, 4, 4, 5], {bins: 4});
// {
//   edges:   [1, 2, 3, 4, 5],
//   counts:  [1, 2, 3, 3],
//   values:  [1, 2, 3, 3],
//   width:   1,
//   rule:    'count',
//   dropped: 0
// }

It takes the same binning options as a histogram dataset: bins, binWidth, binStart, domain, closed, maxBins, stat, cumulative and weights. weights must be an array here, one weight per value.

The result ​

FieldTypeDescription
edgesnumber[]The bin edges in increasing order: one more than the number of bins. Empty when no value was binned
countsnumber[]The number of values in each bin, or the sum of their weights
valuesnumber[]What each bar shows: the counts after stat and cumulative
widthnumber | nullThe common bin width, or null when the widths differ
rulestringHow the bins were chosen: a rule name, 'count', 'width', 'edges', 'custom' (a function) or 'records' (pre-binned data)
droppednumberHow many entries are in no bin: invalid, out of range, or with an invalid weight

Bin i runs from edges[i] to edges[i + 1].

Warnings ​

computeBins never throws on bad input. Each problem falls back to a valid setting and is reported with a code and a message, by default through console.warn. Pass onWarn to handle them yourself:

js
computeBins(values, {
  bins: -3,
  onWarn(code, message) {
    // code: 'invalid-bins'
  }
});

Warnings lists every code.

Edges you can rely on ​

  • The first edge is the smallest value (or binStart, or domain[0]) exactly, and the last edge is the largest value (or domain[1]) exactly, so no value is lost to rounding at the ends.
  • Inner edges are computed as start + i × step, never as a running sum, and rounding noise such as 0.30000000000000004 is removed: you get 0.3.
  • A value that lies exactly on an inner edge goes to the bin on its right (or its left with closed: 'right').
  • When all values are equal, there is one bin centered on them, of width 1 or of binWidth.

Density curves ​

computeDensity is the curve that the histogramDensity dataset draws, also without a chart:

js
import {computeDensity} from 'chartjs-chart-histogram/core';

const {x, y, bandwidth} = computeDensity(values, {bandwidth: 'silverman', points: 200});

By default it is a Gaussian kernel density estimate: the density at x is the mean of normal curves of standard deviation bandwidth centred on the values. The result integrates to 1 over the whole number line.

OptionDefaultDescription
curve'kde''kde', or 'normal' for the normal curve with the values' mean and (population) standard deviation
bandwidth'scott''scott', 'silverman', or the kernel's standard deviation in data units
kernel'gaussian'The kernel; 'gaussian' is the only one
points512Number of evenly spaced points
domain[min, max]: the range of the curve. Values outside it are left out
weightsOne weight per value, read as computeBins reads them
onWarnCalled with each warning's code and message

Without domain, the curve runs from 3 bandwidths (for the normal curve, 3 standard deviations) below the smallest value to 3 above the largest.

The bandwidths are those of scipy.stats.gaussian_kde: the sample standard deviation times n−1/5 for Scott's rule, or times (3_n_/4)−1/5 for Silverman's. With weights, n is the effective sample size (Σ_w_)²/Σ_w_², and the deviation is weighted. The result is checked against scipy on the same 27 datasets as the binning, and the bandwidth against its exact value.

Up to 107 kernel evaluations (values × points), every value adds its kernel directly. Above that, the values are first spread over 2,048 grid points (linear binning), which keeps large datasets fast and changes the curve by far less than 1% of its peak.

FieldTypeDescription
xnumber[]Positions, in increasing order
ynumber[]The density at each position
bandwidthnumber | nullThe bandwidth used; null for the normal curve or when there is none

Fewer than two distinct values give no curve: empty x and y, and the warning density-degenerate.

Released under the MIT License.