Skip to content

Choosing bins ​

Four ways to set the bins. Choosing bins in the guide explains when to use which.

A rule ​

ts
// A named rule: Freedman-Diaconis.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      // A rule picks the bin count from the data: 'sqrt', 'sturges', 'rice',
      // 'scott', 'fd' (or 'freedman-diaconis'), 'doane', or 'auto' (the default).
      {label: "bins: 'fd'", data: heights, bins: 'fd'}
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

A count ​

ts
// A fixed count: 8 equal bins from the smallest to the largest value.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'bins: 8', data: heights, bins: 8}]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

A width ​

ts
// A fixed width and a start: round edges a reader can check.
import type {ChartConfiguration} from 'chart.js';
import {heights} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'binWidth: 5, binStart: 140',
        data: heights,
        // Bins of 5 cm starting at 140: edges 140, 145, 150, ...
        binWidth: 5,
        binStart: 140
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

Explicit edges ​

Edges may have different widths. Use stat: 'density' with them, so that bar areas stay proportional to counts.

ts
// Explicit, unequal edges, drawn as a density.
import type {ChartConfiguration} from 'chart.js';
import {responseTimes} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'Requests',
        data: responseTimes,
        // Explicit edges, of different widths.
        bins: [0, 100, 150, 200, 300, 500, 1000],
        // With unequal widths, density keeps each bar's area proportional to its count.
        stat: 'density',
        backgroundColor: 'rgba(153, 102, 255, 0.7)'
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Response time (ms)'}},
      y: {title: {display: true, text: 'Density'}}
    }
  }
};

export default config;

A domain ​

ts
// A domain: bin only the values in a range.
import type {ChartConfiguration} from 'chart.js';
import {responseTimes} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'Requests under 400 ms',
        data: responseTimes,
        // Only values in [0, 400] are binned; the rest count as dropped.
        domain: [0, 400],
        bins: 20
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Response time (ms)'}},
      y: {title: {display: true, text: 'Requests'}}
    }
  }
};

export default config;

A function ​

ts
// A bin count computed by your own function.
import type {ChartConfiguration} from 'chart.js';
import type {BinCountContext} from 'chartjs-chart-histogram';
import {heights} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'One bin per 25 values, at most 30',
        data: heights,
        // A function gets the values and their extent and returns a count.
        bins: (context: BinCountContext) => Math.min(30, Math.ceil(context.count / 25))
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

A cap on the bin count ​

maxBins, 1000 by default, guards against a width or a rule that asks for far too many bins. Above it, the bins become maxBins equal bins and the console shows the warning max-bins.

ts
// A width of 1 ms would need over a thousand bins here; maxBins caps them at 60.
import type {ChartConfiguration} from 'chart.js';
import {responseTimes} from './data/index.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'Requests',
        data: responseTimes,
        binWidth: 1,
        // 60 equal bins instead, with the warning `max-bins` in the console.
        maxBins: 60
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Response time (ms)'}},
      y: {title: {display: true, text: 'Requests'}}
    }
  }
};

export default config;

A logarithmic axis ​

The bin axis can be logarithmic. A rule or a width gives bins of equal width in values, which look narrow at the right of a log axis; edges that are equal steps on the axis look even. See the FAQ.

ts
// Response times on a logarithmic axis, with edges that are equal steps on that axis.
import type {ChartConfiguration} from 'chart.js';
import {responseTimes} from './data/index.js';

// Four bins per power of ten, from 10 ms to 10 s.
const edges = Array.from({length: 13}, (_, i) => Number((10 * 10 ** (i / 4)).toPrecision(3)));

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'Requests', data: responseTimes, bins: edges}]
  },
  options: {
    scales: {
      x: {type: 'logarithmic', title: {display: true, text: 'Response time (ms)'}},
      y: {title: {display: true, text: 'Requests'}}
    }
  }
};

export default config;

Right-closed bins ​

Hover the bars to see the brackets: [1, 2], (2, 3], ...

ts
// Right-closed bins: an edge value belongs to the bin below it.
import type {ChartConfiguration} from 'chart.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: "closed: 'right'",
        data: [1, 2, 2, 3, 3, 3, 4, 4, 5],
        bins: [1, 2, 3, 4, 5],
        // Bins are (1, 2], (2, 3], ...: a value on an edge goes to the bin on its left.
        closed: 'right'
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Value'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

Released under the MIT License.