get(); //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0 while (strtotime($start) <= strtotime($end)) { $ret[$start] = [ 'x_data' => $start, 'y_field' => $xField, 'y_data' => array_fill(0, count($xField), 0), ]; $startTime = date('Y-m-d', strtotime("+1 day", strtotime($start))); } //遍历查询结果,统计每个日期的社心小程序浏览量 foreach ($result as $item) { $mpCount++; $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++; } } elseif ($dateType == 1) { $endTime = date('Y-m-d'); $startTime = date('Y-m-d', strtotime("-6 days", strtotime($endTime))); //查询指定时间段内的浏览记录 $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get(); //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0 while (strtotime($startTime) <= strtotime($endTime)) { $ret[$startTime] = [ 'x_data' => $startTime, 'y_field' => $xField, 'y_data' => array_fill(0, count($xField), 0), ]; $startTime = date('Y-m-d', strtotime("+1 day", strtotime($startTime))); } //遍历查询结果,统计每个日期的社心小程序浏览量 foreach ($result as $item) { $mpCount++; $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++; } } elseif ($dateType == 2){ //当前周+往前6周 $week = 0; while($week <7) { // 获取当前日期所在周的周日 $endTime = date('Y-m-d', strtotime('sunday -'.$week.' week', strtotime($end))); // 获取六个自然周前的周一 $startTime = date('Y-m-d', strtotime('monday -'.($week+1).' week', strtotime($end))); $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get(); $ret[] = [ 'x_data' => $startTime.'~'.$endTime, 'y_field' => $xField, 'y_data' => [$result->count(), 0], ]; $mpCount+= $result->count(); $week++; } }elseif ($dateType == 3){ //当前周+往前6周 $month = 0; while($month <6) { $endTime = date('Y-m-t', strtotime(' -'.$month.' month', strtotime($end))); $startTime = date('Y-m-1', strtotime(' -'.$month.' month', strtotime($end))); $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get(); $ret[] = [ 'x_data' => date('Y-m', strtotime($startTime)), 'y_field' => $xField, 'y_data' => [$result->count(), 0], ]; $mpCount+= $result->count(); $month++; } } //返回数据包括:总数据、计数数据和按日期分组的数据 return [ 'total' => [['label' => '社心小程序', 'value' => User::count()], ['label' => '新媒体', 'value' => NewMediaAccount::sum('fans_num')]], 'count' => [['label' => '社心小程序', 'value' => $mpCount], ['label' => '新媒体', 'value' => 0]], 'data' => array_values($ret), ]; } }