We will teach you about Laravel - GEO Chart Example using lavacharts in this article. Here, we'll explain in detail how to use Laravel's GEO Chart Example utilizing lavacharts and provide you with a sample if that's what you need.
Today, I'm going to demonstrate how to use the lavacharts package to add a geochart to your Laravel 5 application. Typically, we used geochart on the back end to visually check users according to their country. In this article, we'll use a geo-chart that shows the number of users by country. The benefit of lavachart is that you can manage all data with chart metadata from the controller, rendering just on view.
Other charts, such as bar charts, area charts, column charts, pie charts, and line charts, are also available at lavacharts. We'll utilize a geo chart with excellent graphics in this post. You just need to follow a few steps and you can use it in your Laravel application to receive the output seen in the preview below.
Step 1: Installation
Downloading the Lavacharts package in order to create a chart file from a view blade file is the first step. Run the following command in your terminal first, then:
composer require khill/lavacharts
Open the config/app.php file right now and add the service provider.
'providers' => [ .... KhillLavachartsLaravelLavachartsServiceProvider::class,]
Step 2: Add Table and Model
We need to make a new table called "country users" so that we can get data from it; you can make your own table instead, but this is just an example. Using the Laravel 5 php artisan command, we need to build a migration for the country users table. To start, run the following line:
php artisan make:migration create_country_users_table
Following this command, you will find a file in the database/migrations directory. You must add the following code to your migration file in order to build the country users table.
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateCountryUsersTable extends Migration
{
public function up()
{
Schema::create('country_users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('total_users');
$table->timestamps();
});
}
public function down()
{
Schema::drop("country_users");
}
}
Create the CountryUser model for country users after creating the "country users" table by creating the following file in the app/CountryUser.php directory:
app/CountryUser.php
namespace App; use IlluminateDatabaseEloquentModel; class CountryUser extends Model Okay, you may now add a few records as seen below: See also: How to use Highcharts to add charts in Laravel.{ public $fillable = ['name','total_users']; }
Okay, you may now add a few records as seen below:
See also: How to use Highcharts to add charts in Laravel.
Step 3: Add Route
We must include the route in this stage in order to generate the view. so add the following route by opening your app/Http/routes.php file.
Route::get('laracharts', 'ChartController@getLaraChart');
Step 4: Create Controller
In this directory, app/Http/Controllers/ChartController.php, we should now construct a new controller named ChartController. Ensure that the country users table contains some data. The following information should be included in the controller file because it will manage data, chart data, and view files:
app/Http/Controllers/ChartController.php
namespace AppHttpControllers; use IlluminateHttpRequest; use KhillLavachartsLavacharts; use AppCountryUser; class ChartController extends Controller { public function getLaraChart() { $lava = new Lavacharts; // See note below for Laravel $popularity = $lava->DataTable(); $data = CountryUser::select("name as 0","total_users as 1")->get()->toArray(); $popularity->addStringColumn('Country') ->addNumberColumn('Popularity') ->addRows($data); $lava->GeoChart('Popularity', $popularity); return view('laracharts',compact('lava')); } }
Step 4: Create View File
The final step is to build the view file "laracharts.blade.php" in order to generate the view chart. To do this, create the file and add the following code:
resources/view/laracharts.blade.php
<div id="pop-div" style="width:800px;border:1px solid black"></div>
<?= $lava->render('GeoChart', 'Popularity', 'pop-div') ?>
You may now go and check.
Here is a link where you may learn more about laracharts: laracharts.
I'm hoping this code and text will help you use Laravel 9 GEO Chart Example using LavaCharts Example.