Data
Every D3plus visualization is driven by an array of objects. You tell the chart how to read each object with accessor config keys, and D3plus handles grouping, aggregation, color assignment, and layout.
Accessors
Section titled “Accessors”The common accessors across charts:
| Key | Role |
|---|---|
groupBy |
The property (or array of properties for nesting) that defines each series/group. |
x, y |
Positional accessors for plot-based charts. |
sum / value / size |
The magnitude accessor: sum for Treemap, Pack, Sunburst, Icicle, and Waffle; value for Pie, Funnel, Gauge, Bullet, PolarArea, RadialBar, and Histogram; and size for Plot, Network, ArcDiagram, and Rings. |
time |
Enables the timeline and time-filtering (see Guides → Interactivity). |
id |
Unique identifier for a datum (defaults to groupBy). |
<BarChart config={{ data: [ {region: "North", quarter: "Q1", revenue: 35}, {region: "North", quarter: "Q2", revenue: 50}, {region: "South", quarter: "Q1", revenue: 20}, {region: "South", quarter: "Q2", revenue: 30} ], groupBy: "region", x: "quarter", y: "revenue"}} />Nesting with an array
Section titled “Nesting with an array”Pass an array to groupBy to build a hierarchy. The first key is the top level;
deeper keys drive drill-down and nested layouts:
<Treemap config={{data, groupBy: ["sector", "company"], sum: "value"}} />A nested Treemap names each group in a header strip across its top, with the
group’s tiles below it; hovering a strip highlights the group and clicking it
zooms in. Style the strips with groupLabelConfig, or turn them off with
groupLabels: false.
Time axes
Section titled “Time axes”When x (or y) reads dates and time points at the same field, a Plot draws
a continuous time axis. Markets and offices don’t run around the clock, so an
axis config’s rangeBreaks can hide the time with no data, and the rest
closes up around it:
<LinePlot config={{ data, groupBy: "ticker", x: "date", time: "date", y: "close", xConfig: {rangeBreaks: ["weekends", {hours: [16, 9.5]}, {values: ["2024-07-04"]}]}}} />"weekends" hides Saturdays and Sundays, {hours: [start, end]} the same
hours every day (wrapping past midnight), {values: [...]} whole days such as
holidays, and {bounds: [start, end]} one explicit range, marked on the axis
with the same break marker as yBreak. Rules read dates in local time, like
the axis labels. Ticks never land in hidden time, lines connect across it, and
rows inside it are left out. An axis with rangeBreaks over dates is a time
axis even without time, and the timeline hides the same time as the axis
that plots time (set timelineConfig.rangeBreaks to change that).
A Gantt’s time axis takes the same rules in axisConfig, so
axisConfig: {rangeBreaks: "weekends"} draws a schedule of working days.
Loading from a URL
Section titled “Loading from a URL”data accepts a URL string. D3plus fetches JSON (or CSV/TSV by extension) and
renders when it arrives:
<LinePlot config={{ data: "https://example.com/api/data.json", groupBy: "series", x: "year", y: "value"}} />When the request needs headers or other fetch options, pass an object with a
url and those options:
<LinePlot config={{ data: {url: "https://example.com/api/data.json", headers: {Authorization: token}}, groupBy: "series", x: "year", y: "value"}} />Only an object holding nothing but url and fetch options (headers,
method, body, credentials, …) is fetched. Data rows with a url field,
like {id: "alpha", value: 29, url: "https://…"}, are drawn as data.
Formatting loaded data
Section titled “Formatting loaded data”Set dataFormat to reshape a non-standard response before the chart reads it.
It receives the parsed file (or, when data lists several URLs, an Array of
them) and returns the rows to draw:
<LinePlot config={{ data: "https://example.com/api/data.json", dataFormat: json => json.results, groupBy: "series", x: "year", y: "value"}} />dataFormat is an ordinary config key:
- It runs when the chart renders, so it doesn’t matter whether
dataordataFormatis set first, in oneconfig()call or in separate ones. - It runs on rows passed as
datatoo, not only on fetched files. - Setting a new
dataFormatre-runs it on the next render, over the response the chart already fetched. Settingdataalone keeps the currentdataFormat. - Every
dataPollingIntervalpoll runs it.
A formatter can also return a config object holding the rows as its data
key. The rest of the object is applied to the chart, so a response can set
options from what it contains:
new LinePlot() .config({ data: "https://example.com/api/data.json", dataFormat: json => ({data: json.results, title: json.name}), groupBy: "series", x: "year", y: "value" }) .render();The fluent form takes the formatter as a second argument, which sets
dataFormat: viz.data(url, json => json.results) is the same as
viz.data(url).dataFormat(json => json.results).
The other loaders have their own formatters, which work the same way:
linksFormat and nodesFormat on Network, Rings, Sankey, Chord, and
ArcDiagram, and topojsonFormat and linksFormat on Geomap.
<Network config={{ links: "https://example.com/api/graph.json", linksFormat: json => json.edges, nodes: "https://example.com/api/graph.json", nodesFormat: json => json.vertices}} />Caching
Section titled “Caching”Every chart on the page shares one data cache, holding each response as
fetched, before any formatter runs, keyed by the request (the URL and a request
object’s fetch options). Charts that load the same URL at the same time share
one request, and a chart that loads it later, or sets the same data URL again,
or a new dataFormat, draws from the cache without a request; so do links,
nodes, and topojson. Each chart, and each formatter, gets its own copy of
the rows, so one chart’s dataFormat never changes another’s data. A failed
request is not kept, so setting the URL again retries it.
The cache uses up to about 50 MB of memory and drops the least recently used
first. To fetch on every load, set cache: false on that chart; to re-fetch on
a schedule, use dataPollingInterval, whose polls always go to the server. To
empty the cache, after a user logs out for example, call
clearDataCache():
import {clearDataCache} from "@d3plus/data";
clearDataCache();The CDN build of @d3plus/core has it too, as d3plus.clearDataCache().
Server-side rendering with @d3plus/ssr gives each chart its own cache, so one
render never draws another’s data.
Live data
Section titled “Live data”Set dataPollingInterval (in milliseconds) to re-fetch a URL on a schedule.
Each poll repeats the same request and dataFormat, and the chart transitions
to the new values while keeping hover, zoom, legend toggles, and the timeline
selection:
<BarChart config={{ data: "https://example.com/api/sales.json", dataPollingInterval: 5000, groupBy: "region", x: "region", y: "sales"}} />- A response identical to the drawn data is not redrawn.
- A failed request keeps the last data, logs one warning, and retries on the next tick.
- Polling waits while the browser tab is hidden or, with
detectVisible, while the chart is scrolled out of view, and fetches as soon as it is visible again. destroy(),dataPollingInterval: false(the default), or newdatastops it. Data passed as rows is never polled, and server-side rendering draws once.
Missing values
Section titled “Missing values”A plot leaves out any row whose x or y is null or NaN, so it never
draws a bar or point at a value it doesn’t have. A line skips a missing value
two ways: a row whose value is null or NaN, or a step of the discrete axis
(a year, a date, a category) that another line has and this one doesn’t.
connectGaps sets how lines cross those gaps:
| Value | Line across a gap |
|---|---|
true (the default) or "solid" |
Runs straight on through it. |
false |
Breaks, leaving the gap empty. |
"dashed" |
Breaks, with a short dashed bridge in the line’s color. |
{stroke, strokeDasharray, strokeWidth, opacity} |
Breaks, with a bridge in that style. |
<LinePlot config={{ data, groupBy: "series", x: "year", y: "value", connectGaps: "dashed"}} />A bridge follows the line’s curve and is decoration only: it has no tooltip,
takes no keyboard focus, and the shared tooltip at a missing step lists no
value for that line. Confidence bands stop at each gap. A value with a gap on
both sides draws no line of its own, so add lineMarkers: true to show it.
Areas are drawn as before.
Transforming data with @d3plus/data
Section titled “Transforming data with @d3plus/data”The @d3plus/data package exposes the helpers D3plus uses internally — handy
for preparing data before it reaches a chart:
import {fold, nest, merge, concat, unique} from "@d3plus/data";| Function | Purpose |
|---|---|
fold |
Combine a columnar {headers, data} response (separate header and value arrays) into an array of objects. |
nest / nestGroups |
Group rows into a {key, values} hierarchy. |
merge |
Combine an array of objects into one aggregated record. |
concat |
Flatten/append arrays of data. |
unique |
De-duplicate values, optionally by accessor. |
// columnar response → array of objectsfold({ headers: ["year", "apples", "bananas"], data: [[2020, 5, 8], [2021, 7, 6]]});// → [{year: 2020, apples: 5, bananas: 8}, {year: 2021, apples: 7, bananas: 6}]