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,问题出在哪里?请帮帮忙
发布于 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
https://stackoverflow.com/questions/68453424
复制相似问题