GrapherLoader¶
Builder for rendering a Grapher chart into a DOM container.
Call one of the static factory methods to configure the data source, then call mount to render the chart. The instance acts as the handle for the mounted chart.
Example¶
// From a pre-built OwidTable
GrapherLoader
.fromTable({ config: { title: "My chart" }, data: table })
.mount(container)
// From a CSV URL (or pass the content inline via `csv` instead)
GrapherLoader
.fromCsv({
config: { title: "My chart" },
csvUrl: "./data.csv",
columnDefs: [{ slug: "gdp", name: "GDP" }],
})
.mount(container)
// From the OWID data API (config must include `dimensions`)
GrapherLoader
.fromApi({ config: { title: "My chart", dimensions: [...] } })
.mount(container)
// Optionally await the data, unmount later
const loader = GrapherLoader.fromApi({ config }).mount(container)
await loader.ready
loader.dispose()
Properties¶
| Property | Type | Description |
|---|---|---|
grapherState |
GrapherState |
The underlying GrapherState — use this to read or modify chart state programmatically. |
ready |
Promise<void> |
Resolves once the chart's data has loaded (immediately for fromTable and inline-CSV fromCsv), and rejects if fetching fails — in which case the chart stays in its loading state. Awaiting this is optional; a failure is also logged to the console. |
Methods¶
mount()¶
Render the chart into the given container.
Data fetching (if any) starts at construction time, so the chart will
show a loading state until the data arrives. The chart itself renders
lazily once the container is scrolled into view.
Returns this for optional chaining.
Parameters¶
| Parameter | Type |
|---|---|
container |
HTMLElement |
Returns¶
this
dispose()¶
Unmount the chart and disconnect all observers.
Returns¶
void
fromTable()¶
Prepare a chart whose data comes from a pre-built OwidTable.
Parameters¶
| Parameter | Type |
|---|---|
options |
FromTableOptions |
Returns¶
GrapherLoader
fromCsv()¶
Prepare a chart whose data comes from a CSV, either passed inline as a string or fetched from a URL.
Parameters¶
| Parameter | Type |
|---|---|
options |
FromCsvOptions |
Returns¶
GrapherLoader
fromApi()¶
Prepare a chart whose data will be fetched from the OWID data API.
Parameters¶
| Parameter | Type |
|---|---|
options |
FromApiOptions |
Returns¶
GrapherLoader