Skip to content

Charts in a script

A script can hold \chart as it holds any other meta command. Run the script with \i at the prompt, and usql draws each chart. A script that runs with -f writes a chart to a file, and draws no chart in the terminal.

This script, charts.sql, writes two charts to SVG files:

-- The sales of each month, and the cups of each hour.
select month, coffee, tea, cocoa from sales order by month_no
\chart title='Cups sold' file=sales.svg
select hour, cups from hours order by hour
\chart title='Cups by hour' file=hours.svg

The script uses the tables of the gallery.

\i runs a script at the prompt of usql:

\i charts.sql
Show charts.sql, run it with \i, and list the two SVG files that it writes.

usql writes sales.svg and hours.svg:

A bar chart of the cups of coffee, tea and cocoa sold each month.

A line chart of the cups sold each hour from 7 to 17.

In a terminal that draws images, a script with the same \chart lines without file= draws each chart in place.

-f runs the same script from the shell. \chart with file= writes each file:

Terminal window
usql -f charts.sql cafe.db
Run charts.sql with -f, and list the two SVG files that it writes.

When usql runs a script from -f, or a command from -c, it turns the terminal graphics off, so that the escape codes of an image do not go to a pipe or a file. A \chart without file= then stops with an error:

error: \chart: graphics not supported in terminal

To draw the charts in the terminal, choose the protocol of your terminal on the command line with -U, such as kitty, iterm or sixel:

Terminal window
usql -U term_graphics=kitty -f charts.sql cafe.db

usql then draws each chart without file= to the standard output. Do this only when the output goes to your terminal. Terminal graphics says more about the protocols.