Options
Histogram options are dataset options. Set them on a dataset, or for every histogram dataset of a chart under options.datasets.histogram, or for every chart under Chart.defaults.datasets.histogram:
// One dataset
{type: 'histogram', data: {datasets: [{data: values, bins: 20}]}}
// Every histogram dataset of this chart
{type: 'histogram', data, options: {datasets: {histogram: {bins: 20}}}}
// Every chart
Chart.defaults.datasets.histogram.bins = 20;Binning
| Name | Type | Default | Description |
|---|---|---|---|
bins | number | string | number[] | function | 'auto' | A bin count, a rule name, the bin edges, or a function that returns a count |
binWidth | number | A fixed bin width; wins over a count or rule in bins | |
binStart | number | the smallest value | Where the first bin starts; values below it are left out |
domain | [number, number] | The range to bin over; values outside it are left out | |
closed | 'left' | 'right' | 'left' | Which side of each bin includes its edge |
maxBins | number | 1000 | The largest number of bins |
stat | 'count' | 'relative' | 'percent' | 'density' | 'count' | What the height of each bar shows |
cumulative | false | 'forward' | 'reverse' | false | A running total across the bins |
weights | number[] | string | One weight per value, or the key to read it from object data |
Data sources
| Name | Type | Default | Description |
|---|---|---|---|
source | number | string | Bin the values of another dataset, by index or by id | |
binGroup | string | Datasets with the same group share their bin edges | |
parsing | {key: string} | false | The key to read from object data, or false for pre-binned records |
Appearance and events
| Name | Type | Default | Description |
|---|---|---|---|
gap | number | 0 | Pixels between neighbouring bars |
onBinsComputed | function | Called after every binning with the result |
Every bar option of Chart.js also works: backgroundColor, borderColor, borderWidth, borderRadius, borderSkipped, minBarLength, hoverBackgroundColor, inflateAmount and the rest, scriptable as usual. See Styling, and borders and minBarLength below for how they behave in a histogram. The bar chart's category options barPercentage, categoryPercentage, barThickness, maxBarThickness and grouped have no effect: a bar always spans its bin.
bins
bins says how the edges are chosen.
- A number gives that many equal bins from the smallest to the largest value (or across
domain). A fraction is rounded up. - A rule name lets the data choose the count:
'sqrt'(or'square-root'),'sturges','rice','scott','fd'(or'freedman-diaconis'),'doane'or'auto'. Choosing bins explains each. - An array gives the edges themselves, in any order. They may have different widths. Values outside the first and last edge are left out. Edges win over
binWidth,binStartanddomain. - A function receives
{values, count, min, max}for the valid values and returns a count.
{
bins: 12;
}
{
bins: 'scott';
}
{
bins: [0, 18, 30, 50, 65, 100];
}
{
bins: ({count}) => Math.ceil(count / 20);
}Samples: Choosing bins.
binWidth and binStart
binWidth sets the width of every bin. The first bin starts at binStart, or at the smallest value when binStart is not set, and bins are added until the largest value is inside one. Values below binStart are left out.
{binWidth: 10, binStart: 0} // edges 0, 10, 20, ... up to past the largest valueWhen the width would need more than maxBins bins, maxBins equal bins are used instead, with the warning max-bins.
domain
[min, max] limits the bins to a range: the first edge is min, the last edge is max, and values outside it are left out. A count or rule then splits that range. With binWidth, the bins start at min.
closed
By default a bin includes its lower edge and excludes its upper edge: [10, 20). With closed: 'right' it is the other way: (10, 20]. Either way, the outermost bins include their outer edge, so the smallest and the largest value are always counted.
maxBins
A safety limit, 1000 by default. A count, a rule or a width that asks for more bins than this is capped, with the warning max-bins. Explicit edges are never capped.
stat
| Value | Bar height |
|---|---|
'count' | The number of values in the bin, or the sum of their weights |
'relative' | The bin's share of all binned values; the bars sum to 1 |
'percent' | The same share in percent; the bars sum to 100 |
'density' | The share divided by the bin width; the bar areas sum to 1 |
Use 'density' when bins have different widths: with counts, a wide bin looks more important than it is. The plugin warns (unequal-widths) when stat is 'count' and the widths differ. Samples: Statistics.
cumulative
'forward' makes each bar the total of its bin and every bin before it; with stat: 'percent' the last bar is 100. 'reverse' adds from the last bin backwards. Tooltips then show ≤ upper edge or ≥ lower edge instead of the bin's range. Samples: Cumulative.
weights
With weights, a bin holds the sum of the weights of its values instead of their number. Give an array with one weight per entry of data, or, when data holds objects, the name of the key that holds the weight:
{data: [23, 31, 47], weights: [1.4, 0.9, 0.7]}
{data: [{age: 23, w: 1.4}, {age: 31, w: 0.9}], parsing: {key: 'age'}, weights: 'w'}A weight must be a number of 0 or more. A value with a missing, negative or non-finite weight is left out, with the warning invalid-weights. A value of weight 0 adds nothing to its bin but still counts for the range of the bins. Rules such as 'fd' choose the bin count from the values alone, without their weights. Samples: Weights.
source
Bin the values of another dataset instead of this dataset's data. A number is a dataset index; a string matches a dataset's id, an option every dataset accepts:
datasets: [
{type: 'line', id: 'readings', data: readings, hidden: true},
{type: 'histogram', data: [], source: 'readings'}
];The values are read from the source's raw data through this dataset's parsing.key (default y), so a line of {x, y} points gives its y values. A hidden source still feeds the histogram. When no other dataset matches, the histogram is empty and warns source-missing. Samples: Values from another dataset.
binGroup
Datasets with the same binGroup share their bin edges, computed from the values of all of them, hidden ones included. The first dataset of the group, the one with the lowest index, sets bins, binWidth, binStart, domain, closed and maxBins for the whole group. stat and cumulative stay per dataset. Later datasets need not repeat these options. A later dataset that sets one of them to a different value warns group-options, and the first dataset's options are used. Samples: Shared bins.
parsing
As in Chart.js, parsing: {key: 'value'} reads each value from object data. parsing: false means that data holds bins counted elsewhere, as {x0, x1, y} records. See Data.
gap
Pixels left between neighbouring bars. The bins still touch; only the bars are drawn narrower. The gap never makes a bar thinner than 1 pixel: a bin narrower than the gap keeps a 1 pixel bar. Samples: Styling.
Borders
Neighbouring bars share an edge, so with borderWidth the line between them would be drawn twice, once by each bar. Without a gap, the plugin draws it once: the shorter bar of the two skips its border on that side, and the taller one keeps it, so the line also shows where the taller bar stands above its neighbour. It does this by adding sides to borderSkipped; the sides you skip yourself stay skipped. With a gap, the bars do not touch, and every bar draws all of its borders.
minBarLength
The Chart.js option, in pixels: a bar shorter than this is drawn this long, so that a bin with few values stays visible next to tall ones. An empty bin still has no bar.
onBinsComputed
Called after each binning, once when the chart is created and once per update, with the result plus chart and datasetIndex:
{
data: values,
onBinsComputed({edges, counts, dropped, datasetIndex}) {
console.log(`dataset ${datasetIndex}: ${counts.length} bins, ${dropped} values left out`);
}
}getBins(chart, datasetIndex) returns the same result at any time after the chart has drawn. See the API.