Implementing Charts.js in Laravel 5.1

Hi I was wondering how to get answers, can't find it. I am trying to display a chart using Charts.js ..

In my route:

Route::get('surveys/chart', 'AboutController@projectChartData');

      

In my AboutController: I used json_encode () to pass data to my view

public function projectChartData()
    {
        $devlist = json_encode(DB::table('surveys')
            ->select(DB::raw('MONTHNAME(updated_at) as month'), DB::raw("DATE_FORMAT(updated_at,'%Y-%m') as monthNum"),
                DB::raw('count(*) as projects'))
            ->groupBy('monthNum')
            ->get());

        return view('pages.chart',compact('devlist'));
    }

      

In my opinion:

<canvas id="projects-graph" width="1000" height="400"></canvas>
<script type="text/javascript">
    $(function(){
        $.getJSON("surveys/chart", function (result) {
            alert('');
            var labels = [],data=[];
            for (var i = 0; i < result.length; i++) {
                labels.push(result[i].batch);
                data.push(result[i].created_at);
            }

            var buyerData = {
                labels : labels,
                datasets : [
                    {
                        fillColor : "rgba(240, 127, 110, 0.3)",
                        strokeColor : "#f56954",
                        pointColor : "#A62121",
                        pointStrokeColor : "#741F1F",
                        data : data
                    }
                ]
            };
            var buyers = document.getElementById('projects-graph').getContext('2d');
            new Chart(buyers).Line(buyerData, {
                bezierCurve : true
            });

        });

    });
</script>

      

Problem. It doesn't display a graph. But if I just return the data without using json_encode, it gives me an array (), but charts.js needs json.

I am actually having difficulty doing charts (morris, lavacharts, highcharts, charts.js) in laravel 5 as it is not that much, due to the fact that you have the right horizontal bar. And if anyone can give me a nice package that I could use. many thanks..

+3


source to share


1 answer


You will pass your view details and in your view you just json_encode like this{!! json_encode($devlist) !!}



0


source







All Articles