Summary
Sometimes a fallback table is just too long to follow or to print.
Why?
Tablechart keeps the original (or JSON generated) tables inside the figure elements, alongside the generated charts. Screenreaders can read it. But sometimes you will want to summarize the data for easier consumption.
Imagine a nice line chart. Imagine that it has 60 data points, one for each month. Tablechart will suppress the values inside the chart itself if there is a danger of overlapping values, and tooltips may help in that case. But screen readers can not do that. And they can not reasd the chart. They rely on the table inside the figure to access the data.
A screenreader will still have access to the full table inside the figure, ensuring that all data points are available for those who need them. However, 60 rows can be overwhelming to navigate sequentially, which is why a summary is useful.
Charts can be used in a PDF, but only sighted users may understand them. If the PDF is on a computer, screen readers can read the text, but not the chart.
This is why Tablechart keeps the table available so that it may be shown under (or near) the chart. However, such a table can take up a lot of space. This makes a summary useful for conveying the key information without overwhelming the layout.
A summary says in a few sentences what matters, such as the highest and the lowest value. You write the sentences. Tablechart fills in the numbers from the table, formatted as on the chart.
What a summary needs
A chart only gets a summary if two things are there:
- The summary script, loaded and started before
init(). A page without it loads nothing extra. See Start the summary. - Sentences in the figure: a
divwith the classchart-summary, with your sentences and their placeholders. Without sentences, there is nothing to fill, and the chart has no summary.
So a chart without sentences stays as it is, also on a page that loads the summary script.
Example
The sentences are in a div with the class chart-summary, in the figure. A placeholder in braces, such as {highest.value}, is filled with a value from the table. This chart has data-chart-summary="always", so the summary is also shown on the page.
tonnes sold per month
| Month | Apples | Oranges |
|---|---|---|
| January 2021 | 46.0 | 56.5 |
| February 2021 | 37.2 | 57.2 |
| March 2021 | 31.3 | 49.7 |
| April 2021 | 25.6 | 39.8 |
| May 2021 | 30.7 | 29.9 |
| June 2021 | 36.0 | 26.4 |
| July 2021 | 49.2 | 23.1 |
| August 2021 | 62.4 | 24.6 |
| September 2021 | 67.7 | 30.3 |
| October 2021 | 72.8 | 42.4 |
| November 2021 | 67.1 | 50.5 |
| December 2021 | 61.2 | 56.2 |
| January 2022 | 52.4 | 57.7 |
| February 2022 | 38.6 | 58.4 |
| March 2022 | 32.7 | 50.9 |
| April 2022 | 27.0 | 41.0 |
| May 2022 | 32.1 | 31.1 |
| June 2022 | 42.4 | 27.6 |
| July 2022 | 50.6 | 24.3 |
| August 2022 | 63.8 | 25.8 |
| September 2022 | 69.1 | 31.5 |
| October 2022 | 74.2 | 43.6 |
| November 2022 | 73.5 | 51.7 |
| December 2022 | 62.6 | 57.4 |
| January 2023 | 53.8 | 58.9 |
| February 2023 | 40.0 | 59.6 |
| March 2023 | 34.1 | 52.1 |
| April 2023 | 33.4 | 42.2 |
| May 2023 | 33.5 | 32.3 |
| June 2023 | 43.8 | 28.8 |
| July 2023 | 52.0 | 25.5 |
| August 2023 | 65.2 | 27.0 |
| September 2023 | 75.5 | 32.7 |
| October 2023 | 75.6 | 44.8 |
| November 2023 | 74.9 | 52.9 |
| December 2023 | 64.0 | 58.6 |
| January 2024 | 55.2 | 60.1 |
| February 2024 | 46.4 | 60.8 |
| March 2024 | 35.5 | 53.3 |
| April 2024 | 34.8 | 43.4 |
| May 2024 | 34.9 | 33.5 |
| June 2024 | 45.2 | 30.0 |
| July 2024 | 58.4 | 26.7 |
| August 2024 | 66.6 | 28.2 |
| September 2024 | 76.9 | 33.9 |
| October 2024 | 77.0 | 46.0 |
| November 2024 | 76.3 | 54.1 |
| December 2024 | 70.4 | 59.8 |
| January 2025 | 56.6 | 61.3 |
| February 2025 | 47.8 | 62.0 |
| March 2025 | 36.9 | 54.5 |
| April 2025 | 36.2 | 44.6 |
| May 2025 | 41.3 | 34.7 |
| June 2025 | 46.6 | 31.2 |
| July 2025 | 59.8 | 27.9 |
| August 2025 | 68.0 | 29.4 |
| September 2025 | 78.3 | 35.1 |
| October 2025 | 83.4 | 47.2 |
| November 2025 | 77.7 | 55.3 |
| December 2025 | 71.8 | 61.0 |
Most apples were sold in {highest.name}: {highest.value} tonnes. The fewest in {lowest.name}: {lowest.value} tonnes.
Most oranges were sold in {highest.name}: {highest.value} tonnes. The fewest in {lowest.name}: {lowest.value} tonnes.
Over {rows} months, between {range.from} and {range.to} tonnes of apples were sold a month.
<figure data-chart="line" data-chart-shape="smooth" data-chart-summary="always">
<figcaption><b>Comparing apples and oranges</b><br><span>tonnes sold per month</span></figcaption>
<table>
<thead>
<tr><th>Month</th><th>Apples</th><th data-line="dashed">Oranges</th></tr>
</thead>
<tbody>
<tr><th data-short="Jan ’21">January 2021</th><td>46.0</td><td>56.5</td></tr>
<tr><th data-short="Feb ’21">February 2021</th><td>37.2</td><td>57.2</td></tr>
<!-- …one row per month, 60 in all -->
</tbody>
</table>
<div class="chart-summary">
<p>Most apples were sold in {highest.name}: {highest.value} tonnes. The fewest in {lowest.name}: {lowest.value} tonnes.</p>
<p data-series="2">Most oranges were sold in {highest.name}: {highest.value} tonnes. The fewest in {lowest.name}: {lowest.value} tonnes.</p>
<p data-summary="range">Over {rows} months, between {range.from} and {range.to} tonnes of apples were sold a month.</p>
</div>
</figure>
The summary is where you put it in the figure. Here it is under the table, so it is under the chart. A screen reader reads it right after the title of the chart.
Each month has its full name in the table, which a screen reader and the summary read, and a short name under the chart, in data-short. See Short names.
Many values
kilos per day
| October 1 | 790 |
|---|---|
| October 2 | 570 |
| October 3 | 1210 |
| October 4 | 470 |
| October 5 | 1060 |
| October 6 | 840 |
| October 7 | 450 |
| October 8 | 1020 |
| October 9 | 470 |
| October 10 | 930 |
| October 11 | 85 |
| October 12 | 500 |
| October 13 | 920 |
| October 14 | 1430 |
| October 15 | 540 |
| October 16 | 660 |
| October 17 | 1180 |
| October 18 | 1580 |
| October 19 | 1620 |
| October 20 | 880 |
| October 21 | 1110 |
| October 22 | 440 |
| October 23 | 1470 |
| October 24 | 750 |
| October 25 | 560 |
| October 26 | 530 |
| October 27 | 770 |
| October 28 | 1420 |
| October 29 | 610 |
| October 30 | 1120 |
| October 31 | 1190 |
Most apples were sold on {highest.name}: {highest.value} kilos. The fewest on {lowest.name}: {lowest.value} kilos.
In total, {total} kilos were sold in October.
This column chart has a value for every day of October. The columns are narrower than their numbers, so the chart only shows the numbers that are clear of the other columns, mostly those of the highest days. The summary says what matters. By default it is only for screen readers, which read it right after the title. The switch shows it on screen too.
Start the summary
The summary is a separate import, so a page without summaries does not load it. It needs to start before init().
import { init } from "@martinomagnifico/tablechart";
import { Summary } from "@martinomagnifico/tablechart/summary";
Summary.init();
init();
With a script tag, load tablechart-summary.js after tablechart.js.
<script src="tablechart.js"></script>
<script src="tablechart-summary.js"></script>
<script>
Tablechart.Summary.init();
Tablechart.init();
</script>
Placeholders
| Placeholder | Is |
|---|---|
{highest.name} |
The name of the row with the highest value. If two rows have it, the first. |
{highest.value} |
The highest value |
{lowest.name} |
The name of the row with the lowest value |
{lowest.value} |
The lowest value |
{highest.share}, |
The highest and the lowest value as a share of the total, such as 51%. Only if no value is below zero. |
{total} |
The total: the row with data-kind="total" if the table has one, or else the sum of the values |
{range.from} |
The lowest value |
{range.to} |
The highest value |
{range.difference} |
The highest value less the lowest |
{first.name}, |
The first row with a number, and its value |
{last.name}, |
The last row with a number, and its value |
{change.difference} |
How much the last value differs from the first |
{change.percent} |
The same, as a percentage of the first value |
{change} |
up, down or flat, from the first value to the last |
{trend} |
up, down, flat or unclear: the direction of a straight line through all values |
{trend.percent} |
How much that line rises or falls over all rows, as a percentage of where it starts |
{trend.from}, |
Where that line starts and ends |
{outliers.count} |
How many values are far from that line |
{outliers.percent} |
How far a value must be from the line to be an outlier: 25% by default |
{flat.percent} |
How little the line must rise or fall to be flat: 5% by default |
{title} |
The title of the chart: the bold text of the figcaption, or all of it |
{rows} |
The number of rows |
{values} |
The number of values in the value column |
{series} |
The name of the value column |
A number is written as on the chart: in the language of the page, with the decimals of its column, and with the text around it in the table, such as %. A row with data-kind="total" does not count.
If a sentence has a placeholder that does not exist, the placeholder stays as you wrote it, and a message in the console shows this.
If your template engine uses single braces itself, use other characters:
Summary.init({ delimiters: ["[[", "]]"] });
Which sentences are shown
A sentence is about the first value column. With data-series="2", it is about the second. On a candlestick chart, it is about the close, the fourth column.
A sentence with data-summary is shown only if that part of the summary has a result. extremes has the highest and the lowest value, and range has the range. With data-none, a sentence is shown only if there is no result, such as when no row has a number yet.
<div class="chart-summary">
<p data-summary="extremes">The highest was {highest.value}, in {highest.name}.</p>
<p data-summary="extremes" data-none>There are no numbers yet.</p>
</div>
A sentence without data-summary is always shown.
With data-when, a sentence is shown only for one outcome of change or trend: up, down or flat, and for a trend also unclear. A sentence for each outcome is easier to translate than a word in the middle of a sentence.
<p data-summary="trend" data-when="up">The price went up.</p>
<p data-summary="trend" data-when="down">The price went down.</p>
<p data-summary="trend" data-when="flat">The price stayed about the same.</p>
<p data-summary="trend" data-when="unclear">The price went up and down, without a clear trend.</p>
{trend} and {change} are words too, for a single sentence. The words option sets them, such as Summary.init({ words: { up: "rising", down: "falling", flat: "stable" } }).
Trend and outliers
The trend is a straight line through all values. With fewer than 4 rows, there is no trend. The trend is:
upordown, if the line rises or falls by 5% of the average value or more, and that is not chance: the slope of the line is large compared to how far the values are from it. With few rows that takes a steeper rise than with many.flat, if the line rises or falls by less than 5%, and the values stay close to it: on average within 5% of the average value.unclearotherwise: the values go up and down more than the line does, so a straight line does not say much about them.
Whether a slope is chance is the slope over its standard error, against the threshold of the t-test at 95%. That is 4.3 with 4 rows, about 2.2 with 12, and 2.0 from 30 rows on.
A value more than 25% away from the line is an outlier. An outlier pulls the line towards itself, so the line is drawn again without the outliers, and the trend is that second line. If more than a fifth of the values are that far from the line, they are not outliers: the values are spread wide, and they all count. The flat and outliers options set the two percentages. If the values go below and above zero, such as profit and loss, the percentages are of the range of the values.
per kilo
| Month | Price |
|---|---|
| January 2021 | €1.16 |
| February 2021 | €1.23 |
| March 2021 | €1.22 |
| April 2021 | €1.21 |
| May 2021 | €1.28 |
| June 2021 | €1.27 |
| July 2021 | €1.25 |
| August 2021 | €1.24 |
| September 2021 | €1.31 |
| October 2021 | €1.30 |
| November 2021 | €1.29 |
| December 2021 | €1.36 |
| January 2022 | €1.34 |
| February 2022 | €1.33 |
| March 2022 | €1.32 |
| April 2022 | €1.39 |
| May 2022 | €1.38 |
| June 2022 | €1.36 |
| July 2022 | €1.43 |
| August 2022 | €1.42 |
| September 2022 | €1.41 |
| October 2022 | €1.40 |
| November 2022 | €1.47 |
| December 2022 | €1.45 |
| January 2023 | €1.44 |
| February 2023 | €1.51 |
| March 2023 | €1.50 |
| April 2023 | €1.48 |
| May 2023 | €2.13 |
| June 2023 | €1.54 |
| July 2023 | €1.53 |
| August 2023 | €1.52 |
| September 2023 | €1.59 |
| October 2023 | €1.57 |
| November 2023 | €1.56 |
| December 2023 | €1.55 |
| January 2024 | €1.62 |
| February 2024 | €1.61 |
| March 2024 | €1.59 |
| April 2024 | €1.67 |
| May 2024 | €1.65 |
| June 2024 | €1.64 |
| July 2024 | €1.63 |
| August 2024 | €1.70 |
| September 2024 | €1.68 |
| October 2024 | €1.04 |
| November 2024 | €1.74 |
| December 2024 | €1.73 |
| January 2025 | €1.72 |
| February 2025 | €1.70 |
| March 2025 | €1.77 |
| April 2025 | €1.76 |
| May 2025 | €1.75 |
| June 2025 | €1.82 |
| July 2025 | €1.81 |
| August 2025 | €1.79 |
| September 2025 | €1.78 |
| October 2025 | €1.85 |
| November 2025 | €1.84 |
| December 2025 | €1.82 |
From {first.name} to {last.name}, the price went up by {change.percent}, from {first.value} to {last.value}.
From {first.name} to {last.name}, the price went down by {change.percent}, from {first.value} to {last.value}.
Without the outliers, theThe trend is {trend}: from {trend.from} to {trend.to}.
{outliers.count|one:One month is|other:# months are} more than {outliers.percent} from the trend: {name} ({value}, {deviation} {side} the trend).
No month is more than {outliers.percent} from the trend.
<div class="chart-summary">
<p data-summary="change" data-when="up">From {first.name} to {last.name}, the price went up by {change.percent}, from {first.value} to {last.value}.</p>
<p data-summary="change" data-when="down">From {first.name} to {last.name}, the price went down by {change.percent}, from {first.value} to {last.value}.</p>
<p data-summary="trend"><span data-summary="outliers">Without the outliers, the</span><span data-summary="outliers" data-none>The</span>
trend is {trend}: from {trend.from} to {trend.to}.</p>
<p data-summary="outliers">{outliers.count|one:One month is|other:# months are} more than {outliers.percent} from the trend:
<span data-each="outliers">{name} ({value}, {deviation} {side} the trend)</span>.</p>
<p data-summary="outliers" data-none>No month is more than {outliers.percent} from the trend.</p>
</div>
An element with data-each="outliers" is repeated for each outlier, and the outliers are joined as a list in the language of the page: "A, B and C". In it, {name} and {value} are those of the outlier, {expected} is the value of the line there, {deviation} is how far the value is from it, and {side} is above or below.
{outliers.count|one:One month is|other:# months are} picks the words for the number, by the rules of the language of the page. # is the number. English has one and other; other languages also have zero, two, few and many.
Say what an outlier is, so the reader knows what "far" means: {outliers.percent} is the percentage from the outliers option.
If a reader needs to know how the trend was found, a sentence can say so, with a part that is only there when outliers were left out, as in the first sentence about the trend above:
<p data-summary="trend">Based on a straight line through all values<span data-summary="outliers">, without the outliers</span>,
the trend is {trend}.</p>
Without a clear trend there is nothing to be far from, so there are no outliers either, and their sentences are not shown.
A trend is a straight line, so it is for values without a season. Apples sold, as in the first example, go up and down every year, and many months would be far from a straight line.
Four kinds of trend
The button makes new data for the chart, of one kind at a time: a trend up, down, flat or unclear. The summary is filled again for each, and shows the sentence for that kind.
crates per month
| Month | Crates |
|---|---|
| January 2024 | 77.0 |
| February 2024 | 82.5 |
| March 2024 | 88.0 |
| April 2024 | 86.0 |
| May 2024 | 91.5 |
| June 2024 | 89.5 |
| July 2024 | 95.0 |
| August 2024 | 100.5 |
| September 2024 | 98.5 |
| October 2024 | 144.0 |
| November 2024 | 102.0 |
| December 2024 | 107.5 |
| January 2025 | 113.0 |
| February 2025 | 111.0 |
| March 2025 | 116.5 |
| April 2025 | 114.5 |
| May 2025 | 120.0 |
| June 2025 | 125.5 |
| July 2025 | 123.5 |
| August 2025 | 129.0 |
| September 2025 | 127.0 |
| October 2025 | 132.5 |
| November 2025 | 138.0 |
| December 2025 | 136.0 |
Without the outliers, salesSales went up by {trend.percent}, from {trend.from} to {trend.to} crates a month.
Without the outliers, salesSales went down by {trend.percent}, from {trend.from} to {trend.to} crates a month.
Without the outliers, salesSales stayed about the same, at around {trend.to} crates a month.
Sales went up and down, without a clear trend.
{outliers.count|one:One month was|other:# months were} more than {outliers.percent} from the trend: {name} ({value}).
<div class="chart-summary">
<p data-summary="trend" data-when="up"><span data-summary="outliers">Without the outliers, sales</span><span data-summary="outliers" data-none>Sales</span>
went up by {trend.percent}, from {trend.from} to {trend.to} crates a month.</p>
<p data-summary="trend" data-when="down"><span data-summary="outliers">Without the outliers, sales</span><span data-summary="outliers" data-none>Sales</span>
went down by {trend.percent}, from {trend.from} to {trend.to} crates a month.</p>
<p data-summary="trend" data-when="flat"><span data-summary="outliers">Without the outliers, sales</span><span data-summary="outliers" data-none>Sales</span>
stayed about the same, at around {trend.to} crates a month.</p>
<p data-summary="trend" data-when="unclear">Sales went up and down, without a clear trend.</p>
<p data-summary="outliers">{outliers.count|one:One month was|other:# months were} more than {outliers.percent} from the trend:
<span data-each="outliers">{name} ({value})</span>.</p>
</div>
A part of a sentence can have data-summary too, with data-when and data-none, and is then shown by the same rules. Here, "Without the outliers, sales" is shown only if there are outliers, and "Sales" only if there are none.
Notes
If the table has a column of notes, with data-note on its header, data-each="notes" lists them. In it, {name} is the name of the row and {note} its note. {notes.count} is how many there are. See Notes.
<p data-summary="notes">{notes.count|one:One month has|other:# months have} a note:
<span data-each="notes">{name} ({note})</span>.</p>
Rank
In a long table, a listener cannot remember whether a value heard a minute ago was more or less. {rank} is the place of a value among all values of its column: 1 for the highest. {rank.low} counts from the lowest. Equal values share a place, and {values} is the number of values.
{rank} and {rank.low} are there for each item of data-each="outliers" and data-each="notes". An item of a note also has {value}.
<p data-summary="outliers">Far from the trend:
<span data-each="outliers">{name}, the {rank|one:#st|two:#nd|few:#rd|other:#th} highest of {values} values</span>.</p>
The forms after a rank follow the rules of the language of the page for an order. English has one (1st, 21st), two (2nd, 22nd), few (3rd, 23rd) and other (4th, 11th). If a language has one form, such as Dutch, {rank|other:#e} is enough.
Groups
Between a few sentences and a table of hundreds of rows, groups gives a short table: the rows in groups, each with its lowest, average and highest value. The groups are made by number of rows, in order, so the rows need to be in order, as in Rows in order. 60 months in 5 groups are 5 years.
tonnes per month
| Month | Apples |
|---|---|
| January 2021 | 46.0 |
| February 2021 | 37.2 |
| March 2021 | 31.3 |
| April 2021 | 25.6 |
| May 2021 | 30.7 |
| June 2021 | 36.0 |
| July 2021 | 49.2 |
| August 2021 | 62.4 |
| September 2021 | 67.7 |
| October 2021 | 72.8 |
| November 2021 | 67.1 |
| December 2021 | 61.2 |
| January 2022 | 52.4 |
| February 2022 | 38.6 |
| March 2022 | 32.7 |
| April 2022 | 27.0 |
| May 2022 | 32.1 |
| June 2022 | 42.4 |
| July 2022 | 50.6 |
| August 2022 | 63.8 |
| September 2022 | 69.1 |
| October 2022 | 74.2 |
| November 2022 | 73.5 |
| December 2022 | 62.6 |
| January 2023 | 53.8 |
| February 2023 | 40.0 |
| March 2023 | 34.1 |
| April 2023 | 33.4 |
| May 2023 | 33.5 |
| June 2023 | 43.8 |
| July 2023 | 52.0 |
| August 2023 | 65.2 |
| September 2023 | 75.5 |
| October 2023 | 75.6 |
| November 2023 | 74.9 |
| December 2023 | 64.0 |
| January 2024 | 55.2 |
| February 2024 | 46.4 |
| March 2024 | 35.5 |
| April 2024 | 34.8 |
| May 2024 | 34.9 |
| June 2024 | 45.2 |
| July 2024 | 58.4 |
| August 2024 | 66.6 |
| September 2024 | 76.9 |
| October 2024 | 77.0 |
| November 2024 | 76.3 |
| December 2024 | 70.4 |
| January 2025 | 56.6 |
| February 2025 | 47.8 |
| March 2025 | 36.9 |
| April 2025 | 36.2 |
| May 2025 | 41.3 |
| June 2025 | 46.6 |
| July 2025 | 59.8 |
| August 2025 | 68.0 |
| September 2025 | 78.3 |
| October 2025 | 83.4 |
| November 2025 | 77.7 |
| December 2025 | 71.8 |
Apples sold per year, from {rows} months:
| Year | Lowest | Average | Highest |
|---|---|---|---|
| {from} to {to} | {lowest} | {average} | {highest} |
<figure data-chart="line" data-chart-summary="always" data-summary-groups="5">
…
<div class="chart-summary">
<p>Apples sold per year, from {rows} months:</p>
<table data-summary="groups">
<thead>
<tr><th>Year</th><th>Lowest</th><th>Average</th><th>Highest</th></tr>
</thead>
<tbody>
<tr data-each="groups"><th>{from} to {to}</th><td>{lowest}</td><td>{average}</td><td>{highest}</td></tr>
</tbody>
</table>
</div>
</figure>
A row with data-each="groups" is repeated for each group. In it, {from} and {to} are the names of the first and the last row of the group, and {lowest}, {average} and {highest} are its values. Rows of a table and items of a list (li) are repeated as they are. In a sentence, the items are joined as a list.
The headers are in the template, so they are translated like the sentences. A screen reader can go down a column and compare the groups.
The groups setting is the number of groups: 10 by default. If there are fewer rows than groups, each row is a group of its own.
Rows in order
A change and a trend only make sense if the rows are in order, such as months or years. The rows of a line, area or candlestick chart are. On another chart, add data-chart-ordered to the figure. Without it, the sentences of change, trend and outliers are not shown, also not the ones with data-none.
<figure data-chart="column" data-chart-ordered>
Short names
A screen reader, the summary and the tooltip read the names in the table. Under the chart there may be little room. If a row has a short name in data-short, the chart shows that instead.
<tr><th data-short="Nov ’21">November 2021</th><td>83.4</td></tr>
The summary always uses the full name, so a listener hears "November 2021". For how names fit under a chart with many rows, see Many rows.
Settings per chart
The settings of Summary.init() are for all charts. One chart can have its own, as an attribute with data-summary- and the name of the setting:
<figure data-chart="line" data-summary-flat="10" data-summary-outliers="20" data-summary-groups="5">
If a CMS gives no access to the script, all settings can be one piece of JSON in data-summary-options, on the figure or on a parent, such as a page template. On a parent, it is for every chart in it.
<div data-summary-options='{"flat": 10, "outliers": 20, "words": {"up": "rising", "down": "falling"}}'>
The settings are flat, outliers, groups, words, locale and delimiters. As an attribute, data-summary-words is JSON and data-summary-delimiters is two parts with a space, such as "[[ ]"]. The most specific one counts: an attribute on the figure, then data-summary-options on the figure, then on its parents from the nearest out, then Summary.init().
A change to these attributes is used the next time the summary is filled, such as after Summary.fill(figure).
Who sees what
data-chart-summary on the figure, or the summary option for all charts, says when the summary is shown. It takes the same values as data-chart-table.
| Value | On screen | In print and PDF |
|---|---|---|
never |
For screen readers only | Not printed |
auto |
For screen readers only | Printed, if the chart has to omit numbers and its table is not printed. The default. |
print |
For screen readers only | Printed |
always |
Shown | Printed |
A screen reader always reads the summary, right after the title of the chart. The switch sets data-chart-summary on this chart to always or never, and then calls refresh().
tonnes
| Month | Apples |
|---|---|
| January | 56.6 |
| February | 47.8 |
| March | 36.9 |
| April | 36.2 |
| May | 41.3 |
| June | 46.6 |
| July | 59.8 |
| August | 68.0 |
| September | 78.3 |
| October | 83.4 |
| November | 77.7 |
| December | 71.8 |
Most apples were sold in {highest.name}: {highest.value} tonnes. The fewest in {lowest.name}: {lowest.value} tonnes.
On paper
On paper there is nothing only for screen readers: text that is hidden on the page does not reach a PDF. So in print, a chart that has to omit numbers needs its table or its summary.
With data-chart-table="auto", the table is printed if it has no more than 15 values, rows times value columns. A listener who hears more numbers than that loses track, so a longer table is not printed, and the summary is printed instead. The tablelimit option sets the number.
init({ table: "auto", tablelimit: 24 });
If a chart has to omit numbers, and it prints neither its table nor a summary, those numbers are not on paper at all.
In the print preview of this page, the charts above print their summaries, because they have data-chart-summary="always".
Translation
The sentence with its placeholders is the template. It stays in the page, hidden, with its translation key, so a translation script can translate it. Tablechart writes the filled sentence right after it, without the key, so the script does not overwrite the numbers.
When a template changes, or the lang attribute of the page, the sentence is filled again. A name in a sentence keeps its own translation key, so it is translated too. See Translation.
The buttons do what a translation script does: they replace the text of each element with a key, and then set the lang attribute of the page.
This page has no real translation engine. The buttons use a small script that only knows the words of this example, as a demo. On your site, use your own translation solution.
tonnes sold
| Month | Apples | Oranges |
|---|---|---|
| January | 56.6 | 61.3 |
| February | 47.8 | 62.0 |
| March | 36.9 | 54.5 |
| April | 36.2 | 44.6 |
| May | 41.3 | 34.7 |
| June | 46.6 | 31.2 |
| July | 59.8 | 27.9 |
| August | 68.0 | 29.4 |
| September | 78.3 | 35.1 |
| October | 83.4 | 47.2 |
| November | 77.7 | 55.3 |
| December | 71.8 | 61.0 |
Most apples were sold in {highest.name}: {highest.value} tonnes.
Most oranges were sold in {highest.name}: {highest.value} tonnes.
<div class="chart-summary">
<p data-i18n="fruit-s1">Most apples were sold in {highest.name}: {highest.value} tonnes.</p>
<p data-i18n="fruit-s2" data-series="2">Most oranges were sold in {highest.name}: {highest.value} tonnes.</p>
</div>
{
"fruit-s1": "De meeste appels werden verkocht in {highest.name}: {highest.value} ton.",
"fruit-s2": "De meeste sinaasappels werden verkocht in {highest.name}: {highest.value} ton."
}
PDFs made on a server
A report is often a PDF that a server makes: a template is filled with the data of one client, and a headless browser, such as Puppeteer or Playwright, prints the page. JavaScript runs, so the charts and the summaries are there.
If the window is much wider than the paper, each chart is redrawn for a width of about 700px just before it prints. If you want it exact, the page needs the layout it has on paper before it loads: with the print styles, at the width of the paper less its margins. For A4 with margins of 12.7 mm, that is 698 pixels.
await page.emulateMediaType("print");
await page.setViewport({ width: 698, height: 1000 });
await page.goto(url);
await page.waitForSelector("html[data-tablechart-ready][data-summary-ready]");
await page.evaluate(() => document.fonts.ready);
await page.pdf({ format: "A4", margin: { top: "12.7mm", right: "12.7mm", bottom: "12.7mm", left: "12.7mm" }, tagged: true, outline: true });
When all charts are there, Tablechart sets data-tablechart-ready on html and sends the event tablechart:ready. When all summaries are filled, Summary sets data-summary-ready and sends tablechart:summary-ready. If a page has no summaries or does not load Summary, the script only needs to wait for data-tablechart-ready. If a data source fails, a summary stays empty and data-summary-ready does not come, so the script needs a timeout.
With tagged: true, the headings and the tables are in the structure of the PDF, so a screen reader can go through a printed table as a table. The drawing is a figure with the caption as its alt text. The summary and the table are outside that figure, so a screen reader reads them too.
Template engines such as Twig, Liquid and Handlebars use double braces, such as {{ client.name }}. They leave {highest.value} alone, so the server fills its part, and the browser fills the summary.