# API Charts

{% hint style="success" %}
This is the official documentation of the `forestadmin-agent-django` and `forestadmin-agent-flask` Python agents.
{% endhint %}

API charts in the legacy agents were declared using routes.

In the new agent, you will need to use either the `agent.add_chart` or the `collection.add_chart` function, depending on if the chart is to be displayed on a record of a collection or a dashboard.

{% hint style="info" %}
You can find the full documentation of chart customization [here](/developer-guide-agents-python/agent-customization/charts.md).
{% endhint %}

## Code cheatsheet

| Legacy agent     | New agent                                                                            |
| ---------------- | ------------------------------------------------------------------------------------ |
| route in urls.py | <p>agent.add\_chart<br>collection.add\_chart(...)</p>                                |
| render Chart     | <p>return result\_builder.value(...)<br>return result\_builder.distribution(...)</p> |
| request object   | context.record\_id                                                                   |

## How to migrate

Migrating should be straightforward: the only differences are that:

* dashboard charts are now declared using the `agent.add_chart` function.
* collection charts are now declared using the `collection.add_chart` function, and access the record id using `context.record_id` instead of `request.query?.record_id`.
* Both types should use the `resultBuilder` helper to return the chart data.

{% tabs %}
{% tab title="Before" %}

* Define a new route in `urls.py`:
* Setup an action into a controller

```python
from django_forest.utils.views.base import BaseView

class MrrChartView(BaseView):
    def post(self, request):
        mrr = 0
        stripe = StripeClient("sk_AABBCCDD11223344")
        for charge in stripe.charges.all({"limit": 3}):
            mrr += charge.amount
        res = {
            "data": {
                "attributes": {
                    "value": {"countCurrent": mrr}
                },
                "type": "stats",
                "id": uuid.uuid4(),
            }
        }
        return JsonResponse(status=200, data=res)

```

{% endtab %}

{% tab title="After" %}

```python
from forestadmin.datasource_toolkit.context.agent_context import AgentCustomizationContext
from forestadmin.datasource_toolkit.decorators.chart.result_builder import ResultBuilder

def mrr_chart(context: AgentCustomizationContext, result_builder: ResultBuilder):
    mrr = 0
    stripe = StripeClient("sk_AABBCCDD11223344")
    for charge in stripe.charges.all({"limit": 3}):
        mrr += charge.amount
    return result_builder.value(mrr)

agent.add_chart("monthlyRecuringRevenue", mrr_chart)
```

{% endtab %}
{% endtabs %}


---

# 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/developer-guide-agents-python/getting-started/migrating/code-transformations/api-charts.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.
