Fix chart using wrong dataset

This commit is contained in:
兔姬桑
2021-08-03 23:30:40 +08:00
parent aaa5482dea
commit bd2034f07d
3 changed files with 49 additions and 18 deletions

View File

@@ -10,13 +10,13 @@ class ReportController extends Controller
public function accounting()
{
$orders = Order::where('status', '>=', 2)->whereHas('goods')->latest()->get(['created_at', 'amount']);
$ordersByDay = $orders->groupBy(function ($item) {
$ordersByDay = $orders->where('created_at', '>=', date('Y-m-01', strtotime('-1 months')))->groupBy(function ($item) {
return $item->created_at->format('Y-m-d');
})->map(function ($row) {
return $row->sum('amount');
})->toArray();
$ordersByMonth = $orders->groupBy(function ($item) {
$ordersByMonth = $orders->where('created_at', '>=', date('Y-01-01', strtotime('-1 years')))->groupBy(function ($item) {
return $item->created_at->format('Y-m');
})->map(function ($row) {
return $row->sum('amount');
@@ -36,9 +36,11 @@ class ReportController extends Controller
for ($i = 1; $i <= $currentDays; $i++) {
$data['currentMonth'][] = $ordersByDay[date(sprintf('Y-m-%02u', $i))] ?? 0;
}
for ($i = 1; $i <= $lastDays; $i++) {
$data['lastMonth'][] = $ordersByDay[date(sprintf('Y-m-%02u', $i), strtotime('-1 months'))] ?? 0;
}
for ($i = 1; $i <= date('m'); $i++) {
$data['currentYear'][] = $ordersByMonth[date(sprintf('Y-%02u', $i))] ?? 0;
}

View File

@@ -26,7 +26,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
if ($this->app->isLocal()) {
if ($this->app->isLocal() && \config('debug')) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);

View File

@@ -1,6 +1,6 @@
@extends('admin.layouts')
@section('content')
<div class="page-content container" id="widgetLinearea">
<div class="page-content container">
<div class="card card-shadow">
<div class="card-block p-30">
<div class="row pb-20">
@@ -59,20 +59,35 @@
};
}
const common_options = {
plugins: {
function common_options(label) {
return {
responsive: true,
legend: {
labels: {
padding: 20,
usePointStyle: true,
pointStyle: 'circle',
font: {size: 14},
scales: {
x: {
grid: {
display: false,
},
},
y: {
grid: {
display: false,
},
min: 0,
},
},
tooltip: label_callbacks(' 月'),
},
};
plugins: {
legend: {
labels: {
padding: 20,
usePointStyle: true,
pointStyle: 'circle',
font: {size: 14},
},
},
tooltip: label_callbacks(label),
},
};
}
function area_a(label, data) {
return {
@@ -106,9 +121,9 @@
type: 'line',
data: {
labels: @json($data['days']),
datasets: [area_a(' 本 月 ',@json($data['currentYear'])), area_b(' 上 月 ',@json($data['lastMonth']))],
datasets: [area_a(' 本 月 ',@json($data['currentMonth'])), area_b(' 上 月 ',@json($data['lastMonth']))],
},
options: common_options,
options: common_options(' 日'),
});
new Chart(document.getElementById('months'), {
@@ -117,7 +132,7 @@
labels: @json($data['years']),
datasets: [area_a(' 今 年 ',@json($data['currentYear'])), area_b(' 去 年 ',@json($data['lastYear']))],
},
options: common_options,
options: common_options(' 月'),
});
new Chart(document.getElementById('years'), {
@@ -134,6 +149,20 @@
}],
},
options: {
responsive: true,
scales: {
x: {
grid: {
display: false,
},
},
y: {
grid: {
display: false,
},
min: 0,
},
},
plugins: {
legend: false,
tooltip: label_callbacks(' 年'),