(Laravel)如何将ajax成功回调值传递到ConsoleTv图表中?

时间:2019-02-20 01:30:23

标签: html ajax laravel charts controller

我通过ajax获得了此返回值,同时我将其传递到控制台,因为我不知道如何再次将数据呈现回我的ConsoleTv图表。

这是ajax返回的图表数据的屏幕截图。同时,我将其传递到控制台只是为了测试我的ajax函数是否工作正常。

enter image description here

这是我的ajax代码:

  <script>         
     $('select').on('change', function(chart) {             
        var year = this.value;             
           $.ajax({ 
              headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, 
              url: '/resolution_time_of_calls', 
              type: 'POST', 
              data: {year:year}, 
              success: function (data) {
                 console.log("Success",data);                   
                 {{-- alert(data);  --}}
              }, 
              error: function (data) { 
                 console.log("Error");
              } 
           }); 
        });   </script>  

我的控制器:

public function resolution_time_of_calls(Request $request){         

        //Declarations              
        $arr_val = array();
        $arr_val1 = array();
        $arr_val2 = array();
        $arr_monthname = array();
        $arr_monthInt = array();        

        if($request->ajax()){

            $year = $request->year;

            //SELECTING MONTHS  
                    $quer = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt
                    ,monthname(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Month
                    ,year(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Year               
                    FROM rtdb.Tickets T
                    LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                    AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                    WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                    AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= $year
                    GROUP BY Month,MonthInt,Year Order by MonthInt asc
                ");   

                $chart = new SampleChart;                            
                $chart->title('Resolution Time of Calls');                 
                foreach($quer as $query){
                    array_push($arr_monthname, $query->Month.", ".$query->Year);  
                    array_push($arr_monthInt, $query->MonthInt);   
                }                     
                $chart->labels($arr_monthname); 

            ///////////////////// TOTAL, AVERAGE,MAXIMUM Query  ////////////////////                             
                    foreach($arr_monthInt as $month){
                        $sql = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt                     
                            ,AVG(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS AVG   
                            ,MAX(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS MAX               
                            FROM rtdb.Tickets T
                            LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                            AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                            WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                            AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= $year                    
                            GROUP BY MonthInt Order by MonthInt asc
                            ");
                    }
                    foreach($sql as $value){

                        array_push($arr_val1, $value->AVG );
                        array_push($arr_val2, $value->MAX );
                    }

                    $chart->dataset('Average Hours', 'bar', $arr_val1)->backgroundcolor('#00833E');   
                    $chart->dataset('Maximum Hours', 'bar', $arr_val2)->backgroundcolor('#AF1D2D');   
                    $chart->height(600);                   

             return response()->json(['chart' => $chart]);

}else{

    $year = (int)date("Y");             

        //SELECTING MONTHS  
        $quer = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt
                ,monthname(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Month
                ,year(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Year               
                FROM rtdb.Tickets T
                LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= $year
                GROUP BY Month,MonthInt,Year Order by MonthInt asc
            ");   

            $chart = new SampleChart;                            
            $chart->title('Resolution Time of Calls');                 
            foreach($quer as $query){
                array_push($arr_monthname, $query->Month.", ".$query->Year);  
                array_push($arr_monthInt, $query->MonthInt);   
            }                     
            $chart->labels($arr_monthname); 


        ///////////////////// TOTAL, AVERAGE,MAXIMUM Query  ////////////////////                             
                foreach($arr_monthInt as $month){
                    $sql = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt                     
                        ,AVG(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS AVG   
                        ,MAX(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS MAX               
                        FROM rtdb.Tickets T
                        LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                        AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                        WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                        AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= $year                    
                        GROUP BY MonthInt Order by MonthInt asc
                        ");
                }
                foreach($sql as $value){

                    array_push($arr_val1, $value->AVG );
                    array_push($arr_val2, $value->MAX );
                }

                $chart->dataset('Average Hours', 'bar', $arr_val1)->backgroundcolor('#00833E');   
                $chart->dataset('Maximum Hours', 'bar', $arr_val2)->backgroundcolor('#AF1D2D');   
                $chart->height(600);                   

         return view('resolution_time_of_calls',['chart'=>$chart]);
    }
}

我想要做的就是用我的控制器从ajax返回的数据在html中制作一个图表。谢谢:)

0 个答案:

没有答案