Chart syntax
The chart syntax is how a chart is written as text: one row per line, cells split by a comma, semicolon, pipe or tab, and an optional first line that names the type. It is what you type into the Quick Input, and its format id is
chart-syntax.bar
Month | Online | Retail
Jan | 42 | 30
Feb | 48 | 28The type line
- A first line that holds only a chart type,
bar,pieor a type registered withchartTypes, names the type, as the header of Mermaid source does. Case and the space around it do not matter. - Without it, the chart takes the type it was given, or
chart.defaultType. - It wins over a type given separately, so the text alone says what the chart becomes.
- It stays in the text.
editChartAsTextwrites it back as the first line, andsetDiagramInputTyperewrites it.
How the rows are read
| Rule | How it reads |
|---|---|
| Delimiters | Comma, semicolon, pipe and tab, set with the chart.delimiters option |
| Header | The first row is the header when it holds no number outside the first column |
{H} | In front of a cell, marks it as a header cell explicitly |
| Labels | The first column holds the labels |
{L} | In front of a cell, marks it as a label explicitly |
| Empty cell | A missing value |
| Not a number | A cell that is not a number is kept as text |
| Quotes | A cell in double quotes may contain the delimiter |
| Lists | A leading bullet or number (•, -, *, 1.) is ignored, so a pasted list reads as rows |
| Single column | Lines without any delimiter are one series of values, labeled 1, 2, 3 and so on |
Which delimiter splits
- The first line that contains an allowed delimiter decides it for the whole block.
- On a line with several, a tab, a pipe or a semicolon wins over a comma.
- So
Jan ; 1,5splits at the semicolon and reads1,5as a number.
Numbers
- With
decimalSeparator: 'auto',1.5is read as 1.5. - With
'auto',1,5is read as 1.5 too, when the delimiter is not a comma. - Set
decimalSeparatorto'.'or','to fix the decimal separator. - Thousands separators are not read.
Errors
- A quote that is opened and never closed is an error on that line.
- An error names the line, for example
Line 3: A quote is opened and never closed.
In code
The same text works without the box, through insertChartFrom, updateChartFrom and the
tools for a model:
editor.commands.insertChartFrom({ format: 'chart-syntax', content: 'bar\nJan | 42\nFeb | 48' })editor.storage.diagrams.read({ format: 'chart-syntax' }) writes a chart's data in the chart syntax,
with the delimiter it was typed with.
| Delimiter | Written as |
|---|---|
| Pipe | | |
| Tab | A bare tab |
| Any other | The delimiter, then a space |