Skip to content

Charts from a Vega-Lite file

\chart spec=NAME draws the chart that a Vega-Lite file describes, with the rows of the query as its data. Use it for a chart that the other options of \chart cannot make, such as a donut, or a line for each value of a column.

Draw a donut chart and a line chart from two Vega-Lite files of the charts directory.

usql reads the files of the directory that the setting charts.path names. The default is charts, in the same directory as config.yaml. On Linux, that is ~/.config/usql/charts. Create the directory, and put a Vega-Lite file in it.

spec=share reads the first file of these that exists:

  1. share
  2. share.json
  3. share.vl.json

The name is a file name in the directory. usql refuses a name that is a full path, or that holds ...

This file is charts/share.json. It describes a donut: an arc with a hole, whose angle comes from the column cups, and whose color comes from the column drink.

{
"title": "Share of the drinks sold",
"mark": { "type": "arc", "innerRadius": 60 },
"encoding": {
"theta": { "field": "cups", "type": "quantitative" },
"color": { "field": "drink", "type": "nominal" }
}
}

The query gives one row for each drink, with the columns that the file names:

select 'coffee' as drink, sum(coffee) as cups from sales
union all select 'tea', sum(tea) from sales
union all select 'cocoa', sum(cocoa) from sales
\chart spec=share file=share.svg

A donut chart titled Share of the drinks sold, with one arc each for coffee, tea and cocoa.

This file is charts/trend.vl.json. It draws a line for each value of the column drink, with the months in the order of the rows, and gives its own size.

{
"title": "Cups sold each month",
"width": 520,
"height": 260,
"mark": { "type": "line", "point": true },
"encoding": {
"x": { "field": "month", "type": "ordinal", "sort": null },
"y": { "field": "cups", "type": "quantitative" },
"color": { "field": "drink", "type": "nominal" }
}
}
select month, 'coffee' as drink, coffee as cups, month_no from sales
union all select month, 'tea', tea, month_no from sales
order by month_no \chart spec=trend file=trend.svg

A line chart titled Cups sold each month, with a line for coffee and a line for tea from January to June.

"sort": null keeps the months in the order of the rows. Without it, Vega-Lite sorts them by name.

usql replaces the data of the file with the rows of the query. Each row is one object, with the names of the columns as its keys. A file names the columns in the field of each channel.

  • A column of numbers gives numbers. prec rounds them, as it does for the other charts. A NULL in a column of numbers is null.
  • Any other column gives text. A NULL in it is an empty string.
  • A layer with its own data keeps it.

These options of \chart work with spec:

Option What it does with spec
file writes the chart to an SVG file, as in Save a chart to a file
size the size in pixels, as WIDTHxHEIGHT, used only when the file gives no width and no height
bg the color of the background, used only when the file gives no background
prec the number of decimal places of the values

title, subtitle and type are an error with spec, because the file says how to draw the chart. Put the title in the file.

usql draws the chart itself, with no browser. It draws the marks bar, line, area, point, circle, square, tick, rect, rule, text, arc and trail, and the composite marks boxplot, errorbar and errorband. It leaves out a feature of Vega-Lite that it does not support, and draws the rest of the chart.

A name that is not in the directory gives this error, with the names that usql tried:

error: \chart: no spec "nope" in charts.path: tried nope, nope.json, nope.vl.json

A file that is not a JSON object gives an error that names the file. usql reads the file before it runs the query, so the query does not run when the file is wrong.