nervure / symfony
Symfony UX integration for nervure: animated SVG charts, maps and dashboards in Twig, with a Twig component, a Twig function and a Stimulus controller.
Package info
Type:symfony-bundle
pkg:composer/nervure/symfony
Requires
- php: >=8.2
- symfony/config: ^6.4.14|^7.0|^8.0
- symfony/dependency-injection: ^6.4.14|^7.0|^8.0
- symfony/http-kernel: ^6.4.14|^7.0|^8.0
- symfony/stimulus-bundle: ^2.13
- symfony/ux-twig-component: ^2.13
- twig/twig: ^3.8
Requires (Dev)
- phpunit/phpunit: ^11.5
- symfony/framework-bundle: ^6.4.14|^7.0|^8.0
- symfony/twig-bundle: ^6.4.14|^7.0|^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Animated SVG charts, maps and dashboards of nervure in Twig: a Twig component, a Twig function and a Stimulus controller, for AssetMapper or Webpack Encore, with Turbo and Live Components.
Documentation: https://nervurejs.github.io/guide/symfony-laravel
Install
composer require nervure/symfony
Symfony Flex enables the bundle, adds the controller to assets/controllers.json and, with AssetMapper, adds nervure to importmap.php. Without Flex:
// config/bundles.php BenMacha\UxNervure\NervureBundle::class => ['all' => true],
php bin/console importmap:require nervure # AssetMapper npm install nervure --save-dev && npm install ./vendor/nervure/symfony/assets # Webpack Encore
and in assets/controllers.json:
{
"controllers": {
"@nervure/symfony": { "chart": { "enabled": true, "fetch": "eager" } }
}
}
PHP 8.2+, Symfony 6.4, 7 or 8.
Configure
# config/packages/nervure.yaml nervure: theme: auto # light, dark or auto locale: ~ # the language of the page by default animation: true # true, false, or visible: plays when the chart comes into view toolbar: false # export button (SVG, PNG, CSV) zoom: false height: 300 # pixels, or any CSS length
Use
use BenMacha\UxNervure\Chart; $spec = Chart::xy() ->categories(['Jan', 'Feb', 'Mar']) ->series('Orders', [120, 180, 150], kind: 'bar') ->format(currency: 'EUR');
<twig:Nervure:Chart :spec="spec" height="300" theme="dark" toolbar /> {{ render_nervure_chart(spec, {height: 300, animation: 'visible'}) }}
A spec is a PHP array or a Chart. The builder knows xy, pie, gauge, kpi, map and timeline; Chart::of('sankey', [...]) and ->with('field', $value) reach every other type and field. The specs of the 89 chart types: https://nervurejs.github.io/charts/
Other attributes of the component (class, id, data-*) go on the chart element.
Turbo and Live Components
The chart is removed with its element, and drawn again when its spec changes: a Turbo Stream that replaces the element, a Turbo Frame, or a Live Component that renders again.
#[AsLiveComponent] final class Sales { use DefaultActionTrait; #[LiveProp(writable: true)] public string $year = '2026'; public function spec(): Chart { return Chart::xy()->categories([...])->series('Sales', [...]); } }
<div {{ attributes }}> <select data-model="year">…</select> <twig:Nervure:Chart :spec="this.spec" /> </div>
Events
document.addEventListener('nervure--symfony--chart:connect', (event) => { const chart = event.detail.chart; // chart.toPNG(), chart.setColor(), chart.toggle()… });
License
MIT, Ali Ben Macha.