DEV Community

Cover image for How To Create Dynamic Apexcharts Using Larapex Charts Package in Laravel 11
Saddam Hossain
Saddam Hossain

Posted on

How To Create Dynamic Apexcharts Using Larapex Charts Package in Laravel 11

In this tutorial, I will show you how to create dynamic apexcharts using larapex charts package in laravel 11 application.

ApexCharts is a JavaScript library used for creating beautiful and interactive charts on websites. It makes it easy to visualize data through various chart types like bar, line, pie, and more. Users can customize the appearance, animate the charts, and interact with them to explore data. ApexCharts is popular because it’s easy to use and helps make data look appealing and understandable.

In this example, we will create some dummy user records and then display a pie chart with all months of the current year. So let’s follow the below steps and add a chart to your Laravel 11 apps. You Can Learn How To Move File from One Folder to Another In Laravel

How To Create Dynamic Apexcharts Using Larapex Charts Package in Laravel 11

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

How To Create Dynamic Apexcharts Using Larapex Charts Package in Laravel 11

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install arielmejiadev/larapex-charts

we need to install arielmejiadev/larapex-charts composer package using the following command:

composer require arielmejiadev/larapex-charts
Enter fullscreen mode Exit fullscreen mode

Now, we will publish configuration file using the following command:

php artisan vendor:publish --tag=larapex-charts-config
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Route

First of all, we will create a simple route for creating a simple line chart. So let’s add simple routes as below:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ApexchartsController;

Route::get('charts', [ApexchartsController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Chart Class

In this step, we will create “Charts” folder in app folder. then we will create MonthlyUsersChart.php file with the following code:

app/Charts/MonthlyUsersChart.php

<?php

namespace App\Charts;

use ArielMejiaDev\LarapexCharts\LarapexChart;
use App\Models\User;
use DB;

class MonthlyUsersChart
{
    protected $chart;

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function __construct(LarapexChart $chart)
    {
        $this->chart = $chart;
    } 

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function build()
    {
        $users = User::select(DB::raw("COUNT(*) as count"), DB::raw("MONTHNAME(created_at) as month_name"))
                    ->whereYear('created_at', date('Y'))
                    ->groupBy(DB::raw("Month(created_at)"))
                    ->pluck('count', 'month_name');

        return $this->chart->pieChart()
            ->setTitle('New Users - '.date('Y'))
            ->addData($users->values()->toArray())
            ->setLabels($users->keys()->toArray());
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Create Controller

Here, we will create a new controller named ApexchartsController. So let’s add the code below to that controller file.

app/Http/Controllers/ApexchartsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Charts\MonthlyUsersChart;

class ApexchartsController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(MonthlyUsersChart $chart)
    {
        return view('apexcharts', ['chart' => $chart->build()]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Read Full Tutorials

Top comments (0)