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


Posted in PHP onApril 04, 2020

本文实例讲述了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;
  }

js

// 订单总量对比
  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一定要在模型利定义公共变量

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程序设计有所帮助。

PHP 相关文章推荐
Cannot modify header information错误解决方法
Oct 08 PHP
PHP 配置文件中open_basedir选项作用
Jul 19 PHP
PHP学习笔记 (1) 环境配置与代码调试
Jun 19 PHP
php 带逗号千位符数字的处理方法
Jan 10 PHP
php无限分类且支持输出树状图的详细介绍
Jun 19 PHP
yii操作session实例简介
Jul 31 PHP
PHP实现动态柱状图改进版
Mar 30 PHP
php.ini中date.timezone设置详解
Nov 20 PHP
php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
Nov 28 PHP
PHP 中使用explode()函数切割字符串为数组的示例
May 06 PHP
PHP实现新型冠状病毒疫情实时图的实例
Feb 04 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
Feb 21 PHP
yii框架结合charjs实现统计30天数据的方法
Apr 04 #PHP
yii 框架实现按天,月,年,自定义时间段统计数据的方法分析
Apr 04 #PHP
thinkphp5 框架结合plupload实现图片批量上传功能示例
Apr 04 #PHP
phpQuery采集网页实现代码实例
Apr 02 #PHP
通过PHP的Wrapper无缝迁移原有项目到新服务的实现方法
Apr 02 #PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 #PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 #PHP
You might like
PHP中把错误日志保存在系统日志中(Windows系统)
2015/06/23 PHP
PHP基于PDO扩展操作mysql数据库示例
2018/12/24 PHP
面向对象的Javascript之三(封装和信息隐藏)
2012/01/27 Javascript
jquery判断浏览器类型的代码
2012/11/05 Javascript
javascript实现des解密加密全过程
2014/04/03 Javascript
JS+CSS实现带关闭按钮DIV弹出窗口的方法
2015/02/27 Javascript
JavaScript中实现map功能代码分享
2015/06/11 Javascript
基于jquery css3实现点击动画弹出表单源码特效
2015/08/31 Javascript
javascript定义类和类的实现实例详解
2015/12/01 Javascript
JavaScript设计模式经典之命令模式
2016/02/24 Javascript
vue.js指令v-for使用及索引获取
2016/11/03 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
ReactNative之键盘Keyboard的弹出与消失示例
2017/07/11 Javascript
前端图片懒加载(lazyload)的实现方法(提高用户体验)
2017/08/21 Javascript
Angular 2 利用Router事件和Title实现动态页面标题的方法
2017/08/23 Javascript
JS实现去除数组中重复json的方法示例
2017/12/21 Javascript
JavaScript调用模式与this关键字绑定的关系
2018/04/21 Javascript
小程序图片剪裁加旋转的示例代码
2018/07/10 Javascript
在Webpack中用url-loader处理图片和字体的问题
2020/04/28 Javascript
js实现滑动滑块验证登录
2020/07/24 Javascript
在vue中实现给每个页面顶部设置title
2020/07/29 Javascript
python:socket传输大文件示例
2017/01/18 Python
python 时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果代码详解
2018/04/19 Python
使用python接入微信聊天机器人
2020/03/31 Python
PyCharm安装Markdown插件的两种方法
2019/06/24 Python
python excel转换csv代码实例
2019/08/26 Python
python中的函数递归和迭代原理解析
2019/11/14 Python
美国名牌太阳镜折扣网站:Eyedictive
2017/05/15 全球购物
BCBG官网:BCBGMAXAZRIA
2017/12/29 全球购物
大二学习计划书范文
2014/04/27 职场文书
2014年预备党员学习新党章思想汇报
2014/09/15 职场文书
初二学生评语大全
2014/12/26 职场文书
党员廉洁自律个人总结
2015/02/13 职场文书
婚宴致辞
2015/07/28 职场文书
上级领导检查欢迎词
2015/09/30 职场文书
Java基于字符界面的简易收银台
2021/06/26 Java/Android