Skip to content
Sections
Rastrillo components

Tables and charts

A table of figures and three charts: a bar chart, a line chart and a sparkline. The server draws them without JavaScript.

Each example is live, but its links go nowhere. HTML shows the markup to copy.

data-table

A table of figures in a card. On a narrow screen, scroll the card sideways to see every column. Row headers stay in view.

Sorted by a number

Preview
<div rst-box-head>
  <h2 id="sales-h">Sales by week</h2>
</div>
<div rst-card rst-table-scroll="" tabindex="0" role="region" aria-labelledby="sales-h">
  <table rst-table="" aria-labelledby="sales-h">
    <thead>
      <tr>
        <th scope="col">Week</th>
        <th scope="col" rst-num aria-sort="descending">
          <a href="/sales?sort=sold&amp;dir=asc">Sold<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></a>
        </th>
        <th scope="col" rst-num>
          <a href="/sales?sort=target&amp;dir=desc">Target<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></a>
        </th>
        <th scope="col" rst-num>Refunds</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">3</th>
        <td rst-num>260</td>
        <td rst-num>600</td>
        <td rst-num>5</td>
      </tr>
      <tr>
        <th scope="row">4</th>
        <td rst-num>240</td>
        <td rst-num>600</td>
        <td rst-num>1</td>
      </tr>
      <tr>
        <th scope="row">2</th>
        <td rst-num>180</td>
        <td rst-num>600</td>
        <td rst-num>0</td>
      </tr>
      <tr>
        <th scope="row">1</th>
        <td rst-num>120</td>
        <td rst-num>600</td>
        <td rst-num>2</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">Total</th>
        <td rst-num>800</td>
        <td rst-num>2,400</td>
        <td rst-num>8</td>
      </tr>
    </tfoot>
  </table>
</div>
{{template "data-table" dict
    "Columns" (list
        (dict "Label" "Week")
        (dict "Href" "/sales?sort=sold&dir=asc" "Label" "Sold" "Num" true "Sort" "descending")
        (dict "Href" "/sales?sort=target&dir=desc" "Label" "Target" "Num" true)
        (dict "Label" "Refunds" "Num" true))
    "Foot" (list "Total" 800 2400 8)
    "ID" "sales"
    "Rows" (list (list 3 260 600 5) (list 4 240 600 1) (list 2 180 600 0) (list 1 120 600 2))
    "Title" "Sales by week"}}

Link a column heading to the sorted list, then set Sort on that column. Mark number columns with Num.

Dense, with a fixed header

Preview
<div rst-box-head>
  <h2 id="sales-dense-h">Sales by week</h2>
</div>
<div rst-card rst-table-scroll="bounded" tabindex="0" role="region" aria-labelledby="sales-dense-h">
  <table rst-table="dense" aria-labelledby="sales-dense-h">
    <thead>
      <tr>
        <th scope="col">Week</th>
        <th scope="col" rst-num>Sold</th>
        <th scope="col" rst-num>Target</th>
        <th scope="col" rst-num>Refunds</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">1</th>
        <td rst-num>120</td>
        <td rst-num>600</td>
        <td rst-num>2</td>
      </tr>
      <tr>
        <th scope="row">2</th>
        <td rst-num>180</td>
        <td rst-num>600</td>
        <td rst-num>0</td>
      </tr>
      <tr>
        <th scope="row">3</th>
        <td rst-num>260</td>
        <td rst-num>600</td>
        <td rst-num>5</td>
      </tr>
      <tr>
        <th scope="row">4</th>
        <td rst-num>240</td>
        <td rst-num>600</td>
        <td rst-num>1</td>
      </tr>
      <tr>
        <th scope="row">5</th>
        <td rst-num>390</td>
        <td rst-num>600</td>
        <td rst-num>3</td>
      </tr>
      <tr>
        <th scope="row">6</th>
        <td rst-num>610</td>
        <td rst-num>600</td>
        <td rst-num>4</td>
      </tr>
      <tr>
        <th scope="row">7</th>
        <td rst-num>650</td>
        <td rst-num>600</td>
        <td rst-num>0</td>
      </tr>
      <tr>
        <th scope="row">8</th>
        <td rst-num>690</td>
        <td rst-num>600</td>
        <td rst-num>2</td>
      </tr>
    </tbody>
  </table>
</div>
{{template "data-table" dict
    "Bounded" true
    "Columns" (list
        (dict "Label" "Week")
        (dict "Label" "Sold" "Num" true)
        (dict "Label" "Target" "Num" true)
        (dict "Label" "Refunds" "Num" true))
    "Dense" true
    "ID" "sales-dense"
    "Rows" (list (list 1 120 600 2) (list 2 180 600 0) (list 3 260 600 5) (list 4 240 600 1) (list 5 390 600 3) (list 6 610 600 4) (list 7 650 600 0) (list 8 690 600 2))
    "Title" "Sales by week"}}

Dense tightens the rows. Bounded caps the height and keeps the header in view while the rows scroll.

No rows match

Preview
<div rst-box-head>
  <h2 id="sales-none-h">Sales by week</h2>
</div>
<div rst-card rst-table-scroll="" tabindex="0" role="region" aria-labelledby="sales-none-h">
  <table rst-table="" aria-labelledby="sales-none-h">
    <thead>
      <tr>
        <th scope="col">Week</th>
        <th scope="col" rst-num>Sold</th>
        <th scope="col" rst-num>Target</th>
        <th scope="col" rst-num>Refunds</th>
      </tr>
    </thead>
    <tbody>
      <tr rst-table-empty>
        <td colspan="4">No weeks match.</td>
      </tr>
    </tbody>
  </table>
</div>
{{template "data-table" dict
    "Columns" (list
        (dict "Label" "Week")
        (dict "Label" "Sold" "Num" true)
        (dict "Label" "Target" "Num" true)
        (dict "Label" "Refunds" "Num" true))
    "Empty" "No weeks match."
    "ID" "sales-none"
    "Title" "Sales by week"}}

With no rows, the header stays and Empty says why.

chart-bar

A bar chart drawn as a table: a name, a bar and a value in each row. Values must be zero or more.

Four sources on one scale

Preview
<div rst-box-head>
  <h2 id="sources-h">Tickets by source</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="sources-h" aria-describedby="sources-d">
    <p rst-chart-desc id="sources-d">Most tickets sold online, and none through the newsletter yet.</p>
    <div rst-table-scroll tabindex="0" role="region" aria-labelledby="sources-h">
      <table rst-table="bars" aria-labelledby="sources-h" aria-describedby="sources-d">
        <thead>
          <tr>
            <th scope="col">Source</th>
            <th scope="col">
              <span class="rst-sr-only">Bar</span>
            </th>
            <th scope="col" rst-num>Tickets</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th scope="row">Online</th>
            <td rst-table-bar>
              <svg viewBox="0 0 1000 1" preserveAspectRatio="none" aria-hidden="true" focusable="false"><rect width="1000" height="1"/></svg>
            </td>
            <td rst-num>412</td>
          </tr>
          <tr>
            <th scope="row">Box office</th>
            <td rst-table-bar>
              <svg viewBox="0 0 1000 1" preserveAspectRatio="none" aria-hidden="true" focusable="false"><rect width="572.8" height="1"/></svg>
            </td>
            <td rst-num>236</td>
          </tr>
          <tr>
            <th scope="row">Partners</th>
            <td rst-table-bar>
              <svg viewBox="0 0 1000 1" preserveAspectRatio="none" aria-hidden="true" focusable="false"><rect width="237.9" height="1"/></svg>
            </td>
            <td rst-num>98</td>
          </tr>
          <tr>
            <th scope="row">Newsletter</th>
            <td rst-table-bar>
              <svg viewBox="0 0 1000 1" preserveAspectRatio="none" aria-hidden="true" focusable="false"><rect width="0" height="1"/></svg>
            </td>
            <td rst-num>0</td>
          </tr>
        </tbody>
      </table>
    </div>
  </figure>
</div>
{{template "chart-bar" dict
    "Category" "Source"
    "Desc" "Most tickets sold online, and none through the newsletter yet."
    "ID" "sources"
    "Measure" "Tickets"
    "Points" (list
        (dict "Label" "Online" "Value" 412)
        (dict "Label" "Box office" "Value" 236)
        (dict "Label" "Partners" "Value" 98)
        (dict "Label" "Newsletter" "Value" 0))
    "Title" "Tickets by source"}}

Every bar shares one scale. To compare two charts, give both the same Max.

Nothing sold yet

Preview
<div rst-box-head>
  <h2 id="sources-none-h">Tickets by source</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="sources-none-h">
    <p rst-chart-empty>No tickets sold yet.</p>
  </figure>
</div>
{{template "chart-bar" dict
    "Category" "Source"
    "Empty" "No tickets sold yet."
    "ID" "sources-none"
    "Measure" "Tickets"
    "Title" "Tickets by source"}}

With no points, Empty takes the chart's place.

chart-line

One series as a line over evenly spaced points, with every value in a table under it.

Weekly sales against a target

Preview
<div rst-box-head>
  <h2 id="weekly-h">Weekly sales</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="weekly-h" aria-describedby="weekly-d">
    <p rst-chart-desc id="weekly-d">Sales rose almost every week and passed the target of 600 in week 6.</p>
    <div rst-chart-plot aria-hidden="true">
      <div rst-chart-y>
        <span>800</span><span>600</span><span>400</span><span>200</span><span>0</span>
      </div>
      <svg viewBox="0 0 1000 1000" preserveAspectRatio="none" focusable="false"><path rst-chart-grid d="M0 0H1000M0 250H1000M0 500H1000M0 750H1000"/><path rst-chart-base d="M0 1000H1000"/><path rst-chart-goal d="M0 250H1000"/><polyline rst-chart-line points="0,850 142.9,775 285.7,675 428.6,700 571.4,512.5 714.3,237.5 857.1,187.5 1000,137.5"/><path rst-chart-ring d="M1000 137.5h0"/><path rst-chart-dot d="M1000 137.5h0"/></svg>
      <div rst-chart-hits>
        <span title="1: 120"></span><span title="2: 180"></span><span title="3: 260"></span><span title="4: 240"></span><span title="5: 390"></span><span title="6: 610"></span><span title="7: 650"></span><span title="8: 690"></span>
      </div>
      <div rst-chart-x>
        <span rst-chart-keep>1</span><span>2</span><span rst-chart-keep>3</span><span>4</span><span rst-chart-keep>5</span><span>6</span><span>7</span><span rst-chart-keep>8</span>
      </div>
    </div>
    <p rst-chart-key>Target: 600</p>
    <details rst-chart-data>
      <summary>Show the numbers</summary>
      <div rst-table-scroll tabindex="0" role="region" aria-labelledby="weekly-h">
        <table rst-table="dense" aria-labelledby="weekly-h">
          <thead>
            <tr>
              <th scope="col">Week</th>
              <th scope="col" rst-num>Sold</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">1</th>
              <td rst-num>120</td>
            </tr>
            <tr>
              <th scope="row">2</th>
              <td rst-num>180</td>
            </tr>
            <tr>
              <th scope="row">3</th>
              <td rst-num>260</td>
            </tr>
            <tr>
              <th scope="row">4</th>
              <td rst-num>240</td>
            </tr>
            <tr>
              <th scope="row">5</th>
              <td rst-num>390</td>
            </tr>
            <tr>
              <th scope="row">6</th>
              <td rst-num>610</td>
            </tr>
            <tr>
              <th scope="row">7</th>
              <td rst-num>650</td>
            </tr>
            <tr>
              <th scope="row">8</th>
              <td rst-num>690</td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <th scope="row">Target</th>
              <td rst-num>600</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </details>
  </figure>
</div>
{{template "chart-line" dict
    "Category" "Week"
    "Desc" "Sales rose almost every week and passed the target of 600 in week 6."
    "Goal" 600
    "GoalLabel" "Target"
    "Hover" true
    "ID" "weekly"
    "Measure" "Sold"
    "Points" (list
        (dict "Label" 1 "Value" 120)
        (dict "Label" 2 "Value" 180)
        (dict "Label" 3 "Value" 260)
        (dict "Label" 4 "Value" 240)
        (dict "Label" 5 "Value" 390)
        (dict "Label" 6 "Value" 610)
        (dict "Label" 7 "Value" 650)
        (dict "Label" 8 "Value" 690))
    "Title" "Weekly sales"}}

Write Desc yourself. Say what the line does, not only where it ends. Set Hover to add a tooltip to each point.

A week with no data

Preview
<div rst-box-head>
  <h2 id="weekly-gap-h">Weekly sales</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="weekly-gap-h" aria-describedby="weekly-gap-d">
    <p rst-chart-desc id="weekly-gap-d">No sales were recorded in week 5; the line breaks there.</p>
    <div rst-chart-plot aria-hidden="true">
      <div rst-chart-y>
        <span>800</span><span>600</span><span>400</span><span>200</span><span>0</span>
      </div>
      <svg viewBox="0 0 1000 1000" preserveAspectRatio="none" focusable="false"><path rst-chart-grid d="M0 0H1000M0 250H1000M0 500H1000M0 750H1000"/><path rst-chart-base d="M0 1000H1000"/><path rst-chart-goal d="M0 250H1000"/><polyline rst-chart-line points="0,850 142.9,775 285.7,675 428.6,700"/><polyline rst-chart-line points="714.3,237.5 857.1,187.5 1000,137.5"/><path rst-chart-ring d="M1000 137.5h0"/><path rst-chart-dot d="M1000 137.5h0"/></svg>
      <div rst-chart-x>
        <span rst-chart-keep>1</span><span>2</span><span rst-chart-keep>3</span><span>4</span><span rst-chart-keep>5</span><span>6</span><span>7</span><span rst-chart-keep>8</span>
      </div>
    </div>
    <p rst-chart-key>Target: 600</p>
    <details rst-chart-data>
      <summary>Show the numbers</summary>
      <div rst-table-scroll tabindex="0" role="region" aria-labelledby="weekly-gap-h">
        <table rst-table="dense" aria-labelledby="weekly-gap-h">
          <thead>
            <tr>
              <th scope="col">Week</th>
              <th scope="col" rst-num>Sold</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">1</th>
              <td rst-num>120</td>
            </tr>
            <tr>
              <th scope="row">2</th>
              <td rst-num>180</td>
            </tr>
            <tr>
              <th scope="row">3</th>
              <td rst-num>260</td>
            </tr>
            <tr>
              <th scope="row">4</th>
              <td rst-num>240</td>
            </tr>
            <tr>
              <th scope="row">5</th>
              <td rst-num>No data</td>
            </tr>
            <tr>
              <th scope="row">6</th>
              <td rst-num>610</td>
            </tr>
            <tr>
              <th scope="row">7</th>
              <td rst-num>650</td>
            </tr>
            <tr>
              <th scope="row">8</th>
              <td rst-num>690</td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <th scope="row">Target</th>
              <td rst-num>600</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </details>
  </figure>
</div>
{{template "chart-line" dict
    "Category" "Week"
    "Desc" "No sales were recorded in week 5; the line breaks there."
    "Goal" 600
    "GoalLabel" "Target"
    "Hover" false
    "ID" "weekly-gap"
    "Measure" "Sold"
    "Points" (list
        (dict "Label" 1 "Value" 120)
        (dict "Label" 2 "Value" 180)
        (dict "Label" 3 "Value" 260)
        (dict "Label" 4 "Value" 240)
        (dict "Label" 5 "Missing" true "Value" 390)
        (dict "Label" 6 "Value" 610)
        (dict "Label" 7 "Value" 650)
        (dict "Label" 8 "Value" 690))
    "Title" "Weekly sales"}}

A missing value breaks the line and reads No data in the table. It is never drawn as 0.

sparkline

A small line beside its caption, for a trend next to a number.

Rising, with a dip

Preview
<figure rst-spark>
  <svg viewBox="0 0 100 24" preserveAspectRatio="none" aria-hidden="true" focusable="false"><polyline rst-chart-line points="0,24 14.3,21.5 28.6,18.1 42.9,18.9 57.1,12.6 71.4,3.4 85.7,1.7 100,0"/><path rst-chart-ring d="M100 0h0"/><path rst-chart-dot d="M100 0h0"/></svg>
  <figcaption>
    <strong>Last 8 weeks</strong> Sales rose from 120 to 690, with a dip in week 4.</figcaption>
</figure>
{{template "sparkline" dict
    "Desc" "Sales rose from 120 to 690, with a dip in week 4."
    "Period" "Last 8 weeks"
    "Points" (list
        (dict "Label" 1 "Value" 120)
        (dict "Label" 2 "Value" 180)
        (dict "Label" 3 "Value" 260)
        (dict "Label" 4 "Value" 240)
        (dict "Label" 5 "Value" 390)
        (dict "Label" 6 "Value" 610)
        (dict "Label" 7 "Value" 650)
        (dict "Label" 8 "Value" 690))}}

The caption is all a screen reader gets. Give the period, and describe any dip or spike.