yii框架结合charjs实现统计30天数据的方法

本文实例讲述了yii框架结合charjs实现统计30天数据的方法。分享给大家供大家参考,具体如下:

理论上30天数据应该都有,但实际上却不一定是,所以需要补全

public static function getDayOrderCharData($days = 30) {
    $nowDay = date('Y-m-d', strtotime('-1day')); // 当前前一天
    $lastDay = date("Y-m-d", strtotime('-'.$days.'day')); // days天前
    $daysFormat = [];
    // 获取到days段的日期
    for($i = $days; $i > 0; $i--) {
      $daysFormat[] = date("Y-m-d", strtotime('-'.$i.'day'));
    }
 
    // 所有用户
    $allOrderData = self::find()
            ->select(['FROM_UNIXTIME(create_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(order_amount) as total_order_amount', 'SUM(pay_amount) as total_order_pay_amount'])
            ->where(['>=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $lastDay])
            ->andWhere(['<=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $nowDay])
            ->groupBy('char_time')
            ->all();
    $dayCountTitle   = Yii::t('backend', 'day_order_count_title', ['last_day' => $lastDay, 'now_day' => $nowDay]);
    $dayAmountTitle  = Yii::t('backend', 'day_order_amount_title', ['last_day' => $lastDay, 'now_day' => $nowDay]);
 
    $labels = $daysFormat;
    // 所有用户
    $orderCounts = []; // 订单数量
    $orderAmounts = []; // 订单金额
    $orderPayAmounts = []; // 支付金额
    $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'] = $allVal->total_order_amount;
      $allOrderDataArr[$allVal->char_time]['total_order_pay_amount'] = $allVal->total_order_pay_amount;
    }
 
    foreach($daysFormat as $key => $val) {
      if(array_key_exists($val, $allOrderDataArr)) {
        $orderCounts[] = $allOrderDataArr[$val]['total_order'];
        $orderAmounts[] = $allOrderDataArr[$val]['total_order_amount'];
        $orderPayAmounts[] = $allOrderDataArr[$val]['total_order_pay_amount'];
      } else {
        $orderCounts[] = '0';
        $orderAmounts[] = '0';
        $orderPayAmounts[] = '0';
      }
    }
    $data = [
      'dayCountTitle'    => $dayCountTitle, 
      'dayAmountTitle'    => $dayAmountTitle,
      'orderCountLabel'   => Yii::t('backend', 'day_order_count_label', ['days' => $days]),
      'orderAmountLabel'   => Yii::t('backend', 'day_order_amount_label', ['days' => $days]),
      'orderPayAmountLabel' => Yii::t('backend', 'day_order_pay_amount_label', ['days' => $days]),
      'nowDay'        => $nowDay,
      'lastDay'       => $lastDay,
      'labels'        => $labels,
      'orderCounts'     => $orderCounts,
      'orderAmounts'     => $orderAmounts,
      'orderPayAmounts'   => $orderPayAmounts
    ];
    return $data;
  }


      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1848.html