Skip to content

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 functions

tp_bar

Scalar function Charts
Signature
tp_bar( value: DOUBLE, filled := BOOLEAN = true, max := DOUBLE = 1, min := DOUBLE = 0, off := VARCHAR = '', off_color := VARCHAR = 'white', on := VARCHAR = '', on_color := VARCHAR = 'red', shape := VARCHAR = 'square', thresholds := ANY = [], width := BIGINT = 10 ) → VARCHAR
Arguments
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
1 Simple progress bar (50% filled)
SELECT tp_bar(0.5);

Output

tp_bar(0.5)
🟥🟥🟥🟥🟥⬜⬜⬜⬜⬜
2 Custom width and range — 75 out of 100
SELECT tp_bar(75, min := 0, max := 100, width := 20);

Output

tp_bar(75, min := 0, max := 100, width := 20)
🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥⬜⬜⬜⬜⬜
3 Circle shape with explicit colors
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')
🟢🟢🟢🟢🟢🟢🟢🟢⚪⚪
4 Threshold-based coloring (85 out of 100 → yellow zone)
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')))
🟨🟨🟨🟨🟨🟨🟨🟨🟨⬜
5 Custom characters ("on" and "off" must be quoted)
SELECT tp_bar(0.7, "on" := '█', "off" := '░', width := 15);

Output

tp_bar(0.7, "on" := '█', "off" := '░', width := 15)
███████████░░░░
6 Heart shape
SELECT tp_bar(0.6, shape := 'heart', on_color := 'red');

Output

tp_bar(0.6, shape := 'heart', on_color := 'red')
❤️❤️❤️❤️❤️❤️🤍🤍🤍🤍

tp_density

Scalar function Charts
Signature
tp_density( values: DOUBLE[], graph_chars := VARCHAR[] = [], marker := VARCHAR = '', marker_value := DOUBLE = NaN, style := VARCHAR = '', width := BIGINT = 20 ) → VARCHAR
Arguments
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
1 Default shaded style
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))
█ █ █ █ ▒
2 Wide height-style plot
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')
█ █ ▄
3 ASCII style for terminals without Unicode
SELECT tp_density([1, 5, 3, 8, 2], style := 'ascii');

Output

tp_density(list_value(1, 5, 3, 8, 2), style := 'ascii')
@ @ @ @ @
4 Dot style for subtle visualization
SELECT tp_density([1, 5, 3, 8, 2], style := 'dots');

Output

tp_density(list_value(1, 5, 3, 8, 2), style := 'dots')
● ● ● ● ●
5 Rainbow emoji style
SELECT tp_density([1, 5, 3, 8, 2], style := 'rainbow_circle');

Output

tp_density(list_value(1, 5, 3, 8, 2), style := 'rainbow_circle')
⚪⚫⚪⚫⚫⚪⚫⚫⚫⚫⚫⚪⚫⚫⚫⚫⚫⚫⚫⚪
6 Highlight a value with the default ▼ marker
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)
░ ▓ █ ▓ ▼ ░
7 Custom density characters with graph_chars
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

tp_sparkline

Scalar function Charts
Signature
tp_sparkline( values: DOUBLE[], mode := VARCHAR = 'absolute', theme := VARCHAR = '', width := BIGINT = 20 ) → VARCHAR
Arguments
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
1 Absolute mode (default)
SELECT tp_sparkline([1, 3, 2, 5, 4, 6, 2, 1]);

Output

tp_sparkline(list_value(1, 3, 2, 5, 4, 6, 2, 1))
▃▃▂▂▂▆▆▅▅▅██▂▂▂
2 Stock prices with explicit theme and width
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')
▂▂▂▂▂▂▅▅▅▅███▄▄▄
3 Delta mode with arrow theme
SELECT tp_sparkline([100, 105, 102, 108, 95], mode := 'delta');

Output

tp_sparkline(list_value(100, 105, 102, 108, 95), "mode" := 'delta')
↑↑↑↑↑↓↓↓↓↓↑↑↑↑↑↓↓↓↓↓
4 Delta mode with face theme
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')
😊😊😊😊😊😐😐😐😐😐😊😊😊😊😊😞😞😞😞😞
5 Trend mode with slope theme
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')
/////\\\\\/////\\\\\\\\\\
6 Trend mode with intensity theme
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')
+++++-----+++++----------

tp_sparkline_agg

Aggregate function Charts
Signature
tp_sparkline_agg( value: DOUBLE, mode := VARCHAR = 'absolute', theme := VARCHAR = '', width := BIGINT = 20 ) → VARCHAR
Arguments
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
1 Sparkline straight from rows, ordered by time
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
▃▃▁▁▇▇▄▄██
2 One sparkline per group
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 ▇█▅▁ ▄█▇▃ ▁▆
3 Delta mode over raw rows
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
↑↓→↑↓