Axes
Tick formats, tick counts, gridlines, axis titles and long labels, set on the encoding channel.
Each position channel has an axis object for its title, tick format, tick count and gridlines.
{
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"data": [
{
"month": "Jan",
"high": 9.3
},
{
"month": "Feb",
"high": 9.2
},
{
"month": "Mar",
"high": 10.8
},
{
"month": "Apr",
"high": 19
},
{
"month": "May",
"high": 22.1
},
{
"month": "Jun",
"high": 25.4
},
{
"month": "Jul",
"high": 30.2
},
{
"month": "Aug",
"high": 27.6
},
{
"month": "Sep",
"high": 24.3
},
{
"month": "Oct",
"high": 19.3
},
{
"month": "Nov",
"high": 11.8
},
{
"month": "Dec",
"high": 9.8
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C",
"tickCount": 4,
"grid": true
}
}
},
"chrome": {
"title": "Central Park's average daily high peaked at 30.2°C in July 2023",
"subtitle": "Average of each day's high temperature by month, Central Park, New York, 2023 (°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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C",
"tickCount": 4,
"grid": true
}
}
},
"chrome": {
"title": "Central Park's average daily high peaked at 30.2°C in July 2023",
"subtitle": "Average of each day's high temperature by month, Central Park, New York, 2023 (°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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C",
"tickCount": 4,
"grid": true
}
}
},
"chrome": {
"title": "Central Park's average daily high peaked at 30.2°C in July 2023",
"subtitle": "Average of each day's high temperature by month, Central Park, New York, 2023 (°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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C",
"tickCount": 4,
"grid": true
}
}
},
"chrome": {
"title": "Central Park's average daily high peaked at 30.2°C in July 2023",
"subtitle": "Average of each day's high temperature by month, Central Park, New York, 2023 (°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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C",
"tickCount": 4,
"grid": true
}
}
},
"chrome": {
"title": "Central Park's average daily high peaked at 30.2°C in July 2023",
"subtitle": "Average of each day's high temperature by month, Central Park, New York, 2023 (°C)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Format, tick count and gridlines
format takes a d3-format string for numbers: ".0f" rounds to whole numbers, ",.0f" adds thousands separators and "$,.0f" adds a dollar sign. Temporal axes take a d3-time-format string instead, such as "%Y" or "%b '%y". labelSuffix: "°C" puts the unit on every tick. tickCount: 4 asks for about four ticks, and the engine rounds to clean values near that count. grid: true draws a gridline at each tick.
Long category labels
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"country": "India",
"gdpPerCapita": 9818
},
{
"country": "China",
"gdpPerCapita": 23846
},
{
"country": "United States",
"gdpPerCapita": 75489
},
{
"country": "Indonesia",
"gdpPerCapita": 14470
},
{
"country": "Brazil",
"gdpPerCapita": 19652
},
{
"country": "Mexico",
"gdpPerCapita": 22040
},
{
"country": "Japan",
"gdpPerCapita": 46107
},
{
"country": "Germany",
"gdpPerCapita": 63676
},
{
"country": "United Kingdom",
"gdpPerCapita": 53139
},
{
"country": "France",
"gdpPerCapita": 54799
},
{
"country": "South Korea",
"gdpPerCapita": 55071
},
{
"country": "Canada",
"gdpPerCapita": 58321
}
],
"encoding": {
"x": {
"field": "country",
"type": "nominal",
"sort": "-y"
},
"y": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person",
"format": "$,.0f"
}
}
},
"labels": false,
"chrome": {
"title": "GDP per person in the US is almost eight times India's",
"subtitle": "GDP per person, PPP-adjusted, constant international dollars, about 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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "country",
"type": "nominal",
"sort": "-y"
},
"y": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person",
"format": "$,.0f"
}
}
},
"labels": false,
"chrome": {
"title": "GDP per person in the US is almost eight times India's",
"subtitle": "GDP per person, PPP-adjusted, constant international dollars, about 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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "country",
"type": "nominal",
"sort": "-y"
},
"y": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person",
"format": "$,.0f"
}
}
},
"labels": false,
"chrome": {
"title": "GDP per person in the US is almost eight times India's",
"subtitle": "GDP per person, PPP-adjusted, constant international dollars, about 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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "country",
"type": "nominal",
"sort": "-y"
},
"y": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person",
"format": "$,.0f"
}
}
},
"labels": false,
"chrome": {
"title": "GDP per person in the US is almost eight times India's",
"subtitle": "GDP per person, PPP-adjusted, constant international dollars, about 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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "country",
"type": "nominal",
"sort": "-y"
},
"y": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person",
"format": "$,.0f"
}
}
},
"labels": false,
"chrome": {
"title": "GDP per person in the US is almost eight times India's",
"subtitle": "GDP per person, PPP-adjusted, constant international dollars, about 2022"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Twelve country names under twelve columns do not fit side by side. The engine checks the labels for overlap and, when they collide, sets them at an angle. labelAngle sets the angle yourself. When names are long, the better fix is usually the bar chart, which gives each name its own row.
Hiding an axis
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"state": "California",
"rate": 5.3
},
{
"state": "Delaware",
"rate": 5.4
},
{
"state": "District of Columbia",
"rate": 5.7
},
{
"state": "Illinois",
"rate": 5.1
},
{
"state": "Michigan",
"rate": 5.1
},
{
"state": "Nevada",
"rate": 5.1
},
{
"state": "Oregon",
"rate": 5.2
},
{
"state": "Washington",
"rate": 5.1
}
],
"encoding": {
"y": {
"field": "state",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f",
"suffix": "%"
},
"chrome": {
"title": "DC had the highest unemployment rate, 5.7%, followed by Delaware at 5.4%",
"subtitle": "Unemployment rate, seasonally adjusted, latest month (% of labor force). The eight highest of the 50 states and DC."
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 8 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"y": {
"field": "state",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f",
"suffix": "%"
},
"chrome": {
"title": "DC had the highest unemployment rate, 5.7%, followed by Delaware at 5.4%",
"subtitle": "Unemployment rate, seasonally adjusted, latest month (% of labor force). The eight highest of the 50 states and DC."
}
};
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 = [/* 8 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"y": {
"field": "state",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f",
"suffix": "%"
},
"chrome": {
"title": "DC had the highest unemployment rate, 5.7%, followed by Delaware at 5.4%",
"subtitle": "Unemployment rate, seasonally adjusted, latest month (% of labor force). The eight highest of the 50 states and DC."
}
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"y": {
"field": "state",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f",
"suffix": "%"
},
"chrome": {
"title": "DC had the highest unemployment rate, 5.7%, followed by Delaware at 5.4%",
"subtitle": "Unemployment rate, seasonally adjusted, latest month (% of labor force). The eight highest of the 50 states and DC."
}
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"y": {
"field": "state",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f",
"suffix": "%"
},
"chrome": {
"title": "DC had the highest unemployment rate, 5.7%, followed by Delaware at 5.4%",
"subtitle": "Unemployment rate, seasonally adjusted, latest month (% of labor force). The eight highest of the 50 states and DC."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
axis: false removes an axis and the space it reserves. Do it only when something else carries the numbers: here each bar has a value label from labels: { format: ".1f", suffix: "%" }, so a scale under the bars would repeat them.
Fields that matter
| Field | What it does |
|---|---|
axis.title |
The axis title. None is drawn unless you set one. |
axis.format |
d3-format for numbers, d3-time-format for dates. |
axis.labelSuffix |
Text appended to every tick label, such as a unit. |
axis.tickCount |
About how many ticks to draw. |
axis.values |
Exact tick values. |
axis.grid |
Gridlines at each tick. |
axis.labelAngle |
Rotation for tick labels, such as -45. |
axis: false |
No axis and no space reserved for it. |
Related
- Column uses formatted value axes on categories.
- Line covers date tick formats.
- Field types and scales covers what the axis is drawn from.
- Encoding reference lists every axis option.