最近24小时数据报告显示在Chart.Js中

时间:2019-04-01 04:44:25

标签: javascript php laravel loops chart.js

最近24小时的数据报告显示。 如果一个小时内未创建报告,则默认为零。

我的查询

 $dayReport = Report::select(DB::raw("HOUR(created_at) as hourNo,SUM(site_id) as count"))
                        ->where('site_id',$site->id)
                        ->whereDate('created_at','>=', \Carbon\Carbon::now()->subHours(24))
                        ->orderBy("created_at")
                        ->groupBy(DB::raw("hour(created_at)"))
                        ->get()
                        ->toArray();

输出如下:

  data: [0,2,4,2,2,0,0,0,0,0,0,0,0,0,0,5,6,7,0,8,0,6,10,22]

我的输出是:

  data: [2,4,2,2]

这是Javascript 0中的Chart Js数据,默认值在1小时内未报告创建。

先谢谢您...

3 个答案:

答案 0 :(得分:1)

从查询中,您将获得小时和总计。..您可以尝试以下代码

$results = array(array('hour' => 2, 'count' => 3), array('hour' => 3, 'count' => 3), array('hour' => 13, 'count' => 10));
$hours = array(1,2,3,4,5,6,7,8,9,10,11,12,13); //up to 24
$finalResult = [];
 foreach($hours as $hour){
    $hourSearch = array_search($hour, array_column($results, 'hour')); 
   if(is_numeric($hourSearch)) {
    //  array_push($finalResult, $results[$hourSearch]);
    array_push($finalResult,$results[$hourSearch]['count']);
   } else {
   //   $data['hour'] = $hour;
   //  $data['count'] = 0;
   //array_push($finalResult, $data);
    array_push($finalResult,0);
  }
}

echo json_encode($finalResult);

http://sandbox.onlinephpfunctions.com/code/d3c04e3b58b01b9f70652a3398379d867453b0ea

答案 1 :(得分:1)

我已经阅读了 @Shibon 的答案,它看起来不错

我在根据以下情况创建图表时遇到相同的情况 https://github.com/ConsoleTVs/Charts

所以我为此创建了函数

这里是仅包含值的数组,但我需要将其他值设置为零,以便创建自己的函数

 $arrayWithValues = 
                        [
                            '02' => '20',
                            '09' => '45',
                            '15' => '68',
                            '21' => '28'
                        ];

 $defaultEmptyArray = [
                        '00' => '0',
                        '01' => '0',
                        '02' => '0',
                        '03' => '0',
                        '04' => '0',
                        '05' => '0',
                        '06' => '0',
                        '07' => '0',
                        '08' => '0',
                        '09' => '0',
                        '10' => '0',
                        '11' => '0',
                        '12' => '0',
                        '13' => '0',
                        '14' => '0',
                        '15' => '0',
                        '16' => '0',
                        '17' => '0',
                        '18' => '0',
                        '19' => '0',
                        '20' => '0',
                        '21' => '0',
                        '22' => '0',
                        '23' => '0',
                    ];

现在我们只需要替换具有该值的数组,而我已经编写了自己的函数

function setUnsettedArray($actulHourWithValue = [] ,$defaultEmptyArray = [])
{
    $arraNotExists = [];

     foreach ($defaultEmptyArray as $defKey => $defValue) 
        {
            if (array_key_exists($defKey, $actulHourWithValue)) 
            {
                $arrayEXists[] = [$defKey => $actulHourWithValue[$defKey]];
            }
            elseif (!array_key_exists($defKey, $actulHourWithValue)) 
            {
                $arraNotExists[] =  [$defKey => $defValue];
            }
        }

        $newArray = array_merge($arraNotExists,$arrayEXists);


        foreach ($newArray as $newKey => $newValue) 
        {
            $keys[] = $newKey;
            foreach ($newValue as $key => $value) 
            {
                $allKesy[] = $key;
                $allValues[] = $value;
            }

        }

        $finalArray = array_combine($allKesy,$allValues);
        ksort($finalArray);

        return $finalArray;    
}

并尝试将数组作为print_r(setUnsettedArray($arrayWithValues,$defaultEmptyArray));

传递

这是小提琴

http://phpfiddle.org/main/code/sfq3-adyn

我已经编辑了setUnsettedArray函数

function setUnsettedArray(array $arrayWithValues ,array $defaultArray)
{
        $finalArray = $arrayWithValues+$defaultArray;
        ksort($finalArray);
        return $finalArray;    
}

我已经看过您的查询,但查询结果不佳

所以试试这个

$lastTwentyFour =  Report::where('site_id','=',$site->id)
                        ->whereDate('created_at', '=', Carbon::now()->subHours(24))
                        ->orderBy('created_at')
                        ->get()
                        ->groupBy(function($date) 
                                    {
                                    return Carbon::parse($date->created_at)->format('H');
                                    }
                                );    

您可以将每小时的收集数计为

foreach ($lastTwentyFour as $newGroupByKey => $newGroupByValue) 
        {
            $eachHour[] = $newGroupByKey;
            $eachHourCount[] = $newGroupByValue->count();
        }


$actulHourWithValue = array_combine($eachHour,$eachHourCount);

答案 2 :(得分:1)

我似乎不太了解您的问题,但是使用碳元素在处理日期时会很有帮助,它将使您的代码减少很多行。 `$ now = Carbon \ Carbon :: now;  $ hoursBefore = $ now-> subDays(1);

$ reports = Report :: where('site_id',$ site-> id)-> whereBetween('created_at',[$ hoursBefore,$ now])-> get()-> toArray(); `

我希望这会有所帮助。 谢谢,祝您好运,并祝您编程愉快。...:)