-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (147 loc) · 4.96 KB
/
index.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
import d3 from 'd3'
import _ from 'lodash'
import './index.css'
var TimeLib = require('plusjs/src/time/Time')
var Radial = require('plusjs/src/layout/Radial')
var rayDraw = require('plusjs/src/svg/radial/ray')
var circleDraw = require('plusjs/src/svg/circle')
var arcDraw = require('plusjs/src/svg/radial/arc')
var radialLabel = require('plusjs/src/svg/radial/label')
var width = 800, height = 800;
var container = d3.select('#chart>svg')
.attr('width', width)
.attr('height', height)
//Data
d3.csv('data/runstat.csv', function (data) {
var start = new Date(data[0].date)
var end = new Date(_.last(data).date);
//Config
//---------------------------------------------------------------------------------
var config = {
target: container,
rotate: 0,
size: [width, height],
}
var configDistance = _.extend({}, config, {
name: 'DISTANCE',
position: function (d) { return TimeLib.daysPassed(start, new Date(d.date)) },
positionRange: [0, 365],
value: function (d) { return +d.distance },
domain: [0, 20], //km
range: [240, 500], //px
dashed: 0.9, //'10,5',
})
var configPace = _.extend({}, config, {
name: 'PACE',
position: function (d) { return TimeLib.daysPassed(start, new Date(d.date)) },
positionRange: [0, 365],
value: function (d) {
//baseRadius + (run velocity - baseLine) * factor
return d.distance / TimeLib.decimalMinutes(d.time)
},
domain: [0.142, 0.2], //km/min
range: [230, 206], //pixels from center
})
var config30MinDistance = _.extend({}, config, {
name: 'MIN_MARK_30',
position: function (d) {
return TimeLib.daysPassed(start, new Date(d.date))
},
range: [0,365],
radius: function (d) {
return 240 + d.distance/TimeLib.decimalMinutes(d.time) * 30 * 14
},
})
var config30MinDistanceCircle = _.extend({}, config30MinDistance, {
size: 3,
})
var configMonthLabel = _.extend({}, config, {
name: 'Months',
coordinateSystem: 'polar',
position: function (d) { return d.position },
range: undefined,
rotate: 6,
radius: 160,
})
var configLabel = _.extend({}, config, {
name: 'Labels',
coordinateSystem: 'polar',
range: [0,365],
position: function (d) { return d.position },
radius: function (d) { return d.radius },
})
//Prepare data
//---------------------------------------------------------------------------------
var dates = data.map(function(d) {
return d3.time.format('%d %b %Y').parse(d.date);
});
var t = d3.time.scale()
.domain(d3.extent(dates))
var ticks = t.ticks(d3.time.months, 1)
var monthLabels = [];
ticks.forEach(function (d, i) {
monthLabels.push({ position: i*2, label: d3.time.format('%b %y')(d) })
monthLabels.push({ position: i*2+1, label: ' |' })
})
var labels = [
{
position: 104,
radius: 340,
label: 'INJURY\n LVIV',
},
{
position: 231,
radius: 400,
label: 'HALFMARATHON 21.1 KM\n 01:38:35\n PARIS FRANCE',
},
]
// Create charts
//---------------------------------------------------------------------------------
rayDraw(configDistance, data)
rayDraw(configPace, data)
Radial(config30MinDistance)(data)
circleDraw(config30MinDistanceCircle, data)
////Draw months names on the circumference of specified radius according to the date
radialLabel(configMonthLabel, monthLabels)
radialLabel(configLabel, labels)
//container.append('circle')
//.attr('cx', 400)
//.attr('cy', 400)
//.attr('r', 240)
//.attr('class', 'belt')
//Load second set of data
//---------------------------------------------------------------------------------
d3.csv('data/weather.csv', function (_data) {
var weatherTimeFormatter = d3.time.format('%d/%m/%Y')
//Color settings
var tempRange = d3.extent(_data.map(function (d) { return +d.tave }))
var weatherTempPainter = d3.scale.linear()
.domain([tempRange[0], 0, tempRange[1]])
.range([d3.rgb(117, 179, 216), '#ffffff', d3.rgb(244, 153, 21)])
var configWeather = _.extend({}, config, {
name: 'Weather',
coordinateSystem: 'polar',
position: function (d) {
return TimeLib.daysPassed(start, weatherTimeFormatter.parse(d.date))
},
innerRadius: 100,
radius: 40,
color: function (d) { return weatherTempPainter(+d.tave) },
})
arcDraw(configWeather, _data)
})
interactive()
})
function interactive () {
function showLegend(d) {
document.querySelector('#legend>#distance').innerHTML = d.distance + ' km'
document.querySelector('#legend>#pace').innerHTML = (TimeLib.decimalMinutes(d.time) / d.distance).toFixed(2) + ' min/km'
document.querySelector('#legend>#date').innerHTML = d.date
}
document.querySelector('.barGroup.DISTANCE ').addEventListener('mouseover', function (e) {
showLegend(e.target.__data__)
})
document.querySelector('.barGroup.PACE').addEventListener('mouseover', function (e) {
showLegend(e.target.__data__)
})
}