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>Unpaid intern complaints per month</h4>
<ul class="legend"></ul>
<div class="plot"></div>
<p class="source">Source: Office of Public Awareness (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 withDOI_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>
Color
Charts read their colors from the theme tokens, never from hex values, so every chart works in Dark, Light, Blue and Yellow. 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 | --color-red-100 | The one value that stands out |
| Comparison | --color-blue-100 (light blue on Dark) | What the accent is compared against |
| More series | --color-yellow-100, --color-green-100 | Only past four series; swapped for ink on a page of the same color |
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.