-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.html
149 lines (111 loc) · 3.04 KB
/
graph.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
<!DOCTYPE html>
<meta charset="utf-8">
<link href="stylesheets/d3.css" rel="stylesheet" type="text/css">
<link href="stylesheets/graph.css" rel="stylesheet" type="text/css">
<body>
<div id="chart">
<svg></svg>
</div>
<span id="column">
</span>
<style>
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: black;
stroke: black;
stroke-width: 1;
shape-rendering: crispEdges;
}
div {
margin-bottom: 20px;
}
</style>
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="nvtooltip.js"></script>
<script src="d3legend.js"></script>
<script src="d3line.js"></script>
<script src="d3linewithlegend.js"></script>
<script>
function log(text) {
if (console && console.log) console.log(text);
return text;
}
$(document).ready(function() {
var margin = {top: 30, right: 10, bottom: 50, left: 60},
chart = d3LineWithLegend()
.xAxis.label('Workout week')
.width(500)
.height(300)
.yAxis.label('Time (s)');
chart.xAxis.tickFormat(function(d, i){
return (Math.floor(d) == d) ? "Week " + (d + 1) : ""
})
var svg = d3.select('#chart svg')
.datum(generateData())
svg.transition().duration(500)
.attr('width', 500)
.attr('height', 300)
.call(chart);
chart.dispatch.on('showTooltip', function(e) {
var offset = $('#chart').offset(), // { left: 0, top: 0 }
left = e.pos[0] + offset.left,
top = e.pos[1] + offset.top,
formatter = d3.format(".04f");
var content = '<h3>' + e.series.label + '</h3>' +
'<p>' +
'<span class="value">Week ' + (e.point[0]+1) + ': ' + formatter(e.point[1]) + '</span>' +
'</p>';
nvtooltip.show([left, top], content);
$('#column').html(content);
});
chart.dispatch.on('hideTooltip', function(e) {
nvtooltip.cleanup();
});
$(window).resize(function() {
var margin = chart.margin();
chart
.width(width(margin))
.height(height(margin));
d3.select('#chart svg')
.attr('width', 500)
.attr('height', 300)
.call(chart);
});
function width(margin) {
var w = $(window).width() - 20;
return ( (w - margin.left - margin.right - 20) < 0 ) ? margin.left + margin.right + 2 : w;
}
function height(margin) {
var h = $(window).height() - 20;
return ( h - margin.top - margin.bottom - 20 < 0 ) ?
margin.top + margin.bottom + 2 : h;
}
//data
function generateData() {
usain = [10.03, 9.69, 9.58, 9.82, 9.76, 9.63, 9.77, 9.98];
usain_data = [];
tyson = [9.84, 9.84, 9.77, 9.69, 9.78, 9.79, 9.80, 9.93];
tyson_data = [];
for (var i = 0; i < 8; i++) {
usain_data.push([i, usain[i]])
tyson_data.push([i, tyson[i]])
}
return [
{
data: usain_data,
label: "Usain Bolt"
},
{
data: tyson_data,
label: "Tyson Gay"
},
];
}
});
</script>