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 相关文章推荐
PHP+DBM的同学录程序(5)
Oct 09 PHP
php中文字母数字验证码实现代码
Apr 25 PHP
discuz authcode 经典php加密解密函数解析
Jul 12 PHP
PHP中通过加号合并数组的一个简单方法分享
Jan 27 PHP
PHP中实现生成静态文件的方法缓解服务器压力
Jan 07 PHP
php中time()与$_SERVER[REQUEST_TIME]用法区别
Nov 19 PHP
PHP实现的简单异常处理类示例
May 04 PHP
一个非常实用的php文件上传类
Jul 04 PHP
PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】
Aug 11 PHP
php微信开发之图片回复功能
Jun 14 PHP
详解php命令注入攻击
Apr 06 PHP
关于laravel5.5的定时任务详解(demo)
Oct 23 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
CI(CodeIgniter)框架配置
2014/06/10 PHP
在Yii框架中使用PHP模板引擎Twig的例子
2014/06/13 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
2016/06/30 PHP
PHP用函数嵌入网站访问量计数器
2017/10/27 PHP
js 调整select 位置的函数
2008/02/21 Javascript
Javascript中的isNaN函数使用说明
2011/11/10 Javascript
jquery获取特定name所有选中的checkbox,支持IE9标准模式
2013/03/18 Javascript
jquery滚动条插件jScrollPane的使用介绍
2013/11/08 Javascript
jquery如何通过name名称获取当前name的value值
2013/12/20 Javascript
用循环或if语句从json中取数据示例
2014/08/18 Javascript
JQuery的attr 与 val区别
2016/06/12 Javascript
JS中mouseup事件丢失的原因与解决办法
2017/06/14 Javascript
node.js 发布订阅模式的实例
2017/09/10 Javascript
node app 打包工具pkg的具体使用
2019/01/17 Javascript
[01:45]2014DOTA2 TI预选赛预选赛 战前探营!
2014/05/21 DOTA
布同 统计英文单词的个数的python代码
2011/03/13 Python
python3新特性函数注释Function Annotations用法分析
2016/07/28 Python
python Celery定时任务的示例
2018/03/13 Python
Python 机器学习库 NumPy入门教程
2018/04/19 Python
Python3实现的简单验证码识别功能示例
2018/05/02 Python
Python爬虫之Selenium鼠标事件的实现
2020/12/04 Python
HTML5拖拽功能实现的拼图游戏
2018/07/31 HTML / CSS
前端使用canvas生成盲水印的加密解密的实现
2020/12/16 HTML / CSS
别名指示符是什么
2012/10/08 面试题
大专毕业生自我评价分享
2013/11/10 职场文书
住房公积金接收函
2014/01/09 职场文书
师范教师毕业鉴定
2014/01/13 职场文书
十八大闭幕感言
2014/01/22 职场文书
农民工工资发放承诺书
2014/03/31 职场文书
希特勒经典演讲稿
2014/05/19 职场文书
庆祝教师节演讲稿
2014/09/03 职场文书
优秀教育工作者事迹材料
2014/12/24 职场文书
信息技术国培研修日志
2015/11/13 职场文书
学前班教学反思
2016/02/24 职场文书
MySQL中utf8mb4排序规则示例
2021/08/02 MySQL
Python Django模型详解
2021/10/05 Python