A JSON file or endpoint

A table can also arrive after the page has loaded, from a JSON file or from an API endpoint. The response is turned into a table, and the chart is rendered from that table. This needs JavaScript and a server: a page opened from your computer, with an address that starts with file://, cannot read a file. Without JavaScript, only the header row written in the figure remains, if there is one. See Accessibility.

If the figure has data-chart-src with the address of a JSON file, Tablechart reads the file and makes a table from it. If you also had an HTML table in there, that is then replaced, unless it has a header row: see Missing names.

Apples sold by variety
HTML
<figure data-chart="column" data-chart-src="/tablechart/data/apples.json">
	<figcaption><b>Apples sold by variety</b></figcaption>
</figure>

JSON structure

The rows can be written in two ways. As a list of rows, each with its label and its value, or its values if there are more value columns:

JSON
[
	{ "label": "Jan 2025", "value": "12.4" },
	{ "label": "Feb 2025", "value": "-3.1" },
	{ "label": "Mar 2025", "value": "8.7" }
]

Or as names with their value, or their values:

JSON
{ "Spring": "58", "Summer": "72", "Autumn": "70" }
JSON
{ "Elstar": ["118", "131"], "Jonagold": ["140", "135"], "Braeburn": ["109", "121"] }

If you want to name the columns too, put the rows, in either way, in an object with rows. series has the names of the value columns, and label the name of the first column:

JSON
{
	"label": "Variety",
	"series": ["2022", "2026"],
	"rows": [
		{ "label": "Elstar", "values": ["118", "131"] },
		{ "label": "Jonagold", "values": ["140", "135"] },
		{ "label": "Braeburn", "values": ["109", "121"] }
	]
}

If you want a value to keep its decimals, write it as a string, such as "1.0". A row in a list with "kind": "total" is a total. A row with "short" has a short name under the chart, and a row with "note" a note, such as { "label": "March 2024", "value": "34.1", "note": "Share issue" }. The table then gets a column of notes, named by "note" in the object with rows, or else Note. See Notes. In names with values, names that are whole numbers, such as "2022", come first and from low to high, whatever their order in the file. Other names keep their order.

Missing names

If the file has only rows (so data only, like the top 3 examples above), without label and series, the table has no header row, and screen readers cannot say what the columns are.

Then write just the header row in the figure yourself, in a table with only a thead. Tablechart keeps it and adds the rows from the file. A header row in the figure goes before the names in the file. Screen readers use the header row to name the columns, and with data-i18n on a th, a translation script translates it.

HTML
<figure data-chart="column" data-chart-src="/api/profit-per-month/">
	<figcaption><b>Profit per month</b></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Profit</th></tr>
		</thead>
	</table>
</figure>

What if my API gives other JSON?

Tablechart reads the structures above. Many APIs send their data in a structure of their own, for example with every column in a list of its own:

JSON
{
	"months": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
	"rain": [67, 56, 53, 41, 58, 65]
}

The names and the values are in two separate lists, so it is not clear which value belongs to which name. A transform puts them together. A transform is a small function of your own. It gets the JSON, and gives back rows in one of the structures above. This one makes a row of each month, with the rain of that month as its value:

JS
const perMonth = (data) =>
	data.months.map((month, index) => ({ label: month, value: data.rain[index] }));

init({ transforms: { perMonth } });

There are two parts. In your JavaScript, the transforms option of init() gives the transform a name, once for the whole page. In the HTML, the attribute data-chart-transform on a figure sets which transform that chart uses, by that name:

Rain per month
Month Rain (mm)
HTML
<figure data-chart="column" data-chart-src="/tablechart/data/rain.json" data-chart-transform="perMonth">
	<figcaption><b>Rain per month</b></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Rain (mm)</th></tr>
		</thead>
	</table>
</figure>

The file has no column names. A screen reader reads the chart as a table, and uses the header row to say what each value is: "Rain (mm), 67" instead of only "67". So the header row is written in the figure, and the rows of the transform go under it. See Missing names.

One transform for every chart

If every endpoint on a page has the same structure, the transform option sets one transform for every figure with data-chart-src:

JS
init({ transforms: { perMonth }, transform: "perMonth" });

A figure with data-chart-transform uses its own transform instead. If a file is already in one of the structures above, data-chart-transform="none" on its figure reads the file as it is.

What a transform does

  • It runs after the response is read as JSON, and before the table is made.
  • It gives back one of the structures above. Anything else, such as a text, a number or nothing, counts as an error: see When the data cannot load.
  • It runs at once, so it cannot fetch a second response.
  • A header row written in the figure is kept, and the rows of the transform go under it.

A real example: rain from a weather API

Open-Meteo is a free weather API that needs no key. This address asks it for the rain of every day in 2025, at a place in the Netherlands:

URL
https://archive-api.open-meteo.com/v1/archive?latitude=51.9&longitude=5.4&start_date=2025-01-01&end_date=2025-12-31&daily=precipitation_sum&timezone=Europe%2FAmsterdam

The response

The response starts with information about the place, such as where it is and its time zone. The data itself is in daily, in two lists: the 365 dates of the year, and the rain on each of those days, in millimetres. Shortened, the response looks like this:

JSON
{
	"latitude": 51.915638,
	"longitude": 5.4395604,
	"timezone": "Europe/Amsterdam",
	"daily_units": { "time": "iso8601", "precipitation_sum": "mm" },
	"daily": {
		"time": ["2025-01-01", "2025-01-02", "2025-01-03", …],
		"precipitation_sum": [12.0, 4.0, 1.8, …]
	}
}

What Tablechart expects

For a chart with the rain per month, Tablechart expects twelve rows, each with a name and a value:

JSON
[
	{ "label": "Jan", "value": "95.4" },
	{ "label": "Feb", "value": "37.6" },
	…
]

The response does not fit in two ways. The dates and the amounts are in two lists, so it is not clear which amount belongs to which date. And the response has days, where the chart has months.

The transform

The transform solves both. It goes through the days, adds the rain of each day to its month, and then makes a row of each month:

JS
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

const rainPerMonth = (data) => {
	const totals = Array(12).fill(0);
	data.daily.time.forEach((day, index) => {
		const month = Number(day.slice(5, 7)) - 1; // "2025-03-14" is month 2, March
		totals[month] += data.daily.precipitation_sum[index] ?? 0;
	});
	return totals.map((total, month) => ({ label: MONTHS[month], value: total.toFixed(1) }));
};

init({ transforms: { rainPerMonth } });

A day without data is null in the response, and counts as no rain. toFixed(1) keeps one decimal, as a string, so the numbers in the chart have one decimal too.

The chart

The response has no column names either. So that a screen reader can say what each value is, the header row is written in the figure, as in the example above:

Rain per month, 2025
millimetres
Month Rain (mm)
HTML
<figure data-chart="column" data-chart-src="https://archive-api.open-meteo.com/v1/archive?latitude=51.9&amp;longitude=5.4&amp;start_date=2025-01-01&amp;end_date=2025-12-31&amp;daily=precipitation_sum&amp;timezone=Europe%2FAmsterdam" data-chart-transform="rainPerMonth">
	<figcaption><b>Rain per month, 2025</b><br><span>millimetres</span></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Rain (mm)</th></tr>
		</thead>
	</table>
</figure>

With a summary

A summary works the same with data from an API. Its sentences are written in the figure, with placeholders, and the summary fills them in once the table is made. The total, the wettest and the driest month then come from the response:

Rain per month, 2025
millimetres
Month Rain (mm)

In 2025, {total} mm of rain fell. The wettest month was {highest.name}, with {highest.value} mm. The driest was {lowest.name}, with {lowest.value} mm.

HTML
<figure data-chart="column" data-chart-summary="always" data-chart-src="https://archive-api.open-meteo.com/v1/archive?latitude=51.9&amp;longitude=5.4&amp;start_date=2025-01-01&amp;end_date=2025-12-31&amp;daily=precipitation_sum&amp;timezone=Europe%2FAmsterdam" data-chart-transform="rainPerMonth">
	<figcaption><b>Rain per month, 2025</b><br><span>millimetres</span></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Rain (mm)</th></tr>
		</thead>
	</table>
	<div class="chart-summary">
		<p>In 2025, {total} mm of rain fell. The wettest month was {highest.name}, with {highest.value} mm. The driest was {lowest.name}, with {lowest.value} mm.</p>
	</div>
</figure>

With data-chart-summary="always", the summary is also shown on the page. Without it, only a screen reader reads it, right after the title of the chart.

Weather data by Open-Meteo.com, under CC BY 4.0.

When the data cannot load

A chart is only built from complete data. If the table cannot be made from the response, the chart is not built, and the figure shows a short message in its place. This happens if:

  • the file or endpoint cannot be fetched: a network error, a 404, or a response that is not valid JSON
  • the transform in data-chart-transform, or in the transform option, is not in the transforms option
  • the transform throws an error, or gives back something that is not one of the structures
  • the rows fail one of the checks below

The message is "The data for this chart could not be loaded." The loaderror option sets another text. It has role="status", so a screen reader announces it without interrupting, and the translation key tablechart-load-error, so a translation script translates it. In the console, a message shows the figure, the step that failed (fetch, parse, transform or check), and the cause. The table written in the figure stays as it was.

Checks

Before a chart is rendered, the rows are checked for:

  • at least one row
  • a name in every row
  • the same number of values in every row, as many as there are columns
  • a number, a text or null in every value. As in a table, a value without a number, such as "n/a", is missing.

The message in the console shows the row that failed. The same checks apply to a file that is read without a transform.

No fallback

If a transform fails, the response is not read as it is. A response in the wrong structure could be read as a different table, and a wrong chart is worse than no chart. If you want the response read as it is, set data-chart-transform="none" on the figure.

Next

For a table in the page, or from Markdown, see Data in the page. For what a screen reader gets from a chart, see Accessibility.