Anatomy
Every chart is one block: a Bold title, a legend when there is more than one series, the plot, and a source line in the muted text color.
<div class="chart" data-chart="bars"> <!-- or data-d3="name" -->
<h4>Greenhouse pest reports per month</h4>
<ul class="legend"></ul>
<div class="plot"></div>
<p class="source">Source: Tomato Lab greenhouse log (filler data).</p>
</div>
Two scripts
charts.jsdraws the everyday types straight to SVG:bars,area,dots,ring,treeandline. Its data arrays sit at the top of the file. To add a type, write adrawfunction and aCHARTSentry. See Charts.d3-charts.jsis a small kit around d3 v7.9.0 for everything more complex. Register a chart withTL_D3.register('name', function (c) { … }). The function gets d3 (c.d3), the plot (c.host) and its width (c.W), the theme’s colors (c.t), a shared hover readout (c.tip) and whether to skip motion (c.reduce). See Infographics.
<script src="assets/charts.js"></script>
<script src="https://cdn.jsdelivr.net/npm/d3@7.9.0/dist/d3.min.js"></script>
<script src="assets/d3-charts.js"></script>
The chart styles are in roma.css, so a site that loads the stylesheet only needs the scripts it uses.
Color
Charts read their colors from the theme, never from hex values, so every chart works in Light, Dark and Tomato. In order of use:
| Role | Token | Use for |
|---|---|---|
| Ink | --color-text-primary | The main series, values and lines |
| Secondary and muted | --color-text-secondary, --color-text-muted | Second series, axis labels, the source line |
| Accent | --c-accent (Tomato; black on the Tomato theme) | The one value that stands out |
| Comparison | --c-blue (Water) | What the accent is compared against |
| More series | --c-green (Vine), --c-yellow (Burlap) | Only past four series |
The --c-… properties are set on each .chart in roma.css; change them there to recolor every chart.
Behavior
- Width. Charts draw at their container’s width and redraw when it changes, so type stays at 11px on phones.
- Theme. Changing the theme redraws every chart in the new colors.
- Reveal. A chart draws itself in once, the first time it scrolls into view. With reduced motion, it is simply there.
- Numbers. Hover or tap any mark for its value. Each chart also carries a text description for screen readers.
Pencil Box has the same kit (DOI_D3, the Department's colors and its own data) plus charts that only suit the Department, such as the attention map and the 3D bar chart.