Integrating Highcharts with Laravel 12: A Comprehensive Guide

Author

Kritim Yantra

Apr 07, 2025

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:

  1. Setting up Highcharts in Laravel 12
  2. Passing Laravel data to Highcharts
  3. Creating different chart types
  4. Best practices for performance
  5. 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:

  1. Explore Highcharts Stock for financial charts
  2. Implement Highmaps for geographic data
  3. Add 3D charts for advanced visualizations

🚀 Start building stunning data visualizations in your Laravel apps today!

📌 Need help? Drop your questions below! 👇

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts

How to Upload Images in Laravel 12: A Step-by-Step Guide
Kritim Yantra Kritim Yantra
Feb 28, 2025
Laravel 12 Multi-Auth System: Implementing Separate Admin and User Tables
Kritim Yantra Kritim Yantra
Feb 28, 2025
Laravel 12 Roles and Permissions Setup: Complete Guide
Kritim Yantra Kritim Yantra
Feb 28, 2025