时间:2021-05-25
本文实例讲述了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 : <?= json_encode($dayOrderChar['labels'], true) ?>, datasets: [ { label : '<?= $dayOrderChar['orderCountLabel'] ?>', backgroundColor : 'rgba(0, 192, 293, 0.5)', data : <?= json_encode($dayOrderChar['orderCounts'], true) ?> } ] } var dayOrderCountChartOptions = { scales: { xAxes: [{ gridLines: { display: false } }], yAxes: [{ gridLines: { display: false } }] } } var dayOrderCountChart = new Chart(dayOrderCountChartCanvas, { type: 'line', data: dayOrderCountChartData, options: dayOrderCountChartOptions }); // 按天获取订单及金额 var dayOrderAmounCanvas = $('#dayOrderAmountChart').get(0).getContext('2d') var dayOrderAmounData = { labels : <?= json_encode($dayOrderChar['labels'], true) ?>, datasets: [ { label : '<?= $dayOrderChar['orderAmountLabel'] ?>', backgroundColor : 'rgba(0, 192, 293, 0.5)', data : <?= json_encode($dayOrderChar['orderAmounts'], true) ?> }, { label : '<?= $dayOrderChar['orderPayAmountLabel'] ?>', backgroundColor : 'rgba(0, 166, 90, 0.5)', data : <?= json_encode($dayOrderChar['orderPayAmounts'], true) ?> } ] } var dayOrderAmounOptions = { scales: { xAxes: [{ gridLines: { display: false } }], yAxes: [{ gridLines: { display: false } }] } } var dayOrderAmountChart = new Chart(dayOrderAmounCanvas, { type: 'line', data: dayOrderAmounData, options: dayOrderAmounOptions });记住,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相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了yii框架结合charjs统计上一年与当前年数据的方法。分享给大家供大家参考,具体如下:理论上是1年有12个月,但实际上却是去年12个月已经过了,
本文实例讲述了yii框架实现按天,月,年,自定义时间段统计数据的方法。分享给大家供大家参考,具体如下:天(day):格式Y-m-d月(month):格式Y-m年
本文实例讲述了Yii实现多数据库主从读写分离的方法。分享给大家供大家参考。具体分析如下:Yii框架数据库多数据库、主从、读写分离实现,功能描述:1.实现主从数据
本文实例讲述了Yii框架结合sphinx,Ajax实现搜索分页功能的方法。分享给大家供大家参考,具体如下:效果图:控制器:?123456789101112131
本文实例讲述了Yii框架使用魔术方法实现跨文件调用。分享给大家供大家参考,具体如下:目前项目用yii框架,controller调用facade的方法,facad