本文实例讲述了yii框架结合charjs统计上一年与当前年数据的方法。分享给大家供大家参考,具体如下:
理论上是1年有12个月,但实际上却是去年12个月已经过了,是完整的12个月,今年的12个月还没过,不完整,所以需要补齐
public static function getYearOrderCharData() { // 获取当前年 $months = range(1, 12); $currentYear = date('Y'); $lastYear = date('Y', strtotime("-1 year")); // 所有订单 $allOrderData = self::find() ->select(['FROM_UNIXTIME(create_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(order_amount) as total_order_amount']) ->where(['>=', 'FROM_UNIXTIME(create_at,"%Y")', $lastYear]) ->groupBy('char_time') ->all(); // 已支付订单 $allPayOrderData = self::find() ->select(['FROM_UNIXTIME(create_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['>=', 'FROM_UNIXTIME(create_at,"%Y")', $lastYear]) ->andWhere(['pay_status' => 2]) ->groupBy('char_time') ->all(); $yearCountTitle = Yii::t('backend', 'year_order_count_title', ['last_year' => $lastYear, 'current_year' => $currentYear]); $yearAmountTitle = Yii::t('backend', 'year_order_amount_title', ['last_year' => $lastYear, 'current_year' => $currentYear]); $yearPayCountTitle = Yii::t('backend', 'year_order_pay_count_title', ['last_year' => $lastYear, 'current_year' => $currentYear]); $yearPayAmountTitle = Yii::t('backend', 'year_order_pay_amount_title', ['last_year' => $lastYear, 'current_year' => $currentYear]); $labels = []; // 所有订单 $lastYearCounts = []; // 前一年月订单总量 $lastYearAmounts = []; // 前一年月订单总额 $currentYearCounts = []; // 当前年月订单总量 $currentYearAmounts = []; // 当前年月订单额 $allOrderDataArr = []; foreach($allOrderData as $allKey => $allVal) { $allOrderDataArr[$allVal->char_time]['char_time'] = $allVal->char_time; $allOrderDataArr[$allVal->char_time]['total_order'] = $allVal->total_order; $allOrderDataArr[$allVal->char_time]['total_order_amount'] = number_format($allVal->total_order_amount / 100, 2, '.', ''); } // 已支付订单 $lastYearPayCounts = []; // 前一年月支付订单总量 $lastYearPayAmounts = []; // 前一年月支付订单总额 $currentYearPayCounts = []; // 当前年月支付订单总量 $currentYearPayAmounts = []; // 当前年月支付订单额 $allPayOrderDataArr = []; foreach($allPayOrderData as $payKey => $payVal) { $allPayOrderDataArr[$payVal->char_time]['char_time'] = $payVal->char_time; $allPayOrderDataArr[$payVal->char_time]['total_order'] = $payVal->total_order; $allPayOrderDataArr[$payVal->char_time]['total_order_amount'] = number_format($payVal->total_order_amount / 100, 2, '.', ''); } foreach($months as $key => $val) { $label = $val . Yii::t('backend', 'month'); $labels[] = $label; $theMonth = strlen($val) == 2 ? $val : '0' . $val; // 上一年 $lastYearMonth = $lastYear . '-' . $theMonth; if(array_key_exists($lastYearMonth, $allOrderDataArr)) { $lastYearCounts[] = $allOrderDataArr[$lastYearMonth]['total_order']; $lastYearAmounts[] = $allOrderDataArr[$lastYearMonth]['total_order_amount']; } else { $lastYearCounts[] = '0'; $lastYearAmounts[] = '0'; } if(array_key_exists($lastYearMonth, $allPayOrderDataArr)) { $lastYearPayCounts[] = $allPayOrderDataArr[$lastYearMonth]['total_order']; $lastYearPayAmounts[] = $allPayOrderDataArr[$lastYearMonth]['total_order_amount']; } else { $lastYearPayCounts[] = '0'; $lastYearPayAmounts[] = '0'; } // 当前年 $currentYearMonth = $currentYear . '-' . $theMonth; if(array_key_exists($currentYearMonth, $allOrderDataArr)) { $currentYearCounts[] = $allOrderDataArr[$currentYearMonth]['total_order']; $currentYearAmounts[] = $allOrderDataArr[$currentYearMonth]['total_order_amount']; } else { $currentYearCounts[] = '0'; $currentYearAmounts[] = '0'; } if(array_key_exists($currentYearMonth, $allPayOrderDataArr)) { $currentYearPayCounts[] = $allPayOrderDataArr[$currentYearMonth]['total_order']; $currentYearPayAmounts[] = $allPayOrderDataArr[$currentYearMonth]['total_order_amount']; } else { $currentYearPayCounts[] = '0'; $currentYearPayAmounts[] = '0'; } } $data = [ 'yearCountTitle' => $yearCountTitle, 'yearAmountTitle' => $yearAmountTitle, 'yearPayCountTitle' => $yearPayCountTitle, 'yearPayAmountTitle' => $yearPayAmountTitle, 'lastYear' => $lastYear, 'currentYear' => $currentYear, 'labels' => $labels, 'lastYearCounts' => $lastYearCounts, 'lastYearAmounts' => $lastYearAmounts, 'currentYearCounts' => $currentYearCounts, 'currentYearAmounts' => $currentYearAmounts, 'lastYearPayCounts' => $lastYearPayCounts, 'lastYearPayAmounts' => $lastYearPayAmounts, 'currentYearPayCounts' => $currentYearPayCounts, 'currentYearPayAmounts' => $currentYearPayAmounts, ]; return $data; }
内容版权声明:除非注明,否则皆为本站原创文章。