本文实例讲述了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 = lf::find() ->lect(['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 = lf::find() ->lect(['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 = []; // 前一年月支付订单总量 $las过年七天乐2014全集tyearpayamounts = []; // 前一年月支付订单总额 $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']; } el { $lastyearcounts[] = '0'; $lastyearamounts[] = '0'; } if(array_key_exists($lastyearmonth, $allpayorderdataarr)) { $lastyearpaycounts[] = $allpayorderdataarr[$lastyearmonth]['total_order']; $lastyearpayamounts[] = $allpayorderdataarr[$lastyearmonth]['total_order_amount']; } el { $lastyearpaycounts[] = '0'; $lastyearpayamounts[] = '0'; } // 当前年 $currentyearmonth = $currentyear . '-' . $themonth; if(array_key_exists($currentyearmonth, $allorderdataarr)) { $currentyearcounts[] = $allorderdataarr[$currentyearmonth]['total_order']; $currentyearamounts[] = $allorderdataarr[$currentyearmonth]['total_order_amount']; } el { $currentyearcounts[] = '0'; $currentyearamounts[] = '0'; } if(array_key_exists($currentyearmonth, $allpayorderdataarr)) { $currentyearpaycounts[] = $allpayorderdataarr[$currentyearmonth]['total_order']; $currentyearpayamounts[] = $allpayorderdataarr[$currentyearmonth]['total_order_amount']; } el { $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 yea兰湖rordercountchartdata = { labels : <?= json_encode($orderchar['labels'], true) ?>, datats: [ { 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: fal } }], yaxes: [{ gridlines: { display: fal } }] } } 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) ?>, datats: [ { label : '<?= $orderchar['lastyear'] ?>', backgroundcolor : 'rgba(0, 166, 90, 0.5)', data : <?= json_encode($orderchar['lastyearpaycounts'], true) ?> }, { label : '<?= $orderchar['cur徐怀雯rentyear'] ?>', backgroundcolor : 'rgba(0, 166, 11, 0.5)', data : <?= json_encode($orderchar['currentyearpaycounts'], true) ?> } ] } var yearorderpaycountchartoptions = { scales: { xaxes: [{ gridlines: { display: fal } }], yaxes: [{ gridlines: { display: fal } }] } } 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) ?>, datats: [ { 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: fal } }], yaxes: [{ gridlines: { display: fal } }] } } 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) ?>, datats: [ { 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: fal } }], yaxes: [{ gridlines: { display: fal } }] } } 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; // 支付总额
本文发布于:2023-04-08 16:41:03,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/68f8298836d2e4b58ee712979a48d688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:yii框架结合charjs统计上一年与当前年数据的方法示例.doc
本文 PDF 下载地址:yii框架结合charjs统计上一年与当前年数据的方法示例.pdf
留言与评论(共有 0 条评论) |