Skip to content

Save a chart to a file

file=NAME writes the chart to an SVG file, and usql draws nothing in the terminal. A browser opens the file, and a page or a document can show it.

Write a bar chart of the coffee sold each month to coffee.svg, and look at the file.
select month, coffee from sales order by month_no
\chart title='Coffee sold' file=coffee.svg

usql writes the file, then prints Wrote chart to file "coffee.svg". With -q, it prints nothing. This is the file:

A bar chart titled Coffee sold, with one bar for each month from January to June.

In a terminal that draws images, the same \chart without file= draws the chart in place. The example uses the tables of the gallery.

  • usql writes SVG, whatever the name of the file ends with. Give the file a name that ends with .svg.
  • A name with no directory is in the working directory of usql.
  • The directory of the file must exist. If it does not, \chart stops with an error such as open charts/coffee.svg: no such file or directory.
  • usql replaces a file that exists, and does not ask first.
  • size sets the size of the SVG file, as it does for a chart in the terminal. The default is 800x600.

file= needs no terminal graphics. It works in a terminal that draws no images, in a script of -f, and in a pipe. Without file=, \chart in such a terminal stops with this error:

error: \chart: graphics not supported in terminal

Terminal graphics says how usql chooses the graphics.