本文實(shí)例講述了yii框架結(jié)合charjs實(shí)現(xiàn)統(tǒng)計(jì)30天數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
理論上30天數(shù)據(jù)應(yīng)該都有,但實(shí)際上卻不一定是,所以需要補(bǔ)全
public static function getDayOrderCharData($days = 30) {
$nowDay = date('Y-m-d', strtotime('-1day')); // 當(dāng)前前一天
$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 = []; // 訂單數(shù)量
$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
// 按天獲取訂單數(shù)量
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; // 按時(shí)間統(tǒng)計(jì)
public $total_order; // 所有訂單
public $total_order_amount; // 所有訂單總額
public $total_pay_order; // 支付訂單
public $total_pay_amount; // 支付訂單總額
public $total_order_pay_amount; // 支付總額
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Spring-boot結(jié)合Shrio實(shí)現(xiàn)JWT的方法
- JAVA集合框架Map特性及實(shí)例解析
- TP5框架實(shí)現(xiàn)一次選擇多張圖片并預(yù)覽的方法示例
- yii框架結(jié)合charjs統(tǒng)計(jì)上一年與當(dāng)前年數(shù)據(jù)的方法示例
- thinkphp5 框架結(jié)合plupload實(shí)現(xiàn)圖片批量上傳功能示例
- TP5框架實(shí)現(xiàn)上傳多張圖片的方法分析
- Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解