Bar and line charts
\chart draws the result of a query as a bar chart or a line chart. The columns of the result give the labels and the series of the chart. The option type chooses bars or lines.
The recording writes each chart to an SVG file with file=, because the player of the recordings draws no images. In a terminal that draws images, the same \chart without file= draws the chart in place. The examples use the tables of a small cafe. The gallery has the SQL that makes them.
The columns of the result
Section titled “The columns of the result”usql reads the columns of the result in this way:
- A column is a number when each value in it is a number.
- The first column that is not a number gives the labels of the x axis.
- Each other column that is a number is a series. The legend shows the name of the column.
This query has a column of labels, month, and three series:
select month, coffee, tea, cocoa from sales order by month_no;\chart title='Cups sold'A result with a column of labels gives a bar chart. Each label has one bar for each series, side by side.
\chart with an empty query buffer draws the last query again. The query above ends with a semicolon, so usql runs it and prints the rows. The \chart on the next line draws the same query.
A line chart
Section titled “A line chart”type=line draws the same result as lines:
\chart title='Cups sold' type=lineA line chart shows the name of each series at the end of its line.
When each column of the result is a number, the first column gives the x axis, and usql draws a line chart:
select hour, cups from hours order by hour\chart title='Cups by hour'To draw this result as bars, add type=bar.
The labels
Section titled “The labels”The labels of the x axis are in the order of the rows. To change the order, use order by in the query.
The title of the x axis is the name of the column of labels. The title of the y axis is value, and the title of the legend is series. No option changes them. To change the name of a series, change the name of its column with as. Size, colors and titles shows it.
A NULL value
Section titled “A NULL value”A NULL in a column of numbers is a gap in its series. usql draws no bar for it, and a line has a gap there. To draw a zero in its place, replace NULL with a number in the query:
select month, coffee, coalesce(cocoa, 0) as cocoafrom salesorder by month_no\chartThe precision
Section titled “The precision”prec=N rounds each value to N decimal places before usql draws it. prec=0, the default, does not round the values.
select month, coffee / 30.0 as coffee_per_day from sales order by month_no\chart title='Cups of coffee each day' prec=1To round to whole numbers, use round in the query.