I’m trying to integrate the chartjs-plugin-streaming plugin with Chart.js in a Laravel project using FilamentPHP for real-time data visualization. My goal is to use the realtime scale provided by chartjs-plugin-streaming in a Filament ChartWidget, but the scale isn’t recognized, and I’m not seeing any streaming updates.
Here’s a summary of my setup and what I’ve tried so far:
-
Installed the streaming plugin: npm install chartjs-plugin-streaming --save
-
Configured filament-chart-js-plugins.js: resources/js/filament-chart-js-plugins.js:
import ChartPluginZoom from 'chartjs-plugin-zoom';
import ChartPluginStreaming from 'chartjs-plugin-streaming';
window.filamentChartJsPlugins ??= [];
window.filamentChartJsPlugins.push(ChartPluginZoom);
window.filamentChartJsPlugins.push(ChartPluginStreaming);
- Updated vite.config.js to include filament-chart-js-plugins.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/js/app.js',
'resources/css/filament/admin/theme.css',
'resources/js/filament-chart-js-plugins.js', // Ensuring the plugins file is built
],
}),
],
});
- Registered the custom asset in Filament: AppServiceProvider.php
FilamentAsset::register([
Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(),
]);
- Ran commands:
npm run build
php artisan filament:assets
After performing all the above steps, the plugin doesn’t seem to work correctly: Error: “realtime” is not a registered scale.
If I disable the Streaming Plugin, the Zoom Plugin work’s fine.
My Widget Code for Reference:
protected function getOptions(): RawJs
{
return RawJs::make(
<<<JS
{
responsive: true,
plugins: {
title: {
display: true,
},
subtitle: {
display: false,
},
description: {
display: false,
},
legend: {
display: true,
},
zoom: {
pan: {
enabled: true,
mode: 'x',
},
zoom: {
wheel: {
enabled: true,
},
drag: {
enabled: false,
},
pinch: {
enabled: true,
},
mode: 'x',
onZoomComplete({chart}) {
chart.update('none');
}
}
},
},
scales: {
x: {
type: 'realtime',
realtime: {
delay: 2000,
refresh: 1000,
delay: 2000,
onRefresh: chart => {
console.log('Done');
}
},
},
y: {
type: 'linear',
display: true,
position: 'left',
beginAtZero: true,
max: 200,
},
},
}
JS
);
}