Quick start
With two charts in the editor, such as the one from the Charts quick start
and a second one inserted with insertChart() the same way:
import { chartLook } from '@tiptap-pro/extension-diagrams'
const [first, second] = editor.storage.diagrams.listCharts()
if (first && second) {
// Give the second chart the look of the first one
const look = chartLook(first.spec, first.data)
editor.commands.applyChartLook({ look, pos: second.pos })
// Insert a new line chart with the same look
editor.commands.insertChart({ type: 'line', data: second.data, look })
// A template also holds the chart type
const template = chartLook(first.spec, first.data, { type: true })
editor.commands.insertChart({ data: second.data, look: template })
}listCharts() returns every chart.
What a look holds
chartLook(spec, data, options?) returns a ChartLook with the fields the chart sets: style,
palette, background, border, legend, the grid and axis lines and category label layout in
axes, the reference lines, and the series line styles. Titles, ranges, the tick step and the data
stay with each chart.
Series styles go by position, whatever the column ids: the first series' style goes to the first
series. That is why chartLook takes the data.
| Option | Type | Default | Description |
|---|---|---|---|
references | boolean | true | false leaves out the reference lines |
type | boolean | false | true takes the chart type too |
A chart keeps every field the look does not set. A look with the type is a template: every chart
it is applied to, or starts, becomes that type, unless insertChart gets a type of its own.
Saving looks
A look is plain JSON, so your app can store it and pass it back. Applying it copies its fields into the chart, so documents need no saved looks.
readChartLook(value), from @tiptap-pro/extension-diagrams, drops what does not belong in a
saved look, and returns {} for a value that is not an object. applyChartLook and insertChart
call it themselves, so you need it only before storing a look.
Looks and styles
A chart names a style by id (spec.style) instead of copying its
fields, as a look does. So when you change a registered style, every chart that names it changes.
Commands
applyChartLook
Gives a chart a look, taken from another chart or saved earlier. This call runs inside the if of
Quick start, where look and second are set:
editor.commands.applyChartLook({ look, pos: second.pos })| Option | Type | Default | Description |
|---|---|---|---|
look | ChartLook | Required | The look, from chartLook or a saved one |
pos | number | The selected chart | The chart's position |
Returns false when there is no chart at pos, or no chart is selected.