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 相关文章推荐
用户的详细注册和判断
Oct 09 PHP
如何给phpcms v9增加类似于phpcms 2008中的关键词表
Jul 01 PHP
php以post形式发送xml的方法
Nov 04 PHP
php实现兼容2038年后Unix时间戳转换函数
Mar 18 PHP
PHP文件生成的图片无法使用CDN缓存的解决方法
Jun 20 PHP
基于ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据
Sep 23 PHP
PHP微信开发之根据用户回复关键词\位置返回附近信息
Jun 24 PHP
thinkphp隐藏index.php/home并允许访问其他模块的实现方法
Oct 13 PHP
PHP实现websocket通信的方法示例
Aug 28 PHP
tp5.1 实现setInc字段自动加1
Oct 18 PHP
php中yar框架实例用法讲解
Dec 27 PHP
php解析非标准json、非规范json的方式实例
May 10 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 判断常量,变量和函数是否存在
2009/04/26 PHP
一个PHP验证码类代码分享(已封装成类)
2011/07/17 PHP
php把数组值转换成键的方法
2015/07/13 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
2020/05/15 PHP
Jquery从头学起第四讲 jquery入门教程
2010/08/01 Javascript
js,jQuery 排序的实现代码,网页标签排序的实现,标签排序
2011/04/27 Javascript
Javascript 倒计时源代码.(时.分.秒) 详细注释版
2011/05/09 Javascript
js判断输入是否为数字的具体实例
2013/08/03 Javascript
JavaScript二维数组实现的省市联动菜单
2014/05/08 Javascript
D3.js实现折线图的方法详解
2016/09/21 Javascript
AngularJS学习笔记(三)数据双向绑定的简单实例
2016/11/08 Javascript
javascript表单正则应用
2017/02/04 Javascript
Electron vue的使用教程图文详解
2019/07/05 Javascript
微信小程序接入腾讯云验证码的方法步骤
2020/01/07 Javascript
手把手教你如何编译打包video.js
2020/12/09 Javascript
[01:06]DOTA2小知识课堂 Ep.01 TP出门不要忘记帮队友灌瓶哦
2019/12/05 DOTA
python 爬虫出现403禁止访问错误详解
2017/03/11 Python
Django中login_required装饰器的深入介绍
2017/11/24 Python
利用python将图片转换成excel文档格式
2017/12/30 Python
使用Python和Scribus创建一个RGB立方体的方法
2019/07/17 Python
python处理自动化任务之同时批量修改word里面的内容的方法
2019/08/23 Python
django在保存图像的同时压缩图像示例代码详解
2020/02/11 Python
python实现俄罗斯方块小游戏
2020/04/24 Python
python对输出的奇数偶数排序实例代码
2020/12/04 Python
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
2020/12/07 Python
Asics日本官网:鬼冢八喜郎创立的跑鞋运动品牌
2017/10/18 全球购物
Electrolux伊莱克斯巴西商店:家用电器、小家电和配件
2018/05/23 全球购物
DELPHI中如何调用API,可举例说明
2014/01/16 面试题
优秀实习自我鉴定
2013/12/04 职场文书
精彩自我鉴定
2014/01/16 职场文书
自我鉴定四大框架
2014/01/17 职场文书
家长对孩子的评语
2014/04/18 职场文书
党员个人自我剖析材料
2014/10/08 职场文书
2014年教育工作总结
2014/11/26 职场文书
廉洁自律个人总结
2015/02/14 职场文书
干货:如何写好工作计划!
2019/05/17 职场文书