服务器之家

服务器之家 > 正文

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

时间:2020-06-02 12:38     来源/作者:tang05709

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
  }

js

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

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

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

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/tang05709/article/details/102885014

标签:

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部