-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (157 loc) · 5.6 KB
/
index.html
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
<!--go by week, month, or year. We'll start by showing each month side-by-side. Also need to show legend-->
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
.run {
fill: steelblue;
}
.ride {
fill: lightblue;
}
</style>
<head></head>
<body>
<!-- This is where we call the chart -->
<svg class="chart"></svg>
<div id = "myDiv"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/d3-time.v1.min.js"></script>
<script>
//this is where we are defining dates
var startDate = new Date('January 1, 2017');
var endDate = new Date('December 31, 2017');
var epochStart = startDate.getTime()/1000;
var epochEnd = endDate.getTime()/1000;
function getMonthsObject(startDate, endDate){
var months = d3.timeMonth.range(d3.timeMonth.floor(startDate), d3.timeMonth.ceil(endDate));
var monthsList = [];
for(i=0; i<months.length; i++){
monthsList.push({month:months[i], runTotal:0, rideTotal:0})
}
return monthsList
}
function getDateOfLastActivity(activities) {
var lastActivityDate = activities[activities.length - 1];
var dateInMilliseconds = new Date(lastActivityDate.start_date).getTime()/1000;
return dateInMilliseconds;
}
function getURL(lastActivityDate) {
var access_token = getQueryString("access_token");
var URL = "https://www.strava.com/api/v3/athlete/activities?access_token="+access_token+"&after="+lastActivityDate;
return URL
}
//this is where we make the access token happen through the URL locally
var getQueryString = function (field, url) {
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
};
//this is where we call the data
function loadXMLDoc() {
var runs = [];
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
// document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
var firstMonthRuns = JSON.parse(xmlhttp.responseText);
runs = runs.concat(runs, firstMonthRuns);
var lastActivityDate = getDateOfLastActivity(firstMonthRuns);
var newURL = getURL(lastActivityDate);
var newXML = new XMLHttpRequest();
newXML.onreadystatechange = function() {
if (newXML.readyState == XMLHttpRequest.DONE) {
if(newXML.status == 200) {
newRuns = JSON.parse(newXML.responseText);
runs = runs.concat(runs, newRuns);
debugger
getRuns(runs);
}
}
}
newXML.open("GET", newURL, true);
newXML.send();
}
else if (xmlhttp.status == 400) {
alert('yo dog there was a problem');
}
else {
alert('something else other than 200 was returned what does that even mean');
}
}
};
var access_token = getQueryString("access_token");
var page_size = getQueryString("page_size")
var URL = "https://www.strava.com/api/v3/athlete/activities?access_token="+access_token+"&after="+epochStart;
xmlhttp.open("GET", URL, true);
xmlhttp.send();
}
var rawRunData = loadXMLDoc();
//this is where we loop through the data
function getRuns(activities){
var monthsObject = getMonthsObject(startDate, endDate);
for(var i=0; i<activities.length; i++){
for(var j=0; j<monthsObject.length; j++){
//we want an if statement here to separate by month, as defined in the timeMonth above
var activityDate = new Date(activities[i].start_date);
if(activityDate > monthsObject[j].month && activityDate < monthsObject[j+1].month){
//converting from meters to miles, rounding to 2 decimal places
var distance = parseFloat((activities[i].distance*0.0006213712).toFixed(2));
if(activities[i].type === "Run"){
monthsObject[j].runTotal+=distance;
}
if(activities[i].type === "Ride"){
monthsObject[j].rideTotal+=distance;
}
}
}
}
//this is the chart
var data = monthsObject
var width = 420;
var spaceBetweenMonths = 2;
margin = {top: 10, bottom: 10},
barHeight = 20;
var x = d3.scale.linear()
.domain([0, 100])
.range([0, 400]);
//setting the height and width of the chart
var chart = d3.select(".chart")
.attr("width", width)
.attr("height", barHeight * data.length * 5)
//g is where the bar chart will be drawn
var bar = chart.selectAll("g")
.data(data)
.enter().append("g")
//translate to leave room for the top and left margins. spaceBetweenMonths leaves padding for the alternating bars
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + spaceBetweenMonths +")"; });
bar.append("rect")
.attr("class", "run")
.attr("width", function(d){return x(d.runTotal);
})
.attr("height", barHeight - 1);
bar.append("rect")
.attr("class", "ride")
.attr("y", 20)
.attr("width", function(d){return x(d.rideTotal);
})
.attr("height", barHeight - 1);
bar.append("text")
.attr("x", function(d) {return x(d.runTotal) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d.runTotal; });
bar.append("text")
.attr("x", function(d) {return x(d.rideTotal) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d.rideTotal; });
}
</script>
</body>