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

yii框架结合charjs统计上一年与当前年数据的方法示例

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

本文实例讲述了yii框架结合charjs统计上一年与当前年数据的方法,分享给大家供大家参考,具体如下:

理论上是1年有12个月,但实际上却是去年12个月已经过了,是完整的12个月,今年的12个月还没过,不完整,所以需要补齐

  1. public static function getYearOrderCharData() { 
  2.     // 获取当前年 
  3.     $months = range(1, 12); 
  4.     $currentYear = date('Y'); 
  5.     $lastYear = date('Y'strtotime("-1 year")); 
  6.     // 所有订单 
  7.     $allOrderData = self::find() 
  8.             ->select(['FROM_UNIXTIME(create_at,"%Y-%m") as char_time''COUNT(id) as total_order''SUM(order_amount) as total_order_amount']) 
  9.             ->where(['>=''FROM_UNIXTIME(create_at,"%Y")'$lastYear]) 
  10.             ->groupBy('char_time'
  11.             ->all(); 
  12.     // 已支付订单 
  13.     $allPayOrderData = self::find() 
  14.             ->select(['FROM_UNIXTIME(create_at,"%Y-%m") as char_time''COUNT(id) as total_order''SUM(pay_amount) as total_order_amount']) 
  15.             ->where(['>=''FROM_UNIXTIME(create_at,"%Y")'$lastYear]) 
  16.             ->andWhere(['pay_status' => 2]) 
  17.             ->groupBy('char_time'
  18.             ->all(); 
  19.     $yearCountTitle   = Yii::t('backend''year_order_count_title', ['last_year' => $lastYear'current_year' => $currentYear]); 
  20.     $yearAmountTitle  = Yii::t('backend''year_order_amount_title', ['last_year' => $lastYear'current_year' => $currentYear]); 
  21.     $yearPayCountTitle = Yii::t('backend''year_order_pay_count_title', ['last_year' => $lastYear'current_year' => $currentYear]); 
  22.     $yearPayAmountTitle = Yii::t('backend''year_order_pay_amount_title', ['last_year' => $lastYear'current_year' => $currentYear]); 
  23.    
  24.     $labels = []; 
  25.     // 所有订单 
  26.     $lastYearCounts = []; // 前一年月订单总量 
  27.     $lastYearAmounts = []; // 前一年月订单总额 
  28.     $currentYearCounts = []; // 当前年月订单总量 
  29.     $currentYearAmounts = []; // 当前年月订单额 
  30.     $allOrderDataArr = []; 
  31.     foreach($allOrderData as $allKey => $allVal) { 
  32.       $allOrderDataArr[$allVal->char_time]['char_time'] = $allVal->char_time; 
  33.       $allOrderDataArr[$allVal->char_time]['total_order'] = $allVal->total_order; 
  34.       $allOrderDataArr[$allVal->char_time]['total_order_amount'] = number_format($allVal->total_order_amount / 100, 2, '.'''); 
  35.     } 
  36.    
  37.     // 已支付订单 
  38.     $lastYearPayCounts = []; // 前一年月支付订单总量 
  39.     $lastYearPayAmounts = []; // 前一年月支付订单总额 
  40.     $currentYearPayCounts = []; // 当前年月支付订单总量 
  41.     $currentYearPayAmounts = []; // 当前年月支付订单额 
  42.     $allPayOrderDataArr = []; 
  43.     foreach($allPayOrderData as $payKey => $payVal) { 
  44.       $allPayOrderDataArr[$payVal->char_time]['char_time'] = $payVal->char_time; 
  45.       $allPayOrderDataArr[$payVal->char_time]['total_order'] = $payVal->total_order; 
  46.       $allPayOrderDataArr[$payVal->char_time]['total_order_amount'] = number_format($payVal->total_order_amount / 100, 2, '.'''); 
  47.     } 
  48.    
  49.     foreach($months as $key => $val) { 
  50.       $label = $val . Yii::t('backend''month'); 
  51.       $labels[] = $label
  52.       $theMonth = strlen($val) == 2 ? $val : '0' . $val
  53.       // 上一年 
  54.       $lastYearMonth = $lastYear . '-' . $theMonth
  55.       if(array_key_exists($lastYearMonth$allOrderDataArr)) { 
  56.         $lastYearCounts[] = $allOrderDataArr[$lastYearMonth]['total_order']; 
  57.         $lastYearAmounts[] = $allOrderDataArr[$lastYearMonth]['total_order_amount']; 
  58.       } else { 
  59.         $lastYearCounts[] = '0'
  60.         $lastYearAmounts[] = '0'
  61.       } 
  62.       if(array_key_exists($lastYearMonth$allPayOrderDataArr)) { 
  63.         $lastYearPayCounts[] = $allPayOrderDataArr[$lastYearMonth]['total_order']; 
  64.         $lastYearPayAmounts[] = $allPayOrderDataArr[$lastYearMonth]['total_order_amount']; 
  65.       } else { 
  66.         $lastYearPayCounts[] = '0'
  67.         $lastYearPayAmounts[] = '0'
  68.       } 
  69.    
  70.       // 当前年 
  71.       $currentYearMonth = $currentYear . '-' . $theMonth
  72.       if(array_key_exists($currentYearMonth$allOrderDataArr)) { 
  73.         $currentYearCounts[] = $allOrderDataArr[$currentYearMonth]['total_order']; 
  74.         $currentYearAmounts[] = $allOrderDataArr[$currentYearMonth]['total_order_amount']; 
  75.       } else { 
  76.         $currentYearCounts[] = '0'
  77.         $currentYearAmounts[] = '0'
  78.       } 
  79.       if(array_key_exists($currentYearMonth$allPayOrderDataArr)) { 
  80.         $currentYearPayCounts[] = $allPayOrderDataArr[$currentYearMonth]['total_order']; 
  81.         $currentYearPayAmounts[] = $allPayOrderDataArr[$currentYearMonth]['total_order_amount']; 
  82.       } else { 
  83.         $currentYearPayCounts[] = '0'
  84.         $currentYearPayAmounts[] = '0'
  85.       } 
  86.     } 
  87.     $data = [ 
  88.       'yearCountTitle'     => $yearCountTitle
  89.       'yearAmountTitle'    => $yearAmountTitle
  90.       'yearPayCountTitle'   => $yearPayCountTitle
  91.       'yearPayAmountTitle'   => $yearPayAmountTitle
  92.       'lastYear'        => $lastYear
  93.       'currentYear'      => $currentYear
  94.       'labels'         => $labels
  95.       'lastYearCounts'     => $lastYearCounts
  96.       'lastYearAmounts'    => $lastYearAmounts
  97.       'currentYearCounts'   => $currentYearCounts
  98.       'currentYearAmounts'   => $currentYearAmounts
  99.       'lastYearPayCounts'   => $lastYearPayCounts
  100.       'lastYearPayAmounts'   => $lastYearPayAmounts
  101.       'currentYearPayCounts'  => $currentYearPayCounts
  102.       'currentYearPayAmounts' => $currentYearPayAmounts
  103.     ]; 
  104.     return $data
  105.   } 

js

  1. // 订单总量对比 
  2.   var yearOrderCountChartCanvas = $('#yearOrderCountChart').get(0).getContext('2d'
  3.   var yearOrderCountChartData = { 
  4.    labels : <?= json_encode($orderChar['labels'], true) ?>, 
  5.    datasets: [ 
  6.     { 
  7.      label        : '<?= $orderChar['lastYear'] ?>'
  8.      backgroundColor   : 'rgba(0, 192, 239, 0.5)'
  9.      data        : <?= json_encode($orderChar['lastYearCounts'], true) ?> 
  10.     }, 
  11.     { 
  12.      label        : '<?= $orderChar['currentYear'] ?>'
  13.      backgroundColor   : 'rgba(0, 135, 239, 0.5)'
  14.      data        : <?= json_encode($orderChar['currentYearCounts'], true) ?> 
  15.     } 
  16.    ] 
  17.   } 
  18.    
  19.   var yearOrderCountChartOptions = { 
  20.     scales: { 
  21.       xAxes: [{ 
  22.         gridLines: { 
  23.           display: false 
  24.         } 
  25.       }], 
  26.       yAxes: [{ 
  27.         gridLines: { 
  28.           display: false 
  29.         } 
  30.       }] 
  31.     } 
  32.   } 
  33.    
  34.   var yearOrderCountChart = new Chart(yearOrderCountChartCanvas, { 
  35.     type: 'line'
  36.     data: yearOrderCountChartData, 
  37.     options: yearOrderCountChartOptions 
  38.   }); 
  39.    
  40.   // 支付订单总量对比 
  41.   var yearOrderPayCountChartCanvas = $('#yearOrderPayCountChart').get(0).getContext('2d'
  42.   var yearOrderPayCountChartData = { 
  43.    labels : <?= json_encode($orderChar['labels'], true) ?>, 
  44.    datasets: [ 
  45.     { 
  46.      label        : '<?= $orderChar['lastYear'] ?>'
  47.      backgroundColor   : 'rgba(0, 166, 90, 0.5)'
  48.      data        : <?= json_encode($orderChar['lastYearPayCounts'], true) ?> 
  49.     }, 
  50.     { 
  51.      label        : '<?= $orderChar['currentYear'] ?>'
  52.      backgroundColor   : 'rgba(0, 166, 11, 0.5)'
  53.      data        : <?= json_encode($orderChar['currentYearPayCounts'], true) ?> 
  54.     } 
  55.    ] 
  56.   } 
  57.    
  58.   var yearOrderPayCountChartOptions = { 
  59.     scales: { 
  60.       xAxes: [{ 
  61.         gridLines: { 
  62.           display: false 
  63.         } 
  64.       }], 
  65.       yAxes: [{ 
  66.         gridLines: { 
  67.           display: false 
  68.         } 
  69.       }] 
  70.     } 
  71.   } 
  72.    
  73.   var yearOrderPayCountChart = new Chart(yearOrderPayCountChartCanvas, { 
  74.     type: 'line'
  75.     data: yearOrderPayCountChartData, 
  76.     options: yearOrderPayCountChartOptions 
  77.   }); 
  78.    
  79.   // 订单总额对比 
  80.   var yearOrderAmountChartCanvas = $('#yearOrderAmountChart').get(0).getContext('2d'
  81.   var yearOrderAmountChartData = { 
  82.    labels : <?= json_encode($orderChar['labels'], true) ?>, 
  83.    datasets: [ 
  84.     { 
  85.      label        : '<?= $orderChar['lastYear'] ?>'
  86.      backgroundColor   : 'rgba(0, 192, 239, 0.5)'
  87.      data        : <?= json_encode($orderChar['lastYearAmounts'], true) ?> 
  88.     }, 
  89.     { 
  90.      label        : '<?= $orderChar['currentYear'] ?>'
  91.      backgroundColor   : 'rgba(0, 135, 239, 0.5)'
  92.      data        : <?= json_encode($orderChar['currentYearAmounts'], true) ?> 
  93.     } 
  94.    ] 
  95.   } 
  96.    
  97.   var yearOrderAmountChartOptions = { 
  98.     scales: { 
  99.       xAxes: [{ 
  100.         gridLines: { 
  101.           display: false 
  102.         } 
  103.       }], 
  104.       yAxes: [{ 
  105.         gridLines: { 
  106.           display: false 
  107.         } 
  108.       }] 
  109.     } 
  110.   } 
  111.    
  112.   var yearOrderAmountChart = new Chart(yearOrderAmountChartCanvas, { 
  113.     type: 'line'
  114.     data: yearOrderAmountChartData, 
  115.     options: yearOrderAmountChartOptions 
  116.   }); 
  117.    
  118.   // 支付订单总额对比 
  119.   var yearOrderPayAmountChartCanvas = $('#yearOrderPayAmountChart').get(0).getContext('2d'
  120.   var yearOrderPayAmountChartData = { 
  121.    labels : <?= json_encode($orderChar['labels'], true) ?>, 
  122.    datasets: [ 
  123.     { 
  124.      label        : '<?= $orderChar['lastYear'] ?>'
  125.      backgroundColor   : 'rgba(0, 166, 90, 0.5)'
  126.      data        : <?= json_encode($orderChar['lastYearPayAmounts'], true) ?> 
  127.     }, 
  128.     { 
  129.      label        : '<?= $orderChar['currentYear'] ?>'
  130.      backgroundColor   : 'rgba(0, 166, 11, 0.5)'
  131.      data        : <?= json_encode($orderChar['currentYearPayAmounts'], true) ?> 
  132.     } 
  133.    ] 
  134.   } 
  135.    
  136.   var yearOrderPayAmountChartOptions = { 
  137.     scales: { 
  138.       xAxes: [{ 
  139.         gridLines: { 
  140.           display: false 
  141.         } 
  142.       }], 
  143.       yAxes: [{ 
  144.         gridLines: { 
  145.           display: false 
  146.         } 
  147.       }] 
  148.     } 
  149.   } 
  150.    
  151.   var yearOrderPayAmountChart = new Chart(yearOrderPayAmountChartCanvas, { 
  152.     type: 'line'
  153.     data: yearOrderPayAmountChartData, 
  154.     options: yearOrderPayAmountChartOptions 
  155.   }); 

记住,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统计上一年 charjs

分享到: