Skip to content

Histogram ​

A histogram bins raw values and draws one gap-free bar per bin. The engine bins the data in O(n), so 1,000,000+ values are fine. Use it to show the distribution of a single variable.

Live example

Basic usage ​

ts
import { Charturbo } from 'charturbo';

const canvas = document.querySelector('canvas')!;
const sdk = await Charturbo.create(canvas);

const values = new Float32Array(500_000).map(() => Math.random() * 100);
const bins = sdk.renderHistogram(values, { bins: 'auto', title: 'Response time (ms)' });
console.log(`${bins.binCount} bins (${bins.rule})`);
sdk.enableNodeInteractions(canvas, { autoRender: true });
sdk.render();
ts
renderHistogram(
  values: Float32Array | number[],
  options?: HistogramChartOptions
): HistogramResult

Data point ids are the bin indices.

Data format ​

values is a flat Float32Array (preferred) or number[] of raw samples. Non-finite values and values outside range are ignored.

The method returns the bins the engine computed:

FieldTypeDescription
binCountnumberNumber of bins.
edgesFloat64ArraybinCount + 1 ascending bin edges.
countsFloat64ArraySamples per bin.
heightsFloat64ArrayBar heights: counts, or densities with density: true.
totalnumberFinite values inside the range.
rule'freedman-diaconis' | 'sturges' | 'fixed'Binning rule that was used.

Options ​

HistogramChartOptions:

OptionTypeDefaultDescription
bins'auto' | number'auto''auto': Freedman–Diaconis, or Sturges when the IQR is 0 (max 1000 bins). A number sets equal-width bins (max 10000).
range[number, number]finite data extentBinned range. Values outside are ignored.
densitybooleanfalseBar heights are densities whose total area is 1.
colorColorInputtheme palette[0]Bar color.
barGapnumber1CSS px between bars.
cornerRadiusnumber0Rounded bar tops in CSS px.
titlestring—Chart title.
descriptionstring—Accessible name of the canvas.
localestringSDK localeLocale of default labels.
axes{ x?: ChartAxisOptions; y?: ChartAxisOptions }—x ticks land on bin edges and receive the edge value.
paddingChartPaddingOptionsautoPlot margins in CSS px.
hoverChartHoverStyle—Hover style.
gridChartGridStyle—Grid style.
widthnumber164World width of the plot.
maxBarHeightnumber78World height of the tallest bar.

Hover ​

The tooltip label is the bin range a – b, with the count (or the density and the count when density: true).

Updating data ​

Call renderHistogram again with the new values. Binning is O(n), so re-binning large arrays is cheap.

Notes ​

  • Histograms are always vertical and 2D. They have no zoom or pan and no animation option.
  • For grouped distributions with quartiles, use Box Plot · Violin.

Frameworks ​

tsx
<CharturboChart type="histogram" data={values} options={{ bins: 40 }} />

See also ​

MIT License