Skip to content
OpenChart

Edit mode

Let a reader drag annotations and edit text in place, and apply each edit transaction to the spec.

In edit mode a reader can drag annotations, nudge them with the keyboard and rewrite chart text in place. Each change reaches onEdit as one transaction, and your code decides whether to apply it to the spec.

20192020202120222023202402468Inflation (%)Peak: 8.5%Fed 2% targetUS inflation peaked at 8.5% in 2022 and wasdown to 2.6% by late 2024US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)20192020202120222023202402468Inflation (%)Peak: 8.5%Fed 2% targetUS inflation peaked at 8.5% in 2022 and wasdown to 2.6% by late 2024US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)
Drag the note, or select it and nudge it with the arrow keys. Source: US Bureau of Labor Statistics, CPI-U (via FRED CPIAUCSL).
{
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "data": [
    {
      "date": "2019-01-01",
      "rate": 1.6
    },
    {
      "date": "2019-04-01",
      "rate": 2
    },
    {
      "date": "2019-07-01",
      "rate": 1.8
    },
    {
      "date": "2019-10-01",
      "rate": 1.8
    },
    {
      "date": "2020-01-01",
      "rate": 2.5
    },
    {
      "date": "2020-04-01",
      "rate": 0.3
    },
    // … 18 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 24 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}

export function Example() {
  return <Chart spec={spec} interaction="edit" onEdit={onEdit} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 24 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}
</script>

<template>
  <Chart :spec="spec" interaction="edit" @edit="onEdit" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 24 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}
</script>

<Chart {spec} interaction="edit" onedit={onEdit} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 24 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec, { interaction: "edit", onEdit });

Mount with interaction: "edit" to turn it on. Drag the note and the event log shows the transaction the gesture produced: a list of operations, each a set, add or remove at a JSON Pointer path into the spec, such as /annotations/0/offset. This page applies each transaction and redraws the chart with update, so the note stays where you leave it. The Code panel shows the spec as the page loaded it, not the state or edits on screen.

Applying an edit and undoing it

applyEditTransaction from the core package applies a transaction to a copy of the spec. Keep its inverse and an undo is the same call with the inverse:

edit.ts
import { applyEditTransaction, type EditTransaction } from "@opendata-ai/openchart-core";
import { createChart } from "@opendata-ai/openchart-vanilla";

let spec = initialSpec;
const undoStack: EditTransaction[] = [];

const chart = createChart(container, spec, {
  interaction: "edit",
  onEdit(transaction) {
    const result = applyEditTransaction(spec, transaction);
    if (!result.ok) return; // a stale or invalid edit: keep the current spec
    spec = result.spec;
    undoStack.push(result.inverse);
    chart.update(spec);
  },
});

function undo() {
  const inverse = undoStack.pop();
  if (!inverse) return;
  const result = applyEditTransaction(spec, inverse);
  if (!result.ok) return;
  spec = result.spec;
  chart.update(spec);
}

The helper checks every operation before it returns anything and never changes the spec you pass in. When one operation fails, for example because the annotation it targets has been removed or reordered since the gesture began, result.ok is false and result.error names the operation’s path and the reason. Save result.spec wherever your page keeps the document; React, Vue and Svelte take the same handler as onEdit, @edit and onedit.

Editing text

0.0500M1.0B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.46B1.41B347M285M255M240M217M175M144M135MIndia and China each have more than 1.4billion peoplePopulation of the 10 most populous countries, 2025, UN projection0.0500M1.0B1.5BPopulationIndiaChinaUnited StatesIndonesiaPakistanNigeriaBrazilBangladeshRussiaEthiopia1.46B1.41B347M285M255M240M217M175M144M135MIndia and China each have more than 1.4billion peoplePopulation of the 10 most populous countries, 2025, UN projection
Select the title, press Enter, type a new one and press Enter again. Source: UN Population Division, World Population Prospects 2024.
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1463000000
    },
    {
      "country": "China",
      "population": 1410000000
    },
    {
      "country": "United States",
      "population": 347000000
    },
    {
      "country": "Indonesia",
      "population": 285000000
    },
    {
      "country": "Pakistan",
      "population": 255000000
    },
    {
      "country": "Nigeria",
      "population": 240000000
    },
    {
      "country": "Brazil",
      "population": 217000000
    },
    {
      "country": "Bangladesh",
      "population": 175000000
    },
    {
      "country": "Russia",
      "population": 144000000
    },
    {
      "country": "Ethiopia",
      "population": 135000000
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}

export function Example() {
  return <Chart spec={spec} interaction="edit" onEdit={onEdit} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}
</script>

<template>
  <Chart :spec="spec" interaction="edit" @edit="onEdit" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}
</script>

<Chart {spec} interaction="edit" onedit={onEdit} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { EditTransaction, ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
};

// Each finished edit arrives as one transaction. Apply it to your spec with
// applyEditTransaction and redraw: https://openchart.steamfield.io/docs/behavior/edit-mode/
function onEdit(transaction: EditTransaction) {
  console.log(transaction.ops);
}

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec, { interaction: "edit", onEdit });

Select the title, the subtitle or a text annotation and press Enter, or double-click it, to edit its text in place. Enter commits the new text as one transaction and Escape cancels. Double-clicking an empty spot in the plot area opens the same editor and adds a new text annotation there.

Keyboard

Key In edit mode
Tab, Shift+Tab Select the next or previous element: each chrome text the chart shows (title, subtitle, source, byline, footer), each annotation, then the legend. Past the last one, focus leaves the chart.
Arrow keys Nudge the selected text annotation 1px.
Shift+arrow keys Nudge it 10px.
Enter Edit the selected element’s text.
Delete, Backspace Remove the selected element.
Escape Close the text editor, or clear the selection.

A run of nudges becomes one transaction 500 milliseconds after the last arrow key is released, or sooner if the reader selects something else or focus leaves the chart. Because the arrow keys nudge, edit mode turns off the arrow-key navigation readers use to step through data points; select mode keeps it.

Editing a playback chart

A chart with encoding.time mounted in edit mode never plays by itself. It opens on the final period with the player below it, so an author can press Play or scrub to check another period while editing. Starting a drag pauses the playback.

Edits to the title, annotations and legend work as on any chart. When your onEdit handler applies one with update, the chart keeps its current period and recomputes its axes and colors over every period, so they stay fixed while it plays. See Playback for the player and its controls.

Fields that matter

Field What it does
interaction Mount option. "edit" allows selection plus every edit gesture. An onEdit handler alone also turns it on.
onEdit Receives one EditTransaction per finished gesture.
applyEditTransaction Applies a transaction to a copy of the spec and returns the new spec and its inverse, or an error.
revision Mount option for your own bookkeeping, stamped on each transaction emitted after it is set.
annotations[].id A stable id that goes into each transaction’s expect, so an edit made before the list changed is rejected as stale instead of applied to the wrong annotation.
  • Events reference covers persisting edits and the other handlers.
  • Annotations shows the callouts, reference lines and ranges a reader can move.
  • Line covers annotated line charts.