【问题标题】:Call to undefined method App\Charts\SampleChart::labels(), chartjs调用未定义的方法 App\Charts\SampleChart::labels(), chartjs
【发布时间】:2021-07-20 10:35:36
【问题描述】:

我目前正在我们的项目中做仪表板,由于这个错误而无法进行。调用未定义的方法 App\Charts\SampleChart::labels()。我怀疑我错过了一些安装和配置,但我已经做了好几次了。

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]);
    }
}

仪表板控制器

   <?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',
];

我关注了这个网站,就像 youtube 上的创作者所说的那样:https://charts.erik.cat/guide/installation.html 可能是什么问题呢?请帮忙

【问题讨论】:

  • 您缺少new SimpleChart() 的括号。
  • 对不起,还是一样
  • 我听到很多人抱怨最新版本的 chartjs,他们也遇到了同样的错误。会是这个原因吗?

标签: php laravel chart.js


【解决方案1】:

您似乎正试图根据 php 中的多继承原则在 SampleChart 的实例上调用 -&gt;labels()here 的问题在于 -&gt;labels()Chartisan 类的方法而不是 SampleChartBaseChart 它扩展,因此你得到的错误(基本上只是说嘿,我不能找到labels())。

查看answer 了解 PHP 中的多级继承。 更多信息:https://www.php.net/manual/en/language.oop5.inheritance.php

我很好奇您在控制器中执行此操作的用例是什么。如果是用于渲染,您提供的文档有:https://charts.erik.cat/guide/render_charts.html

【讨论】:

  • 我一直在关注这个人的代码。 youtube.com/watch?v=pqVoUYzZQgk&t=13s 虽然我明白问题出在哪里,但我只是不知道如何解决它。对不起,我有点新。你能指导我吗?
  • 当我看到他的 samplechart.php 时,我们有不同的扩展。这是因为我们有不同的版本吗?
  • 正确,你有不同的版本,所以我猜自从那个视频以来,渲染图表的方式发生了变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2021-10-22
相关资源
最近更新 更多