dafuer/dafuer-jpgraph-bundle
Installation
Add the bundle to your composer.json:
composer require dafuer/dafuer-jpgraph-bundle
Enable it in config/bundles.php:
Dafuer\JpgraphBundle\DafuerJpgraphBundle::class => ['all' => true],
Configuration Publish the default config (if needed):
php bin/console dafuer:jpgraph:install
Verify config/packages/dafuer_jpgraph.yaml exists.
First Use Case Generate a basic line plot via a controller:
use Dafuer\JpgraphBundle\Controller\GraphController;
class MyController extends AbstractController {
public function showGraph(GraphController $graphController) {
$data = [1, 2, 3, 4, 5];
return $graphController->linePlot($data, 'My Line Graph');
}
}
Route it:
# config/routes.yaml
my_graph:
path: /graph
controller: App\Controller\MyController::showGraph
Static Graphs
Use GraphController actions (linePlot, barPlot, piePlot) for pre-defined charts:
// Bar plot with custom labels
return $graphController->barPlot(
[10, 20, 30],
['Jan', 'Feb', 'Mar'],
'Monthly Sales'
);
Dynamic AJAX Graphs
Extend Dafuer\JpgraphBundle\Event\GraphEvent to modify plots on-the-fly:
// src/EventListener/MyGraphListener.php
class MyGraphListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return ['dafuer.jpgraph.pre_render'];
}
public function onPreRender(GraphEvent $event) {
$event->getGraph()->setTitle('Dynamic Title');
}
}
Custom Styling Override default styles via YAML config:
# config/packages/dafuer_jpgraph.yaml
dafuer_jpgraph:
styles:
line:
color: '#FF0000'
width: 2
Data Integration Fetch data from services/repositories:
public function dynamicDataPlot(GraphController $graphController, DataService $dataService) {
$data = $dataService->getChartData();
return $graphController->linePlot($data, 'Live Data');
}
<img src="{{ path('my_graph') }}" alt="Graph">
# config/packages/dafuer_jpgraph.yaml
dafuer_jpgraph:
cache_enabled: true
cache_lifetime: 3600
return new BinaryFileResponse($graph->stroke(), 200, [
'Content-Type' => 'image/png',
]);
Bundle Maturity
Configuration Overrides
dafuer_jpgraph.yaml do not merge with defaults—they replace them. Use !merge tags if needed:
styles: * = %dafuer_jpgraph.styles%
styles:
line:
width: 3 # Override only width
AJAX Limitations
$event->getRequest()->headers->set('X-Requested-With', 'XMLHttpRequest');
Memory Issues
memory_limit. Increase it temporarily:
php -d memory_limit=512M bin/console ...
jpgraph directory in var/cache/ for generated files. Clear cache:
php bin/console cache:clear
line for linePlot).Custom Graph Types
Extend Dafuer\JpgraphBundle\Graph\AbstractGraph:
class CustomGraph extends AbstractGraph {
protected function build() {
$this->graph->setTheme('custom');
// Add custom logic
}
}
Register it in services:
services:
App\Graph\CustomGraph:
tags: ['dafuer.jpgraph.graph']
Event Subscribers
Hook into dafuer.jpgraph.pre_render or dafuer.jpgraph.post_render for cross-cutting concerns (e.g., logging, analytics).
Data Transformers Create a service to pre-process data:
class DataTransformer {
public function transform(array $data): array {
return array_map(fn($v) => $v * 10, $data);
}
}
Inject it into your controller or event listener.
GraphEvent for Reusability
Share graph configurations across controllers by modifying the event object.GraphEvent:
$event->setParameter('theme', 'modern');
jpgraph CLI
Validate graphs independently:
vendor/bin/jpgraph --help
How can I help you explore Laravel packages today?