My Jinji
My Jinji

Reputation: 1

Call to undefined method App\Charts\SampleChart::labels(), chartjs

I'm currently doing the dashboard in our project and can't progress because of this error. Call to undefined method App\Charts\SampleChart::labels(). I suspect that I missed some installation and configurations but I've done it several times now.

SampleChart.php

<?php

declare(strict_types = 1);

namespace App\Charts;

use Chartisan\PHP\Chartisan;
use ConsoleTVs\Charts\BaseChart;
use Illuminate\Http\Request;


class SampleChart extends BaseChart
{
    /**
     * Handles the HTTP request for the given chart.
     * It must always return an instance of Chartisan
     * and never a string or an array.
     */

    public function handler(Request $request): Chartisan
    {   

        return Chartisan::build()
            ->labels(['First', 'Second', 'Third'])
            ->dataset('Sample', [1, 2, 3])
            ->dataset('Sample 2', [3, 2, 1]);
    }
}

dashboardcontroller

   <?php

namespace App\Http\Controllers;
use App\Charts\SampleChart;
use Illuminate\Http\Request;
use App\Models\ratings;


class DashboardController extends Controller
{
    public function dashboard() {
        $rating = ratings::all();

        $chart = new SampleChart;
        $chart->labels(['One', 'Two', 'Three']);
        $chart->dataset('My Dataset 1', 'line', [1,2,4]);
        
        
        return view('user/admin/dashboard', compact('rating'));
    }
}

charts.php

 <?php

declare(strict_types=1);

return [
    /*
    |--------------------------------------------------------------------------
    | Global Route Prefix
    |--------------------------------------------------------------------------
    |
    | This option allows to modify the prefix used by all the chart routes.
    | It will be applied to each and every chart created by the library. This
    | option comes with the default value of: 'api/chart'. You can still define
    | a specific route prefix to each individual chart that will be applied after this.
    |
    */
    'global_route_prefix' => 'api/chart',



    /*
    |--------------------------------------------------------------------------
    | Global Middlewares.
    |--------------------------------------------------------------------------
    |
    | This option allows to apply a list of middlewares to each and every
    | chart created. This is commonly used if all your charts share some
    | logic. For example, you might have all your charts under authentication
    | middleware. If that's the case, applying a global middleware is a good
    | choice rather than applying it individually to each chart.
    |
    */
    'global_middlewares' => ['web'],

    /*
    |--------------------------------------------------------------------------
    | Global Route Name Prefix
    |--------------------------------------------------------------------------
    |
    | This option allows to modify the prefix used by all the chart route names.
    | This is mostly used if there's the need to modify the route names that are
    | binded to the charts.
    |
    */
    'global_route_name_prefix' => 'charts',
];

i followed this site just like what creators on youtube tells: https://charts.erik.cat/guide/installation.html what could be the problem? please help

Upvotes: 0

Views: 793

Answers (1)

unkind_sauce1
unkind_sauce1

Reputation: 528

It looks like you are trying to call ->labels() on an instance of SampleChart based on the principle of multi-inheritance in php. The problem with that here is that ->labels() is a method of Chartisan class not SampleChart or BaseChart which it extends, hence the error you get (basically just saying hey, I cannot find labels()).

See this answer for an explanation of multi-level inheritance in PHP. More info: https://www.php.net/manual/en/language.oop5.inheritance.php

I'm curious what your use case for doing this in your controller is though. If it is for rendering, the docs you provided has this: https://charts.erik.cat/guide/render_charts.html

Upvotes: 1

Related Questions