跳到内容
栏目
Rastrillo 组件

表格和图表

一个数字表格和三种图表:条形图、折线图和迷你走势图。服务器绘制它们,不需要 JavaScript。

每个示例都能真实操作,但其中的链接不会跳转到任何地方。 HTML 显示可复制的标记。

data-table

卡片中的数字表格。在窄屏上,左右滚动卡片即可看到每一列。行标题始终可见。

按数字排序

预览
<div rst-box-head>
  <h2 id="sales-h">按周统计的销量</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">周</th>
        <th scope="col" rst-num aria-sort="descending">
          <a href="/sales?sort=sold&amp;dir=asc">售出<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">目标<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>退款</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">合计</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" "周")
        (dict "Href" "/sales?sort=sold&dir=asc" "Label" "售出" "Num" true "Sort" "descending")
        (dict "Href" "/sales?sort=target&dir=desc" "Label" "目标" "Num" true)
        (dict "Label" "退款" "Num" true))
    "Foot" (list "合计" 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" "按周统计的销量"}}

把列标题链接到排序后的列表,再给这一列设置 Sort。用 Num 标出数字列。

紧凑,表头固定

预览
<div rst-box-head>
  <h2 id="sales-dense-h">按周统计的销量</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">周</th>
        <th scope="col" rst-num>售出</th>
        <th scope="col" rst-num>目标</th>
        <th scope="col" rst-num>退款</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" "周")
        (dict "Label" "售出" "Num" true)
        (dict "Label" "目标" "Num" true)
        (dict "Label" "退款" "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" "按周统计的销量"}}

Dense 让行更紧凑。Bounded 限制高度,滚动行时表头保持可见。

没有匹配的行

预览
<div rst-box-head>
  <h2 id="sales-none-h">按周统计的销量</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">周</th>
        <th scope="col" rst-num>售出</th>
        <th scope="col" rst-num>目标</th>
        <th scope="col" rst-num>退款</th>
      </tr>
    </thead>
    <tbody>
      <tr rst-table-empty>
        <td colspan="4">没有符合条件的周。</td>
      </tr>
    </tbody>
  </table>
</div>
{{template "data-table" dict
    "Columns" (list
        (dict "Label" "周")
        (dict "Label" "售出" "Num" true)
        (dict "Label" "目标" "Num" true)
        (dict "Label" "退款" "Num" true))
    "Empty" "没有符合条件的周。"
    "ID" "sales-none"
    "Title" "按周统计的销量"}}

没有行时,表头仍在,Empty 说明原因。

chart-bar

以表格绘制的条形图:每行一个名称、一个条形和一个数值。数值必须大于或等于零。

四个来源,同一刻度

预览
<div rst-box-head>
  <h2 id="sources-h">按来源划分的门票</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="sources-h" aria-describedby="sources-d">
    <p rst-chart-desc id="sources-d">大部分门票在线上售出,通过邮件通讯售出的还一张都没有。</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">来源</th>
            <th scope="col">
              <span class="rst-sr-only">条形</span>
            </th>
            <th scope="col" rst-num>门票</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th scope="row">线上</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">售票处</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">合作伙伴</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">邮件通讯</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" "来源"
    "Desc" "大部分门票在线上售出,通过邮件通讯售出的还一张都没有。"
    "ID" "sources"
    "Measure" "门票"
    "Points" (list
        (dict "Label" "线上" "Value" 412)
        (dict "Label" "售票处" "Value" 236)
        (dict "Label" "合作伙伴" "Value" 98)
        (dict "Label" "邮件通讯" "Value" 0))
    "Title" "按来源划分的门票"}}

所有条形共用一个刻度。要比较两张图,给它们设置相同的 Max。

尚未售出

预览
<div rst-box-head>
  <h2 id="sources-none-h">按来源划分的门票</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="sources-none-h">
    <p rst-chart-empty>还没有卖出门票。</p>
  </figure>
</div>
{{template "chart-bar" dict
    "Category" "来源"
    "Empty" "还没有卖出门票。"
    "ID" "sources-none"
    "Measure" "门票"
    "Title" "按来源划分的门票"}}

没有数据点时,Empty 会取代图表。

chart-line

一组数据以折线画在等距的点上,每个数值都列在下方的表格里。

每周销量与目标对比

预览
<div rst-box-head>
  <h2 id="weekly-h">每周销量</h2>
</div>
<div rst-card>
  <figure rst-chart aria-labelledby="weekly-h" aria-describedby="weekly-d">
    <p rst-chart-desc id="weekly-d">销量几乎每周都在增长,并在第 6 周超过了 600 的目标。</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>目标:600</p>
    <details rst-chart-data>
      <summary>显示数值</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">周</th>
              <th scope="col" rst-num>售出</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">目标</th>
              <td rst-num>600</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </details>
  </figure>
</div>
{{template "chart-line" dict
    "Category" "周"
    "Desc" "销量几乎每周都在增长,并在第 6 周超过了 600 的目标。"
    "Goal" 600
    "GoalLabel" "目标"
    "Hover" true
    "ID" "weekly"
    "Measure" "售出"
    "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" "每周销量"}}

自己写 Desc。说清折线的走势,而不只是它停在哪里。设置 Hover 可为每个点加上提示。

有一周没有数据

预览
<div rst-box-head>
  <h2 id="weekly-gap-h">每周销量</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">第 5 周没有销售记录,折线在那里断开。</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>目标:600</p>
    <details rst-chart-data>
      <summary>显示数值</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">周</th>
              <th scope="col" rst-num>售出</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>无数据</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">目标</th>
              <td rst-num>600</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </details>
  </figure>
</div>
{{template "chart-line" dict
    "Category" "周"
    "Desc" "第 5 周没有销售记录,折线在那里断开。"
    "Goal" 600
    "GoalLabel" "目标"
    "Hover" false
    "ID" "weekly-gap"
    "Measure" "售出"
    "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" "每周销量"}}

缺失的数值会让折线断开,并在表格中显示为“无数据”。它永远不会画成 0。

sparkline

说明文字旁的一条小折线,用来在数字旁显示趋势。

上升,中间有回落

预览
<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>最近 8 周</strong> 销量从 120 涨到 690,第 4 周有一次回落。</figcaption>
</figure>
{{template "sparkline" dict
    "Desc" "销量从 120 涨到 690,第 4 周有一次回落。"
    "Period" "最近 8 周"
    "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))}}

屏幕阅读器只能读到说明文字。写明时间段,并描述任何回落或骤升。