Skip to content

Other plugins ​

A histogram is a Chart.js chart, so the community plugins work with it. Three of the most used ones are below; each is tested with this plugin.

Data labels ​

chartjs-plugin-datalabels writes a label on each bar. By default the label is dataset.data[index], which in a histogram is one of the raw values, not the bin. Give it a formatter that reads the bin from getBins, and a display that skips empty bins:

ts
// The count of each bin above its bar, with chartjs-plugin-datalabels.
import type {ChartConfiguration} from 'chart.js';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import type {Context} from 'chartjs-plugin-datalabels';
import {getBins} from 'chartjs-chart-histogram';
import {heights} from './data/index.js';

/** The plotted value of the bin a label belongs to. */
const binValue = (context: Context) =>
  getBins(context.chart, context.datasetIndex)?.values[context.dataIndex] ?? 0;

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'Adults', data: heights, binWidth: 4, binStart: 140}]
  },
  // Only this chart gets labels.
  plugins: [ChartDataLabels],
  options: {
    plugins: {
      datalabels: {
        // `dataset.data` holds the raw values, so read the bins instead.
        formatter: (_value, context) => binValue(context),
        // Empty bins have no bar, so no label either.
        display: (context) => binValue(context) > 0,
        anchor: 'end',
        align: 'end'
      }
    },
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      // Room above the tallest bar for its label.
      y: {grace: '10%', title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

The value is the one the bar shows, so it follows stat and cumulative. For a percentage, format it in the formatter, for example `${value.toFixed(1)}%`.

Annotations ​

chartjs-plugin-annotation places lines, boxes and labels at data values. The bin axis is a linear scale, so value, xMin and xMax are values in your data's unit, not bin numbers:

ts
// Lines and a band with chartjs-plugin-annotation, placed in data values.
import {Chart} from 'chart.js';
import type {ChartConfiguration} from 'chart.js';
import annotationPlugin from 'chartjs-plugin-annotation';
import {heights, mean, sd} from './data/index.js';

// Registered once, before the first chart is created: the plugin sets up defaults and
// keeps state for each chart from its creation.
Chart.register(annotationPlugin);

const m = mean(heights);
const s = sd(heights);

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'Adults', data: heights, binWidth: 4, binStart: 140}]
  },
  options: {
    plugins: {
      annotation: {
        annotations: {
          // One standard deviation either side of the mean.
          band: {
            type: 'box',
            xMin: m - s,
            xMax: m + s,
            backgroundColor: 'rgba(255, 159, 64, 0.15)',
            borderWidth: 0
          },
          mean: {
            type: 'line',
            scaleID: 'x',
            value: m,
            borderColor: 'rgb(255, 99, 132)',
            borderWidth: 2,
            label: {display: true, content: `Mean ${m.toFixed(1)} cm`, position: 'start'}
          }
        }
      }
    },
    scales: {
      x: {title: {display: true, text: 'Height (cm)'}},
      y: {title: {display: true, text: 'Count'}}
    }
  }
};

export default config;

Zoom and pan ​

chartjs-plugin-zoom zooms and pans the scales. Zooming changes the view, not the bins: the bars keep their edges. Scroll on the chart to zoom, and drag to pan:

ts
// Zoom with the mouse wheel and pan by dragging, with chartjs-plugin-zoom.
import {Chart} from 'chart.js';
import type {ChartConfiguration} from 'chart.js';
import zoomPlugin from 'chartjs-plugin-zoom';
import {responseTimes} from './data/index.js';

Chart.register(zoomPlugin);

const config: ChartConfiguration<'histogram'> = {
  type: 'histogram',
  data: {
    datasets: [{label: 'Requests', data: responseTimes, binWidth: 10, binStart: 0}]
  },
  options: {
    plugins: {
      zoom: {
        // Along the bin axis only: the counts keep their scale.
        zoom: {wheel: {enabled: true}, pinch: {enabled: true}, mode: 'x'},
        pan: {enabled: true, mode: 'x'},
        // Not past the first and last edges.
        limits: {x: {min: 'original', max: 'original'}}
      }
    },
    scales: {
      x: {title: {display: true, text: 'Response time (ms)'}},
      y: {title: {display: true, text: 'Requests'}}
    }
  }
};

export default config;

Released under the MIT License.