如何将 Laravel 图表 (ConsoleTVs/Charts) 作为模型内部的可调用方法而不是控制器?

How make Laravel Charts (ConsoleTVs/Charts) as recallable method inside model instead of controller?

我正在使用 Laravel 5.7 并使用 Laravel 图表 ( ConsoleTVs/Charts ) https://charts.erik.cat/ ,并且因为我在我的应用程序的许多位置需要相同的图表,也想传递变量来生成许多图表,我试图通过调用模型 class 内的方法来生成图表,而不是像文档中那样使用控制器,代码示例:

内部模型:

namespace App;

use App\fleet;
use Carbon\Carbon;
use App\Charts\echart_pi;
use Illuminate\Database\Eloquent\Model;

class occ_report extends Model{

/**
* Occurrence Report Trends
*/
public static function occ_trend($type){    

/**
* Occurrence report chart statistics
*/

    $occ_reports=occ_report::all();
foreach($occ_reports as $report){

    $types[]=(fleet::find($report->ac_id))['type'];
}

$fleet_types=fleet::all()->unique('type');
foreach($fleet_types as $ac_type){

    $occ_count[]=[
        ($ac_type['type']) => count(array_keys($types,$ac_type['type']))
    ];
}

foreach($occ_count as $occ_report_count){
    foreach($occ_report_count as $key=>$value){

        $keys[]=$key;
        $values[]=$value;

    }
}

$occ_chart = new echart_pi;

$occ_chart->dataset('Occurrence Report Statistics', 'pie', $values)->options([
    'radius'    => (['35%','55%']),
    'color'     => ['#6c757d','#ffc107','#28a745','#17a2b8','#dc3545','#007bff','#ff4500'],
    'roseType'  => true,
]);

$occ_chart->options([
    'toolbox'   =>[
        'show'      =>true,
        'orient'    =>'vertical',
        'feature'   =>[

            'restore'=>[
                'show'  =>true,
                'title' =>'restore'
            ],
            'saveAsImage'=>[
                'show'  =>true,
                'title' =>'Save'
            ],
        ]
    ],
    'tooltip'=>[
        'formatter'=>'<div class="text-center">{a}<span class="text-info">{b}</span><br/>{c}<br/>( {d} % )</div>'
    ],
    'title' =>[
        'show'      =>true,
        'text'      =>'Occurrence Report Statistics',
        'bottom'    =>0,
        'left'      =>'30%',

        'textStyle' =>[
            'color'         =>'#17a2b8',
        ],
    ],

    'legend'=>[
        'show'      =>true,
        'orient'    =>'vertical',
        'left'      =>0,

        'textStyle' =>[
            'color' =>['#ffc107','#dc3545','#28a745']
        ],
    ]
]);
$occ_chart->labels($keys);
$occ_chart->displayAxes(false);
$occ_chart->theme('light');
$occ_chart->displayLegend(true);

return ($occ_chart);

}
}

并且在视图中我使用类似的东西:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.2/echarts-en.min.js" charset="utf-8"></script>

@foreach($types as $type)
    {!! App\occ_report::occ_trend($type)->script() !!}
@endforeach

<div class="row mt-4">
        @foreach($types as $type)
            <div class="col-md-6 mt-4">
                {!! App\occ_report::occ_trend($type)->container()  !!}
            </div>
        @endforeach
    </div>    

当然我通过控制器传递变量 $types

我希望输出是根据传递的变量生成的图表,但它并没有毫无错误地呈现图表,任何帮助!

而不是在你的模型中使用 render,你是否可以尝试 return $chart 模型并在其上链接 container/render 方法,类似于此:

{!! App\occ_report::occ_trend($type)->container()  !!}
// or
{!! App\occ_report::occ_trend($type)->render($type . 'Occ_report_kpi')  !!}

这需要您从模型中的方法 occ_trend return $chart

P.S:我不确定 Chart library syntax,但也许您渲染图表的方式不对?

编辑:我认为您丢失了对 $chart 最初由 script 方法创建的引用,因为您正在循环两次。也许这样的事情可能会有所帮助:

<div class="row mt-4">
        @foreach($types as $type)
            <div class="col-md-6 mt-4">
                <?php $chart = App\occ_report::occ_trend($type); ?>
                {!! $chart->script()  !!}
                {!! $chart->container()  !!}
            </div>
        @endforeach
    </div>