Skip to content

Pre-binned data ​

With parsing: false, data holds bins counted elsewhere as {x0, x1, y} records. They are drawn on their edges like any other histogram; a gap between records is an empty bin.

ts
// Pre-binned records: bins counted elsewhere, drawn on their edges.
import type {ChartConfiguration} from 'chart.js';

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [
      {
        label: 'Households',
        // Counts that were binned elsewhere, such as in a database query.
        parsing: false,
        data: [
          {x0: 0, x1: 20, y: 1250},
          {x0: 20, x1: 40, y: 2840},
          {x0: 40, x1: 60, y: 3110},
          {x0: 60, x1: 100, y: 3620},
          // No record for 100 to 150: that bin is drawn empty.
          {x0: 150, x1: 250, y: 610}
        ],
        // The bins have different widths, so show densities.
        stat: 'density'
      }
    ]
  },
  options: {
    scales: {
      x: {title: {display: true, text: 'Income (thousands)'}},
      y: {title: {display: true, text: 'Density'}}
    }
  }
};

export default config;

Released under the MIT License.