Kritim Yantra
Apr 07, 2025
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:
You can include Highcharts via CDN or npm:
Add to your Blade template:
<script src="https://code.highcharts.com/highcharts.js"></script>
npm install highcharts
Then import in your JavaScript file:
import Highcharts from 'highcharts';
<div id="chart-container" style="width: 100%; height: 400px;"></div>
// ChartController.php
public function salesChart()
{
$salesData = [
'Jan' => 4000,
'Feb' => 3000,
'Mar' => 5000,
'Apr' => 7000,
'May' => 6000
];
return view('dashboard', compact('salesData'));
}
<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>
Highcharts.chart('line-chart', {
chart: { type: 'line' },
title: { text: 'Website Traffic' },
series: [{
name: 'Visitors',
data: [1000, 1200, 1300, 1450, 1700]
}]
});
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 }
]
}]
});
// Fetch data from Laravel API
fetch('/api/sales-data')
.then(response => response.json())
.then(data => {
Highcharts.chart('dynamic-chart', {
series: [{ data: data }]
});
});
// In Controller
return response()->json(
Sales::query()
->whereYear('created_at', now()->year)
->orderBy('month')
->cursor() // Uses lazy collection
);
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();
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
// 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);
});
Highcharts.chart('drilldown-chart', {
series: [{
name: 'Categories',
data: [{
name: 'Electronics',
y: 55,
drilldown: 'electronics'
}]
}],
drilldown: {
series: [{
id: 'electronics',
data: [
['Phones', 35],
['Laptops', 20]
]
}]
}
});
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
🚀 Start building stunning data visualizations in your Laravel apps today!
📌 Need help? Drop your questions below! 👇
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google