How To Create Charts Using Laravel In Your Web Application

create charts with laravel

Laravel is a PHP framework used to develop web applications. As a framework, Laravel is composed of a series of ready-to-use tools, libraries, and assets. It is based on the MVC paradigm, which allows the developer to extract applications into a business layer, the model, a rendering layer, the view, an application layer, and the controller.

Like most frameworks nowadays, Laravel depends heavily on Composer to handle its dependencies. Since Laravel is concocted from various libraries, it keeps them neat and tidy. It’s partially based on Symfony, which is why part of the file structure and syntax is similar.
Let’s take a look at what Laravel requires to work properly. The earliest PHP version required for Laravel to work is 5.6.4. Laravel will also work with PHP’s latest version, PHP 7.

Part 1: Including Dependencies

To render FusionCharts using Laravel, we need to include following:
Composer (Download): Download and run Composer-Setup.exe.

Laravel 5: Like most frameworks nowadays, there are a couple of ways to install Laravel. In this example, we’ll do the simplest installation using Composer.

composer create-project --prefer-dist laravel/laravel5 blog

Note: Here, blog is the name of our application folder.

FusionCharts Suite XT (Download): Download FusionCharts Suite XT zip file and store all the extracted script files inside the public/js folder. Include it in the html section of index.blade.php file which is inside the views/fusioncharts section using the following format:

FusionCharts PHP Wrapper (Download): Extract the fusioncharts PHP wrapper and save the fusioncharts.php inside the composer.json file. Add the required files in autoload section – in a new entry called files as shown below:

Note: Don’t forget to run composer dump-autoload for changes to take effect.

Part 2: Creating Controller and its View to Render the chart

Step 1: Implementing the Controller

Create a FusionCharts controller using the command php artisan make:controller FusionCharts .

Step 2: Routing to Controllers

Define a route for the FusionCharts page, which will be managed by the FusionCharts Controller, since it will only be a static page with no functionality. Now let’s open routes, web.php, inside that add the route as shown below:

Route::get('/fusioncharts', '[email protected]');

Step 3: Defining Methods

In the previous snippet, home is the name of the method which is defined within the FusionCharts controller as shown below:

 

Here index is the view page which resides within the view\fusioncharts folder.

Step 4: Creating the View

The view page contains two sections, the html and the php section. In the very 1st step of part-1 , we have added the js dependencies into the html section. Here, we will add the php code inside the body as shown below:

Thereafter, add a div container where the chart will render.

<div id="chart-1">Fusion Charts will render here</div>

Step 5: Executing the Code:

Finally, run the server by the command php artisan serve and then route to the path which we have already set-up in our route directory for the view of the chart –
e.g. http://127.0.0.1:8000/fusioncharts

Your output should look like the image shown below:

If you see any errors in your code, click here to download the complete source code of the sample project we have created for this tutorial.

Leave a Comment