# Scrollytelling

> Step a chart through states as the reader scrolls with createChartStory, each step a spec change.

A scroll story is one chart and a list of steps. As the reader scrolls past each paragraph, `createChartStory` merges that step's change into the spec and moves the chart with the same transition `update` plays.

```json
{
  "mark": "bar",
  "data": [
    {
      "year": "1900",
      "anomaly": -0.08,
      "trend": "Cooler"
    },
    {
      "year": "1910",
      "anomaly": -0.43,
      "trend": "Cooler"
    },
    {
      "year": "1920",
      "anomaly": -0.27,
      "trend": "Cooler"
    },
    {
      "year": "1930",
      "anomaly": -0.16,
      "trend": "Cooler"
    },
    {
      "year": "1940",
      "anomaly": 0.12,
      "trend": "Warmer"
    },
    {
      "year": "1950",
      "anomaly": -0.17,
      "trend": "Cooler"
    },
    {
      "year": "1960",
      "anomaly": -0.03,
      "trend": "Cooler"
    },
    {
      "year": "1970",
      "anomaly": 0.03,
      "trend": "Warmer"
    },
    {
      "year": "1980",
      "anomaly": 0.26,
      "trend": "Warmer"
    },
    {
      "year": "1990",
      "anomaly": 0.45,
      "trend": "Warmer"
    },
    {
      "year": "2000",
      "anomaly": 0.39,
      "trend": "Warmer"
    },
    {
      "year": "2010",
      "anomaly": 0.72,
      "trend": "Warmer"
    },
    {
      "year": "2020",
      "anomaly": 1.01,
      "trend": "Warmer"
    },
    {
      "year": "2025",
      "anomaly": 1.19,
      "trend": "Warmer"
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "trend",
      "type": "nominal",
      "scale": {
        "domain": [
          "Cooler",
          "Warmer"
        ],
        "range": [
          "#3b82c4",
          "#d1495b"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "Every reading since 1970 is above the 1951–1980 average",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
  }
}
```

The buttons stand in for scrolling: each shows the chart as it would be at one step. The first step is the base spec. The second adds a shaded range from 1980, the third a note on the 2025 reading, and the fourth highlights the warmer readings and fades the rest. The title changes at every step, so each one states what the reader is looking at. The Code panel shows the spec as the page loaded it, not the state on screen.

## Building the story

The same four steps, written for `createChartStory` from `@opendata-ai/openchart-vanilla/story`:

```ts title="story.ts"
import { createChartStory } from "@opendata-ai/openchart-vanilla/story";

const zeroLine = { type: "refline", y: 0, style: "solid", strokeWidth: 1 };
const since1980 = { type: "range", x1: "1980", x2: "2025", label: "Since 1980", labelAnchor: "top" };
const latest = {
  type: "text",
  x: "2025",
  y: 1.19,
  text: "+1.19°C",
  anchor: "top",
  offset: { dx: 0, dy: -8 },
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');

// baseSpec is the spec in the demo's Code panel.
const story = createChartStory(container, {
  spec: baseSpec,
  steps: [
    {},
    {
      spec: {
        annotations: [zeroLine, since1980],
        chrome: { title: "Global temperature rose 0.93°C from 1980 to 2025" },
      },
    },
    {
      spec: {
        annotations: [zeroLine, since1980, latest],
        chrome: { title: "The 2025 reading was 1.19°C above the 1951–1980 average" },
      },
    },
    {
      highlight: ["Warmer"],
      spec: { chrome: { title: "Of the 14 readings, 8 are above the 1951–1980 average" } },
    },
  ],
});

document.querySelectorAll<HTMLElement>("[data-step]").forEach((element, index) => {
  story.registerStep(index, element);
});
```

Each step is a partial spec. Steps add up, so step three is the base with the changes from steps one to three merged in. Objects merge key by key, but arrays replace, which is why each step lists every annotation it shows. `highlight` is shorthand for `encoding.color.highlight`. `registerStep` ties a step to a paragraph, and the step applies when that paragraph crosses the trigger line, 40% of the way down the viewport by default. `story.goTo(index)` jumps to a step from your own code.

## Highlighting one series at a time

```json
{
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    {
      "year": "2017",
      "source": "Coal",
      "share": 30.7
    },
    {
      "year": "2017",
      "source": "Gas",
      "share": 33
    },
    {
      "year": "2017",
      "source": "Nuclear",
      "share": 20.5
    },
    {
      "year": "2017",
      "source": "Hydro",
      "share": 7.5
    },
    {
      "year": "2017",
      "source": "Wind",
      "share": 6.5
    },
    {
      "year": "2017",
      "source": "Solar",
      "share": 2
    },
    {
      "year": "2018",
      "source": "Coal",
      "share": 28.2
    },
    {
      "year": "2018",
      "source": "Gas",
      "share": 36
    },
    {
      "year": "2018",
      "source": "Nuclear",
      "share": 19.8
    },
    {
      "year": "2018",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2018",
      "source": "Wind",
      "share": 6.7
    },
    {
      "year": "2018",
      "source": "Solar",
      "share": 2.3
    },
    {
      "year": "2019",
      "source": "Coal",
      "share": 23.9
    },
    {
      "year": "2019",
      "source": "Gas",
      "share": 39.2
    },
    {
      "year": "2019",
      "source": "Nuclear",
      "share": 20
    },
    {
      "year": "2019",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2019",
      "source": "Wind",
      "share": 7.3
    },
    {
      "year": "2019",
      "source": "Solar",
      "share": 2.6
    },
    {
      "year": "2020",
      "source": "Coal",
      "share": 19.6
    },
    {
      "year": "2020",
      "source": "Gas",
      "share": 41.3
    },
    {
      "year": "2020",
      "source": "Nuclear",
      "share": 20.1
    },
    {
      "year": "2020",
      "source": "Hydro",
      "share": 7.1
    },
    {
      "year": "2020",
      "source": "Wind",
      "share": 8.6
    },
    {
      "year": "2020",
      "source": "Solar",
      "share": 3.3
    },
    {
      "year": "2021",
      "source": "Coal",
      "share": 22.2
    },
    {
      "year": "2021",
      "source": "Gas",
      "share": 39
    },
    {
      "year": "2021",
      "source": "Nuclear",
      "share": 19.3
    },
    {
      "year": "2021",
      "source": "Hydro",
      "share": 6.1
    },
    {
      "year": "2021",
      "source": "Wind",
      "share": 9.3
    },
    {
      "year": "2021",
      "source": "Solar",
      "share": 4.1
    },
    {
      "year": "2022",
      "source": "Coal",
      "share": 19.9
    },
    {
      "year": "2022",
      "source": "Gas",
      "share": 40.4
    },
    {
      "year": "2022",
      "source": "Nuclear",
      "share": 18.5
    },
    {
      "year": "2022",
      "source": "Hydro",
      "share": 6
    },
    {
      "year": "2022",
      "source": "Wind",
      "share": 10.4
    },
    {
      "year": "2022",
      "source": "Solar",
      "share": 4.9
    },
    {
      "year": "2023",
      "source": "Coal",
      "share": 16.2
    },
    {
      "year": "2023",
      "source": "Gas",
      "share": 43.5
    },
    {
      "year": "2023",
      "source": "Nuclear",
      "share": 18.6
    },
    {
      "year": "2023",
      "source": "Hydro",
      "share": 5.8
    },
    {
      "year": "2023",
      "source": "Wind",
      "share": 10.1
    },
    {
      "year": "2023",
      "source": "Solar",
      "share": 5.8
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y"
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal"
    }
  },
  "endpointLabels": true,
  "chrome": {
    "title": "Gas and coal made more than half of generation from the six main sources every year",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
}
```

A step that only changes the highlight leaves every line where it is. When a step changes the data, marks move as they do on `update`, and when no transition applies, for example because the mark type changes, the story crossfades between the two charts so nothing jumps.

A story skips the entrance unless the base spec sets `animation`, because the reader's scroll position decides what the chart shows. The scroll story on the [OpenChart home page](/#story) runs this way on four stock indexes. React, Vue and Svelte wrap the same API in a `ChartStory` component.

## Fields that matter

| Field | What it does |
| --- | --- |
| `spec` | The chart before the first step. |
| `steps[].spec` | A partial spec merged into the running spec at that step. |
| `steps[].highlight` | Series to highlight, the same as `encoding.color.highlight`; `null` clears it. |
| `steps[].camera` | Zooms to a region of the chart, in data coordinates. |
| `triggerPosition` | Where the trigger line sits, as a fraction of the viewport height. Default 0.4. |
| `registerStep`, `goTo` | Tie a step to an element on the page, or jump to a step. |

## Related

- [Motion](/docs/behavior/motion/) covers the update transitions each step plays.
- [Bar](/docs/comparison/bar/) and [Line](/docs/change-over-time/line/) show the two chart types these stories use.
- [Animation reference](/docs/spec/animation/#data-update-transitions) lists which changes move and which crossfade.
