dpodsiadlo/svg-charts
SVG Charts is a Laravel/PHP package for generating lightweight, scalable SVG charts server-side without JavaScript. Use a simple API to add clean visuals to dashboards, reports, emails, and PDFs.
Install the package with Composer:
composer require dpodsiadlo/svg-charts
This is a standalone PHP SVG charting library, not a Laravel-first package. There is typically no service provider, facade, config file, or migration to publish. Start with the package README and its examples/ directory to see the available chart types and their option arrays.
A minimal first use case is rendering a line chart from a controller:
use Podsiadlo\SvgCharts\Chart; // adjust namespace to the package's actual API
public function chart()
{
$svg = Chart::line()
->setData([10, 25, 18, 40])
->setLabels(['Jan', 'Feb', 'Mar', 'Apr'])
->setWidth(600)
->setHeight(300)
->render();
return response($svg, 200, ['Content-Type' => 'image/svg+xml']);
}
If your version exposes concrete chart classes instead of a fluent factory, follow the README's constructor/options pattern. The first practical integration is usually a controller returning image/svg+xml or a Blade partial embedding the SVG inline.
Controller response pattern. Generate the SVG in a controller or dedicated action and return it directly. This is useful for dashboards, email charts, and printable reports.
return response($chart->render())
->header('Content-Type', 'image/svg+xml')
->header('Cache-Control', 'public, max-age=3600');
Blade component pattern. Wrap chart generation in an anonymous or class-based Blade component so views stay clean:
<x-svg-chart :data="$sales" :labels="$months" />
Inside the component, build the chart and return {!! $svg !!}. Only do this with trusted labels; see the XSS note below.
Service/action pattern. Put chart construction in a dedicated class such as App\Charts\RevenueChart or App\Services\ChartRenderer. Inject it into controllers, commands, and jobs. This keeps option arrays and formatting rules out of controllers and makes snapshot testing easier.
Caching pattern. SVG generation is CPU-bound for large datasets. Cache the rendered string with Laravel's cache or store it on disk:
$svg = Cache::remember("chart.revenue.{$year}", now()->addHour(), fn () => $chart->render());
For report generation, write SVGs to storage/app/charts/ and reference them from PDF or email tooling.
Queued generation pattern. For large datasets or many charts, generate SVGs in a queued job and store the result. Avoid rendering dozens of charts synchronously in a single request.
Testing pattern. Use string assertions rather than pixel comparisons:
$response->assertHeader('Content-Type', 'image/svg+xml');
$this->assertStringContainsString('<svg', $response->getContent());
For complex charts, store a known-good SVG fixture and compare normalized output.
Integration tips. Keep chart option arrays in config or dedicated classes. Cast numeric data explicitly to int or float before passing it in. If the package exposes formatters or callbacks, use them for axis labels rather than post-processing the SVG string.
composer.json and run your chart tests after updates.storage/app/debug/chart.svg and open it in a browser. Inspect the generated markup for NaN, missing dimensions, or unescaped characters.How can I help you explore Laravel packages today?