-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
141 lines (118 loc) · 4.2 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
<!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;
}
.legend_text {
text-align: right;
fill: black;
}
</style>
<script type="text/javascript">
function draw(data) {
/*
D3.js setup code
*/
// set margin,width,height
"use strict";
var margin = 75,
width = 1500 - margin,
height = 850 - margin;
// add chart title
d3.select('body')
.append('h2')
.text("Baseball Player Height vs Home Runs");
// create SVG
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart');
/*
Dimple.js Chart construction code
*/
var myChart = new dimple.chart(svg, data);
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(1260, 75, 150, 1100, "Left");
// render chart
myChart.draw();
// style legned.
myLegend.shapes.selectAll("text").style("font-size", "11px");
// orphan legend to not respond to graph updates.
myChart.legends = [];
// add legend title and instructions how interact with the legend.
svg.selectAll("title_text")
.data(["Click legend to hide/unhide:\n","Right Handed Batters(R)\n", "Left Handed Batters(L)\n", "Both handed Batters(B)"])
.enter()
.append("text")
.attr('class', 'legend_text')
.attr("x", 1270)
.attr("y", function (d, i) { return 25 + i * 14; })
.style("font-family", "sans-serif")
.style("font-size", "11px")
.text(function (d) { return d; });
// create filter list that will be used to update the graph.
var filterValues = dimple.getUniqueValues(data,"handedness");
myLegend.shapes.selectAll("rect")
// create a click event to each lengend square
.on("click", function (e) {
var hide = false;
var newFilters = [];
// check if the clicked square is in the filter and flag it to be hidden if it is.
filterValues.forEach(function (f) {
if (f === e.aggField[3]) {
hide = true;
} else {
newFilters.push(f);
}
});
// hide square if it is flagged in the filter list. If a square is not flagged, it will be shown.
if (hide) {
d3.select(this).style("opacity", 0.2);
} else {
newFilters.push(e.aggField[3]);
d3.select(this).style("opacity", 0.8);
}
// update filter list
filterValues = newFilters;
// update chart data with the filtered data
myChart.data = dimple.filterData(data, "handedness", filterValues);
// re-draw chart.
myChart.draw(800);
});
};
</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>