Density curves
A histogramDensity dataset draws a smooth curve over a histogram. Name the histogram in its source, by index or by id; the curve reads that histogram's values and weights, spans its bins, and is scaled to its stat, so it follows the bars whatever they show. Register HistogramDensityController once, beside the histogram:
import {
HistogramDensityController,
HistogramController,
HistogramElement
} from 'chartjs-chart-histogram';
Chart.register(HistogramController, HistogramElement, HistogramDensityController);The curve is a line: it takes every line option of Chart.js, such as borderColor, borderWidth, borderDash or fill. Its points are hidden. Chart.js draws the first dataset on top, so put the curve before the histogram, or give it a lower order. Chart.js colors datasets only when none has a color of its own: once the curve has a borderColor, give the bars theirs too.
Kernel density estimate
By default the curve is a Gaussian kernel density estimate, as scipy.stats.gaussian_kde computes it. Over a count histogram it is multiplied by the number of values and the bin width, so the area under it equals the area of the bars.
// A kernel density estimate over a count histogram, scaled to the bars.
import type {ChartConfiguration} from 'chart.js';
import {waitingTimes} from './data/index.js';
const config: ChartConfiguration<'histogram' | 'histogramDensity'> = {
type: 'histogram',
data: {
datasets: [
{
// Chart.js draws the first dataset on top: the curve comes before the bars.
type: 'histogramDensity',
label: 'Density estimate',
data: [],
source: 'waiting',
borderColor: 'rgb(255, 99, 132)'
},
{
id: 'waiting',
label: 'Eruptions',
data: waitingTimes,
binWidth: 2.5,
// A dataset with colors turns off Chart.js's Colors plugin: give the bars theirs.
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgb(54, 162, 235)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Waiting time (min)'}},
y: {title: {display: true, text: 'Eruptions'}}
}
}
};
export default config;Normal curve
curve: 'normal' draws the normal distribution with the values' mean and standard deviation, to compare the data with it.
// The normal curve with the sample's mean and standard deviation, over a density histogram.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';
const config: ChartConfiguration<'histogram' | 'histogramDensity'> = {
type: 'histogram',
data: {
datasets: [
{
type: 'histogramDensity',
label: 'Normal curve',
data: [],
source: 1,
curve: 'normal',
borderColor: 'rgb(255, 159, 64)'
},
{
label: 'Adults',
data: heights,
binWidth: 4,
binStart: 140,
stat: 'density',
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgb(54, 162, 235)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Height (cm)'}},
y: {title: {display: true, text: 'Density'}}
}
}
};
export default config;Bandwidth
bandwidth sets how smooth the estimate is. 'scott' (the default) and 'silverman' pick it from the data, as scipy does; a number sets it in data units. A narrow bandwidth follows every bump of the data, a wide one blurs real features: here the two clusters merge at 8 minutes.
// The bandwidth sets how smooth the estimate is: a rule, or a width in data units.
import type {ChartConfiguration} from 'chart.js';
import {waitingTimes} from './data/index.js';
const curve = (label: string, bandwidth: 'scott' | 'silverman' | number, color: string) => ({
type: 'histogramDensity' as const,
label,
data: [],
source: 'waiting',
bandwidth,
borderColor: color
});
const config: ChartConfiguration<'histogram' | 'histogramDensity'> = {
type: 'histogram',
data: {
datasets: [
curve('Bandwidth 1', 1, 'rgb(255, 99, 132)'),
curve("Scott's rule", 'scott', 'rgb(75, 192, 192)'),
curve('Bandwidth 8', 8, 'rgb(153, 102, 255)'),
{
id: 'waiting',
label: 'Eruptions',
data: waitingTimes,
binWidth: 2.5,
stat: 'density',
backgroundColor: 'rgba(201, 203, 207, 0.5)'
}
]
},
options: {
scales: {
x: {title: {display: true, text: 'Waiting time (min)'}},
y: {title: {display: true, text: 'Density'}}
}
}
};
export default config;Options
| Option | Default | Description |
|---|---|---|
source | The histogram dataset to draw the curve of: an index, or a dataset id | |
curve | 'kde' | 'kde' for a kernel density estimate, 'normal' for the normal curve |
bandwidth | 'scott' | 'scott', 'silverman', or the kernel's standard deviation in data units |
kernel | 'gaussian' | The kernel; 'gaussian' is the only one |
points | 512 | Number of points on the curve |
The curve is scaled by 1 for stat: 'density', by the bin width w for 'relative', by 100 w for 'percent', and by N w for 'count', where N is the total count (or weight) in the bins. No single factor fits a cumulative histogram, or bins of different widths with a stat other than 'density': the curve is then not drawn, and the console shows the warning density-scale. Fewer than two distinct values give no curve and the warning density-degenerate.
The same curve is available without a chart: see computeDensity.