-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
161 lines (136 loc) · 4.65 KB
/
main.js
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
/**
* Stravasicht
*
* A simple (but configurable) widget to display Strava data.
*
* Getting Started:
* 1. You will need a Strava API access token to allow the widget
* to pull data from the Strava API.
* 1.1 Simply go to: https://www.strava.com/settings/api
* 1.2 Create an application and insert the access token below
*
* 2. You should be good to go! Feel free to muck around with configuration :)
*
* Author: Dragan Marjanovic - marjanovic.io
*/
// User configuration and Global Variables
// Set your access token here
accessToken: "",
color: 'FFFFFF', // The hex color code for the desired graph color
// Options: distance, elapsed_time, max_speed
metric: "distance", // The metric to track and display
dataFreq: (1000 * 60 * 15), // Data refresh interval (Strava API)
refreshFrequency: 5000,
pinged: 0, // Time of the last data update
data: null, // The data from the last update
httpGet: function(url) {
var xmlHttp = new XMLHttpRequest(); // Create the request object
xmlHttp.open("GET", url, false); // Make the request
xmlHttp.send(null);
return xmlHttp.responseText; //Return the response
},
getData: function () {
var baseURL = "https://www.strava.com/api/v3/athlete/activities";
var requestURL = baseURL + "?access_token=" + this.accessToken;
return this.httpGet(requestURL);
},
getDayCount: function(now) {
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
},
command: function (callback) {
var now = new Date();
// Has been more than 15 minutes since last fetch
if (now - this.pinged >= this.dataFreq ) {
this.data = this.getData();
this.pinged = now; // Update the last request date
}
// Feed the data back to the renderer for processing
callback(0, this.data);
},
render: function (output) {
// Initialise the Chart library and canvas container
content = "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js\"></script>"
content += '<canvas id="myChart"></canvas>'
return content;
},
afterRender: function(domEl) {
var ctx = document.getElementById('myChart');
activityData = JSON.parse(this.getData());
var trackedMetricData = [];
var labelData = [];
var day = this.getDayCount(new Date());
for (var i = 0; i < day; i++) {
labelData.push("");
trackedMetricData.push(0.001);
}
for (var i=0; i < activityData.length; i++) {
var activity = activityData[i];
var dayBin;
// activityDate = new Date(activity.start_date_local);
activityDate = new Date(activity.start_date);
if (activityDate.getFullYear() >= new Date().getFullYear()) {
var sdLocal = activity.start_date_local;
var date = sdLocal.substring(0, sdLocal.indexOf("T"));
dayBin = this.getDayCount(new Date(date)) - 1;
switch (this.metric) {
case "elapsed_time":
trackedMetricData[dayBin] += activity.elapsed_time/60;
break;
case "max_speed": // The max speed for that day
maxKph = activity.max_speed * 60 * 60 / 1000;
if (maxKph > trackedMetricData[dayBin]) {
trackedMetricData[dayBin] = maxKph;
}
case "distance":
default: // Default should be distance
trackedMetricData[dayBin] += activity.distance/1000;
break;
}
}
}
var dataz = {
labels: labelData,
datasets: [{
data: trackedMetricData,
backgroundColor: this.color
}]
}
config = {
responsive: true,
maintainAspectRatio: false,
barThickness: 5,
scales: {
xAxes: [{ display: false }],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
fontColor: this.color,
fontSize: 11,
fontStyle: "bold"
},
gridLines: {
color: "rgba(0, 0, 0, 0)",
display: false
}
}]
},
legend: { display: false }
}
var myChart = new Chart(ctx, {
type: 'bar',
data: dataz,
options: config
});
// Increase the refresh frequency now that everything is ready
this.refreshFrequency = this.dataFreq;
},
style: " \n\
bottom: .25% \n\
left: 0.25% \n\
height: 200px \n\
width: 99% \n\
"