本文实例讲述了yii框架结合charjs实现统计30天数据的方法。分享给大家供大家参考,具体如下:
理论上30天数据应该都有,但实际上却不一定是,所以需要补全
public static function getDayOrderCharData($days = 30) { $nowDay = date('Y-m-d', strtotime('-1day')); // 当前前一天 $lastDay = date("Y-m-d", strtotime('-'.$days.'day')); // days天前 $daysFormat = []; // 获取到days段的日期 for($i = $days; $i > 0; $i--) { $daysFormat[] = date("Y-m-d", strtotime('-'.$i.'day')); } // 所有用户 $allOrderData = self::find() ->select(['FROM_UNIXTIME(create_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(order_amount) as total_order_amount', 'SUM(pay_amount) as total_order_pay_amount']) ->where(['>=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $lastDay]) ->andWhere(['<=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $nowDay]) ->groupBy('char_time') ->all(); $dayCountTitle = Yii::t('backend', 'day_order_count_title', ['last_day' => $lastDay, 'now_day' => $nowDay]); $dayAmountTitle = Yii::t('backend', 'day_order_amount_title', ['last_day' => $lastDay, 'now_day' => $nowDay]); $labels = $daysFormat; // 所有用户 $orderCounts = []; // 订单数量 $orderAmounts = []; // 订单金额 $orderPayAmounts = []; // 支付金额 $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'] = $allVal->total_order_amount; $allOrderDataArr[$allVal->char_time]['total_order_pay_amount'] = $allVal->total_order_pay_amount; } foreach($daysFormat as $key => $val) { if(array_key_exists($val, $allOrderDataArr)) { $orderCounts[] = $allOrderDataArr[$val]['total_order']; $orderAmounts[] = $allOrderDataArr[$val]['total_order_amount']; $orderPayAmounts[] = $allOrderDataArr[$val]['total_order_pay_amount']; } else { $orderCounts[] = '0'; $orderAmounts[] = '0'; $orderPayAmounts[] = '0'; } } $data = [ 'dayCountTitle' => $dayCountTitle, 'dayAmountTitle' => $dayAmountTitle, 'orderCountLabel' => Yii::t('backend', 'day_order_count_label', ['days' => $days]), 'orderAmountLabel' => Yii::t('backend', 'day_order_amount_label', ['days' => $days]), 'orderPayAmountLabel' => Yii::t('backend', 'day_order_pay_amount_label', ['days' => $days]), 'nowDay' => $nowDay, 'lastDay' => $lastDay, 'labels' => $labels, 'orderCounts' => $orderCounts, 'orderAmounts' => $orderAmounts, 'orderPayAmounts' => $orderPayAmounts ]; return $data; }
js
// 按天获取订单数量 var dayOrderCountChartCanvas = $('#dayOrderCountChart').get(0).getContext('2d') var dayOrderCountChartData = { labels : <"htmlcode">public $char_time; // 按时间统计 public $total_order; // 所有订单 public $total_order_amount; // 所有订单总额 public $total_pay_order; // 支付订单 public $total_pay_amount; // 支付订单总额 public $total_order_pay_amount; // 支付总额更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]