I'm working with Laravel 5.7 and use the Laravel Chart ( ConsoleTVs/Charts ) https://charts.erik.cat/ ,and because i need same chart in many positions in my application ,also want to pass variable to generate many chart , I'm trying to generate chart from calling method inside model class instead using the controller as in documentation ,code example:
inside model:
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);
}
}
and in view i use something like:
<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>
and off course i pass variable $types through the controller
i expected the output to be the charts according to passed variable , but it didn't render the chart at all with no errors, any help !