Skip to content
OpenChart

Column

Compare one value across ordered categories such as months or years with vertical columns, then annotate, color by sign and label them.

A column chart compares one number across a short, ordered set of categories, with each category on the x axis and the value as the height of its column. It suits time periods such as months or years.

Use a bar chart instead when the category names are long, since horizontal bars keep them at full length. Use a line chart when the periods are many and the trend matters more than each value. The spec is the same as a bar chart; only the channels swap.

A basic column chart

Set mark to "bar", put the category on x and the number on y. Here labels prints every value above its column, formatted with a d3-format string.

JanFebMarAprMayJunJulAugSepOctNovDec050100150200250300350400Jobs added (thousands)35327530317527220611414225412227256US employers added jobs in every month of 2024Monthly US nonfarm payroll additions, 2024 (thousands of jobs)JanFebMarAprMayJunJulAugSepOctNovDec050100150200250300350400Jobs added (thousands)35327530317527220611414225412227256US employers added jobs in every month of 2024Monthly US nonfarm payroll additions, 2024 (thousands of jobs)
US payroll additions by month. Source: Bureau of Labor Statistics.
{
  "mark": "bar",
  "data": [
    {
      "month": "Jan",
      "jobs": 353
    },
    {
      "month": "Feb",
      "jobs": 275
    },
    {
      "month": "Mar",
      "jobs": 303
    },
    {
      "month": "Apr",
      "jobs": 175
    },
    {
      "month": "May",
      "jobs": 272
    },
    {
      "month": "Jun",
      "jobs": 206
    },
    {
      "month": "Jul",
      "jobs": 114
    },
    {
      "month": "Aug",
      "jobs": 142
    },
    {
      "month": "Sep",
      "jobs": 254
    },
    {
      "month": "Oct",
      "jobs": 12
    },
    {
      "month": "Nov",
      "jobs": 227
    },
    {
      "month": "Dec",
      "jobs": 256
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ",.0f"
  },
  "chrome": {
    "title": "US employers added jobs in every month of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ",.0f"
  },
  "chrome": {
    "title": "US employers added jobs in every month of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};

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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ",.0f"
  },
  "chrome": {
    "title": "US employers added jobs in every month of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ",.0f"
  },
  "chrome": {
    "title": "US employers added jobs in every month of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ",.0f"
  },
  "chrome": {
    "title": "US employers added jobs in every month of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Annotating a column chart

A refline annotation draws a reference value in data units, here the yearly average on y. A text annotation attaches a note to one category by naming it on x, and connector: true connects the note to the column.

JanFebMarAprMayJunJulAugSepOctNovDec050100150200250300350400Jobs added (thousands)Hurricanes and strikesstalled October hiringavg: 216KOctober's job gain was the smallest of 2024Monthly US nonfarm payroll additions, 2024 (thousands of jobs)JanFebMarAprMayJunJulAugSepOctNovDec050100150200250300350400Jobs added (thousands)Hurricanes and strikesstalled October hiringavg: 216KOctober's job gain was the smallest of 2024Monthly US nonfarm payroll additions, 2024 (thousands of jobs)
Payroll additions with an average line and a note. Source: Bureau of Labor Statistics.
{
  "mark": "bar",
  "data": [
    {
      "month": "Jan",
      "jobs": 353
    },
    {
      "month": "Feb",
      "jobs": 275
    },
    {
      "month": "Mar",
      "jobs": 303
    },
    {
      "month": "Apr",
      "jobs": 175
    },
    {
      "month": "May",
      "jobs": 272
    },
    {
      "month": "Jun",
      "jobs": 206
    },
    {
      "month": "Jul",
      "jobs": 114
    },
    {
      "month": "Aug",
      "jobs": 142
    },
    {
      "month": "Sep",
      "jobs": 254
    },
    {
      "month": "Oct",
      "jobs": 12
    },
    {
      "month": "Nov",
      "jobs": 227
    },
    {
      "month": "Dec",
      "jobs": 256
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "text",
      "x": "Oct",
      "y": 12,
      "text": "Hurricanes and strikes\nstalled October hiring",
      "anchor": "top",
      "offset": {
        "dy": -170
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 216,
      "label": "avg: 216K",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1,
      "labelPlacement": "outside"
    }
  ],
  "chrome": {
    "title": "October's job gain was the smallest of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "text",
      "x": "Oct",
      "y": 12,
      "text": "Hurricanes and strikes\nstalled October hiring",
      "anchor": "top",
      "offset": {
        "dy": -170
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 216,
      "label": "avg: 216K",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1,
      "labelPlacement": "outside"
    }
  ],
  "chrome": {
    "title": "October's job gain was the smallest of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};

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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "text",
      "x": "Oct",
      "y": 12,
      "text": "Hurricanes and strikes\nstalled October hiring",
      "anchor": "top",
      "offset": {
        "dy": -170
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 216,
      "label": "avg: 216K",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1,
      "labelPlacement": "outside"
    }
  ],
  "chrome": {
    "title": "October's job gain was the smallest of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "text",
      "x": "Oct",
      "y": 12,
      "text": "Hurricanes and strikes\nstalled October hiring",
      "anchor": "top",
      "offset": {
        "dy": -170
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 216,
      "label": "avg: 216K",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1,
      "labelPlacement": "outside"
    }
  ],
  "chrome": {
    "title": "October's job gain was the smallest of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "jobs",
      "type": "quantitative",
      "axis": {
        "title": "Jobs added (thousands)"
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "text",
      "x": "Oct",
      "y": 12,
      "text": "Hurricanes and strikes\nstalled October hiring",
      "anchor": "top",
      "offset": {
        "dy": -170
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 216,
      "label": "avg: 216K",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1,
      "labelPlacement": "outside"
    }
  ],
  "chrome": {
    "title": "October's job gain was the smallest of 2024",
    "subtitle": "Monthly US nonfarm payroll additions, 2024 (thousands of jobs)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Positive and negative values

Columns grow up from zero for positive values and down for negative ones. A color channel on a field in the data splits the two cases, and a scale with a domain and a range pairs each value with a color. A solid refline at y: 0 marks the baseline.

These years are a decade apart until the last, five years after 2020, so x is type: "temporal" with axis.format: "%Y": a time scale spaces the columns by date, where an ordinal axis would draw those five years as wide as a decade.

1900192019401960198020002020−0.6−0.4−0.2+0.0+0.2+0.4+0.6+0.8+1.0+1.2Temperature anomaly (°C)CoolerWarmerEvery reading since 1970 is above the1951–1980 averageGlobal surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)1900192019401960198020002020−0.6−0.4−0.2+0.0+0.2+0.4+0.6+0.8+1.0+1.2Temperature anomaly (°C)CoolerWarmerEvery reading since 1970 is above the1951–1980 averageGlobal surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)
Global temperature anomaly, 1900 to 2025. Source: NASA GISS Surface Temperature Analysis (GISTEMP v4).
{
  "mark": "bar",
  "data": [
    {
      "year": "1900",
      "anomaly": -0.08,
      "trend": "Cooler"
    },
    {
      "year": "1910",
      "anomaly": -0.43,
      "trend": "Cooler"
    },
    {
      "year": "1920",
      "anomaly": -0.27,
      "trend": "Cooler"
    },
    {
      "year": "1930",
      "anomaly": -0.16,
      "trend": "Cooler"
    },
    {
      "year": "1940",
      "anomaly": 0.12,
      "trend": "Warmer"
    },
    {
      "year": "1950",
      "anomaly": -0.17,
      "trend": "Cooler"
    },
    // … 8 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Temperature anomaly (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Temperature anomaly (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
};

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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Temperature anomaly (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
};
</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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Temperature anomaly (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
};
</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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Temperature anomaly (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Long category names

When category names do not fit horizontally, the axis angles them, then truncates them, before it drops any ticks. If the names still crowd the axis, switch to a bar chart.

Research & DevelopmentMarketingHuman ResourcesIT InfrastructureCustomer SuccessLegal & ComplianceSupply Chain010203040Budget ($M)42312824191512R&D has the largest budgetAnnual departmental budget allocations, FY 2025 ($M)Research & DevelopmentMarketingHuman ResourcesIT InfrastructureCustomer SuccessLegal & ComplianceSupply Chain010203040Budget ($M)42312824191512R&D has the largest budgetAnnual departmental budget allocations, FY 2025 ($M)
Departmental budgets. Source: Illustrative data.
{
  "mark": "bar",
  "data": [
    {
      "department": "Research & Development",
      "budget": 42
    },
    {
      "department": "Marketing",
      "budget": 31
    },
    {
      "department": "Human Resources",
      "budget": 28
    },
    {
      "department": "IT Infrastructure",
      "budget": 24
    },
    {
      "department": "Customer Success",
      "budget": 19
    },
    {
      "department": "Legal & Compliance",
      "budget": 15
    },
    {
      "department": "Supply Chain",
      "budget": 12
    }
  ],
  "encoding": {
    "x": {
      "field": "department",
      "type": "nominal"
    },
    "y": {
      "field": "budget",
      "type": "quantitative",
      "axis": {
        "title": "Budget ($M)"
      }
    }
  },
  "chrome": {
    "title": "R&D has the largest budget",
    "subtitle": "Annual departmental budget allocations, FY 2025 ($M)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 7 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "department",
      "type": "nominal"
    },
    "y": {
      "field": "budget",
      "type": "quantitative",
      "axis": {
        "title": "Budget ($M)"
      }
    }
  },
  "chrome": {
    "title": "R&D has the largest budget",
    "subtitle": "Annual departmental budget allocations, FY 2025 ($M)"
  }
};

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 = [/* 7 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "department",
      "type": "nominal"
    },
    "y": {
      "field": "budget",
      "type": "quantitative",
      "axis": {
        "title": "Budget ($M)"
      }
    }
  },
  "chrome": {
    "title": "R&D has the largest budget",
    "subtitle": "Annual departmental budget allocations, FY 2025 ($M)"
  }
};
</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 = [/* 7 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "department",
      "type": "nominal"
    },
    "y": {
      "field": "budget",
      "type": "quantitative",
      "axis": {
        "title": "Budget ($M)"
      }
    }
  },
  "chrome": {
    "title": "R&D has the largest budget",
    "subtitle": "Annual departmental budget allocations, FY 2025 ($M)"
  }
};
</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 = [/* 7 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "department",
      "type": "nominal"
    },
    "y": {
      "field": "budget",
      "type": "quantitative",
      "axis": {
        "title": "Budget ($M)"
      }
    }
  },
  "chrome": {
    "title": "R&D has the largest budget",
    "subtitle": "Annual departmental budget allocations, FY 2025 ($M)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Labels at the column ends

labels.placement: "outside" puts each value label at the end of its column, so labels follow the positive or negative end without changing the order of the data. sort: null keeps the order as authored, and mark.size sets an explicit column size.

ResearchOperationsTrainingContingencyAdjustments050100Millions128.4M75.2M8.6M0.0M−24.3MPositive and negative values follow theirendpointsBudget allocations, millionsResearchOperationsTrainingContingencyAdjustments050100Millions128.4M75.2M8.6M0.0M−24.3MPositive and negative values follow theirendpointsBudget allocations, millions
Signed values with outside labels. Source: Illustrative data.
{
  "mark": {
    "type": "bar",
    "size": 28
  },
  "data": [
    {
      "name": "Research",
      "value": 128.4
    },
    {
      "name": "Operations",
      "value": 75.2
    },
    {
      "name": "Training",
      "value": 8.6
    },
    {
      "name": "Contingency",
      "value": 0
    },
    {
      "name": "Adjustments",
      "value": -24.3
    }
  ],
  "encoding": {
    "x": {
      "field": "name",
      "type": "nominal",
      "sort": null,
      "scale": {
        "paddingInner": 0.4
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "axis": {
        "title": "Millions"
      }
    }
  },
  "labels": {
    "placement": "outside",
    "density": "all",
    "format": ".1f",
    "suffix": "M"
  },
  "legend": {
    "show": false
  },
  "chrome": {
    "title": "Positive and negative values follow their endpoints",
    "subtitle": "Budget allocations, millions"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const spec: ChartSpec = {
  "mark": {
    "type": "bar",
    "size": 28
  },
  "data": [/* 5 rows, as in the Spec tab; Copy includes them */],
  "encoding": {
    "x": {
      "field": "name",
      "type": "nominal",
      "sort": null,
      "scale": {
        "paddingInner": 0.4
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "axis": {
        "title": "Millions"
      }
    }
  },
  "labels": {
    "placement": "outside",
    "density": "all",
    "format": ".1f",
    "suffix": "M"
  },
  "legend": {
    "show": false
  },
  "chrome": {
    "title": "Positive and negative values follow their endpoints",
    "subtitle": "Budget allocations, millions"
  }
};

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 spec: ChartSpec = {
  "mark": {
    "type": "bar",
    "size": 28
  },
  "data": [/* 5 rows, as in the Spec tab; Copy includes them */],
  "encoding": {
    "x": {
      "field": "name",
      "type": "nominal",
      "sort": null,
      "scale": {
        "paddingInner": 0.4
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "axis": {
        "title": "Millions"
      }
    }
  },
  "labels": {
    "placement": "outside",
    "density": "all",
    "format": ".1f",
    "suffix": "M"
  },
  "legend": {
    "show": false
  },
  "chrome": {
    "title": "Positive and negative values follow their endpoints",
    "subtitle": "Budget allocations, millions"
  }
};
</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 spec: ChartSpec = {
  "mark": {
    "type": "bar",
    "size": 28
  },
  "data": [/* 5 rows, as in the Spec tab; Copy includes them */],
  "encoding": {
    "x": {
      "field": "name",
      "type": "nominal",
      "sort": null,
      "scale": {
        "paddingInner": 0.4
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "axis": {
        "title": "Millions"
      }
    }
  },
  "labels": {
    "placement": "outside",
    "density": "all",
    "format": ".1f",
    "suffix": "M"
  },
  "legend": {
    "show": false
  },
  "chrome": {
    "title": "Positive and negative values follow their endpoints",
    "subtitle": "Budget allocations, millions"
  }
};
</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 spec: ChartSpec = {
  "mark": {
    "type": "bar",
    "size": 28
  },
  "data": [/* 5 rows, as in the Spec tab; Copy includes them */],
  "encoding": {
    "x": {
      "field": "name",
      "type": "nominal",
      "sort": null,
      "scale": {
        "paddingInner": 0.4
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "axis": {
        "title": "Millions"
      }
    }
  },
  "labels": {
    "placement": "outside",
    "density": "all",
    "format": ".1f",
    "suffix": "M"
  },
  "legend": {
    "show": false
  },
  "chrome": {
    "title": "Positive and negative values follow their endpoints",
    "subtitle": "Budget allocations, millions"
  }
};

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: "bar" Draws bars. With the category on x and the number on y they run vertically.
encoding.x The category channel. Use type: "nominal" for names, "ordinal" for evenly spaced ordered values such as months, and "temporal" for dates or years with uneven gaps. sort: null keeps the data order.
encoding.y The value channel, with axis.title and axis.format for the axis.
encoding.color Colors columns by a field. A scale with domain and range sets the colors.
labels density, format, placement and suffix control the value labels.
annotations A refline marks a value; a text note points at one category.
  • Bar for the horizontal form of the same chart.
  • Grouped and stacked bars for splitting each column into series.
  • Line for a trend across many periods.
  • Histogram for counting how often values fall in each range.
  • Marks reference lists the channels and options each mark type takes.