-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbesttime-forecast-weekheat-apex.html
More file actions
338 lines (296 loc) · 10.8 KB
/
Copy pathbesttime-forecast-weekheat-apex.html
File metadata and controls
338 lines (296 loc) · 10.8 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html>
<head>
<!-- ApexCharts CSS file -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/apexcharts/3.17.1/apexcharts.min.css"
integrity="sha256-Z+b0591KmC0xEn2bVTikG6Ic6LzwqrV5x7IXZpTKI2o=" crossorigin="anonymous" />
</head>
<body>
<h1>BestTime API - Week foot traffic heatmap example</h1>
<p>A demo using the <a target="_blank" href="https://besttime.app/?utm_source=besttime&utm_campaign=examples">BestTime</a> <a target="_blank" href="https://documentation.besttime.app/?javascript#new-foot-traffic-forecast">New Foot Traffic Forecast</a> API endpoint. Vizualized using <a target="_blank" href="https://echarts.apache.org/en/index.html">eCharts</a></p>
<div>
<form>
<label for="api_key_private">Insert here your BestTime <a target="_blank" href="https://besttime.app/api/v1/api_keys_list">private API key</a></label><br>
<input type="text" id="api_key_private" placeholder="Your private API Key" value="pri_xxxx" />
<p>Never use your private API key on your own public website to avoid abuse. Only use it on this page for testing purposes. Get the API data through your back-end or use the public API key in combination with a <a href="https://documentation.besttime.app/#query-week-raw">query</a> API endpoint</p>
</form>
<br>
<form>
<label for="api_key_private">Type a venue name and address and press the Forecast button</label><br>
<input type="text" id="venue_name" placeholder="Venue Name" value="Central Park" />
<input type="text" id="venue_address" placeholder="Venue Address" value="New York City Manhattan New York County" />
<button id="btnForecast" type="submit">Foot Traffic Forecast</button>
</form>
</div>
<br>
<!-- The div where the chart will be shown. A width and heigth is required -->
<div id="footTrafficWeekHeatmap" style="width:800px; height:600px;" ></div>
<p>Check out the <a target="_blank" href="https://blog.besttime.app/tag/tutorials/?utm_source=besttime&utm_campaign=examples">BestTime tutorial section</a> for more information</p>
</body>
</html>
<!-- ApexCharts is used to generate the graph -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/apexcharts/3.8.6/apexcharts.min.js"
integrity="sha256-fPasH9WrBndkSzQggD2jDhtCOJ8MapJPWFDUjLGPNPM=" crossorigin="anonymous"></script>
<!-- Script to get the foot traffic from the BestTime API and vizualize the data using eCharts -->
<script>
// Heatmap colors and categories
var busyCategories = {
notBusy: {
min: 1,
max: 39,
name: 'Not busy',
color: '#00A100'
},
littleBusy: {
min: 40,
max: 69,
name: 'Little busy',
color: '#FFB200'
},
Busy: {
min: 70,
max: 500,
name: "Busy",
color: '#FF0000'
}
}
var sampleData = [0, 5, 10, 10, 15, 40, 30, 20, 15, 20, 30, 50, 60, 70, 90, 70, 40, 10, 0, 0, 0, 0, 0];
// Graph options
var options = {
series: [
{
name: 'Sun',
data: sampleData
},
{
name: 'Sat',
data: sampleData
},
{
name: 'Fri',
data: sampleData
},
{
name: 'Thu',
data: sampleData
},
{
name: 'Wed',
data: sampleData
},
{
name: 'Tue',
data: sampleData
},
{
name: 'Mon',
data: sampleData
}
],
chart: {
height: 350,
type: 'heatmap',
},
plotOptions: {
heatmap: {
shadeIntensity: 0.5,
//radius: 10,
useFillColorAsStroke: true,
colorScale: {
ranges: [
//{
// from: 0,
// to: 1,
// name: 'Closed',
// color: '#00A100'
// },
{
from: busyCategories.notBusy.min,
to: busyCategories.notBusy.max,
name: busyCategories.notBusy.name,
color: busyCategories.notBusy.color
},
{
from: 40,
to: 69,
name: 'Usually a little busy',
color: '#FFB200'
},
{
from: 70,
to: 100,
name: 'As busy as it gets',
color: '#FF0000'
}
]
}
}
},
dataLabels: {
enabled: false,
formatter: function (val) {
return val + "%";
}
},
xaxis: {
labels: {
rotate: 90,
rotateAlways: true,
trim: false,
hideOverlappingLabels: true,
//min: 4, //clip x-axis based on category index
//max: 10,
offsetY: 25,
},
categories: ['6AM', '7AM', '8AM', '9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM', '4PM', '5PM', '6PM',
'7PM', '8PM', '9PM', '10PM', '11PM', '12AM', '1AM', '2AM', '3AM', '4AM', '5AM'],
tickPlacement: 'between' // 'on'
},
axisTicks: {
show: true,
borderType: 'solid',
color: '#78909C',
height: 6,
offsetX: 0,
offsetY: 0
},
stroke: {
width: 1
},
title: {
text: 'Week foot traffic forecast'
},
tooltip: {
enabled: true,
x: {
show: false
},
y: {
formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
return value + "%";
}
},
fixed: {
enabled: false,
position: 'topLeft',
offsetX: 0,
offsetY: 0,
},
// x: {
// format: 'HH'
// },
},
};
chart = new ApexCharts(document.querySelector("#footTrafficWeekHeatmap"), options);
chart.render();
function getFootTrafficData() {
const params = new URLSearchParams({
'api_key_private': document.getElementById("api_key_private").value,
'venue_name': document.getElementById("venue_name").value,
'venue_address': document.getElementById("venue_address").value
});
fetch(`https://besttime.app/api/v1/forecasts?${params}`, {
method: 'POST'
}).then((response) => response.json())
.then((data) => {
console.log(data)
document.getElementById("btnForecast").disabled = false;
if (data.status == "error") {
if ('message' in data) {
console.log();
if ('api_key_private' in data) {
alert(JSON.stringify(data.message.api_key_private[0]))
} else {
alert(JSON.stringify(data.message))
console.log(data.message)
}
}
} else {
// Find longest opening hours of the week
open_ix = []
close_ix = []
data.analysis.forEach(function (item, index) {
venue_open = item.day_info.venue_open;
venue_closed = item.day_info.venue_closed;
if (venue_open != "Closed") {
open_ix.push(hour2index(venue_open));
close_ix.push(hour2index(venue_closed));
}
});
open_ix_min = Math.min(...open_ix);
close_ix_max = Math.max(...close_ix);
if (open_ix_min > close_ix_max || open_ix_min == close_ix_max ) {
// Do not slice if open hour has greater index value than close (e.g. close and opens midnight)
open_ix_min = 0;
close_ix_max = 23;
} else {
// Adjust
//open_ix_min = 0;
close_ix_max = close_ix_max;
}
chart.updateOptions({
title: {
text: 'Week foot traffic forecast for ' + data.venue_info.venue_name + ' ' + data.venue_info.venue_address
},
xaxis: {
// The hours for the X-axis are sliced to the range of the longest opening hours of the week
categories: ['6AM', '7AM', '8AM', '9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM', '4PM', '5PM', '6PM',
'7PM', '8PM', '9PM', '10PM', '11PM', '12AM', '1AM', '2AM', '3AM', '4AM', '5AM'].slice(open_ix_min, close_ix_max)
}
});
chart.updateSeries([
{
name: 'Sun',
data: data.analysis[6].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Sat',
data: data.analysis[5].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Fri',
data: data.analysis[4].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Thu',
data: data.analysis[3].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Wed',
data: data.analysis[2].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Tue',
data: data.analysis[1].day_raw.slice(open_ix_min, close_ix_max)
},
{
name: 'Mon',
data: data.analysis[0].day_raw.slice(open_ix_min, close_ix_max)
}
])
}
})
.catch(error => {
console.error;
document.getElementById("btnForecast").disabled = false;
});
}
// Load on page load
//getFootTrafficData();
// Reload the forecast data when clicking the Forecast button
document.getElementById("btnForecast").addEventListener('click', (event) => {
event.preventDefault();
document.getElementById("btnForecast").disabled = 'true';
getFootTrafficData();
});
// Convert the hour to the correct array index value
function hour2index(hour) {
if (hour >= 6 && hour <= 23) {
index = hour - 6
} else {
index = hour + 18
}
// console.log(hour, index)
return index
}
</script>