Appearance
Charts > Histogram
A histogram is a graphical representation of the distribution of numerical data, where bars of varying heights depict the frequency of values within specified ranges.
The Chart
undefined (NaN%)
Download example CSV
Copy HTML
Playground
Data
Number of bins (approximation)
Style
Color for bars
Component API
This chart is built using SVG and d3. The d3 bin function is used to group data points in buckets of equal width. Use the thresholds prop to change the number of bins (not guaranteed, only an approximation).
Usage Example (with tooltip)
vue
<template>
<Histogram
:key="key"
:data="data"
:color="color"
:thresholds="20"
@hover="setTooltip"
/>
<OTTooltip :info="tooltip">
<p class="font-semibold my-0">
{{
`${tooltip.data.length} (${(tooltip.data.length * data.length) / 100}%)`
}}
</p>
</OTTooltip>
</template>
<script setup>
import useTooltip from "@/functions/useTooltip.js";
const [tooltip, setTooltip] = useTooltip();
</script>Data
js
[
{ id: "data1", name: "Data 1", value: 1 },
{ id: "data2", name: "Data 2", value: 1 },
{ id: "data3", name: "Data 3", value: 1 },
{ id: "data4", name: "Data 4", value: 3 },
...
]Props
| Name | Default | Type | Description |
|---|---|---|---|
data | [] | Array | The list of values |
margin | { top: 0, right: 0, bottom: 18, left: 25 } | Object | The space around the chart |
color | #e5e7eb | String | The color for bins |
colorScale | null | Function | The color scale for bins instead of uniform color |
selected | null | Array | The selected range as [min, max] (e.g. with slider) |
unselectedClass | opacity-50 | String | The class to style unselected bins |
thresholds | null | Number | The approximate number of bins |
showCompare | false | Boolean | Whether to show compare marks |
xFormat | null | Function | The formatter for x axis labels |
yFormat | null | Function | The formatter for y axis labels |
highlighted | null | Number | String | The id of the bar to highlight |
Dependencies
- d3