lovasoa / SQLpage

SQL-only webapp builder, empowering data analysts to build websites and applications quickly
https://sql.ophir.dev
MIT License
896 stars 63 forks source link

explain how to build a graph from a remote database please #230

Closed SyntaxEvg closed 5 months ago

SyntaxEvg commented 5 months ago

explain how to build a graph from a remote database please select [date], [value] From bd.charts How can I build a graph using my example, please explain I need a for loop ? https://sql.ophir.dev/documentation.sql?component=chart#component Example 1

select 
    'chart'             as component,
    'Quarterly Revenue' as title,
    'area'              as type,
    'indigo'            as color,
    5                   as marker,
    TRUE                as time;
select 
    '2022-01-01T00:00:00Z' as x,
    15                     as y;
select 
    '2022-04-01T00:00:00Z' as x,
    46                     as y;
lovasoa commented 5 months ago

Hello and welcome to SQLPage !

Let's say you have a table named charts with the following contents :

date value
2022-01-01 15
2022-04-01 46

and you want to generate the same graph as in the documentation.

You start by connecting SQLPage to your database by editing sqlpage/sqlpage.json. Given the sql syntax you used above, it looks like you are using Microsoft SQL Server, so the connection string will have the following form, if the database is running locally on your computer (on localhost) :

{
    "database_url": "mssql://username:password@localhost/YourDatabaseName"
}

Then you create a file named my_chart.sql with the following contents :

select 
    'chart'             as component,
    'Quarterly Revenue' as title,
    'area'              as type,
    'indigo'            as color,
    5                   as marker,
    TRUE                as time;

select 
    date as x,
    value as y
from
    charts;

The second select will fetch the values from your table and return them in the format expected by the SQLPage chart component.

You can then launch SQLPage and open http://localhost:8080/my_chart.sql and you will see your graph appear.