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.
The charts directory
Section titled “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:
shareshare.jsonshare.vl.json
The name is a file name in the directory. usql refuses a name that is a full path, or that holds ...
A donut chart
Section titled “A donut chart”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 salesunion all select 'tea', sum(tea) from salesunion all select 'cocoa', sum(cocoa) from sales\chart spec=share file=share.svgA line for each group
Section titled “A line for each group”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 salesunion all select month, 'tea', tea, month_no from salesorder by month_no \chart spec=trend file=trend.svg"sort": null keeps the months in the order of the rows. Without it, Vega-Lite sorts them by name.
The data
Section titled “The data”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.
precrounds them, as it does for the other charts. A NULL in a column of numbers isnull. - Any other column gives text. A NULL in it is an empty string.
- A layer with its own
datakeeps it.
The options
Section titled “The options”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.
What usql draws
Section titled “What usql draws”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.
Errors
Section titled “Errors”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.jsonA 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.