Charts functions in the Textplot DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Fixtures for textplot extension SQL examples.
-- Provides small, deterministic data so the validator can parse and
-- execute the snippets in cookbook.mdx, technical-details.mdx, and
-- metadata.json that reference real tables.
-- A numeric column with a roughly normal-looking distribution. Built
-- deterministically from generate_series + a sum of trig terms so the
-- spread is realistic (a single hump centred near zero with tails) and
-- the same numbers come out every run. Used by the `tp_density(...)`
-- "see the shape of a column" examples.
CREATE OR REPLACE TABLE measurements AS
SELECT (
sin(i * 0.13)
+ sin(i * 0.29 + 1.7)
+ sin(i * 0.71 + 0.4)
+ sin(i * 1.13 + 2.1)
)::DOUBLE AS value
FROM range(0, 200) t(i);
-- A handful of hosts each with a 24-point CPU history array and a
-- disk-utilization percentage. Used by the `tp_sparkline(cpu_history)`
-- and `tp_bar(disk_pct, ...)` per-row examples.
CREATE OR REPLACE TABLE hosts_status AS
SELECT
'host-' || lpad(h::VARCHAR, 2, '0') AS host,
[
40 + 20 * sin(h * 0.5 + 0.0) + 5 * sin(h * 1.7 + 0.1),
40 + 20 * sin(h * 0.5 + 0.3) + 5 * sin(h * 1.7 + 0.4),
40 + 20 * sin(h * 0.5 + 0.6) + 5 * sin(h * 1.7 + 0.7),
40 + 20 * sin(h * 0.5 + 0.9) + 5 * sin(h * 1.7 + 1.0),
40 + 20 * sin(h * 0.5 + 1.2) + 5 * sin(h * 1.7 + 1.3),
40 + 20 * sin(h * 0.5 + 1.5) + 5 * sin(h * 1.7 + 1.6),
40 + 20 * sin(h * 0.5 + 1.8) + 5 * sin(h * 1.7 + 1.9),
40 + 20 * sin(h * 0.5 + 2.1) + 5 * sin(h * 1.7 + 2.2),
40 + 20 * sin(h * 0.5 + 2.4) + 5 * sin(h * 1.7 + 2.5),
40 + 20 * sin(h * 0.5 + 2.7) + 5 * sin(h * 1.7 + 2.8),
40 + 20 * sin(h * 0.5 + 3.0) + 5 * sin(h * 1.7 + 3.1),
40 + 20 * sin(h * 0.5 + 3.3) + 5 * sin(h * 1.7 + 3.4),
40 + 20 * sin(h * 0.5 + 3.6) + 5 * sin(h * 1.7 + 3.7),
40 + 20 * sin(h * 0.5 + 3.9) + 5 * sin(h * 1.7 + 4.0),
40 + 20 * sin(h * 0.5 + 4.2) + 5 * sin(h * 1.7 + 4.3),
40 + 20 * sin(h * 0.5 + 4.5) + 5 * sin(h * 1.7 + 4.6),
40 + 20 * sin(h * 0.5 + 4.8) + 5 * sin(h * 1.7 + 4.9),
40 + 20 * sin(h * 0.5 + 5.1) + 5 * sin(h * 1.7 + 5.2),
40 + 20 * sin(h * 0.5 + 5.4) + 5 * sin(h * 1.7 + 5.5),
40 + 20 * sin(h * 0.5 + 5.7) + 5 * sin(h * 1.7 + 5.8),
40 + 20 * sin(h * 0.5 + 6.0) + 5 * sin(h * 1.7 + 6.1),
40 + 20 * sin(h * 0.5 + 6.3) + 5 * sin(h * 1.7 + 6.4),
40 + 20 * sin(h * 0.5 + 6.6) + 5 * sin(h * 1.7 + 6.7),
40 + 20 * sin(h * 0.5 + 6.9) + 5 * sin(h * 1.7 + 7.0)
]::DOUBLE[] AS cpu_history,
-- Spread disk_pct across 10..98 so the threshold-coloured bar
-- example exercises the green / yellow / red zones.
(10 + (h * 13) % 89)::DOUBLE AS disk_pct
FROM range(0, 8) t(h);
Function category
Charts
4 functionsRender numeric values and arrays as Unicode/ASCII charts directly in your query results — bars, sparklines, and density plots that fit in a single VARCHAR cell.
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
value
|
Type
DOUBLE
|
Mode Positional | Description Numeric value to visualize. The only positional argument. |
Argument
min
|
Type
DOUBLE
|
Mode Named |
Description
Default: 0
Minimum of the value range. Must be less than max.
|
Argument
max
|
Type
DOUBLE
|
Mode Named | Description Default: 1 Maximum of the value range. |
Argument
width
|
Type
BIGINT
|
Mode Named | Description Default: 10 Bar width in characters. |
Argument
on
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Custom string for filled blocks; the default '' uses the colored shape glyph. Must be quoted in SQL — "on" — because on is a reserved keyword.
|
Argument
off
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Custom string for unfilled blocks; the default '' uses the colored shape glyph. Must be quoted as "off".
|
Argument
on_color
|
Type
VARCHAR
|
Mode Named | Description Default: 'red' Color name for filled portion (e.g. 'red', 'green', 'blue', 'yellow'). |
Argument
off_color
|
Type
VARCHAR
|
Mode Named | Description Default: 'white' Color name for unfilled portion. |
Argument
shape
|
Type
VARCHAR
|
Mode Named | Description Default: 'square' Block shape: 'square', 'circle', or 'heart'. |
Argument
filled
|
Type
BOOLEAN
|
Mode Named | Description Default: true Fill all blocks up to the value (true) or just mark the endpoint (false). |
Argument
thresholds
|
Type
ANY
|
Mode Named |
Description
Default: []
List of two-field structs (threshold, color) for value-dependent coloring; each block uses the highest threshold ≤ value. Field names don't matter — [(90, 'red'), (0, 'green')] and [{'threshold': 90, 'color': 'red'}] both work. The default [] means no thresholds.
|
Description
Render a horizontal bar showing a numeric value as a fraction of a min/max range. Configurable width, shape (square/circle/heart), foreground/background colors, custom characters, and threshold-based coloring. Returns a VARCHAR you can drop into any SELECT. The first argument is positional; every option is a keyword-only named parameter with a default.
SELECT tp_bar(0.5);
Output
| tp_bar(0.5) |
|---|
| 🟥🟥🟥🟥🟥⬜⬜⬜⬜⬜ |
SELECT tp_bar(75, min := 0, max := 100, width := 20);
Output
| tp_bar(75, min := 0, max := 100, width := 20) |
|---|
| 🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥⬜⬜⬜⬜⬜ |
SELECT tp_bar(0.8, shape := 'circle', on_color := 'green', off_color := 'white');
Output
| tp_bar(0.8, shape := 'circle', on_color := 'green', off_color := 'white') |
|---|
| 🟢🟢🟢🟢🟢🟢🟢🟢⚪⚪ |
SELECT tp_bar(85, min := 0, max := 100, thresholds := [{'threshold': 90, 'color': 'red'}, {'threshold': 70, 'color': 'yellow'}, {'threshold': 0, 'color': 'green'}]);
Output
| tp_bar(85, min := 0, max := 100, thresholds := list_value(struct_pack(threshold := 90, color := 'red'), struct_pack(threshold := 70, color := 'yellow'), struct_pack(threshold := 0, color := 'green'))) |
|---|
| 🟨🟨🟨🟨🟨🟨🟨🟨🟨⬜ |
SELECT tp_bar(0.7, "on" := '█', "off" := '░', width := 15);
Output
| tp_bar(0.7, "on" := '█', "off" := '░', width := 15) |
|---|
| ███████████░░░░ |
SELECT tp_bar(0.6, shape := 'heart', on_color := 'red');
Output
| tp_bar(0.6, shape := 'heart', on_color := 'red') |
|---|
| ❤️❤️❤️❤️❤️❤️🤍🤍🤍🤍 |
Related functions
- tp_density() — Render an array of numeric values as a density plot — a compact distribution visualization where the character intensity at each x-position reflects how many input values fall in that bin
- tp_sparkline() — Render an array of numeric values as a sparkline — a compact one-line trend chart
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
values
|
Type
DOUBLE[]
|
Mode Positional | Description Array of numeric values to plot the distribution of. |
Argument
width
|
Type
BIGINT
|
Mode Named | Description Default: 20 Plot width in characters. |
Argument
style
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Character-set style. The default '' means 'shaded' unless graph_chars is given (the two are mutually exclusive): 'shaded' (default ░▒▓█), 'ascii' ( .:+#@), 'dots' ( .•●), 'height' ( ▁▂▃▄▅▆▇█), 'circles' (⚫⚪🟡🟠🔴), 'safety', 'rainbow_circle' (⚫🟤🟣🔵🟢🟡🟠🔴⚪), 'rainbow_square', 'moon', 'sparse', or 'white'.
|
Argument
marker
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Character drawn at the bin containing marker_value. Requires marker_value; when only marker_value is given it defaults to ▼.
|
Argument
marker_value
|
Type
DOUBLE
|
Mode Named |
Description
Default: NaN
Value to highlight with marker. Must be finite; the default NaN means "no marker", so marker_value := 'nan'::DOUBLE is the same as leaving it out.
|
Argument
graph_chars
|
Type
VARCHAR[]
|
Mode Named |
Description
Default: []
Custom list of characters for density levels, lowest to highest. Mutually exclusive with style. The default [] means "use style".
|
Description
Render an array of numeric values as a density plot — a compact distribution visualization where the character intensity at each x-position reflects how many input values fall in that bin. Multiple character-set styles, including ASCII for terminal compat and emoji for visual punch. The first argument is positional; every option is a keyword-only named parameter with a default.
SELECT tp_density([1, 2, 3, 4, 5, 4, 3, 2, 1]);
Output
| tp_density(list_value(1, 2, 3, 4, 5, 4, 3, 2, 1)) |
|---|
| █ █ █ █ ▒ |
SELECT tp_density([1, 2, 3, 2, 1], width := 30, style := 'height');
Output
| tp_density(list_value(1, 2, 3, 2, 1), width := 30, style := 'height') |
|---|
| █ █ ▄ |
SELECT tp_density([1, 5, 3, 8, 2], style := 'ascii');
Output
| tp_density(list_value(1, 5, 3, 8, 2), style := 'ascii') |
|---|
| @ @ @ @ @ |
SELECT tp_density([1, 5, 3, 8, 2], style := 'dots');
Output
| tp_density(list_value(1, 5, 3, 8, 2), style := 'dots') |
|---|
| ● ● ● ● ● |
SELECT tp_density([1, 5, 3, 8, 2], style := 'rainbow_circle');
Output
| tp_density(list_value(1, 5, 3, 8, 2), style := 'rainbow_circle') |
|---|
| ⚪⚫⚪⚫⚫⚪⚫⚫⚫⚫⚫⚪⚫⚫⚫⚫⚫⚫⚫⚪ |
SELECT tp_density([1, 2, 2, 3, 3, 3, 4, 4, 5], width := 20, marker_value := 4.5);
Output
| tp_density(list_value(1, 2, 2, 3, 3, 3, 4, 4, 5), width := 20, marker_value := 4.5) |
|---|
| ░ ▓ █ ▓ ▼ ░ |
SELECT tp_density([1, 5, 3, 8, 2], graph_chars := ['.', 'o', 'O'], width := 16);
Output
| tp_density(list_value(1, 5, 3, 8, 2), graph_chars := list_value('.', 'o', 'O'), width := 16) |
|---|
| O.O.O....O.....O |
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
values
|
Type
DOUBLE[]
|
Mode Positional | Description Array of numeric values, ordered (typically by time). |
Argument
width
|
Type
BIGINT
|
Mode Named | Description Default: 20 Sparkline width in characters. |
Argument
mode
|
Type
VARCHAR
|
Mode Named | Description Default: 'absolute' Visualization mode: 'absolute' (height ∝ value), 'delta' (up/same/down), or 'trend' (signed magnitude). |
Argument
theme
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Character-set theme. The default '' picks the mode's default theme: utf8_blocks for absolute, arrows for delta and trend. Options vary by mode — absolute: utf8_blocks, ascii_basic, hearts, faces. delta: arrows, triangles, ascii_arrows, math, faces, thumbs, trends, simple. trend: arrows, ascii, slopes, intensity, faces, chart.
|
Description
Render an array of numeric values as a sparkline — a compact one-line trend chart. Three modes: absolute (height ∝ value), delta (direction of change), trend (direction with magnitude). Each mode has multiple themes: arrows, faces, slopes, hearts, and more. The first argument is positional; every option is a keyword-only named parameter with a default. To build the array from rows, use list(v ORDER BY t) or the aggregate tp_sparkline_agg (DuckDB 2.0 build).
SELECT tp_sparkline([1, 3, 2, 5, 4, 6, 2, 1]);
Output
| tp_sparkline(list_value(1, 3, 2, 5, 4, 6, 2, 1)) |
|---|
| ▃▃▂▂▂▆▆▅▅▅██▂▂▂ |
SELECT tp_sparkline([45.2, 47.1, 46.8, 49.3, 52.1, 48.7], width := 20, theme := 'utf8_blocks');
Output
| tp_sparkline(list_value(45.2, 47.1, 46.8, 49.3, 52.1, 48.7), width := 20, theme := 'utf8_blocks') |
|---|
| ▂▂▂▂▂▂▅▅▅▅███▄▄▄ |
SELECT tp_sparkline([100, 105, 102, 108, 95], mode := 'delta');
Output
| tp_sparkline(list_value(100, 105, 102, 108, 95), "mode" := 'delta') |
|---|
| ↑↑↑↑↑↓↓↓↓↓↑↑↑↑↑↓↓↓↓↓ |
SELECT tp_sparkline([72, 75, 75, 78, 71], mode := 'delta', theme := 'faces');
Output
| tp_sparkline(list_value(72, 75, 75, 78, 71), "mode" := 'delta', theme := 'faces') |
|---|
| 😊😊😊😊😊😐😐😐😐😐😊😊😊😊😊😞😞😞😞😞 |
SELECT tp_sparkline([10, 12, 11, 15, 8], mode := 'trend', theme := 'slopes');
Output
| tp_sparkline(list_value(10, 12, 11, 15, 8), "mode" := 'trend', theme := 'slopes') |
|---|
| /////\\\\\/////\\\\\\\\\\ |
SELECT tp_sparkline([100, 110, 105, 125, 90], mode := 'trend', theme := 'intensity');
Output
| tp_sparkline(list_value(100, 110, 105, 125, 90), "mode" := 'trend', theme := 'intensity') |
|---|
| +++++-----+++++---------- |
Related functions
- tp_sparkline_agg() — Aggregate form of [`tp_sparkline`](#tp_sparkline): collects a group's numeric values and renders them as one sparkline, so you can chart raw rows without building a list first
- tp_density() — Render an array of numeric values as a density plot — a compact distribution visualization where the character intensity at each x-position reflects how many input values fall in that bin
- tp_bar() — Render a horizontal bar showing a numeric value as a fraction of a min/max range
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
value
|
Type
DOUBLE
|
Mode Positional | Description Numeric value to add to the sparkline. Any numeric type is cast to DOUBLE. |
Argument
width
|
Type
BIGINT
|
Mode Named | Description Default: 20 Sparkline width in characters (1 to 1,000,000). |
Argument
mode
|
Type
VARCHAR
|
Mode Named | Description Default: 'absolute' Visualization mode: 'absolute' (height ∝ value), 'delta' (up/same/down), or 'trend' (signed magnitude). |
Argument
theme
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
Character-set theme; the default '' picks the mode's default theme. Same themes as tp_sparkline.
|
Description
Aggregate form of tp_sparkline: collects a group's numeric values and renders them as one sparkline, so you can chart raw rows without building a list first. Put ORDER BY inside the call — after any options — to fix the plotted order; without it the order is not guaranteed. Takes the same width, mode and theme options as tp_sparkline, and tp_sparkline_agg(v ORDER BY t) returns exactly tp_sparkline(list(v ORDER BY t)). NULL, NaN and infinite values are skipped; a group with no non-NULL value returns NULL. Also works as a window function. DuckDB 2.0 build only.
SELECT tp_sparkline_agg(v, width := 12 ORDER BY t) AS trend
FROM (VALUES (1, 2), (2, 4), (3, 3), (4, 7), (5, 5), (6, 8)) AS d(t, v);
Output
| trend |
|---|
| ▃▃▁▁▇▇▄▄██ |
SELECT host, tp_sparkline_agg(cpu, width := 12 ORDER BY ts) AS cpu_trend
FROM (SELECT 'host-' || (i % 3) AS host, i // 3 AS ts, 40 + 20 * sin(i * 0.37) AS cpu FROM range(36) t(i))
GROUP BY host ORDER BY host;
Output
| host | cpu_trend |
|---|---|
| host-0 | ▄█▇▃ ▁▅█▆▂ ▃ |
| host-1 | ▅█▆▂ ▃▇█▅▁ ▄ |
| host-2 | ▇█▅▁ ▄█▇▃ ▁▆ |
SELECT tp_sparkline_agg(v, mode := 'delta', width := 5 ORDER BY t) AS moves
FROM (VALUES (1, 100), (2, 105), (3, 102), (4, 102), (5, 108), (6, 95)) AS d(t, v);
Output
| moves |
|---|
| ↑↓→↑↓ |
Related functions
- tp_sparkline() — Render an array of numeric values as a sparkline — a compact one-line trend chart
- tp_density() — Render an array of numeric values as a density plot — a compact distribution visualization where the character intensity at each x-position reflects how many input values fall in that bin