Skip to content

Commit 1588cea

Browse files
authored
Video-43-Dashboard-Chart (basir#43)
1 parent 4423080 commit 1588cea

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

backend/routers/orderRouter.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import expressAsyncHandler from 'express-async-handler';
33
import { isAuth, isAdmin } from '../utils';
44
import Order from '../models/orderModel';
55
import User from '../models/userModel';
6+
import Product from '../models/productModel';
67

78
const orderRouter = express.Router();
89
orderRouter.get(
@@ -27,7 +28,24 @@ orderRouter.get(
2728
},
2829
},
2930
]);
30-
res.send({ users, orders });
31+
const dailyOrders = await Order.aggregate([
32+
{
33+
$group: {
34+
_id: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt' } },
35+
orders: { $sum: 1 },
36+
sales: { $sum: '$totalPrice' },
37+
},
38+
},
39+
]);
40+
const productCategories = await Product.aggregate([
41+
{
42+
$group: {
43+
_id: '$category',
44+
count: { $sum: 1 },
45+
},
46+
},
47+
]);
48+
res.send({ users, orders, dailyOrders, productCategories });
3149
})
3250
);
3351
orderRouter.get(

frontend/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
rel="stylesheet"
88
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
99
/>
10+
<link
11+
rel="stylesheet"
12+
href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css"
13+
/>
1014
<link rel="stylesheet" href="style.css" />
1115
<title>JS Amazona</title>
1216
</head>

frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"webpack-dev-server": "^3.11.0"
1717
},
1818
"dependencies": {
19-
"axios": "^0.19.2"
19+
"axios": "^0.19.2",
20+
"chartist": "^0.11.4"
2021
}
2122
}

frontend/src/srceens/DashboardScreen.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
/* eslint-disable no-new */
2+
import Chartist from 'chartist';
13
import DashboardMenu from '../components/DashboardMenu';
24
import { getSummary } from '../api';
35

46
let summary = {};
57
const DashboardScreen = {
6-
after_render: () => {},
8+
after_render: () => {
9+
new Chartist.Line(
10+
'.ct-chart-line',
11+
{
12+
labels: summary.dailyOrders.map((x) => x._id),
13+
series: [summary.dailyOrders.map((x) => x.sales)],
14+
},
15+
{
16+
showArea: true,
17+
}
18+
);
19+
new Chartist.Pie(
20+
'.ct-chart-pie',
21+
{
22+
labels: summary.productCategories.map((x) => x._id),
23+
series: summary.productCategories.map((x) => x.count),
24+
},
25+
{
26+
donut: true,
27+
donutWidth: 60,
28+
startAngle: 270,
29+
showLabel: true,
30+
donutSolid: true,
31+
}
32+
);
33+
},
734
render: async () => {
835
summary = await getSummary();
936
return `
@@ -32,6 +59,16 @@ const DashboardScreen = {
3259
<div class="summary-body">$${summary.orders[0].totalSales}</div>
3360
</li>
3461
</ul>
62+
<div class="charts">
63+
<div>
64+
<h2>Sales</h2>
65+
<div class="ct-perfect-fourth ct-chart-line"></div>
66+
</div>
67+
<div>
68+
<h2>Categories</h2>
69+
<div class="ct-perfect-fourth ct-chart-pie"></div>
70+
</div>
71+
</div>
3572
</div>
3673
</div>
3774
`;

frontend/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,10 @@ td {
449449
.summary-title.color3 {
450450
background-color: #e0e0f0;
451451
}
452+
.charts {
453+
display: flex;
454+
justify-content: space-between;
455+
}
456+
.charts > div {
457+
flex: 1;
458+
}

0 commit comments

Comments
 (0)