Introduction
Technical Guides
Documentation
Create ChartLines
Tutorial
Here is a step-by-step tutorial on how to complete the function fn:
Define the initial SQL query: The SQL query should select the necessary data from the database. In this case, we want to count the number of users created in the last 30 days and group them by month and year.
tslet sql = `SELECT date_format(creationDate, "%m/%Y") as label, count(1) as conteo from users WHERE creationDate >= NOW() - interval 30 day orderBy creationDate ASC`;
Execute the SQL query: Use the query method provided in req to execute the SQL query and get the results.
tslet results = await req.query(sql);
Display the chart: Use the chartLine method provided in req to send the formatted data as a line chart response.
tsreturn res.chartLine(data);
Final Result
With these steps, you have completed the fn function. Now, when this function is called, it will return a line chart showing the number of users created per month in the last 30 days. Make sure to adjust the code according to your application's specifications and structure.
tsconst fn = async function (req: Request, res: Response) { let sql = `SELECT date_format(creationDate, "%m/%Y") as label, count(1) as conteo from users WHERE creationDate >= NOW() - interval 30 day orderBy creationDate ASC`; let results = await req.query(sql); return res.chartLine(results); };