ob/highcharts-bundle
Symfony bundle that simplifies using Highcharts by generating chart configuration in PHP and rendering via Twig extensions. Build rich, interactive graphs with less JS and DRY chart code. Note: Highcharts requires a commercial license for commercial use.
Install via Composer (fork or create a wrapper package):
composer require marcaube/ob-highcharts-bundle
Note: Since this is a Symfony bundle, create a Laravel-compatible package by extracting core classes (Series, Options, Chart) into a new package (e.g., laravel-highcharts).
Extract Core Classes: Copy these files from the bundle to your Laravel project:
src/Ob/HighchartsBundle/Chart/Series.phpsrc/Ob/HighchartsBundle/Chart/Options.phpsrc/Ob/HighchartsBundle/Chart/Chart.php
Place them in app/Highcharts/ or a new vendor/laravel-highcharts/src/ directory.Register Autoloading:
Add to composer.json:
"autoload": {
"psr-4": {
"App\\Highcharts\\": "app/Highcharts/",
"LaravelHighcharts\\": "vendor/laravel-highcharts/src/"
}
}
Run composer dump-autoload.
Publish Highcharts Assets: Download Highcharts JS/CSS from their site and place in:
public/vendor/highcharts/
Or use a CDN:
<script src="https://code.highcharts.com/highcharts.js"></script>
First Chart in Blade: Create a simple line chart in a controller:
// app/Http/Controllers/ChartController.php
use App\Highcharts\Chart;
use App\Highcharts\Series;
use App\Highcharts\Options;
public function showChart() {
$chart = new Chart();
$chart->setTitle('Monthly Visits')
->setType('line');
$series = new Series();
$series->setName('Traffic')
->setData([1, 2, 3, 4, 5]);
$chart->addSeries($series);
return view('charts.example', ['chart' => $chart]);
}
Render in Blade (resources/views/charts/example.blade.php):
<div id="container" style="height: 400px;"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
Highcharts.chart('container', @json($chart->getConfig()));
});
</script>
Pattern: Define chart configurations as PHP classes for reuse across controllers/views. Example:
// app/Highcharts/Definitions/UserActivityChart.php
namespace App\Highcharts\Definitions;
use App\Highcharts\Chart;
use App\Highcharts\Series;
class UserActivityChart extends Chart {
public function __construct($data) {
$this->setTitle('User Activity')
->setType('spline')
->setXAxis(['categories' => ['Jan', 'Feb', 'Mar']]);
$series = new Series();
$series->setName('Active Users')
->setData($data);
$this->addSeries($series);
}
}
Usage in Controller:
$chart = new UserActivityChart([100, 200, 150]);
return view('dashboard', ['chart' => $chart]);
Pattern: Fetch data from Eloquent and bind it to charts dynamically. Example:
// Controller
public function salesChart() {
$sales = Sales::selectRaw('DATE(created_at) as date, SUM(amount) as total')
->groupBy('date')
->pluck('total', 'date')
->toArray();
$chart = new Chart();
$chart->setTitle('Monthly Sales')
->setType('column');
$series = new Series();
$series->setName('Revenue')
->setData(array_values($sales));
$chart->addSeries($series)
->setXAxis(['categories' => array_keys($sales)]);
return view('reports.sales', ['chart' => $chart]);
}
Pattern: Create Blade directives to mimic Twig syntax (optional).
Register in AppServiceProvider:
use Illuminate\Support\Facades\Blade;
Blade::directive('highchart', function ($expression) {
return "<?php echo app('highcharts')->chart($expression)->render(); ?>";
});
Usage in Blade:
@highchart($chart)
Pattern: Fetch chart data via API endpoints and update dynamically. Controller:
public function getChartData() {
$data = Analytics::getWeeklyData();
return response()->json($data);
}
JavaScript (Blade):
<script>
fetch('/api/chart-data')
.then(response => response.json())
.then(data => {
const chart = Highcharts.chart('container', {
series: [{
data: data.series
}]
});
});
</script>
Pattern: Extend Options class to apply consistent themes.
Example:
// app/Highcharts/Themes/DarkTheme.php
namespace App\Highcharts\Themes;
use App\Highcharts\Options;
class DarkTheme extends Options {
public function __construct() {
$this->setChart(['backgroundColor' => '#333'])
->setTitle(['style' => ['color' => '#fff']])
->setPlotOptions(['series' => ['color' => '#fff']]);
}
}
Usage:
$chart = new Chart();
$chart->applyTheme(new DarkTheme());
Pattern: Use Laravel events to modify charts globally. Example:
// app/Providers/EventServiceProvider.php
protected $listen = [
'highcharts.build' => [
'App\Listeners\AddGlobalTooltip',
],
];
Listener:
// app/Listeners/AddGlobalTooltip.php
public function handle($event) {
$event->chart->getOptions()->setTooltip(['shared' => true]);
}
Twig Dependency:
{% highcharts_chart %}).$chart->render()).<script>
const config = @json($chart->getConfig());
Highcharts.chart('container', config);
</script>
Highcharts Licensing:
Asset Loading:
// resources/js/app.js
import Highcharts from 'highcharts';
window.Highcharts = Highcharts;
<script src="{{ mix('js/app.js') }}"></script>
Deprecated Highcharts Version:
npm install highcharts@latest
Symfony-Specific Features:
ContainerAware and EventDispatcher won’t work in Laravel.ServiceProvider or standalone implementations:
// Replace ContainerAware with Laravel's Container
$this->container = app();
Dynamic Data Binding:
Series may cause serialization errors.$series->setData($collection->pluck('value')->toArray());
Inspect Chart Config: Dump the raw config to debug:
dd($chart->getConfig());
Highcharts Console: Enable Highcharts’ built-in console for errors:
Highcharts.setOptions({
debug: true
});
Blade Debugging: For custom directives, use:
@dump($chart->getConfig())
Asset Paths: Ensure Highcharts JS is loaded before rendering:
@vite(['
How can I help you explore Laravel packages today?