Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Svg Charts Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

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.

Gotchas and Tips

  • v1.0.5 fixes implicit conversion. This is a patch-level bug fix, likely addressing PHP 8.1+ implicit float-to-int conversion notices or similar type coercion. Update to v1.0.5 if you render charts with numeric strings or float values, and still cast data explicitly to avoid notices and inconsistent output.
  • No native Laravel integration. Do not expect a facade, config file, artisan command, or auto-discovered service provider. Treat it as a plain Composer library and wrap it yourself.
  • SVG label escaping. The package may not escape user-supplied labels. If labels come from users, sanitize or escape them before rendering, or wrap the output in a trusted-only context. Inline SVG in Blade is an XSS vector if labels are untrusted.
  • Large datasets. SVG output grows quickly with data points. Downsample or aggregate before charting, and cache rendered output.
  • Type coercion. Numeric values passed as strings or floats can produce warnings or malformed coordinates. Normalize data at the boundary of your chart class.
  • Semver caution. A patch release fixing conversion behavior indicates the package is still stabilizing. Pin the version in composer.json and run your chart tests after updates.
  • Debugging. When a chart renders blank or malformed, write the raw SVG to storage/app/debug/chart.svg and open it in a browser. Inspect the generated markup for NaN, missing dimensions, or unescaped characters.
  • Extension points. Prefer subclassing chart classes or wrapping them in your own services over editing vendor files. If the package exposes setters for dimensions, colors, and formatters, centralize those defaults in your own chart base class.
  • Response caching. SVG responses are cheap to cache. Add cache headers for public charts and use Laravel cache for server-side reuse.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
skyraptor/chatter
edlugz/sasapay
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource