1
1
<?php
2
2
include_once 'functions/connection.php ' ;
3
3
4
- $ sql = "SELECT YEAR(created_at) AS year, MONTH(created_at) AS month, SUM(total) AS total_sales
5
- FROM transactions
6
- WHERE status = 4
7
- GROUP BY YEAR(created_at), MONTH(created_at)
8
- ORDER BY YEAR(created_at), MONTH(created_at) " ;
9
-
10
- $ stmt = $ db ->prepare ($ sql );
11
- $ stmt ->execute ();
12
-
13
- $ labels = [];
14
- $ data = [];
15
- while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
4
+ function daily_chart (){
5
+ global $ db ;
6
+ $ sql = "SELECT DATE(created_at) AS date, SUM(total) AS total_sales
7
+ FROM transactions
8
+ WHERE status = 4
9
+ GROUP BY DATE(created_at)
10
+ ORDER BY DATE(created_at) " ;
11
+
12
+ $ stmt = $ db ->prepare ($ sql );
13
+ $ stmt ->execute ();
14
+
15
+ $ labels = [];
16
+ $ data = [];
17
+ while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
18
+ $ date = date ("M d, Y " , strtotime ($ row ['date ' ]));
19
+ $ labels [] = $ date ;
20
+ $ data [] = $ row ['total_sales ' ];
21
+ }
22
+ $ chartData = [
23
+ 'labels ' => $ labels ,
24
+ 'datasets ' => [
25
+ [
26
+ 'label ' => 'Daily Earnings ' ,
27
+ 'fill ' => true ,
28
+ 'data ' => $ data ,
29
+ 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
30
+ 'borderColor ' => 'rgba(78, 115, 223, 1) '
31
+ ]
32
+ ]
33
+ ];
34
+
35
+ $ chartDataJson = json_encode ($ chartData );
36
+ ?>
37
+ <canvas data-bss-chart='{"type":"line","data":<?php echo $ chartDataJson ; ?> ,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas>
38
+ <?php
39
+ }
40
+
41
+
42
+ function month_chart (){
43
+ global $ db ;
44
+ $ sql = "SELECT YEAR(created_at) AS year, MONTH(created_at) AS month, SUM(total) AS total_sales
45
+ FROM transactions
46
+ WHERE status = 4
47
+ GROUP BY YEAR(created_at), MONTH(created_at)
48
+ ORDER BY YEAR(created_at), MONTH(created_at) " ;
49
+
50
+ $ stmt = $ db ->prepare ($ sql );
51
+ $ stmt ->execute ();
52
+
53
+ $ labels = [];
54
+ $ data = [];
55
+ while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
16
56
$ monthName = date ("M " , mktime (0 , 0 , 0 , $ row ['month ' ], 10 ));
17
57
$ labels [] = $ monthName . ' ' . $ row ['year ' ];
18
58
$ data [] = $ row ['total_sales ' ];
19
- }
20
- $ chartData = [
59
+ }
60
+ $ chartData = [
21
61
'labels ' => $ labels ,
22
62
'datasets ' => [
23
- [
24
- 'label ' => 'Earnings ' ,
25
- 'fill ' => true ,
26
- 'data ' => $ data ,
27
- 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
28
- 'borderColor ' => 'rgba(78, 115, 223, 1) '
29
- ]
63
+ [
64
+ 'label ' => 'Earnings ' ,
65
+ 'fill ' => true ,
66
+ 'data ' => $ data ,
67
+ 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
68
+ 'borderColor ' => 'rgba(78, 115, 223, 1) '
69
+ ]
30
70
]
31
- ];
71
+ ];
32
72
33
73
34
- $ chartDataJson = json_encode ($ chartData );
35
- ?>
74
+ $ chartDataJson = json_encode ($ chartData );
75
+ ?>
76
+ <canvas data-bss-chart='{"type":"line","data":<?php echo $ chartDataJson ; ?> ,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas>
77
+ <?php
78
+ }
36
79
37
- <canvas data-bss-chart='{"type":"line","data":<?php echo $ chartDataJson ; ?> ,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas>
0 commit comments