当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-24 08:20:53 浏览: 评论:0 

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

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

  1. public static function getDayOrderCharData($days = 30) { 
  2.     $nowDay = date('Y-m-d'strtotime('-1day')); // 当前前一天 
  3.     $lastDay = date("Y-m-d"strtotime('-'.$days.'day')); // days天前 
  4.     $daysFormat = []; 
  5.     // 获取到days段的日期 
  6.     for($i = $days$i > 0; $i--) { 
  7.       $daysFormat[] = date("Y-m-d"strtotime('-'.$i.'day')); 
  8.     } 
  9.    
  10.     // 所有用户 
  11.     $allOrderData = self::find() 
  12.             ->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']) 
  13.             ->where(['>=''FROM_UNIXTIME(create_at,"%Y-%m-%d")'$lastDay]) 
  14.             ->andWhere(['<=''FROM_UNIXTIME(create_at,"%Y-%m-%d")'$nowDay]) 
  15.             ->groupBy('char_time'
  16.             ->all(); 
  17.     $dayCountTitle   = Yii::t('backend''day_order_count_title', ['last_day' => $lastDay'now_day' => $nowDay]); 
  18.     $dayAmountTitle  = Yii::t('backend''day_order_amount_title', ['last_day' => $lastDay'now_day' => $nowDay]); 
  19.    
  20.     $labels = $daysFormat
  21.     // 所有用户 
  22.     $orderCounts = []; // 订单数量 
  23.     $orderAmounts = []; // 订单金额 
  24.     $orderPayAmounts = []; // 支付金额 
  25.     $allOrderDataArr = []; 
  26.     foreach($allOrderData as $allKey => $allVal) { 
  27.       $allOrderDataArr[$allVal->char_time]['char_time'] = $allVal->char_time; 
  28.       $allOrderDataArr[$allVal->char_time]['total_order'] = $allVal->total_order; 
  29.       $allOrderDataArr[$allVal->char_time]['total_order_amount'] = $allVal->total_order_amount; 
  30.       $allOrderDataArr[$allVal->char_time]['total_order_pay_amount'] = $allVal->total_order_pay_amount; 
  31.     } 
  32.    
  33.     foreach($daysFormat as $key => $val) { 
  34.       if(array_key_exists($val$allOrderDataArr)) { 
  35.         $orderCounts[] = $allOrderDataArr[$val]['total_order']; 
  36.         $orderAmounts[] = $allOrderDataArr[$val]['total_order_amount']; 
  37.         $orderPayAmounts[] = $allOrderDataArr[$val]['total_order_pay_amount']; 
  38.       } else { 
  39.         $orderCounts[] = '0'
  40.         $orderAmounts[] = '0'
  41.         $orderPayAmounts[] = '0'
  42.       } 
  43.     } 
  44.     $data = [ 
  45.       'dayCountTitle'    => $dayCountTitle,  
  46.       'dayAmountTitle'    => $dayAmountTitle
  47.       'orderCountLabel'   => Yii::t('backend''day_order_count_label', ['days' => $days]), 
  48.       'orderAmountLabel'   => Yii::t('backend''day_order_amount_label', ['days' => $days]), 
  49.       'orderPayAmountLabel' => Yii::t('backend''day_order_pay_amount_label', ['days' => $days]), 
  50.       'nowDay'        => $nowDay
  51.       'lastDay'       => $lastDay
  52.       'labels'        => $labels
  53.       'orderCounts'     => $orderCounts
  54.       'orderAmounts'     => $orderAmounts
  55.       'orderPayAmounts'   => $orderPayAmounts 
  56.     ]; 
  57.     return $data
  58.   } 

js

  1. // 按天获取订单数量 
  2.   var dayOrderCountChartCanvas = $('#dayOrderCountChart').get(0).getContext('2d'
  3.   var dayOrderCountChartData = { 
  4.    labels : <?= json_encode($dayOrderChar['labels'], true) ?>, 
  5.    datasets: [ 
  6.     { 
  7.      label        : '<?= $dayOrderChar['orderCountLabel'] ?>'
  8.      backgroundColor   : 'rgba(0, 192, 293, 0.5)'
  9.      data        : <?= json_encode($dayOrderChar['orderCounts'], true) ?> 
  10.     } 
  11.    ] 
  12.   } 
  13.    
  14.   var dayOrderCountChartOptions = { 
  15.     scales: { 
  16.       xAxes: [{ 
  17.         gridLines: { 
  18.           display: false 
  19.         } 
  20.       }], 
  21.       yAxes: [{ 
  22.         gridLines: { 
  23.           display: false 
  24.         } 
  25.       }] 
  26.     } 
  27.   } 
  28.    
  29.   var dayOrderCountChart = new Chart(dayOrderCountChartCanvas, { 
  30.     type: 'line'
  31.     data: dayOrderCountChartData, 
  32.     options: dayOrderCountChartOptions 
  33.   }); 
  34.    
  35.   // 按天获取订单及金额 
  36.   var dayOrderAmounCanvas = $('#dayOrderAmountChart').get(0).getContext('2d'
  37.   var dayOrderAmounData = { 
  38.    labels : <?= json_encode($dayOrderChar['labels'], true) ?>, 
  39.    datasets: [ 
  40.     { 
  41.      label        : '<?= $dayOrderChar['orderAmountLabel'] ?>'
  42.      backgroundColor   : 'rgba(0, 192, 293, 0.5)'
  43.      data        : <?= json_encode($dayOrderChar['orderAmounts'], true) ?> 
  44.     }, 
  45.     { 
  46.      label        : '<?= $dayOrderChar['orderPayAmountLabel'] ?>'
  47.      backgroundColor   : 'rgba(0, 166, 90, 0.5)'
  48.      data        : <?= json_encode($dayOrderChar['orderPayAmounts'], true) ?> 
  49.     } 
  50.    ] 
  51.   } 
  52.    
  53.   var dayOrderAmounOptions = { 
  54.     scales: { 
  55.       xAxes: [{ 
  56.         gridLines: { 
  57.           display: false 
  58.         } 
  59.       }], 
  60.       yAxes: [{ 
  61.         gridLines: { 
  62.           display: false 
  63.         } 
  64.       }] 
  65.     } 
  66.   } 
  67.    
  68.   var dayOrderAmountChart = new Chart(dayOrderAmounCanvas, { 
  69.     type: 'line'
  70.     data: dayOrderAmounData, 
  71.     options: dayOrderAmounOptions 
  72.   }); 

记住,yii的as一定要在模型利定义公用变量

  1. public $char_time// 按时间统计 
  2.   public $total_order// 所有订单 
  3.   public $total_order_amount// 所有订单总额 
  4.   public $total_pay_order// 支付订单 
  5.   public $total_pay_amount// 支付订单总额 
  6.   public $total_order_pay_amount// 支付总额

Tags: yii统计30天数据 charjs

分享到: