FAQ
Why is a bar missing?
A bin with no values has no bar, but it keeps its place on the axis, so you see a gap. That gap is information: no value fell in that range. If you expected values there, check dropped in getBins or onBinsComputed: values that are not numbers, or that fall outside domain, binStart or explicit edges, are left out.
Why are my bars pale grey?
Chart.js colors datasets that set no colors with its Colors plugin. The plugin steps aside as soon as any dataset of the chart sets backgroundColor or borderColor, or Chart.defaults.backgroundColor or Chart.defaults.borderColor changes. A histogram dataset without colors then gets the Chart.js default, a pale grey. Set backgroundColor on each histogram dataset, or plugins.colors.forceOverride: true to let the plugin color them all.
Why do my counts differ from Excel, Python or another tool?
Usually because the bins differ. Compare the edges, not just the counts:
- The default rule is
'auto', as in numpy. Excel uses a different rule, and many tools use a fixed count. - A value on an inner edge goes to the bin on its right. With
closed: 'right'it goes to the bin on its left, as in R'shist()and Excel. - The first and last bins include their outer edge, so the smallest and largest values are always counted.
To get the same numbers as another tool, give the same edges with bins: [...]. For numpy, see Differences.
Which bin does a value on an edge go to?
With the default closed: 'left', bins are [a, b): the value goes to the bin that starts at it. The last bin is [a, b], so the largest value is counted. With closed: 'right', bins are (a, b] and the first bin is [a, b]. The tooltip shows the brackets.
What is tooltipItem.raw?
In a histogram, raw (in tooltips and in scriptable options) and parsed are the bin, not one of your values:
{x0: 10, x1: 20, count: 12, index: 1, x: 15, y: 12}x0 and x1 are the edges, count the number of values (or sum of weights), and index the bin's position. x and y are what Chart.js plots: the bin's middle and its bar height. With indexAxis: 'y' those two swap; x0 and x1 do not.
How do I set bar options for every histogram?
Bars of a histogram read their defaults from options.elements.histogram, not options.elements.bar:
options: {
elements: {
histogram: {backgroundColor: 'rgba(54, 162, 235, 0.6)', borderWidth: 0}
}
}Histogram options such as bins go in options.datasets.histogram.
Why does the tooltip show one bin per dataset?
The histogram's default interaction mode, 'bin', selects the bin under the pointer in every histogram dataset, anywhere along the bin and at any height. Empty bins are never selected. With other modes, such as 'index', datasets with different edges can show different ranges at the same position; the tooltip then prefixes each line with its range. Use a binGroup to give the datasets the same bins.
Can I use a time or logarithmic axis?
A logarithmic axis works: set scales.x.type to 'logarithmic'. The bins are still computed on the values, so a rule or a binWidth gives bins of equal width in values, which look narrower to the right. For bins of equal width on the log axis, give the edges, such as bins: [1, 10, 100, 1000]. An edge at 0 or below has no place on the axis: that bar starts where the axis starts.
A time axis is not supported in this version. To bin dates, convert them to numbers, such as milliseconds, and format the ticks and tooltips yourself.
Can I switch an existing chart to and from histogram?
Yes: set chart.config.type and call chart.update(). Chart.js keeps the scale types it built when the chart was created, so a chart switched from histogram to bar keeps its linear x axis; set options.scales.x.type as well if you need a category axis.
Does it work with server-side rendering?
Yes. With a canvas implementation such as canvas, a histogram renders in Node. See Server-side rendering.