/symfony-ux-skills:ux-chartjsfrom symfony-ux-skills
Builds bar, line, pie and other charts from PHP data using Chart.js in Symfony and Twig. Renders via render_chart() with Stimulus controller.
Ships in symfony-ux-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Build a chart in PHP, render it with render_chart(), and let a Stimulus controller draw it with Chart.js (version 3 or 4). Data and options are plain PHP arrays passed as-is to Chart.js.
composer require symfony/ux-chartjs
With AssetMapper, the Flex recipe adds chart.js to importmap.php. With Webpack Encore or Reprise, run npm install --force and rebuild.
ChartBuilderInterface::createChart(Chart::TYPE_LINE) New chart
$chart->setData([...]) Chart.js "data" (labels, datasets)
$chart->setOptions([...]) Chart.js "options"
{{ render_chart(chart) }} <canvas> with the Stimulus controller
{{ render_chart(chart, {class: 'h-64'}) }} Extra HTML attributes on the <canvas>
Types: TYPE_LINE, TYPE_BAR, TYPE_RADAR, TYPE_PIE, TYPE_DOUGHNUT, TYPE_POLAR_AREA, TYPE_BUBBLE, TYPE_SCATTER.
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;
final class DashboardController extends AbstractController
{
#[Route('/dashboard', name: 'app_dashboard')]
public function __invoke(ChartBuilderInterface $chartBuilder): Response
{
$chart = $chartBuilder->createChart(Chart::TYPE_LINE)
->setData([
'labels' => ['January', 'February', 'March', 'April'],
'datasets' => [[
'label' => 'Sales',
'backgroundColor' => 'rgb(255, 99, 132)',
'borderColor' => 'rgb(255, 99, 132)',
'data' => [12, 19, 3, 25],
]],
])
->setOptions([
'maintainAspectRatio' => false,
'scales' => ['y' => ['suggestedMin' => 0]],
]);
return $this->render('dashboard/index.html.twig', ['chart' => $chart]);
}
}
<div style="position: relative; height: 320px">
{{ render_chart(chart) }}
</div>
Keys follow the Chart.js documentation exactly (backgroundColor, suggestedMin...). Chart.js sizes the canvas from its parent: give the parent a height when maintainAspectRatio is false.
Data and options travel as JSON, so PHP can only send values. For a tick formatter, a tooltip callback or onClick, attach your own Stimulus controller and edit the config before the chart is created:
{{ render_chart(chart, {'data-controller': 'sales-chart'}) }}
// assets/controllers/sales_chart_controller.js
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
initialize() {
this.onPreConnect = this.onPreConnect.bind(this);
this.onConnect = this.onConnect.bind(this);
}
connect() {
this.element.addEventListener('chartjs:pre-connect', this.onPreConnect);
this.element.addEventListener('chartjs:connect', this.onConnect);
}
disconnect() {
this.element.removeEventListener('chartjs:pre-connect', this.onPreConnect);
this.element.removeEventListener('chartjs:connect', this.onConnect);
}
onPreConnect(event) {
// Merge into the existing options instead of replacing them
const scales = (event.detail.config.options.scales ??= {});
const y = (scales.y ??= {});
y.ticks = { ...y.ticks, callback: (value) => `${value} EUR` };
}
onConnect(event) {
event.detail.chart.options.onClick = (mouseEvent, elements) => { /* ... */ };
}
}
Dispatched on the <canvas> with the chartjs: prefix; they bubble.
| Event | event.detail | When |
|---|---|---|
chartjs:init | Chart (the class) | Once, before the first chart on the page |
chartjs:pre-connect | config, options | Before new Chart(): edit the config here |
chartjs:connect | chart | Right after creation |
chartjs:view-value-change | data, options | Before an update from new server data |
chartjs:disconnect | chart | Before the chart is destroyed |
Install the plugin, then register it globally on chartjs:init, in a file loaded before any chart connects:
php bin/console importmap:require chartjs-plugin-zoom # or: npm install chartjs-plugin-zoom
// assets/app.js
import zoomPlugin from 'chartjs-plugin-zoom';
document.addEventListener('chartjs:init', (event) => {
event.detail.Chart.register(zoomPlugin);
});
Configure it from PHP like any option: 'plugins' => ['zoom' => ['zoom' => ['wheel' => ['enabled' => true], 'mode' => 'xy']]].
When the chart data changes, the controller updates the existing chart instead of recreating it. That makes it work inside a LiveComponent:
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent]
final class SalesChart
{
use DefaultActionTrait;
#[LiveProp(writable: true)]
public string $period = 'month';
public function __construct(
private readonly ChartBuilderInterface $chartBuilder,
private readonly SalesRepository $sales,
) {}
public function getChart(): Chart
{
$points = $this->sales->totalsFor($this->period);
return $this->chartBuilder->createChart(Chart::TYPE_BAR)->setData([
'labels' => array_keys($points),
'datasets' => [['label' => 'Sales', 'data' => array_values($points)]],
]);
}
}
<div{{ attributes }}>
<select data-model="period">
<option value="week">Week</option>
<option value="month">Month</option>
</select>
{{ render_chart(this.chart) }}
</div>
chartjs:init from app.js, loaded before the first chart connects, to catch that one-time event.setOptions() and setData() replace the whole array; build each one completely.type works out of the box.pre-connect, merge into config.options rather than reassigning a whole branch (scales, plugins): reassigning drops what PHP sent.symfony/ux-chartjs on Packagist.chartjs:* eventsWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub smnandre/symfony-ux-skills --plugin symfony-ux-skills20 skills in this plugin
/live-componentSymfony UX LiveComponent: live search, live filters, dependent selects and form validation as users type. Use for reactive PHP and Twig components, #[AsLiveComponent], LiveProp, LiveAction, data-model, loading states, emit/listen, lazy rendering and LiveResponse downloads./stimulusSymfony UX Stimulus: JavaScript behavior for dropdowns, tabs, modals, clipboard buttons and third-party widgets. Use for StimulusBundle, data-controller, data-action, targets, values, outlets, lazy controllers and Twig stimulus_* helpers./symfony-uxSymfony UX: choose and combine packages for a Symfony frontend. Use when deciding between Stimulus, Turbo, TwigComponent and LiveComponent, or fitting several Symfony UX packages into the same feature. Prefer the dedicated skill for a task that fits one package./turboSymfony UX Turbo: Turbo Frames for partial navigation and form submissions, Turbo Streams for multiple updates and Mercure broadcasts, and Turbo Drive for links and forms. Use for turbo-frame, turbo-stream, twig:Turbo, TurboFrame, TurboStreamResponse, turbo_stream_from and #[Broadcast]./twig-componentSymfony UX TwigComponent: reusable Twig buttons, cards, alerts and design system components. Use for twig: tags, #[AsTwigComponent], props, blocks, attributes.defaults(), anonymous components, computed properties, provide()/inject() and html_cva./ux-autocompleteSymfony UX Autocomplete: searchable selects and Ajax suggestions for large Symfony form lists. Use for EntityType, ChoiceType, Tom Select, the autocomplete option, #[AsEntityAutocompleteField], #[AsAutocompleteField], AutocompleterInterface, extra_options and tom_select_options.Other plugins' skills for the same job