Histogram
Bin one numeric field and count each bin to show where values cluster, then change the bin count, overlap groups and compare them as shares.
A histogram shows the shape of one number across many observations. OpenChart bins the raw values, counts each bin and draws a bar per bin on a continuous x axis, so bar widths follow the value range instead of equal-width categories.
Use a density curve when you want the shape without bin edges, or a beeswarm when every observation should stay visible. If you already have one total per category, a bar chart is the right chart.
A basic histogram
Set mark to { type: "histogram", binCount: 24 }, which asks for up to 24 bins, and put the raw field on x. Each row of the data is one observation, and the y axis defaults to the count per bin. binCount defaults to 20.
{
"mark": {
"type": "histogram",
"binCount": 24
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 30 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "More than a third of small gifts are under $100",
"subtitle": "Individual contributions by size, $20 bins"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "More than a third of small gifts are under $100",
"subtitle": "Individual contributions by size, $20 bins"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "More than a third of small gifts are under $100",
"subtitle": "Individual contributions by size, $20 bins"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "More than a third of small gifts are under $100",
"subtitle": "Individual contributions by size, $20 bins"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "More than a third of small gifts are under $100",
"subtitle": "Individual contributions by size, $20 bins"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fewer bins
binCount sets the most bins you want. OpenChart rounds the bin width to a round step (1, 2 or 5 times a power of ten), so the count you get can differ. Wider bins give a smoother outline and lose detail. The data is the same as the first figure.
{
"mark": {
"type": "histogram",
"binCount": 8
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 30 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Wider bins smooth the shape and hide detail",
"subtitle": "Individual contributions by size, $100 bins"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 8
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Wider bins smooth the shape and hide detail",
"subtitle": "Individual contributions by size, $100 bins"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 8
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Wider bins smooth the shape and hide detail",
"subtitle": "Individual contributions by size, $100 bins"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 8
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Wider bins smooth the shape and hide detail",
"subtitle": "Individual contributions by size, $100 bins"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 8
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Wider bins smooth the shape and hide detail",
"subtitle": "Individual contributions by size, $100 bins"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Overlapping groups
Add a color channel and each group gets its own histogram. Without stack, the groups overlap at the same x and the fill drops to 55% opacity, so both shapes stay visible. Overlap suits comparing where two groups sit on the same scale.
{
"mark": {
"type": "histogram",
"binCount": 24
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 54 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Large gifts center near $680, small gifts near $280",
"subtitle": "Individual contributions by size"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Large gifts center near $680, small gifts near $280",
"subtitle": "Individual contributions by size"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Large gifts center near $680, small gifts near $280",
"subtitle": "Individual contributions by size"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Large gifts center near $680, small gifts near $280",
"subtitle": "Individual contributions by size"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Large gifts center near $680, small gifts near $280",
"subtitle": "Individual contributions by size"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Groups as shares
normalize: true plots each group’s share of its own total instead of a raw count, and formats the y axis as a percentage. Use it when the groups have different sizes, since raw counts would let the larger group dominate.
{
"mark": {
"type": "histogram",
"binCount": 24,
"normalize": true
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 54 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Each group's bars sum to 100%",
"subtitle": "Share of each group's contributions by size"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24,
"normalize": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Each group's bars sum to 100%",
"subtitle": "Share of each group's contributions by size"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24,
"normalize": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Each group's bars sum to 100%",
"subtitle": "Share of each group's contributions by size"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24,
"normalize": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Each group's bars sum to 100%",
"subtitle": "Share of each group's contributions by size"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "histogram",
"binCount": 24,
"normalize": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Each group's bars sum to 100%",
"subtitle": "Share of each group's contributions by size"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fields that matter
| Field | What it does |
|---|---|
mark.type |
"histogram" bins the x values and counts each bin. |
mark.binCount |
The bin count to aim for; the bin width is rounded to a round number. Defaults to 20. |
mark.normalize |
Plots each group’s share of its own total. Formats the y axis as a percentage. |
mark.fillOpacity |
Overrides the 55% opacity used for overlapping groups. |
encoding.x |
The raw quantitative field to bin. Required. |
encoding.y |
Optional. Defaults to the count per bin. |
encoding.color |
Optional. One overlapping histogram per group. |
labels |
Has no effect on a histogram, and the compiler warns if you set it. |
Related
- Density: the same comparison as smoothed curves.
- Beeswarm: one dot per observation.
- Tick strip: one tick per observation on a shared axis.
- Bar: compare one total per category.
- Scatter: relate two numbers instead of counting one.
- Transforms reference: every transform and its fields.