每月或每周如何分离数据

时间:2019-06-25 06:56:09

标签: php laravel charts

我正在Laravel中创建一个图表。它显示年度数据。我也想使用选择按钮显示每周,每月。如何分离数据?

查看文件

    <script>

        var chartData = new Array();
        @foreach($monthlyVisits as $key => $val)
            chartData.push('<?php echo $val; ?>');
        @endforeach

        var barData = {
            labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
            datasets: [{
                label: "Total Number of Visits",
                backgroundColor: 'rgba(26,179,148,0.5)',
                borderColor: "rgba(26,179,148,0.7)",
                pointBackgroundColor: "rgba(26,179,148,1)",
                pointBorderColor: "#fff",
                 // data: [ 150, 48, 40, 19, 86, 27, 90, 100, 75, 50, 60, 90]
                data: chartData
            }]
        };
        var barOptions = {
            responsive: true
        };
        var ctx2 = document.getElementById("barChart").getContext("2d");
        new Chart(ctx2, {
            type: 'bar',
            data: barData,
            options: barOptions
        });

        var polarData = {
            datasets: [{
                data: [
                    300, 140, 200
                ],
                backgroundColor: [
                    "#a3e1d4", "#dedede", "#b5b8cf"
                ],
                label: [
                    "My Radar chart"
                ]
            }],
            labels: [
                "App", "Software", "Laptop"
            ]
        };
    </script>

控制器文件

    public function index()
    {

        // get monthly visits for chart
        $monthlyVisits = $this->getMonthlyVisits();
        $visits        = Shortener::all()->sum( 'total_visits' );
        $leads         = Lead::count();
        $domains       = Domain::count();


        return view( 'user.dashboard.index', compact( 'domains', 'leads', 'visits', 'monthlyVisits' ) );
    }

    /**
     * GEt the monthly visits of whole year to display on chart
     *
     * @return array
     */
    public function getMonthlyVisits()
    {
        $janVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '01' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $febVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '02' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $marVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '03' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $aprVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '04' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $mayVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '05' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $junVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '06' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $julVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '07' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $augVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '08' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $sepVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '09' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $octVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '10' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $novVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '11' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();
        $decVisits     = DB::table( "shorteners" )->whereMonth( 'created_at', '=', '12' )->select( DB::raw( "SUM(total_visits) as total_visits" ) )->get();

        $monthlyVisits = [
            $janVisits[ 0 ]->total_visits,
            $febVisits[ 0 ]->total_visits,
            $marVisits[ 0 ]->total_visits,
            $aprVisits[ 0 ]->total_visits,
            $mayVisits[ 0 ]->total_visits,
            $junVisits[ 0 ]->total_visits,
            $julVisits[ 0 ]->total_visits,
            $augVisits[ 0 ]->total_visits,
            $sepVisits[ 0 ]->total_visits,
            $octVisits[ 0 ]->total_visits,
            $novVisits[ 0 ]->total_visits,
            $decVisits[ 0 ]->total_visits,
        ];

        return $monthlyVisits;
    }
    }

1 个答案:

答案 0 :(得分:0)

对于每月访问,您的查询应类似于:

    public function getMonthlyVisits()
    {
        $monthlyVisitors = DB::table('shorteners')
            ->select([
                DB::raw('MONTH(created_at) as month'),
                DB::raw('count(total_visits as total_visits'),
            ])
            ->groupBy('month')
            ->get();
    }

对于年度访问:

    public function getYearlyVisits()
    {
        $monthlyVisitors = DB::table('shorteners')
            ->select([
                DB::raw('YEAR(created_at) as year'),
                DB::raw('count(total_visits as total_visits'),
            ])
            ->groupBy('year')
            ->get();
    }

每周访问:

    public function getWeeklyVisits()
    {
        $monthlyVisitors = DB::table('shorteners')
            ->select([
                DB::raw('WEEK(created_at) as week'),
                DB::raw('count(total_visits as total_visits'),
            ])
            ->groupBy('week')
            ->get();
    }