Editing in the chart

Users double-click a text in a drawn chart, such as its title or a series name, and type a new one in a text field over it. This needs no setup and works while the editor is editable.
New tab

Renaming a text

Each text goes through its command:

TextWritten by, as
The titleupdateChartSpec, spec.title
An axis titleupdateChartAxis, that axis's title
A series name in the legendupdateChartData, its column header
A category label, or a pie sliceupdateChartData, its row's label cell

Enter, or moving the focus out, writes the trimmed text in one undo step and selects the chart. An empty or unchanged text writes nothing, and Escape cancels. A renamed column keeps its id, so its spec.series entry still applies. Some charts behave differently:

  • encoding.seriesIn: 'rows'. A series name is a row's label cell, a category a column header.
  • A read-only Data Source. Only the title and axis titles open a field, because the source owns the data.
  • Value axis numbers. They are not stored, so they open nothing.
  • Renderers. ECharts and D3 open the field. For your own, see Text in the chart.

Without a drawn chart

renameChartDataText, from the editor-free /core entry, renames a series or category in Chart Data. It returns new Chart Data with the text changed and leaves data as it is. It returns null when the target is not a series or category of that data, such as an unknown column or a row past the last one. updateChartData writes the result into the selected chart. editor is the one from A first chart, with the chart it inserted still selected. This renames its series Revenue to Sales:

import { parseChartSyntax, renameChartDataText } from '@tiptap-pro/extension-diagrams/core'

const { data } = parseChartSyntax('Month | Revenue\nJan | 120\nFeb | 150\nMar | 110')
const renamed = renameChartDataText({
  data,
  target: { kind: 'series', column: 'revenue' },
  text: 'Sales',
})
editor.commands.updateChartData({ data: renamed ?? data })
OptionTypeDefaultDescription
dataChartDataRequiredThe Chart Data
encodingChartEncodingNoneThe chart's spec.encoding, so seriesIn: 'rows' counts
targetChartTextTargetRequired{ kind: 'series', column } with a column id, or { kind: 'category', row } counted from 0
textstringRequiredThe new text

Use the column's id from data.columns. A new chart's ids come from its headers, such as revenue for Revenue, and the same header twice gives revenue and revenue-2. A rename keeps the id, so after the example the column is still revenue.

With encoding: { seriesIn: 'rows' }, the targets follow the drawn chart. A series is a row, and its column is the id its label gives, such as jan for Jan. A category is a column, and its row counts the series columns from 0.