# Create a chart

## Creating a Chart with the UI

Forest Admin provides a straightforward UI to configure the charts you want.

First, turn on the Layout Editor mode **(1)**, then click *Add a new chart* **(2)**.

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-4d2daa12f49575ed129d84def01441a082a1f5c4%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2009.15.18.png?alt=media)

{% hint style="info" %}
To add a new dashboard, click on *+ New* while in Layout Editor mode **(0)**.
{% endhint %}

Then add a *Name* and optionally a *Description* **(1)** and select a [chart type](https://docs.forestadmin.com/user-guide/dashboards/charts/..#what-types-of-charts-exist-in-forest-admin) **(2)**:

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-aea41277a4d32c30a72efce7f09430a40a05d017%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2009.23.00.png?alt=media)

Next, in **Simple** mode, you need to provide the following information, depending on your chart type:

* 1 collection
* 1 aggregate function (`count`, `sum`, …)
* 1 group by field
* 1 time frame (day, week, month, year) option.
* 1 or multiple filters.

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-6e3dc81a279b9c02456cc74b1e38e19138c2a21e%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2010.47.13.png?alt=media)

## Creating a Chart with SQL

{% hint style="info" %}
To enable this feature in agent v2, your developers have to add a connection name to the datasources on which you want to execute live queries chart.

After that, you must select on which one you want to run the query.
{% endhint %}

{% hint style="warning" %}
The **Query** mode is only available for SQL databases.

For **security** reasons, only `SELECT` queries are allowed.
{% endhint %}

The **Query** mode has been designed to provide you with a flexible, easy to use and accessible interface when hard questions need to be answered. Simply type SQL queries using the online editor and visualize your data graphically.

The syntax of the SQL examples below can be different depending on the database type (SQLite, MySQL, Postgres, SQL Server, etc.). Please, refer to your database documentation for more information.

### Single value

The returned column **must** be named `value`. In the following example, we simply count the number of `customers`.

```sql
SELECT COUNT(*) AS value
FROM customers;
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-97d3156cc1d24a87b4b357b8e078b1052a08b45e%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2010.50.31.png?alt=media)

### Single value (with growth percentage)

The returned columns **must** be named `value` and `previous`. In the following example, we simply count the number of Appointments booked in January 2018 and compare this value to the number of Appointments booked in the previous month.

```sql
SELECT current.count AS value, previous.count AS previous
FROM (
    SELECT COUNT(*)
    FROM appointments
    WHERE start_date BETWEEN '2018-01-01' AND '2018-02-01'
) as current, (
    SELECT COUNT(*)
    FROM appointments
    WHERE start_date BETWEEN '2017-12-01' AND '2018-01-01'
) as previous;
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-3e30a1ae144b1cefa716454c1b434a61cae9a40c%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2010.53.33.png?alt=media)

### Repartition

The returned columns **must** be named `key` and `value`. In the following example, we simply count the number of transactions distributed by status.

```sql
SELECT transactions.status AS key, COUNT(*) AS value
FROM transactions
GROUP BY status;
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-04750705438c6c3df3884a9b74f077f15157ad82%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2014.08.05.png?alt=media)

### Time-based

The returned columns **must** be named `key` and `value`. In the following example, we simply count the number of `appointments` per month.

```sql
SELECT DATE_TRUNC('month', start_date) AS key, COUNT(*) as value
FROM appointments
GROUP BY key
ORDER BY key;
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-6389f28d1761555ad933d7eca37415ec2d98c852%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2014.10.02.png?alt=media)

### Objective

The returned columns **must** be named `value` and `objective`. In the following example, we set manually the objective to 750.

```sql
SELECT COUNT(orders) AS value, 750 AS objective
FROM orders
WHERE orders.created_at < date_trunc('year', now());
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-aa98b708a4c44f854cc9eef39ec98a0c196cdff1%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2014.16.46.png?alt=media)

### Leaderboard

The returned columns **must** be named `key` and `value` and `LIMIT` **must** be defined. In the following example, we limited the leaderboard to 10 items.

```sql
SELECT companies.name AS key, SUM(transactions.amount) AS value
FROM transactions
JOIN companies ON transactions.beneficiary_company_id = companies.id
GROUP BY key
ORDER BY value DESC
LIMIT 10;
```

![](https://85223878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOx0Wo3NZjrQrGQthTy6o%2Fuploads%2Fgit-blob-e545ce8d0a7d226d0d285c1aada07ab09c9d2cb4%2FCapture%20d%E2%80%99e%CC%81cran%202019-07-02%20a%CC%80%2014.12.28.png?alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/user-guide/dashboards/charts/create-a-chart.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
