DEV Community

Discussion on: Use Laravel charts in Laravel.

sammymwangangi profile image
Sammy Mwangangi

This is the github code:

github.com/sammymwangangi/TALL-Das...

Thread Thread
arielmejiadev profile image
Ariel Mejia Author

Hi Sammy, excuse me for the wait, today I make some time to review the code, I create an example using the data that you want, everything is working fine just remeber in docs I add an alert on line, area charts because in this charts you can represent more than one series of data you need to set the data like this:

$chart->setDataset([
    [
        "name" => "Earnings",
        "data" => [100, 200, 300]
    ]
])

As you can see its a multidimensional array an every subset is an associative array, then you can see that the "data" key has an array as its value, I add a live example here: github.com/ArielMejiaDev/Larapex-C...

you can check the code, thanks for using Larapex Charts, I saw your dashboard project so I add the example using Tailwindcss to you can get a pretty good idea how it looks on your project, please let me know about your project it seems very interesting, there is something more that I could help you I will be here.

Thread Thread
sammymwangangi profile image
Sammy Mwangangi • Edited on

Thank you for your feedback. I appreciate your effort to help me. I was trying to use your package for charts on my project instead of using chartsJs. I will follow your advice and also check your example there to rectify the problem.
Thanks also for the interest in my project. It's an open-source TALL Stack dashboard. I am trying to make sure all the technologies have been represented well on the dashboard. Tailwind for UI design, Alpine Js, and Livewire for UX and of course Laravel for backend stuff. I have implemented the new Laravel component features also. It's still a WIP though. I'll need to clean the code once I am done with everything.
You're welcome to fork or do a pull request. Or give me a star.

Thread Thread
sammymwangangi profile image
Sammy Mwangangi

I have noticed something with the "undefined variable $chart" error. It was caused when I moved the main layout file to the components folder. That is when I started to implement the Laravel's components feature.
I have created a similar file and placed it on the layouts folder and changed my view file to use the initial methods; @extends and @yield().

Thread Thread
arielmejiadev profile image
Ariel Mejia Author

Hi, you are using the new Laravel components? in this case if you are using variables you need to add a class to add the component logic, look at "app/Views" folder, you can create a view composition class with the artisan command: "php artisan make:component yourcomponent", then it will add a file with a render function that calls directly the view then in the constructor add the $chart variable that you need maybe something like:

php artisan make:component EarningsChart

go to app/Views/chart.php

<?php
namespace App\View\Components;

use Illuminate\View\Component;
use ArielMejiaDev\LarapexCharts\LarapexChart;

class Alert extends Component
{

    /**
     * Chart instance.
     *
     */
    public $chart;

    /**
     * Create the component instance.
     *
     * @param $chart
     * @return void
     */
    public function __construct(LarapexChart $chart)
    {
        $this->chart = (new LarapexChart)
        ->setType('line')
        ->setTitle('Earnings')
        ->setXAxis([
            'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
        ])
        ->setDataset([
            [
                'name'  =>  'Earnings',
                'data'  =>  [0, 10000, 15000, 10000, 20000, 15000, 25000, 20000, 30000, 25000, 40000]
            ]
        ])
    ;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.alert');
    }
}

Please let me know if this approach works, and congrats your Tall dashboard looks amazing, I am doing a secret open source project with similar aproach but I prefer VueJS

Thread Thread
sammymwangangi profile image
Sammy Mwangangi

Thanks for your feedback. I will try out this approach and I'll let you know if it works. Also, I can't wait to see your project.

Thread Thread
arielmejiadev profile image
Ariel Mejia Author

Thanks!

Thread Thread
alexander0312 profile image
alexander lim

hi,, i'm using larapexchart in laravel but i keep on getting a error .. saying that Call to a member function container() on array.. can you help me please...

Thread Thread
arielmejiadev profile image
Ariel Mejia Author

Hi there! thanks for using Larapex charts, here is a live sample with TailwindCSS: larapex-chart-example.herokuapp.com/

Here the controller where I built the charts: github.com/ArielMejiaDev/Larapex-C...

Here the view: github.com/ArielMejiaDev/Larapex-C...

If you want to share the code I can give a look at the code, alternatively you can make a "dd()" to the chart variable before pass it to the view and check that its a LarapexChart object instance.