# API Charts

{% hint style="success" %}
This is the official documentation of the `forestadmin/laravel-forestadmin` v2+ and `forestadmin/symfony-forestadmin` PHP agents.
{% endhint %}

API charts in the legacy agents were declared using routes.

In the new agent, you will need to use either the `agent.addChart` or the `collection.addChart` 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-php/agent-customization/charts.md).
{% endhint %}

## Code cheatsheet

| Legacy agent     | New agent                                                                            |
| ---------------- | ------------------------------------------------------------------------------------ |
| route in web.php | <p>$agent->addChart<br>collection.addChart(...)</p>                                  |
| render Chart     | <p>return $resultBuilder->value(...)<br>return $resultBuilder->distribution(...)</p> |
| request object   | $context->recordId                                                                   |

## How to migrate

Migrating should be straightforward: the only differences are that:

* dashboard charts are now declared using the `agent.addChart` function.
* collection charts are now declared using the `collection.addChart` function, and access the record id using `context.recordId` 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 `web.php`:
* Setup an action into a controller

```php
<?php

namespace App\Http\Controllers;

use ForestAdmin\LaravelForestAdmin\Facades\ChartApi;
use Stripe\StripeClient;

class ChartsController extends Controller
{
    public function mrr()
    {
        $mrr = 0;
        $stripe = new StripeClient('sk_AABBCCDD11223344');
        $charges = $stripe->charges->all(['limit' => 3]);
        foreach ($charges as $charge) {
            $mrr += $charge->amount;
        }
        return ChartApi::renderValue($mrr);
    }
}
```

{% endtab %}

{% tab title="After" %}

```php
<?php

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\DatasourceEloquent\EloquentDatasource;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Context\AgentCustomizationContext;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Chart\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\Aggregation;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\ConditionTreeFactory;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\Filters\Filter;

return static function () {
	$forestAgent = app()->make(AgentFactory::class);
    $forestAgent->addDatasource(
        new EloquentDatasource(...),
    )
    ->addChart(
    'monthlyRecuringRevenue',
    function (AgentCustomizationContext $context, ResultBuilder $resultBuilder) {
        $mrr = 0;
        $stripe = new StripeClient('sk_AABBCCDD11223344');
        $charges = $stripe->charges->all(['limit' => 3]);
        foreach ($charges as $charge) {
            $mrr += $charge->amount;
        }

        return $resultBuilder->value($mrr)
    }
);

```

{% 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-php/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.
