Charts

A chart is a chart Node that holds its data (Chart Data) and how to show it (Chart Spec). This page lists the chart types and how to insert a chart. To insert a first chart, see Getting started.
Pro ExtensionAlphaPilot program
New tab

Quick start

editor.commands.insertFromText({ text: 'Month | Sales\nJan | 2\nFeb | 3' })

More about charts

Each page beside this one covers one part of the Chart Spec.

PageCovers
AppearanceTitle, colors, frame, caption, series line styles, the Renderer
Editing in the pictureRenaming the title, axis titles, series and categories with a double click
AxesAxis titles and ranges, grid and axis lines, ticks, category labels
Reference lines and bandsReference lines and bands, such as a target or a range to stay in
LooksTaking the look of one chart to another, or to a new one
ExportSVG, PNG, WebP, Word, and replacing a chart with an image
Size and viewSize, alignment, zoom, pan, maximize, narrow screens

Chart types

A chart stores its type as spec.type. Each type reads the same rows in one of these ways:

TypeReadsWord document
line, area, scatterA label column and one or more seriesWord chart
barThe same. spec.orientation: 'horizontal' lays the bars on their sideWord chart
comboThe same. See Combo chartsWord chart
radarA label per spoke, a closed line per seriesWord chart
pie, doughnutA label column and one series, the parts of a wholeWord chart
funnelA label per stage and one value, widest firstPicture
gaugeOne value. spec.range: { min, max } sets its range, 0 to 100 by defaultPicture
treemap, treeLabels as paths and one value per row. Europe / Germany places Germany under EuropePicture
sankey, networkOne link per row: the label column is the source, the next column the target, the one after it the weightPicture
matrixWords in a grid, see MatrixWord table
  • A Renderer without the type leaves the chart to the next Renderer that has it. Not every Renderer draws every type.
  • A sankey link that would close a loop is dropped.
  • A type you register with the chartTypes option is stored, offered to a model and drawn by any Renderer that lists it.
  • A type this editor does not know is kept in the document, unchanged.

Combo charts

FieldValues
spec.series[columnId].as'bar' (default) or 'line'
spec.series[columnId].axis'right' puts the series on a second value axis

Matrix

matrix is a grid of words such as a RACI matrix: the tasks in the label column, a column per role and a letter in each cell.

Task   | Lead | Team | Board
Plan   | A    | R    | I
Build  | C    | R    |
  • Each distinct cell text gets a color from the series colors.
  • R, A, C and I come first, in that order.
  • A matrix is drawn by the ECharts Renderer.
  • A matrix cannot be written as Mermaid.
  • In a Word document, a matrix is a table with shaded cells.

How a chart is stored

A chart's attributes hold everything needed to draw it again:

AttributeContent
dataChart Data: columns (id, label, role of label or series) and rows
specChart Spec: type, title, description, legend, palette, axes, references, stacked, innerRadius, orientation, range, series, and renderer for options a single Renderer understands
rendererThe id of the Renderer chosen for this chart, or null for the default
sourceThe Data Source the data was read from, or null
delimiterThe delimiter the data was typed with, so it is shown the same way when edited as text
previewUrlAn optional image URL written into the HTML output

The drawn picture, its size and its loading state are never stored.

In HTML

  • A chart is a <figure data-type="chart">.
  • Its data is a <table>, so the numbers survive an HTML or Markdown export and are read by a screen reader.
  • Its title is the <figcaption>.
  • The spec and the source travel in data-tt-chart-* attributes.

Commands

insertChart

Inserts a chart at the selection, or at pos, and selects it.

editor.commands.insertChart({
  type: 'bar',
  data: {
    columns: [
      { id: 'month', label: 'Month', role: 'label' },
      { id: 'revenue', label: 'Revenue', role: 'series' },
    ],
    rows: [
      ['Jan', 120],
      ['Feb', 150],
    ],
  },
})
OptionTypeDescription
dataChartDataThe Chart Data
specPartial<ChartSpec>The Chart Spec. Wins over look where both set a field
typeChartTypeShortcut for spec.type
rendererstring | nullThe id of the Renderer to draw with. null uses the default
sourceChartSource | nullThe Data Source the chart follows
delimiterstring | nullThe delimiter the data was typed with
posnumberThe position to insert at. Defaults to the selection
lookChartLookA saved look the chart starts with, see applyChartLook
  • An empty paragraph at the selection is replaced.
  • With a chart selected, the new one goes after it.

insertChartFrom

Inserts a chart written in a registered format.

editor.commands.insertChartFrom({
  format: 'chart-syntax',
  content: 'Month | Revenue\nJan | 120\nFeb | 150',
})
OptionTypeDescription
formatstring'chart-json', 'chart-syntax', 'mermaid', or one you added
contentstringThe text in that format
typeChartTypeThe type, used when the content does not say
rendererstring | nullThe id of the Renderer to draw with
posnumberThe position to insert at. Defaults to the selection

Returns false when the content cannot be read.

importMermaidChart

Inserts a chart from Mermaid chart source.

editor.commands.importMermaidChart({ source: 'pie\n  "Dogs" : 386\n  "Cats" : 85' })
OptionTypeDescription
sourcestringThe Mermaid source
posnumberThe position to insert at. Defaults to the selection

These Mermaid kinds are charts:

First lineChart typeData
piepieA row per slice
xychart-betabar or lineA row per x-axis category, a series per bar or line
sankey-betasankeyA row per flow: source, target, value
radar-betaradarA row per axis, a series per curve; min, max kept
treemap-betatreemapA row per node with a value, its label the path, A / B
  • A title comes from title, or from frontmatter for sankey and treemap.
  • How Mermaid colors it (classDef, showLegend, ticks, graticule) is left out.
  • Back to Mermaid, toMermaidSource and the mermaid format write the same kinds.
  • A value below zero has no place in Mermaid's pie, radar and treemap, which read no minus sign. Written back, a pie slice or a treemap node below zero is left out, and a radar value is written as zero.

Pasting Mermaid chart source calls it, when the source is:

  • bare: one of these kinds on the first line and at least one data row,
  • in a ```mermaid fence,
  • or a code block with the language mermaid.

Pasting into a code block keeps the text.

Returns false for any other Mermaid diagram.

convertCodeBlockToChart

Replaces a code block with the chart its Mermaid chart source describes.

if (editor.can().convertCodeBlockToChart()) editor.commands.convertCodeBlockToChart()
OptionTypeDescription
posnumberA position inside the code block. Defaults to the selection
  • Any Node with code: true in its spec counts as a code block.
  • editor.can().convertCodeBlockToChart() says whether to offer it.
  • Mod-Enter in the code block runs it too, unless codeBlockShortcut says otherwise.

Returns false for a code block holding anything else.

insertDiagramInput

Opens an empty Quick Input at the selection, as typing the trigger does.

editor.chain().focus().insertDiagramInput({ type: 'pie' }).run()
OptionTypeDescription
typeChartTypeThe chart type the box gets