-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.html
224 lines (174 loc) · 6.88 KB
/
index1.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v2.0.0.min.js"></script>
<style>
circle.dimple-series-0{
opacity: .4;
}
h2{
text-align: center;
}
</style>
<script type="text/javascript">
function draw(data) {
// add chart title
d3.select('body')
.append('h2')
.text("Baseball Player Height vs Home Runs");
// create SVG
var svg = dimple.newSvg("body", 1400, 800);
// create indicator chart on the right of the main chart
var indicator = new dimple.chart(svg,data);
//debugger;
var defaultColor = indicator.defaultColors[0]
var indicatorColor = indicator.defaultColors[2];
var frame = 3000;
var firstTick = true;
//place the indicator bar chart to the right
indicator.setBounds(934,69,153,311);
//Add handedness along the y axis
var y = indicator.addCategoryAxis('y', "handedness")
y.addOrderRule(['B', 'L', 'R']);
// use handedness scale for bar size
var x = indicator.addMeasureAxis('x', "name");
x.hidden = true;
// Add the bars to the indicator and add event handlers
var s = indicator.addSeries(null, dimple.plot.bar);
s.addEventHandler("click", onClick);
// function to calculate average Home Runs by batter handedness
function calc_HR_avg(data,filter_col,filter_val){
// filter data by handedness column and value
var filtered_data = dimple.filterData(data, filter_col,filter_val);
// extract values from HR column and convert to integers
var HomeRuns = []
filtered_data.map(function(d) {
temp = +d.HR
HomeRuns.push(temp);
});
// calculate average
var total = 0;
for (var i = 0; i< HomeRuns.length;i++){
total = total + HomeRuns[i];
}
debugger;
var avg = total/HomeRuns.length
return avg;
}
var left_HR_avg = calc_HR_avg(data,'handedness','L');
var right_HR_avg = calc_HR_avg(data,'handedness','R');
var both_HR_avg = calc_HR_avg(data,'handedness','B');
//debugger;
indicator.draw();
// Remove the title from the y axis
y.titleShape.remove();
// Remove the lines from the y axis
y.shapes.selectAll("line,path").remove();
// Move the y axis text inside the plot area
y.shapes.selectAll("text")
.style("text-anchor", "start")
.style("font-size", "11px")
.attr("transform", "translate(18, 0.5)");
// Add instuctions to start/stop animation
svg.selectAll("title_text")
.data(["Bars below represent player batting handedness",
"Click bar to select",
"and pause. Click again",
"to resume animation"])
.enter()
.append("text")
.attr("x", 935)
.attr("y", function (d, i) { return 15 + i * 12; })
.style("font-family", "sans-serif")
.style("font-size", "10px")
.style("color", "Black")
.text(function (d) { return d; });
// Color the indicator bars according to the current frame.
s.shapes.attr("rx", 10)
.attr("ry", 10)
.style("fill", function (d) { return (d.y === 'B' ? indicatorColor.fill : defaultColor.fill) })
.style("stroke", function (d) { return (d.y === 'B' ? indicatorColor.stroke : defaultColor.stroke) })
.style("opacity", 0.4);
//debugger;
/*
Dimple.js Chart construction code
*/
var myChart = new dimple.chart(svg, data);
myChart.setBounds(60,60,835,610);
myChart.defaultColors = [
new dimple.color("dodgerblue", "dodgerblue", 1),
new dimple.color("Red",'Red', 1), //
new dimple.color("yellow", 'yellow',1)
];
// Create x and y axis and set axis limit.
var x = myChart.addMeasureAxis("x", "height");
x.overrideMin = 60;
x.overrideMax = 85;
x.title = "Player Height in Inches";
var y = myChart.addMeasureAxis("y", "HR");
y.title = "Total Home Runs (HR)";
// add data and create bubble chart. Color by handedness
myChart.addSeries(["name",'weight','avg', "handedness" ], dimple.plot.bubble);
//create legend
var myLegend = myChart.addLegend(178,40,410,60, [myChart]);
// create story for animation
var story = myChart.setStoryboard("handedness", onTick);
// set frame duration.
story.frameDuration = frame;
// render chart
myChart.draw();
// style color legned and add legend description
myLegend.shapes.selectAll("text").style("font-size", "11px");
svg.selectAll("legend_text")
.data(["Right Handed Batters(R)\n", "Left Handed Batters(L)\n", "Both Handed Batters(B)"])
.enter()
.append("text")
.attr("x", 460)
.attr("y", function (d, i) { return 11.5 + i * 12; })
.style("font-family", "sans-serif")
.style("font-size", "10px")
.style("color", "Black")
.text(function (d) { return d; });
// remove story label
story.storyLabel.remove();
// orphan legend to not respond to graph updates.
myChart.legends = [];
//click event function for side chart
function onClick(e){
story.pauseAnimation();
// check if clicked bar is the current
if (e.yValue===story.getFrameValue()){
// if yes, start animation
story.startAnimation();
} else {
// else pause animation and go to clicked bar's frame.
story.goToFrame(e.yValue);
story.pauseAnimation();
}
}
// On tick of the main charts storyboard
function onTick(e){
if(!firstTick){
//change indicator color as animation progresses over each frame.
s.shapes.transition()
.duration(frame /2)
.style("fill", function (d) { return (d.y === e ? indicatorColor.fill : defaultColor.fill) })
.style("stroke", function (d) { return (d.y === e ? indicatorColor.stroke : defaultColor.stroke) });
}
firstTick = false;
}
};
</script>
</head>
<body>
<script type="text/javascript">
/*
Using D3 to load the CSV file
and pass the contents of it to the draw function
*/
d3.csv("baseball_data.csv", draw);
</script>
</body>
</html>