creative-web-solution/svg-graph-bundle
Laravel bundle for generating SVG-based graphs and charts. Provides helpers to build and render scalable, lightweight visuals in your app, suitable for dashboards and reports, with customizable output for embedding in views or exports.
Installation Add the bundle via Composer:
composer require creative-web-solution/svg-graph-bundle
Register the bundle in config/app.php under providers:
CreativeWebSolution\SvgGraphBundle\SvgGraphBundle::class,
Basic Configuration Publish the default config (if needed):
php artisan vendor:publish --provider="CreativeWebSolution\SvgGraphBundle\SvgGraphBundle" --tag="config"
Key config options in config/svg_graph.php:
default_chart_type (e.g., 'line', 'bar', 'pie').default_options (global styling/behavior).First Use Case: Render a Simple Graph Inject the service into a controller or Blade view:
use CreativeWebSolution\SvgGraphBundle\Service\GraphService;
public function showGraph(GraphService $graphService) {
$data = [
'labels' => ['Jan', 'Feb', 'Mar'],
'values' => [10, 20, 15],
];
$svg = $graphService->render('line', $data);
return view('graphs.example', compact('svg'));
}
In Blade:
{!! $svg !!}
Workflow: Fetch data from models/DB, transform into graph-compatible format.
$users = User::selectRaw('COUNT(*) as count, MONTH(created_at) as month')
->groupBy('month')
->get()
->toArray();
$graphData = array_map(fn($item) => [
'labels' => ['Month ' . $item['month']],
'values' => [$item['count']],
]);
Tip: Use Laravel collections to reshape data efficiently:
$graphData = User::pluck('created_at', 'id')
->mapToGroups(fn($date, $id) => ['values' => [$date]])
->toArray();
GraphService facade or helper:
// app/Helpers/GraphHelper.php
class GraphHelper {
public static function userActivityGraph() {
return app(GraphService::class)->render('bar', [
'labels' => User::select('month')->distinct()->pluck('month'),
'values' => User::selectRaw('COUNT(*)')->groupBy('month')->pluck('COUNT(*)'),
]);
}
}
Usage:
{!! GraphHelper::userActivityGraph() !!}
Integration: Return SVG as API response (e.g., for SPAs):
return response($graphService->render('pie', $data), 200, [
'Content-Type' => 'image/svg+xml',
]);
Caching: Cache generated SVGs for performance:
$cacheKey = 'graph:user_activity';
$svg = Cache::remember($cacheKey, now()->addHours(1), fn() =>
$graphService->render('line', $data)
);
// app/Providers/BladeServiceProvider.php
Blade::directive('graph', function ($type) {
return "<?php echo app(\\CreativeWebSolution\\SvgGraphBundle\\Service\\GraphService::class)->render($type, $data); ?>";
});
Usage:
@graph('bar', $chartData)
Data Format Strictness
labels, values). Misnamed arrays will fail silently.$data = collect($rawData)->only(['labels', 'values'])->toArray();
SVG Output Escaping
$svg in Blade may cause XSS warnings. Use {!! !!} or sanitize:
$sanitizedSvg = htmlspecialchars($svg, ENT_QUOTES, 'UTF-8');
Chart Type Limitations
line, bar, and pie (per README). Custom types require extension (see below).Config Overrides
default_options may conflict with per-render options. Explicitly pass overrides:
$graphService->render('line', $data, ['colors' => ['#ff0000']]);
file_put_contents(storage_path('app/debug.svg'), $svg);
APP_DEBUG=true) for missing config/dependency issues.Add Custom Chart Types
CreativeWebSolution\SvgGraphBundle\Contract\ChartInterface:
class CustomChart implements ChartInterface {
public function render(array $data, array $options) {
// Custom logic
return '<svg>...</svg>';
}
}
config/svg_graph.php:
'charts' => [
'custom' => \App\Charts\CustomChart::class,
],
Override Default Templates
php artisan vendor:publish --tag="svg-graph-views"
resources/views/vendor/svg_graph/.Hook into Render Pipeline
GraphService:
$graphService = new GraphService();
$graphService->setPreRenderCallback(function($type, $data) {
// Modify data before rendering
return $data;
});
memory_limit or simplify styles.How can I help you explore Laravel packages today?