Bar
Compare one value across named categories with horizontal bars, then sort, group, stack, label and highlight them.
A bar chart compares one number across a set of categories, and the reader judges each value by the length of its bar. Bars run horizontally, so category names sit on the left at full length, however long they are.
Use a column chart instead when the categories are time periods or have a natural left-to-right order, such as months or years. The spec is the same; only the channels swap. OpenChart draws horizontal bars when y is the category and x is the number, and vertical columns when x is the category and y is the number.
A basic bar chart
Set mark to "bar", put the number on x and the category on y. Value labels show by default on the bars where they fit.
{
"mark": "bar",
"data": [
{
"country": "India",
"population": 1463000000
},
{
"country": "China",
"population": 1410000000
},
{
"country": "United States",
"population": 347000000
},
{
"country": "Indonesia",
"population": 285000000
},
{
"country": "Pakistan",
"population": 255000000
},
{
"country": "Nigeria",
"population": 240000000
},
{
"country": "Brazil",
"population": 217000000
},
{
"country": "Bangladesh",
"population": 175000000
},
{
"country": "Russia",
"population": 144000000
},
{
"country": "Ethiopia",
"population": 135000000
}
],
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal"
}
},
"chrome": {
"title": "The ten most populous countries",
"subtitle": "Population by country, 2025 estimates"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal"
}
},
"chrome": {
"title": "The ten most populous countries",
"subtitle": "Population by country, 2025 estimates"
}
};
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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal"
}
},
"chrome": {
"title": "The ten most populous countries",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal"
}
},
"chrome": {
"title": "The ten most populous countries",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal"
}
},
"chrome": {
"title": "The ten most populous countries",
"subtitle": "Population by country, 2025 estimates"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Sorted bars
A ranking reads fastest when the longest bar is first. sort: "-x" on the category channel orders the categories by their x value, largest at the top. sort: null keeps the order of the data, and an array of names sets the order by hand.
{
"mark": "bar",
"data": [
{
"country": "India",
"population": 1463000000
},
{
"country": "China",
"population": 1410000000
},
{
"country": "United States",
"population": 347000000
},
{
"country": "Indonesia",
"population": 285000000
},
{
"country": "Pakistan",
"population": 255000000
},
{
"country": "Nigeria",
"population": 240000000
},
{
"country": "Brazil",
"population": 217000000
},
{
"country": "Bangladesh",
"population": 175000000
},
{
"country": "Russia",
"population": 144000000
},
{
"country": "Ethiopia",
"population": 135000000
}
],
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
}
},
"chrome": {
"title": "India and China each have about four times the US population",
"subtitle": "Population by country, 2025 estimates"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
}
},
"chrome": {
"title": "India and China each have about four times the US population",
"subtitle": "Population by country, 2025 estimates"
}
};
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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
}
},
"chrome": {
"title": "India and China each have about four times the US population",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
}
},
"chrome": {
"title": "India and China each have about four times the US population",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
}
},
"chrome": {
"title": "India and China each have about four times the US population",
"subtitle": "Population by country, 2025 estimates"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Grouped bars
Add a color channel to split each category into series. Bars stack by default; stack: null on the value channel places the series side by side instead, which suits comparing series against each other rather than reading totals. This example uses columns because the categories are years.
{
"mark": "bar",
"data": [
{
"year": "2019",
"capacity": 98,
"type": "Solar"
},
{
"year": "2019",
"capacity": 58,
"type": "Wind"
},
{
"year": "2019",
"capacity": 12,
"type": "Hydro"
},
{
"year": "2020",
"capacity": 127,
"type": "Solar"
},
{
"year": "2020",
"capacity": 90,
"type": "Wind"
},
{
"year": "2020",
"capacity": 20,
"type": "Hydro"
},
// … 9 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": null,
"axis": {
"title": "Capacity added (GW)"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"labels": {
"density": "none"
},
"chrome": {
"title": "Solar added more capacity than wind and hydro combined in every year",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": null,
"axis": {
"title": "Capacity added (GW)"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"labels": {
"density": "none"
},
"chrome": {
"title": "Solar added more capacity than wind and hydro combined in every year",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
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 = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": null,
"axis": {
"title": "Capacity added (GW)"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"labels": {
"density": "none"
},
"chrome": {
"title": "Solar added more capacity than wind and hydro combined in every year",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
</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 = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": null,
"axis": {
"title": "Capacity added (GW)"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"labels": {
"density": "none"
},
"chrome": {
"title": "Solar added more capacity than wind and hydro combined in every year",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
</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 = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": null,
"axis": {
"title": "Capacity added (GW)"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"labels": {
"density": "none"
},
"chrome": {
"title": "Solar added more capacity than wind and hydro combined in every year",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Stacked bars
With stack: "zero" the segments stack end to end, so each bar shows its total and the share of each part. Here every bracket sums to 100%, which makes the housing segment comparable across incomes.
{
"mark": "bar",
"data": [
{
"bracket": "Under $30K",
"pct": 40,
"category": "Housing"
},
{
"bracket": "Under $30K",
"pct": 18,
"category": "Food"
},
{
"bracket": "Under $30K",
"pct": 15,
"category": "Transport"
},
{
"bracket": "Under $30K",
"pct": 12,
"category": "Healthcare"
},
{
"bracket": "Under $30K",
"pct": 15,
"category": "Other"
},
{
"bracket": "$30K - $50K",
"pct": 35,
"category": "Housing"
},
// … 19 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "pct",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of spending (%)"
}
},
"y": {
"field": "bracket",
"type": "nominal"
},
"color": {
"field": "category",
"type": "nominal"
}
},
"chrome": {
"title": "Lower-income households spend a larger share on housing",
"subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "pct",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of spending (%)"
}
},
"y": {
"field": "bracket",
"type": "nominal"
},
"color": {
"field": "category",
"type": "nominal"
}
},
"chrome": {
"title": "Lower-income households spend a larger share on housing",
"subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
}
};
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 = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "pct",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of spending (%)"
}
},
"y": {
"field": "bracket",
"type": "nominal"
},
"color": {
"field": "category",
"type": "nominal"
}
},
"chrome": {
"title": "Lower-income households spend a larger share on housing",
"subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
}
};
</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 = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "pct",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of spending (%)"
}
},
"y": {
"field": "bracket",
"type": "nominal"
},
"color": {
"field": "category",
"type": "nominal"
}
},
"chrome": {
"title": "Lower-income households spend a larger share on housing",
"subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
}
};
</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 = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "pct",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of spending (%)"
}
},
"y": {
"field": "bracket",
"type": "nominal"
},
"color": {
"field": "category",
"type": "nominal"
}
},
"chrome": {
"title": "Lower-income households spend a larger share on housing",
"subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Value labels
labels controls the numbers printed on the bars. They show by default wherever they fit, and a label that would collide with another is left out. format takes a d3-format string: "+.1f" prints a sign and one decimal. density: "none" turns the labels off. A negative value grows left from zero, and a refline at x: 0 marks the baseline. The color channel maps a derived direction field to two colors so the loss stands out.
{
"mark": "bar",
"data": [
{
"sector": "Communication Services",
"return": 39.7,
"direction": "Gain"
},
{
"sector": "Information Technology",
"return": 37.6,
"direction": "Gain"
},
{
"sector": "Consumer Discretionary",
"return": 29.5,
"direction": "Gain"
},
{
"sector": "Financials",
"return": 28.9,
"direction": "Gain"
},
{
"sector": "Utilities",
"return": 20.1,
"direction": "Gain"
},
{
"sector": "Industrials",
"return": 16.2,
"direction": "Gain"
},
{
"sector": "Consumer Staples",
"return": 12.2,
"direction": "Gain"
},
{
"sector": "Real Estate",
"return": 2,
"direction": "Gain"
},
{
"sector": "Energy",
"return": 1.9,
"direction": "Gain"
},
{
"sector": "Health Care",
"return": 1.1,
"direction": "Gain"
},
{
"sector": "Materials",
"return": -1.2,
"direction": "Loss"
}
],
"encoding": {
"x": {
"field": "return",
"type": "quantitative",
"axis": {
"title": "Total return (%)",
"format": "+.0f"
}
},
"y": {
"field": "sector",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "direction",
"type": "nominal",
"scale": {
"domain": [
"Gain",
"Loss"
],
"range": [
"#0e7490",
"#d1495b"
]
}
}
},
"legend": {
"show": false
},
"labels": {
"format": "+.1f"
},
"annotations": [
{
"type": "refline",
"x": 0,
"style": "solid",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Only materials ended 2024 in the red",
"subtitle": "S&P 500 total return by sector, full year 2024"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 11 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "return",
"type": "quantitative",
"axis": {
"title": "Total return (%)",
"format": "+.0f"
}
},
"y": {
"field": "sector",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "direction",
"type": "nominal",
"scale": {
"domain": [
"Gain",
"Loss"
],
"range": [
"#0e7490",
"#d1495b"
]
}
}
},
"legend": {
"show": false
},
"labels": {
"format": "+.1f"
},
"annotations": [
{
"type": "refline",
"x": 0,
"style": "solid",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Only materials ended 2024 in the red",
"subtitle": "S&P 500 total return by sector, full year 2024"
}
};
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 = [/* 11 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "return",
"type": "quantitative",
"axis": {
"title": "Total return (%)",
"format": "+.0f"
}
},
"y": {
"field": "sector",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "direction",
"type": "nominal",
"scale": {
"domain": [
"Gain",
"Loss"
],
"range": [
"#0e7490",
"#d1495b"
]
}
}
},
"legend": {
"show": false
},
"labels": {
"format": "+.1f"
},
"annotations": [
{
"type": "refline",
"x": 0,
"style": "solid",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Only materials ended 2024 in the red",
"subtitle": "S&P 500 total return by sector, full year 2024"
}
};
</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 = [/* 11 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "return",
"type": "quantitative",
"axis": {
"title": "Total return (%)",
"format": "+.0f"
}
},
"y": {
"field": "sector",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "direction",
"type": "nominal",
"scale": {
"domain": [
"Gain",
"Loss"
],
"range": [
"#0e7490",
"#d1495b"
]
}
}
},
"legend": {
"show": false
},
"labels": {
"format": "+.1f"
},
"annotations": [
{
"type": "refline",
"x": 0,
"style": "solid",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Only materials ended 2024 in the red",
"subtitle": "S&P 500 total return by sector, full year 2024"
}
};
</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 = [/* 11 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "return",
"type": "quantitative",
"axis": {
"title": "Total return (%)",
"format": "+.0f"
}
},
"y": {
"field": "sector",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "direction",
"type": "nominal",
"scale": {
"domain": [
"Gain",
"Loss"
],
"range": [
"#0e7490",
"#d1495b"
]
}
}
},
"legend": {
"show": false
},
"labels": {
"format": "+.1f"
},
"annotations": [
{
"type": "refline",
"x": 0,
"style": "solid",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Only materials ended 2024 in the red",
"subtitle": "S&P 500 total return by sector, full year 2024"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Highlighting a bar
highlight on the color channel keeps the named categories in color and greys out the rest. A refline annotation draws a reference value in data units, here 1 billion people, and its label sits outside the plot.
{
"mark": "bar",
"data": [
{
"country": "India",
"population": 1463000000
},
{
"country": "China",
"population": 1410000000
},
{
"country": "United States",
"population": 347000000
},
{
"country": "Indonesia",
"population": 285000000
},
{
"country": "Pakistan",
"population": 255000000
},
{
"country": "Nigeria",
"population": 240000000
},
{
"country": "Brazil",
"population": 217000000
},
{
"country": "Bangladesh",
"population": 175000000
},
{
"country": "Russia",
"population": 144000000
},
{
"country": "Ethiopia",
"population": 135000000
}
],
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "country",
"type": "nominal",
"highlight": [
"India",
"China"
]
}
},
"legend": {
"show": false
},
"annotations": [
{
"type": "refline",
"x": 1000000000,
"label": "1 billion",
"labelPlacement": "outside",
"style": "dashed"
}
],
"chrome": {
"title": "India and China are the only countries above 1 billion people",
"subtitle": "Population by country, 2025 estimates"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "country",
"type": "nominal",
"highlight": [
"India",
"China"
]
}
},
"legend": {
"show": false
},
"annotations": [
{
"type": "refline",
"x": 1000000000,
"label": "1 billion",
"labelPlacement": "outside",
"style": "dashed"
}
],
"chrome": {
"title": "India and China are the only countries above 1 billion people",
"subtitle": "Population by country, 2025 estimates"
}
};
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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "country",
"type": "nominal",
"highlight": [
"India",
"China"
]
}
},
"legend": {
"show": false
},
"annotations": [
{
"type": "refline",
"x": 1000000000,
"label": "1 billion",
"labelPlacement": "outside",
"style": "dashed"
}
],
"chrome": {
"title": "India and China are the only countries above 1 billion people",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "country",
"type": "nominal",
"highlight": [
"India",
"China"
]
}
},
"legend": {
"show": false
},
"annotations": [
{
"type": "refline",
"x": 1000000000,
"label": "1 billion",
"labelPlacement": "outside",
"style": "dashed"
}
],
"chrome": {
"title": "India and China are the only countries above 1 billion people",
"subtitle": "Population by country, 2025 estimates"
}
};
</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 = [/* 10 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "population",
"type": "quantitative",
"axis": {
"title": "Population",
"format": ".2~s"
}
},
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"color": {
"field": "country",
"type": "nominal",
"highlight": [
"India",
"China"
]
}
},
"legend": {
"show": false
},
"annotations": [
{
"type": "refline",
"x": 1000000000,
"label": "1 billion",
"labelPlacement": "outside",
"style": "dashed"
}
],
"chrome": {
"title": "India and China are the only countries above 1 billion people",
"subtitle": "Population by country, 2025 estimates"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Mirrored bars
mark: { type: "bar", mirror: true } draws two groups back to back from a center column of category labels, the layout of a population pyramid. The color field must have exactly two values, and the first goes on the left. The population pyramid page covers the layout and how to play it through the years.
Related
- Column for categories that run along time.
- Grouped and stacked bars for splitting each bar into series.
- Population pyramid for two groups back to back.
- Stacked to 100% when the share of each part matters more than the totals.
- Marks reference lists the channels and options each mark type takes.