-1

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 !

IBRAHIM EZZAT
  • 964
  • 9
  • 22

1 Answers1

1

Instead of using render inside your model, could you maybe try to return the $chart model and chain the container/render method on it, something similar to this:

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

This would require that you return $chart from the method occ_trend inside your model.

P.S: Im not sure about the Chart library syntax, but maybe you are rendering the chart the wrong way?

EDIT: I think you are losing a reference to the $chart first created by script method and because you are looping twice. Maybe something like this might help:

<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>
Nikola Gavric
  • 3,507
  • 1
  • 8
  • 16