Skip to content
OpenChart

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.

0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.5B1.4B347M285M255M240M217M175M144M135MThe ten most populous countriesPopulation by country, 2025 estimates0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.5B1.4B347M285M255M240M217M175M144M135MThe ten most populous countriesPopulation by country, 2025 estimates
Population by country. Source: UN Population Division, World Population Prospects 2024.
{
  "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.

0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.5B1.4B347M285M255M240M217M175M144M135MIndia and China each have about four times theUS populationPopulation by country, 2025 estimates0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.5B1.4B347M285M255M240M217M175M144M135MIndia and China each have about four times theUS populationPopulation by country, 2025 estimates
Population by country, ranked. Source: UN Population Division, World Population Prospects 2024.
{
  "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.

20192020202120222023050100150200250300350Capacity added (GW)SolarWindHydroSolar added more capacity than wind and hydrocombined in every yearGlobal renewable capacity additions by source, 2019-2023 (gigawatts)20192020202120222023050100150200250300350Capacity added (GW)SolarWindHydroSolar added more capacity than wind and hydrocombined in every yearGlobal renewable capacity additions by source, 2019-2023 (gigawatts)
Renewable capacity added by source. Source: International Energy Agency.
{
  "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.

020406080100Share of spending (%)Under $30K$30K - $50K$50K - $80K$80K - $120KOver $120K40181512153516181021321419926301217833271014643HousingFoodTransportHealthcareOtherLower-income households spend a larger shareon housingShare of annual household expenditure by category and income bracket, 2022 (%)020406080100Share of spending (%)Under $30K$30K - $50K$50K - $80K$80K - $120KOver $120K40181512153516181021321419926301217833271014643HousingFoodTransportHealthcareOtherLower-income households spend a larger shareon housingShare of annual household expenditure by category and income bracket, 2022 (%)
Household spending by income bracket. Source: Bureau of Labor Statistics, Consumer Expenditure Survey.
{
  "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.

+0+10+20+30+40Total return (%)Communication ServicesInformation TechnologyConsumer DiscretionaryFinancialsUtilitiesIndustrialsConsumer StaplesReal EstateEnergyHealth CareMaterials+39.7+37.6+29.5+28.9+20.1+16.2+12.2+2.0+1.9+1.1−1.2Only materials ended 2024 in the redS&P 500 total return by sector, full year 2024+0+10+20+30+40Total return (%)Communication ServicesInformation TechnologyConsumer DiscretionaryFinancialsUtilitiesIndustrialsConsumer StaplesReal EstateEnergyHealth CareMaterials+39.7+37.6+29.5+28.9+20.1+16.2+12.2+2.0+1.9+1.1−1.2Only materials ended 2024 in the redS&P 500 total return by sector, full year 2024
S&P 500 return by sector. Source: S&P Global.
{
  "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.

0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1 billion1.5B1.4B347M285M255M240M217M175M144M135MIndia and China are the only countries above 1billion peoplePopulation by country, 2025 estimates0500M1B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1 billion1.5B1.4B347M285M255M240M217M175M144M135MIndia and China are the only countries above 1billion peoplePopulation by country, 2025 estimates
India and China against the 1 billion mark. Source: UN Population Division, World Population Prospects 2024.
{
  "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.