-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatterPointVsPopularity.html
More file actions
228 lines (190 loc) · 7.48 KB
/
Copy pathscatterPointVsPopularity.html
File metadata and controls
228 lines (190 loc) · 7.48 KB
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
225
226
227
228
<!DOCTYPE html>
<html>
<head>
<style>
.bar{
fill: #20c997;
}
.bar:hover{
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tooltip {
position: absolute;
width: 150px;
height: 45px;
pointer-events: none;
background-color: #e7eaed;
}
body {
font: 11px sans-serif;
}
.dot {
stroke: #000;
}
</style>
</head>
<body>
<div id = "title">
<h3>Points per Gamer VS Popularity</h3></div>
<div id="barChart"></div>
<div id="scatterPlot"></div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script type="text/javascript" src="static/js/crossfilter.min.js"></script>
<script type="text/javascript">
d3.csv("data/nba_data.csv", function(error, data) {
data.forEach(function (d,i) {
d.popularity = parseInt(d.PAGEVIEWS,10) + parseInt(d.TWITTER_FAVORITE_COUNT,10) + parseInt(d.TWITTER_RETWEET_COUNT,10);
})
var scatterChart = scatterPlot("#scatterPlot",data,100,"POINTS","popularity","Points per Gamer","Popularity");
function scatterPlot(chartContainer,data,number,dimensionVal,grpVal,xTitle,yTitle) {
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
/*
* value accessor - returns the value to encode for a given data object.
* scale - maps value to a visual display encoding, such as a pixel position.
* map function - maps from data value to display value
* axis - sets up axis
*/
var ndx = crossfilter(data);
dim = ndx.dimension(function (d) {
return JSON.stringify ( { xAxisValue: +d[dimensionVal] , player: d.PLAYER} ) ;
//return +d[dimensionVal];
});
var group = dim.group().reduceSum(function (d) {
return +d[grpVal]
});
group.all().forEach(function (d) {
d.key = JSON.parse(d.key);
})
fakeGroup = getTops(group,number);
function getTops(source_group,number) {
return {
all: function () {
return source_group.top(number);
}
};
}
// setup x
var xValue = function (d) {
return d.key.xAxisValue;
}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function (d) {
return xScale(xValue(d));
}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function (d) {
return d.value;
}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function (d) {
return yScale(yValue(d));
}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
// setup fill color
var cValue = function (d) {
return d.value;
},
color = d3.scale.category10();
// add the graph canvas to the body of the webpage
var svg = d3.select(chartContainer).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(fakeGroup.all(), xValue) - 1, d3.max(fakeGroup.all(), xValue) + 1]);
yScale.domain([d3.min(fakeGroup.all(), yValue) - 1, d3.max(fakeGroup.all(), yValue) + 1]);
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xTitle);
// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(yTitle);
// draw dots
svg.selectAll(".dot")
.data(fakeGroup.all())
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function (d) {
return color(cValue(d));
})
.on("mouseover", function (d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html( "Player Name: "+d.key.player +"</br>"+xTitle+": "+xValue(d)
+ "</br>"+yTitle+": " + yValue(d) )
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function (d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
// draw legend
/*var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function (d, i) {
return "translate(0," + i * 20 + ")";
});
// draw legend colored rectangles
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
// draw legend text
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function (d) {
return d;
})
*/
};
});
</script>
</html>