首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用未定义的方法App\Charts\SampleChart::labels(),chartjs

问调用未定义的方法App\Charts\SampleChart::labels(),chartjs
EN

Stack Overflow用户
提问于 2021-07-20 18:35:36
回答 1查看 121关注 0票数 0

SampleChart.php

代码语言:javascript
复制
<?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]);
    }
}

仪表板控制器

代码语言:javascript
复制
   <?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

代码语言:javascript
复制
 <?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',
];

我关注了这个网站,就像youtube上的创建者告诉我的那样:https://charts.erik.cat/guide/installation.html,问题出在哪里?请帮帮忙

EN

回答 1

Stack Overflow用户

发布于 2021-07-20 19:21:54

看起来您正在尝试基于php中的多重继承原理在SampleChart的实例上调用->labels()。这里的问题是,->labels()是Chartisan类的方法,而不是它扩展的SampleChart或BaseChart,所以你会得到错误(基本上就是说,嘿,我找不到labels())。

有关answer中的多级继承的说明,请参阅此PHP。更多信息:https://www.php.net/manual/en/language.oop5.inheritance.php

我很好奇你在控制器中做这件事的用例是什么。如果它是用于呈现的,那么您提供的文档具有以下内容:https://charts.erik.cat/guide/render_charts.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68453424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档