Skip to content

React and Vue ​

The wrappers react-chartjs-2 and vue-chartjs work with any registered chart type. Register the histogram once, then use the wrapper's generic Chart component with type="histogram".

The wrappers update the chart when the data object changes. Pass a new array when the values change rather than changing the old one in place: the histogram is binned again on each update.

React ​

tsx
import {Chart as ChartJS, LinearScale, Tooltip, Legend} from 'chart.js';
import {HistogramController, HistogramElement} from 'chartjs-chart-histogram';
import {Chart} from 'react-chartjs-2';

ChartJS.register(HistogramController, HistogramElement, LinearScale, Tooltip, Legend);

export function Durations({values}: {values: number[]}) {
  return (
    <Chart
      type="histogram"
      data={{datasets: [{label: 'Duration (s)', data: values, bins: 'fd'}]}}
      options={{plugins: {legend: {display: false}}}}
    />
  );
}

Vue ​

vue
<script setup lang="ts">
import {computed} from 'vue';
import {Chart as ChartJS, LinearScale, Tooltip, Legend} from 'chart.js';
import {HistogramController, HistogramElement} from 'chartjs-chart-histogram';
import {Chart} from 'vue-chartjs';

ChartJS.register(HistogramController, HistogramElement, LinearScale, Tooltip, Legend);

const props = defineProps<{values: number[]}>();
const data = computed(() => ({
  datasets: [{label: 'Duration (s)', data: [...props.values], bins: 'fd'}]
}));
</script>

<template>
  <Chart type="histogram" :data="data" />
</template>

Released under the MIT License.