---
title: Draw charts and maps
description: How to turn SQL results into Vega-Lite charts and maps, written the same way in reports and in AI chat
---

Queria draws a chart from an SQL result with a [Vega-Lite](https://vega.github.io/vega-lite/) chart spec (JSON). A [report](/en/reports)'s `chart` block and the `chart` of `show_chart` in AI chat take the same spec. Maps are drawn with Queria's map ([Maps](#maps)).

## In a report

Name an SQL block (`sql id=<name>`) and read its result from a `chart` block with `"data": { "name": "<name>" }`. Put column names in the `field` of each `encoding` channel. A named SQL block is not shown as a table.

````md
```sql id=pref_pop
SELECT prefecture_name, population FROM ... ORDER BY population DESC LIMIT 10
```

```chart caption="Most populous prefectures"
{
  "data": { "name": "pref_pop" },
  "mark": "bar",
  "encoding": {
    "y": { "field": "prefecture_name", "type": "nominal", "sort": "-x", "title": null },
    "x": { "field": "population", "type": "quantitative", "title": "Population" }
  }
}
```
````

- To show the table too, write `sql id=<name> display`
- `caption` is optional
- The spec is JSON. Quote keys and strings with `"`
- Leave out the width: the chart fits the page. Set the height with `height`
- Inside `layer`, `hconcat` and `vconcat`, each view can read a different SQL block. Small values such as the ends of a reference line can be written inline as `"data": { "values": [...] }`
- External files (`data.url`), image marks and maps (`geoshape`, `projection`) are not accepted. Draw maps with a `map` block

## In AI chat

In AI chat, the AI passes the same spec as the `chart` of `show_chart`. There is one result, read with `"data": { "name": "result" }`. A collapsed table comes under the chart. The AI also gets the way to keep the same chart in a report (`sql id=result` and a `chart` block).

```json
{
  "data": { "name": "result" },
  "mark": "bar",
  "encoding": {
    "x": { "field": "name", "type": "nominal" },
    "y": { "field": "value", "type": "quantitative" }
  }
}
```

## Common charts

| To show | `mark` | Main settings |
| --- | --- | --- |
| Rankings and comparisons | `bar` | For horizontal bars, names in `y` (`"sort": "-x"` for largest first) and values in `x` |
| Time series | `line` | Dates or years in `x` (`"type": "temporal"` or `"ordinal"`) |
| Relation between two values | `point` | Numeric columns in `x` and `y` |
| Breakdown | `arc` | Values in `theta`, names in `color` |
| A number card | `text` | The value column in `text`, `"format": ","` for thousands separators |

To draw one series per group, return the SQL in long form (a group column and a value column) and put the group column in `color`.

## Highlight values

Marks are drawn in gray. List values in `usermeta.highlight` of the spec to draw their marks in purple. A value is an axis value (a bar's name or a year) or a series name in `color`, up to 20 values. When none of the values is in the data, the chart is drawn without highlighting.

```json
{
  "data": { "name": "pref_pop" },
  "mark": "bar",
  "usermeta": { "highlight": ["東京都"] },
  "encoding": {
    "y": { "field": "prefecture_name", "type": "nominal", "sort": "-x", "title": null },
    "x": { "field": "population", "type": "quantitative", "title": "Population" }
  }
}
```

In AI chat, the AI passes the same values as the `highlight` argument of `show_chart`. The `chart` block that `show_chart` returns for saving to a report includes these values in `usermeta.highlight`.

## Maps

Values by area and points go on Queria's map. The SQL returns an area code column, or longitude and latitude columns, and Queria joins the boundary data.

- In a report, place them with a `map` block. See [Put SQL results on a map](/en/reports#put-sql-results-on-a-map)
- In AI chat, the AI adds `map` to `show_chart`. It is written like a report's layer, except that a column returning shapes as GeoJSON strings (`geojson`) works only in reports

```json
{ "area": "city", "code": "lg_code", "color": "population", "label": "city_name", "name": "Population" }
```

- `area` is `prefecture` (2 digits), `city` (5- or 6-digit local government code) or `small_area` (KEY_CODE of the 町丁目), and `code` names the column holding that code
- For points, give longitude and latitude columns in `lng` and `lat` instead of `area` and `code`
- `color` shades by a numeric column. Larger values are shaded darker blue. When the column has both positive and negative values, such as changes or net migration, 0 is white, negative values are red, positive values are blue, and the legend shows 0 in the middle. Values beyond either end (2% of all values on each side) take the end color, and the legend marks the ends with "≤" and "≥". When every value is positive and half or more of the areas would end up in the palest colors (such as prefecture populations, where Tokyo stands far above the rest), the colors follow a log scale and the legend shows the middle value. The `label` column shows on hover
- `color` can also name a text column such as a type. The first 7 values in the order they appear in the SQL result each get a color and are listed in the legend; the rest are grouped in gray as "Other" ("その他" on the Japanese site). Use `ORDER BY` to set the order
- With a map in AI chat, the AI also gets the way to keep the same map in a report. Ask it to save the report and it can create it as is

For spatial SQL such as joining boundaries and facilities, see the [cookbook recipe for geographic data](/en/cookbook/nlftp/spatial).

## When a chart does not draw

- A column that is not in the result cannot be drawn. Use the SQL alias as the column name in `field`
- The name in `"data": { "name": … }` must match an `sql id=`. In AI chat it is `result`
- If numbers come as strings, the axis is not numeric. `CAST` them in SQL
- Too many rows make it heavy. Aggregate in SQL before charting
