acseo/graphic-bundle
Symfony bundle to build chart objects (timeline, pie, bar, etc.) and render them via a Twig extension using jQuery Flot. Create graphs in controllers with datasets and output ready-to-use JS/HTML in your Twig views.
Installation:
composer require acseo/graphic-bundle:dev-master
Enable the bundle in app/AppKernel.php:
new ACSEO\Bundle\GraphicBundle\ACSEOGraphicBundle(),
First Use Case: Generate a basic PieChart in a controller:
use ACSEO\Bundle\GraphicBundle\Graphic\Flot\Type\Pie;
public function showChartAction()
{
$data = ['label' => 'Series 1', 'data' => [3, 5, 7]];
$pie = new Pie("#chart-container", [$data]);
return $this->render('template.html.twig');
}
Include jQuery Flot in your Twig template:
{{ parent() }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.min.js"></script>
Data Preparation:
[['label' => 'Label', 'data' => [1, 2, 3]]]).strtotime() for timestamps (e.g., strtotime("01/01/2024") * 1000).Chart Initialization:
$timeline = new TimeLine("#domId", $data);
$pie = new Pie("#domId", $data);
#chart-container) and data array.Twig Integration:
<div id="chart-container"></div>
{{ include('ACSEOGraphicBundle::flot.js') }} <!-- Auto-renders JS -->
Dynamic Updates:
Custom Styling: Override default Flot options via constructor:
$pie = new Pie("#domId", $data, [
'series' => ['shadowSize' => 0],
'grid' => ['hoverable' => true]
]);
Multiple Charts: Instantiate multiple chart types in a single controller:
$timeline = new TimeLine("#timeline", $timelineData);
$pie = new Pie("#pie", $pieData);
Reusable Services: Create a service to centralize chart logic:
# services.yml
acseo.graphic.service:
class: AppBundle\Service\GraphicService
arguments: ['@twig']
// GraphicService.php
public function generatePie($data, $domId) {
$pie = new Pie($domId, $data);
return $this->twig->render('ACSEOGraphicBundle::flot.js', ['chart' => $pie]);
}
DOM ID Conflicts: Ensure the target DOM ID exists in the Twig template. Flot throws silent errors if the element is missing.
Timestamp Formatting:
Flot expects timestamps in milliseconds (e.g., strtotime("date") * 1000). Incorrect formatting breaks TimeLine charts.
Dependency Order:
jQuery and Flot must be loaded before the bundle’s JS. Use Twig’s {{ parent() }} to ensure proper ordering.
Dev-Master Dependency:
The package is in dev-master. Pin the version in composer.json to avoid breaking changes:
"acseo/graphic-bundle": "dev-master as 1.0.0"
Check Console Errors: Open browser dev tools to verify Flot/Flot initialization errors (e.g., missing data or DOM elements).
Log Chart Data: Dump chart data before instantiation to validate format:
var_dump($data); // Ensure structure matches Flot expectations
Inspect Generated JS: The bundle auto-renders JS via Twig. Inspect the rendered output to debug:
{{ dump(app.request->getContent()) }} <!-- Debug helper -->
Custom Chart Types:
Extend the bundle by creating new chart classes (e.g., LineChart) in ACSEO\Bundle\GraphicBundle\Graphic\Flot\Type\.
Override Templates:
Override the flot.js.twig template in your bundle to modify JS output:
{# app/Resources/ACSEOGraphicBundle/views/flot.js.twig #}
{{ include('ACSEOGraphicBundle::flot.js') }}
Add New Providers:
The bundle supports multiple providers (e.g., Highcharts). Fork the bundle and extend the GraphicProvider interface to add support.
Configuration: Use Symfony’s configuration system to centralize chart defaults:
# config.yml
acseo_graphic:
default_options:
series: { shadowSize: 0 }
(Note: Requires bundle extension to support this.)
How can I help you explore Laravel packages today?