(Laravel) Как передать значение обратного вызова ajax success в ConsoleTv Charts? - PullRequest
0 голосов
/ 20 февраля 2019

У меня есть это возвращаемое значение от 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]);
    }
}

Все, что я хочу, - это создать диаграмму в моем html из данных, которые ajax вернул из моего контроллера.Спасибо:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...