Smart Charts
This is the official documentation of the forestadmin/laravel-forestadmin v2+ and forestadmin/symfony-forestadmin PHP agents.
Live Query Charts ↗ allowed the creation of charts from SQL queries from the UI.
Steps
Step 1: Retrieve the SQL query from the UI

You can retrieve the SQL query of a Live query chart by clicking on the Cog icon of the chart when using the Edit Layout mode.
Step 2: Create a new API chart
The next step will be to create a new API chart using the SQL query you retrieved in the previous step.
use ForestAdmin\AgentPHP\DatasourceCustomizer\Context\AgentCustomizationContext;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Chart\ResultBuilder;
$forestAgent->addChart('appointments',
  function (AgentCustomizationContext $context, ResultBuilder $resultBuilder) {
  $conn = $context->dataSource->getCollection('appointments')
      ->getNativeDriver()->getConnection();
  $query = $conn->executeQuery("
      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;");
    $rows = $query->fetchAllAssociative();
  return $resultBuilder->value(rows[0]->value, rows[0]->previous);
});Step 3: Retrieve the URL of the generated chart
The url of your newly created api chart is /forest/_charts/$chartName. The $chartName is the name given to the addChart method. For the previous example the url should be /forest/_charts/appointments
Step 4: Change the old chart configuration
Instead of Query, select Smart in the data source selector, and enter the path that was printed to the console in the previous step.
Last updated
Was this helpful?
