-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheatmap.html
154 lines (135 loc) · 3.57 KB
/
heatmap.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
<!doctype html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<meta charset="utf-8">
<title>ESPN Cricinfo - Match Centre</title>
<link rel="stylesheet" href="./css/styles.css">
</head>
<style type="text/css" media="screen">
html{
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}
body{
background: #f0f7ff;
}
.espn-gfx{
float:left;
clear:both;
padding-bottom:10px;
}
.tooltip{
position: relative;
display: none;
}
.mcr-chart-key text{
cursor: help;
}
</style>
<body>
<div id="espn-heatmap" class="espn-gfx"></div>
<div id="espn-heatmap2" class="espn-gfx"></div>
<div id="espn-heatmap3" class="espn-gfx"></div>
<div class="tooltip"></div>
<script src="./bower_components/d3/d3.js"></script>
<script src="./bower_components/d3.chart/d3.chart.js"></script>
<script src="./js/espn.gfx.random.js"></script>
<script src="./js/espn.gfx.heatmap.js"></script>
<script>
function tooltip(event, data, svgElem, bbox){
// Crude example of tooltip
// TODO: proper tooltip positioning by getting positions for svgElem, use jQuery for cross browser support
var pos = getNodePos(svgElem);
d3.select('.tooltip').attr({style:"display:block;left:" + (pos.x + bbox.x) + "px;top:" + (pos.y + bbox.y) + "px;"}).html(data.runs + ' Run/s');
}
function closeTip(){
d3.select('.tooltip').style('display','none');
}
function getNodePos(el){
var body = d3.select('body').node();
for (var lx = 0, ly = 0;
el != null && el != body;
lx += (el.offsetLeft || el.clientLeft), ly += (el.offsetTop || el.clientTop), el = (el.offsetParent || el.parentNode));
return {x: lx, y: ly};
}
var interval = 4000;
setInterval(function(){
heatmap.draw(espn.gfx.random.heatmap());
heatmap2.draw(espn.gfx.random.heatmap());
},interval);
var heatmap = d3.select("#espn-heatmap")
.chart("heatmap",{
width : 100,
height : 125,
legends : true,
mapKey : 'runs',
onMouseover : tooltip,
onMouseout : closeTip,
showValues : true,
onBaseClick : function(){
console.log('clicked');
},
onKeyMouseover : function(event,data){
console.log(event,data,'over');
},
onKeyMouseout : function(event,data){
console.log(event,data,'out');
}
});
heatmap.draw(espn.gfx.random.heatmap());
var heatmap2 = d3.select("#espn-heatmap2")
.chart("heatmap",{
width : 140,
height : 190,
legends : true,
mapKey : 'runs',
showValues : true,
textHandler : function(d){
if(d.wickets) return d.wickets + 'w';
}
});
heatmap2.draw(espn.gfx.random.heatmap());
var pitchMap = [[0,0,0],[0,2,0],[0,3,0],[1,0,0],[0,0,0]];
function getPitchMap(pitchMap){
var zone = 1,
arr = [],
rows;
for (var i =0, ilen = pitchMap.length; i < ilen; i++){
rows = pitchMap[i];
for (var j = 0, jlen = rows.length; j < jlen; j++){
arr.push({
runs : rows[j],
zone : zone
});
zone++
}
}
return arr;
}
function getPitchGrid(pitchMap){
return {
x : (pitchMap && pitchMap[0] && pitchMap[0].length || 5),
y : (pitchMap && pitchMap.length || 5)
};
}
var grid = getPitchGrid(pitchMap);
var heatmap3 = d3.select("#espn-heatmap3")
.chart("heatmap",{
width : 140,
height : 190,
legends : true,
showValues : true,
xGridLength : grid.x,
yGridLength : grid.y,
mapKey : 'runs',
keyLegends: [
['O', 'S', 'L'],
['Y', 'F', 'L', 'S', 'B']
],
flipLegends : 1,
onMouseover : tooltip,
onMouseout : closeTip
});
heatmap3.draw(getPitchMap(pitchMap));
</script>
</body>