Building a Dashboard with Charts

By the end you have a dashboard page with number tiles, a table of recent items and a line chart, all fed by one metrics function, with tiles that refresh themselves.

1. One function gathers every metric

Dashboards read, they never write. Build a function starting at trigger_custom_function (create) with one search per metric, and end it with function_return (return) so one call gives the page one payload.

MetricBlocksRead it from
A count, such as open tasksA filter query plus query_count (count only) on function_search (search)hits
Recent itemsfunction_search with query_sort_field (sorting) on _meta.created descending and limit 10array
A value per dayaggregation_type_date_histogram_bp_field (date histogram) in query_aggregation (aggregation) on function_search, then array_get_buckets (get aggregation buckets)array of the buckets, each with key_as_string and doc_count
Two search details that bite Without query_count or query_include_hits, hits is the number of documents returned, not the total. And a search with an aggregation returns one object on array instead of documents; array_get_buckets reads its alphabetically first aggregation only, so give each chart its own search.

For anything heavy, precompute counters on write instead of counting at read time; see step 4. More recipes are in search tips.

2. The page prints the numbers and hands the chart its data

  1. Add the page

    web_page (new web page) with event dashboard, scoped for your team: *loggedin or a custom scope.

  2. Run the metrics

    Connect its flow to function_custom_execute (execute) with your metrics function selected. Its return values appear as pins on that block.

  3. Render the recent items

    Wire the recent items into web_foreach (web for each) with the row template below. It keys every row with the document's guid.

  4. Render the page

    Connect the flow to web_render (web render) and add custom pins: open_tasks and shipped (number), recent (value, the html of web_foreach) and signups (array, the buckets).

Row template on web_foreach
<tr>
  <td>{{title}}</td>
  <td>{{_meta.created | date "DD-MM-YYYY HH:mm"}}</td>
</tr>
Page template
<main class="dashboard">
  <h1>{{title}}</h1>

  <section id="tiles" data-action="refresh-tiles" data-trigger="every 30s">
    {{> dashboard-tiles}}
  </section>

  <canvas id="signups-chart" width="800" height="300"></canvas>

  <table>
    <tbody id="recent">{{{recent}}}</tbody>
  </table>
</main>

<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.js" nonce="{{nonce}}"></script>
<script nonce="{{nonce}}">
var data = {{signups}};
new Chart(document.getElementById('signups-chart'), {
  type: 'line',
  data: {
    labels: data.map(function (row) { return row.key_as_string; }),
    datasets: [
      { label: 'Signups per day', data: data.map(function (row) { return row.doc_count; }) }
    ]
  }
});
</script>

Inside a script, {{signups}} is written as JSON that is safe there, so it goes without quotes. The Content Security Policy allows external https scripts and inline scripts with the page's nonce, so any charting library works. Chart.js is only the example here.

The tiles live in a stored template of type web named dashboard-tiles, so the page and the refresh action print the same markup. See Layouts and Partials.

Stored template: dashboard-tiles
<div class="tile">
  <span>Open tasks</span>
  <strong>{{open_tasks | number}}</strong>
</div>
<div class="tile">
  <span>Shipped orders</span>
  <strong>{{shipped | number}}</strong>
</div>

How you know it worked: open /dashboard. The tiles show your counts, the table your newest documents, and the chart one point per day. Set the format pin on the histogram for readable labels.

3. The tiles refresh themselves

The data-trigger="every 30s" above runs the action named refresh-tiles while the page is visible. Answer it with only the tiles:

  1. Catch it

    web_action (on web action) with event refresh-tiles, then function_custom_execute of the same metrics function.

  2. Render the tiles

    web_template (web template) with the template {{> dashboard-tiles}} and custom pins open_tasks and shipped.

  3. Send them

    web_update (web update) with target #tiles and that html. Only those few bytes cross the network.

The chart stays as drawn, because HTML an action returns never runs its scripts. To refresh it too, let the page script fetch JSON from a small on_startup_register_uri_get (On API GET) endpoint on a timer and update the chart. Polling is worth it on shared boards and overkill on quiet data; see real-time vs polling.

4. Cache what is read often, count what is written

Checklist

  • One metrics function, one payload.
  • Sort and limit on every "recent" list.
  • A cache key on expensive searches, counters for hot stats.
  • Scope set on purpose: no accidental *public dashboards.

Next steps

Lists and Tables Rows with web_foreach, keys and empty states. Actions Everything an action can change on the page. Common Blueprint Patterns Caching and transformation recipes for the metrics function. Storage Examples Data models that aggregate well.

Frequently asked

How do I build a dashboard page in RUAL?

Build one function that gathers every metric, counts via the hits out-pin of function_search, recent items via a sort on _meta.created with a limit, grouped stats via aggregations, and return everything as one object built with object_new_fields. The page runs that function on load and passes the results into state elements or a chart component.

How do I show charts on a RUAL page?

Render the data into the page with a script carrying the page's nonce (var data = {{rows}};) and load a charting library yourself. Number tiles and recent-items lists are plain template markup.

How do I keep a RUAL dashboard fast?

Cache the metrics function with a cache_key on function_search and a short TTL in Redis, precompute counters on write with mutations_increment_by_field instead of counting at read time, and do per-item computation in the function with array_map rather than in the render.