Amalmax
283
2
Laravel

not displaying google charts in Laravel app

Posted 9 months ago by Amalmax

Hi, I am working with laravel 5.6 and I need displaying categoryname column values in google charts of vehicles table. My blade file is as following,


    @extends('layouts.app')

    @section('content')
    <title>pie chart</title>
    <script src="{{ url('/js/jquery.js') }}"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    

    <script type="text/javascript">
        var analytics = <?php echo $categoryname;  ?>
        google.charts.load('current', {'packages':['corechart']});

        google.charts.setOnLoadCallback(drawChart);

        function drawChart()
        {
            var data = google.visulization.arrayToDataTable(analytics);

            var options = {
                title : 'Presentage of Categories'
            };

            var chart = new google.visulization.PieChart(document.getElementById('pie_chart'));
            chart.draw(data,options);
        }
    </script>
</head>
<body>
<div class="container">
    <h3 align="center">Category Chart</h3>
    <br>

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-heading">Percentage of Categoryies</h3>
        </div>
        <div class="panel-body">
            <div id="pie_chart" style="width:750px; height: 450px;">
                 
            </div>
        </div>
    </div>
</div>
    

</body>

@endsection

and My ChartController is as

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class ChartController extends Controller
{
    function index()
    {
        $data = DB::table('vehicles')
              ->select(
                DB::raw('categoryname as categoryname'),
                DB::raw('count(*)as number'))
              ->groupBy('categoryname')
              ->get();
        $array[] = ['Category','Number'];
        foreach($data as $key => $value)
        {
            $array[++$key] = [$value->categoryname, $value->number];
        }
        return view('reports.pie_chart')->with('categoryname', json_encode($array));
    }
}

when I visit blade file I can see Only blade file and not displaying Google chart. How can fix this problem?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.