score:0

Accepted answer

return just array of data from laravel and in view balde use

var data = json.parse( '{!! json_encode($chartdata) !!}' );

i used this in my code

score:0

wanted to give a code example for how i did this. kind of a pain but from samsquanch i guess this is normal. this does several database calls but still plenty fast for me. this is using laravel framework.

    // main chart info
    $printerchartarray ["chart"] = array (
            'type' => 'line' 
    );
    $printerchartarray ["title"] = array (
            "text" => "units per printer" 
    );
    $printerchartarray ["credits"] = array (
            "enabled" => false 
    );
    $printerchartarray ["xaxis"] = array (
            "categories" => array () 
    );
    $printerchartarray ["tooltip"] = array (
            "valuesuffix" => " units" 
    );
    $printerchartarray ["yaxis"] = array (
            "title" => array (
                    "text" => "units" 
            ) 
    );

    // get series names
    $printernames = printjob::distinct()->select('printer')
        ->wheredate('job_date', '>=', date($from))
        ->wheredate('job_date', '<=', date($to))
        ->get();

    // loop through series names and set series name and data for high charts
    $chartseries = [];
    foreach($printernames as $printer)
    {
        $printname = $printer->printer;

        // used where inside join so wouldnt miss dates
        // used a calendar table with all dates to fill in missing dates
        $data = printjob::select(db::raw('coalesce(sum(print_jobs.quantity), 0) as sum'), db::raw('calendar.datefield'))
        ->rightjoin('calendar', function($join) use ($printname)
        {
            $join->on('print_jobs.job_date', '=', 'calendar.datefield')
                ->where('printer', '=', $printname);
        })
        ->wheredate('calendar.datefield', '>=', date($from))
        ->wheredate('calendar.datefield', '<=', date($to))         
        ->groupby('calendar.datefield')
        ->orderby('calendar.datefield', 'asc')
        //->tosql();
        ->get();
        //->pluck('sum');

        // needed to convert the returned value from a string value to a int value (apparently a driver issue with php and mysql)
        foreach ($data as $key => $var) {
            $data[$key] = (int)$var->sum;
        }
        // add series to high chart
        $chartseries[] = array(
            "name" => $printname,
            "data" => $data
        );
    }

    // unessesary to use printjob for this but keeping similar to series to not mess up label
    $chartlabel = printjob::select(db::raw('calendar.datefield'))
        ->rightjoin('calendar', function($join) use ($printname)
        {
            $join->on('print_jobs.job_date', '=', 'calendar.datefield');
        })
        ->wheredate('calendar.datefield', '>=', date($from))
        ->wheredate('calendar.datefield', '<=', date($to))         
        ->groupby('calendar.datefield')
        ->orderby('calendar.datefield', 'asc')
        //->tosql(); --- this will show the actual query
        //->get(); --- will give array of objects
        ->pluck('calendar.datefield'); // --- will give a array of just the values wanted

    $printerchartarray ["series"] = $chartseries;
    $printerchartarray ["xaxis"] = array (
            "categories" => $chartlabel 
    );
    //return $printerchartarray; -- used to see output without view

  return view('print.charts', compact('jobsbydaylabels', 'jobsbydayvalues', 'unitsbydaylabels', 'unitsbydayvalues', 'printerchartarray'));

and this is the view part

    <!-- chart for units by printer by day -->
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div id="printerunitsbyday" style="min-width: 310px; height: 500px; margin: 0 auto"></div>
        </div>
    </div>
</div>

and this below is the script on the view

$(function() {
  $('#printerunitsbyday').highcharts({!! json_encode($printerchartarray) !!})
});

Related Query

More Query from same tag