Create interactive charts, tables, and data visualizations
Learn how to create beautiful data visualizations in SQLPage using charts, tables, and interactive components. Transform your SQL query results into compelling visual displays.
select 'chart' as component, 'Revenue per country' as title, 'bar' as type, 'time' as xtitle, 'price' as ytitle, true as stacked;select 'Russia' as series, '2022-01' as label, 2 as valueunion allselect 'Russia', '2022-02', 4union allselect 'Russia', '2022-03', 2;select 'Brasil' as series, '2022-01' as label, 4 as valueunion allselect 'Brasil', '2022-03', 1union allselect 'Brasil', '2022-04', 1;
Component Properties
type: Chart type - bar, line, area, scatter, pie, etc.
xtitle: X-axis label
ytitle: Y-axis label
stacked: Stack multiple series on top of each other
title: Chart title displayed above the chart
Row Properties
Each row in your query represents one data point:
series: Name of the data series (for multi-series charts)
select 'chart' as component, 'Collatz conjecture' as title, 'area' as type;SELECT 'syracuse' as series, x, yFROM ( SELECT 0 AS x, 15 AS y UNION SELECT 1, 46 UNION SELECT 2, 23 UNION SELECT 3, 70 UNION SELECT 4, 35 UNION SELECT 5, 106 UNION SELECT 6, 53 UNION SELECT 7, 160 UNION SELECT 8, 80 UNION SELECT 9, 40 UNION SELECT 10, 20 UNION SELECT 11, 10 UNION SELECT 12, 5) AS syracuse ORDER BY x;
Use subqueries and CTEs to perform calculations before charting. This example shows the Collatz conjecture sequence calculated in SQL.
select 'chart' as component, 'Sales by Product' as title, 'bar' as type;select product_name as label, sum(sales) as valuefrom salesgroup by product_name;
Best for: Showing trends over time
select 'chart' as component, 'User Growth' as title, 'line' as type, true as marker;select date as label, count(*) as valuefrom usersgroup by dateorder by date;
Best for: Showing volume or cumulative totals
select 'chart' as component, 'Cumulative Revenue' as title, 'area' as type, true as fill;select month as label, sum(sum(revenue)) over (order by month) as valuefrom transactionsgroup by month;
Best for: Showing parts of a whole
select 'chart' as component, 'Market Share' as title, 'pie' as type;select company_name as label, market_share as valuefrom companieswhere market_share > 0;
select 'table' as component, true as sort, true as search, true as hover, true as striped_rows;select 'John' as "First Name", 'Doe' as "Last Name", 1994 as "Birth Date"union allselect 'Jane', 'Smith', 1989;
select 'table' as component, true as sort, true as search, 'actions' as markdown; -- Render the 'actions' column as markdownselect id, name, email, '[Edit](edit.sql?id=' || id || ') | [Delete](delete.sql?id=' || id || ')' as actionsfrom usersorder by id;
The markdown property specifies which columns should be rendered as Markdown, allowing you to embed links, images, and formatted text in table cells.
Create interactive learning tools with computed values.
select 'card' as component, 5 as columns;WITH nums(x) AS ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10)SELECT a.x || ' times ' || b.x as title, CASE a.x % 4 WHEN 0 THEN 'red' WHEN 1 THEN 'green' WHEN 3 THEN 'yellow' ELSE 'blue' END as color, a.x || ' x ' || b.x || ' = ' || (a.x * b.x) as description, 'This is basic math' as footer, '?x=' || a.x as linkFROM nums as a, nums as bWHERE $x IS NULL OR b.x = CAST($x AS DECIMAL)ORDER BY a.x, b.x;
Combine forms with data visualization for complete applications.
select 'form' as component, 'Create' as validate, 'New User' as title;select 'number' as type, 'age' as name, 'Age' as label;select 'First Name' as name, true as autocomplete, true as required, 'We need your name for legal reasons.' as description;select 'Last name' as name, true as autocomplete;select 'radio' as type, 'favorite_food' as name, 'banana' as value, 'I like bananas the most' as label;select 'radio' as type, 'favorite_food' as name, 'cake' as value, 'I like cake more' as label, 'Bananas are okay, but I prefer cake' as description;select 'checkbox' as type, 'checks[]' as name, 1 as value, 'Accept the terms and conditions' as label;
-- Page Headerselect 'shell' as component, 'Sales Dashboard' as title, 'chart-bar' as icon;-- Key Metricsselect 'card' as component, 4 as columns;select 'Total Revenue' as title, '$' || sum(amount) as description, 'green' as color, 'currency-dollar' as iconfrom sales;select 'Orders Today' as title, count(*) as description, 'blue' as color, 'shopping-cart' as iconfrom orderswhere date(created_at) = date('now');select 'Active Users' as title, count(distinct user_id) as description, 'orange' as color, 'users' as iconfrom sessionswhere last_active > datetime('now', '-1 hour');select 'Conversion Rate' as title, round(100.0 * count(*) / (select count(*) from visits), 2) || '%' as description, 'purple' as color, 'percentage' as iconfrom conversions;-- Sales Trend Chartselect 'chart' as component, 'Sales Trend (Last 30 Days)' as title, 'area' as type, 'Date' as xtitle, 'Revenue ($)' as ytitle;select date(created_at) as label, sum(amount) as valuefrom saleswhere created_at > date('now', '-30 days')group by date(created_at)order by date(created_at);-- Top Products Tableselect 'table' as component, 'Top Selling Products' as title, true as sort, true as hover;select product_name as "Product", count(*) as "Orders", '$' || sum(amount) as "Revenue", round(avg(amount), 2) as "Avg Price"from salesjoin products on sales.product_id = products.idgroup by product_nameorder by sum(amount) desclimit 10;
select 'chart' as component, 'Sales by Region' as title, 'line' as type;select region as series, month as label, sum(sales) as valuefrom sales_datagroup by region, monthorder by month, region;
Computed Window Functions
Use SQL window functions for running totals and moving averages:
select 'chart' as component, 'Running Total' as title, 'area' as type;select date as label, sum(sum(amount)) over (order by date) as valuefrom transactionsgroup by date;
Conditional Formatting
Apply colors based on data values:
select 'card' as component;select product_name as title, stock_level as description, case when stock_level < 10 then 'red' when stock_level < 50 then 'orange' else 'green' end as colorfrom inventory;
Dynamic Chart Types
Let users choose chart type via URL parameter:
select 'chart' as component, 'Flexible Visualization' as title, coalesce($chart_type, 'bar') as type;select category as label, sum(value) as valuefrom datagroup by category;
Users can then append ?chart_type=line or ?chart_type=pie to change visualization.
Keep chart data points under 1000 for optimal performance. Use aggregation:
-- Bad: 100,000 individual pointsselect timestamp as label, value from metrics;-- Good: Aggregated to hourlyselect strftime('%Y-%m-%d %H:00', timestamp) as label, avg(value) as valuefrom metricsgroup by strftime('%Y-%m-%d %H', timestamp);
2
Use Indexes
Add indexes on columns used in WHERE, GROUP BY, and ORDER BY:
CREATE INDEX idx_sales_date ON sales(created_at);CREATE INDEX idx_sales_product ON sales(product_id);
3
Materialize Complex Views
For expensive calculations, create materialized views or summary tables that update periodically.