This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart-day-month.php
178 lines (149 loc) · 5.85 KB
/
chart-day-month.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* chart_presences_day();
* Create a chart with number of presences by day
*/
function chart_presences_day() {
$date_from = date('Y-m-d', strtotime("-28 days"));
$date_to = date('Y-m-d', strtotime("-1 day"));
$data_day = file_get_contents('https://stats.coworking-metz.fr/stats/day?from=' . $date_from . '&to=' . $date_to);
$json_day = json_decode($data_day, true);
foreach ($json_day as $key => $value) {
$date = $value['date'];
$date_transform = DateTime::createFromFormat('Y-m-d', $date)->format('D d/m');
$date_fr = strftime($date_transform);
$search = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$replace = array('Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche');
$date_in_french = str_replace($search, $replace, $date_fr);
$day = date('D', $date_timestamp);
$coworkers = $value['data']['coworkersCount'];
$array_day [] = $date_in_french;
$array_count[] = $coworkers;
}
echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.js"></script>';
echo '<script src="https://www.coworking-metz.fr/charts/chartjs-plugin-datalabels.js"></script>';
echo '<canvas id="myChartDay"></canvas>';
?>
<script>
var ctx = document.getElementById('myChartDay');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: <?php echo json_encode($array_day); ?>,
datasets: [{
label: '',
data: <?php echo json_encode($array_count); ?>,
backgroundColor: 'rgba(224, 171, 78, 0.5)',
borderColor: 'rgb(224, 171, 78)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
gridLines: {
color: 'rgba(255,255,255,0.25)'
},
ticks: {
beginAtZero: true
}
}]
},
aspectRatio: 5,
legend: false,
animation: {
onComplete: function() {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
ctx.fillStyle = '#eab234';
this.data.datasets.forEach(function(dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function(bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y - 5);
});
});
}
}
}
});
Chart.defaults.global.defaultFontColor = "#fff";
</script>
<?php
}
/**
* chart_presences_month();
* Create a chart with number of presences by month
*/
function chart_presences_month() {
$date_from = date('Y-m-d', strtotime("-735 days"));
$date_to = date('Y-m-d', strtotime("-1 day"));
$data_month = file_get_contents('https://stats.coworking-metz.fr/stats/month?from=' . $date_from . '&includesCurrent=1');
$json_month = json_decode($data_month , true);
foreach ($json_month as $key => $value) {
$date = $value['date'];
$date_transform = DateTime::createFromFormat('Y-m-d', $date)->format('M Y');
$date_fr = strftime($date_transform);
$search = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$replace = array('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
$date_in_french = str_replace($search, $replace, $date_fr);
$day = date('D', $date_timestamp);
$coworkers = $value['data']['coworkedDaysCount'];
$array_day [] = $date_in_french;
$array_count[] = $coworkers;
}
echo '<canvas id="myChartMonth"></canvas>';
?>
<script>
var ctx = document.getElementById('myChartMonth');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: <?php echo json_encode($array_day); ?>,
datasets: [{
label: '',
data: <?php echo json_encode($array_count); ?>,
backgroundColor: 'rgba(224, 171, 78, 0.5)',
borderColor: 'rgb(224, 171, 78)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
gridLines: {
color: 'rgba(255,255,255,0.25)'
},
ticks: {
beginAtZero: true
}
}]
},
aspectRatio: 5,
legend: false,
animation: {
onComplete: function() {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
ctx.fillStyle = '#eab234';
this.data.datasets.forEach(function(dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function(bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y - 5);
});
});
}
}
}
});
Chart.defaults.global.defaultFontColor = "#fff";
</script>
<?php
}