Integrating Highcharts with Laravel 12: A Comprehensive Guide
Introduction
Highcharts is a powerful JavaScript charting library that enables developers to create interactive, visually appealing charts for data visualization. When combined with Laravel 12, you can build dynamic dashboards, analytics tools, and data-driven applications with ease.
In this guide, we'll cover:
- Setting up Highcharts in Laravel 12
- Passing Laravel data to Highcharts
- Creating different chart types
- Best practices for performance
- Real-world examples
1. Setting Up Highcharts in Laravel 12
1.1 Install Highcharts
You can include Highcharts via CDN or npm:
Option 1: CDN (Quick Start)
Add to your Blade template:
<script src="https://code.highcharts.com/highcharts.js"></script>
Option 2: NPM (Recommended for SPAs)
npm install highcharts
Then import in your JavaScript file:
import Highcharts from 'highcharts';
1.2 Create a Chart Container
<div id="chart-container" style="width: 100%; height: 400px;"></div>
2. Passing Laravel Data to Highcharts
2.1 Prepare Data in Controller
// ChartController.php
public function salesChart()
{
$salesData = [
'Jan' => 4000,
'Feb' => 3000,
'Mar' => 5000,
'Apr' => 7000,
'May' => 6000
];
return view('dashboard', compact('salesData'));
}
2.2 Render Chart in Blade
<script>
document.addEventListener('DOMContentLoaded', function () {
Highcharts.chart('chart-container', {
chart: { type: 'column' },
title: { text: 'Monthly Sales Data' },
xAxis: {
categories: @json(array_keys($salesData))
},
yAxis: { title: { text: 'Sales ($)' } },
series: [{
name: 'Sales',
data: @json(array_values($salesData))
}]
});
});
</script>
3. Creating Different Chart Types
3.1 Line Chart
Highcharts.chart('line-chart', {
chart: { type: 'line' },
title: { text: 'Website Traffic' },
series: [{
name: 'Visitors',
data: [1000, 1200, 1300, 1450, 1700]
}]
});
3.2 Pie Chart
Highcharts.chart('pie-chart', {
chart: { type: 'pie' },
title: { text: 'Market Share' },
series: [{
name: 'Brands',
data: [
{ name: 'Apple', y: 45 },
{ name: 'Samsung', y: 30 },
{ name: 'Huawei', y: 15 },
{ name: 'Others', y: 10 }
]
}]
});
3.3 Dynamic AJAX Chart
// Fetch data from Laravel API
fetch('/api/sales-data')
.then(response => response.json())
.then(data => {
Highcharts.chart('dynamic-chart', {
series: [{ data: data }]
});
});
4. Best Practices for Performance
4.1 Use Lazy Loading for Large Datasets
// In Controller
return response()->json(
Sales::query()
->whereYear('created_at', now()->year)
->orderBy('month')
->cursor() // Uses lazy collection
);
4.2 Server-Side Data Processing
Process aggregations in Laravel before sending to frontend:
$sales = DB::table('orders')
->selectRaw('MONTH(created_at) as month, SUM(amount) as total')
->groupBy('month')
->get();
4.3 Enable Chart Exporting
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
5. Real-World Examples
Example 1: Live Dashboard
// Using Laravel Echo for real-time updates
Echo.channel('sales')
.listen('NewSale', (data) => {
const chart = Highcharts.charts[0];
chart.series[0].addPoint(data.sale, true, true);
});
Example 2: Drilldown Chart
Highcharts.chart('drilldown-chart', {
series: [{
name: 'Categories',
data: [{
name: 'Electronics',
y: 55,
drilldown: 'electronics'
}]
}],
drilldown: {
series: [{
id: 'electronics',
data: [
['Phones', 35],
['Laptops', 20]
]
}]
}
});
Conclusion
You've now learned how to:
✅ Integrate Highcharts with Laravel 12
✅ Pass dynamic data from Laravel to JavaScript
✅ Create various chart types (line, column, pie)
✅ Optimize performance for large datasets
✅ Implement real-time charts with Laravel Echo
Next Steps:
- Explore Highcharts Stock for financial charts
- Implement Highmaps for geographic data
- Add 3D charts for advanced visualizations
🚀 Start building stunning data visualizations in your Laravel apps today!
📌 Need help? Drop your questions below! 👇
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.