Laravel Charts is a package that allows developers to easily integrate charting capabilities into Laravel applications. It provides a fluent interface for creating various types of charts using popular JavaScript charting libraries like Chart.js, Google Charts, and Highcharts, making it easy to visualize data in a user-friendly manner.
Key Features of Laravel Charts:
- Multiple Charting Libraries: Supports various libraries like Chart.js, Google Charts, and Highcharts.
- Easy Integration: Simple installation and setup process.
- Fluent API: Provides a clean and fluent API for building charts with minimal configuration.
- Customizable: Allows customization of chart properties to fit your needs.
- Data Fetching: Integrates seamlessly with Eloquent models to fetch data for charts.
Installation
To get started with Laravel Charts, follow these steps:
- Require the Package: Install the package via Composer:
1composer require consoleTVs/charts - Publish Configuration (optional): You can publish the configuration file using:
1php artisan vendor:publish --provider="ConsoleTVs\Charts\ChartsServiceProvider" - Install Chart.js (or any preferred charting library): Depending on which library you want to use, you need to install it. For example, to use Chart.js, you can include it via npm or a CDN.
Using npm:
1npm install chart.jsUsing a CDN: You can also include it in your Blade template:
1<script src="https://cdn.jsdelivr.net/npm/chart.js"></script
Creating a Chart
- Setting Up a Controller: Create a controller to handle the chart data. For example:
1php artisan make:controller ChartController
- Building a Chart: In the
ChartController
, use the Charts facade to create a chart. Here’s an example of creating a simple bar chart:123456789101112131415161718192021namespace App\Http\Controllers;use ConsoleTVs\Charts\Facades\Charts;use App\Models\Post; // Assuming you have a Post modelclass ChartController extends Controller{public function index(){$chart = Charts::create('bar', 'chartjs') // Type and library->title('Posts Per Month')->elementLabel('Number of Posts')->labels(['January', 'February', 'March', 'April', 'May'])->values([5, 10, 15, 20, 25]) // Sample data->responsive(true)->dimensions(1000, 500)->draw();return view('charts.index', compact('chart'));}} - Creating a View: Create a view file at
resources/views/charts/index.blade.php
to display the chart:123456789101112@extends('layouts.app')@section('content')<div class="container"><h1>Posts Per Month Chart</h1><div>{!! $chart->container() !!}</div></div>{!! $chart->script() !!}@endsection - Defining Routes: Add a route in
routes/web.php
to access the chart page:123use App\Http\Controllers\ChartController;Route::get('/charts', [ChartController::class, 'index'])->name('charts.index');
Customizing Charts
You can customize various aspects of the charts, including colors, tooltips, and legends. Here’s an example of customizing the bar chart further:
1 2 3 4 5 6 7 8 9 |
$chart = Charts::create('bar', 'chartjs') ->title('Posts Per Month') ->elementLabel('Number of Posts') ->labels(['January', 'February', 'March', 'April', 'May']) ->values([5, 10, 15, 20, 25]) ->colors(['#FF6384', '#36A2EB', '#FFCE56']) ->responsive(true) ->dimensions(1000, 500) ->draw(); |
Using Dynamic Data
You can fetch data dynamically from your Eloquent models. For example, to create a chart showing the number of posts created per month:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$postsPerMonth = Post::selectRaw('MONTH(created_at) as month, COUNT(*) as count') ->groupBy('month') ->pluck('count'); $months = Post::selectRaw('MONTH(created_at) as month') ->groupBy('month') ->pluck('month'); $chart = Charts::create('bar', 'chartjs') ->title('Posts Per Month') ->elementLabel('Number of Posts') ->labels($months) ->values($postsPerMonth) ->responsive(true) ->dimensions(1000, 500) ->draw(); |
Conclusion
Laravel Charts provides a powerful and flexible way to integrate charting capabilities into your Laravel applications. With its easy setup and various customization options, you can visualize data effectively to enhance user experience.
Additional Considerations
- Documentation: For more detailed information, advanced features, and customization options, refer to the official Laravel Charts documentation.
- Chart Types: Explore different types of charts available in the package, including line, pie, and area charts.