Skip to content

Symfony and Laravel ​

The <nervure-chart> web component needs no build tool and follows Turbo and Livewire: it cleans up when its element goes away.

Symfony ​

AssetMapper ​

bash
php bin/console importmap:require nervure/element
js
// assets/app.js
import 'nervure/element';
php
#[Route('/orders')]
public function orders(OrderRepository $orders): Response
{
    $rows = $orders->countByMonth();

    return $this->render('orders.html.twig', ['spec' => [
        'type' => 'xy',
        'categories' => array_column($rows, 'month'),
        'series' => [['name' => 'Orders', 'kind' => 'bar', 'data' => array_column($rows, 'count')]],
    ]]);
}
twig
<nervure-chart style="height: 320px" toolbar spec="{{ spec|json_encode|e('html_attr') }}"></nervure-chart>

A Twig component ​

twig
{# templates/components/Chart.html.twig #}
{% props spec, height = 320, animation = 'visible' %}
<nervure-chart {{ attributes.defaults({style: 'height: ' ~ height ~ 'px'}) }} animation="{{ animation }}" spec="{{ spec|json_encode|e('html_attr') }}"></nervure-chart>
twig
<twig:Chart :spec="spec" height="400" />

Stimulus and Symfony UX ​

js
// assets/controllers/chart_controller.js
import { Controller } from '@hotwired/stimulus';
import { chartController } from 'nervure/stimulus';

export default chartController(Controller);
twig
<div {{ stimulus_controller('chart', { spec: spec, options: { toolbar: true } }) }} style="height: 320px"></div>

The controller draws again when its value changes, and dispatches chart:ready with the chart.

Turbo and Mercure ​

A Turbo Frame or a Turbo Stream that replaces the element replaces the chart. For a chart that moves with your data, listen to Mercure:

js
import { append, events, live } from 'nervure/live';

live(chart, spec, events('https://example.com/.well-known/mercure?topic=prices'), {
  apply: (data, previous) => append(previous, data.time, { Price: data.price }, 60),
});

Webpack Encore ​

npm install nervure, then the same imports.

Laravel ​

Vite and Blade ​

bash
npm install nervure
js
// resources/js/app.js
import 'nervure/element';
blade
<nervure-chart style="height: 320px" toolbar spec="{{ json_encode($spec) }}"></nervure-chart>

A Blade component ​

blade
{{-- resources/views/components/chart.blade.php --}}
@props(['spec', 'height' => 320])
<nervure-chart {{ $attributes->merge(['style' => "height: {$height}px"]) }} spec="{{ json_encode($spec) }}"></nervure-chart>
blade
<x-chart :spec="$spec" height="400" />

Livewire ​

blade
<div wire:ignore>
    <nervure-chart id="orders" style="height: 320px" spec="{{ json_encode($spec) }}"></nervure-chart>
</div>
php
$this->dispatch('orders-changed', spec: $this->spec());
js
Livewire.on('orders-changed', ({ spec }) => { document.querySelector('#orders').spec = spec; });

Inertia ​

Use the React, Vue or Svelte component.

Rails, Django, WordPress ​

The same web component, with one script tag.