-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxplot_gpu.html
204 lines (161 loc) · 5.95 KB
/
boxplot_gpu.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
<!DOCTYPE HTML>
<meta charset="utf-8">
<! boiler plate code >
<link rel = "stylesheet" href="./boxplot_gpustyles.css">
<h1 class="head">
GPU PRICE ANALYSIS OVER 1 WEEK
</h1>
<body> </body>
<script src="https://d3js.org/d3.v4.js"></script>
<div id = "boxplot" class = "boxplot"> </div>
<script>
//LOOKING FORWARD: make mouseover function that animates scales the boxes(IQR) & draws max and min.
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
width = 800 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#boxplot")
.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 + ")");
// var parser1 = d3.timeParse("%m/%d/%Y");
// console.log(parser1("9/6/2021"));
//test parser1 - get date object
var parser = d3.timeParse("%m/%d/%Y");
var format = d3.timeFormat("%m/%d/%Y");
// Read the data and compute summary statistics for each day
d3.csv("gpuebay_master.csv", function(data) {
//need help reading data
var sumstat = d3.nest() // allows group calc per level, in our case the date
.key(function(d) {return (d.date) } )
.rollup(function (d) {
q1 = d3.quantile(d.map(function(h) {return +h.price;}).sort(d3.ascending), .25)
median = d3.quantile(d.map(function(h) {return +h.price;}).sort(d3.ascending), .5)
q3 = d3.quantile(d.map(function(h) {return +h.price;}).sort(d3.ascending), .75)
interQR = q3 - q1
min = q1 - 1.5 * interQR
max = q3 + 1.5 * interQR
return ({q1: q1, median: median, q3: q3, interQR: interQR, min: min, max: max})
})
.entries(data)
//TESTING
//price is undefined
console.log(d3.extent(data, function(h) {return (+ h.price) ;}))
//show x scale
var x = d3.scaleTime()
.range([0 + margin.left, width - margin.left])
.domain(d3.extent(data, function(d) { return parser(d.date);})) // BUGS?
svg.append("g")
.attr("transform", "translate(0," + height +")")
.call(d3.axisBottom(x))
// let linearScale = d3.scaleLinear()
// .domain(d3.extent(data, median))
// .range(['green', 'red']);
// var x = d3.scaleBand()
// .range([ 0, width ])
// .domain(["setosa", "versicolor", "virginica"])
// .paddingInner(1)
// .paddingOuter(.5)
// svg.append("g")
.attr("transform", "translate(0," + height + ")")
// .call(d3.axisBottom(x))
// Show the Y scale
var y = d3.scaleLinear()
.domain([-700, 2500])
// .domain(d3.extent(data, function(h) { return +h.price; }))
.range([height, 0])
svg.append("g").call(d3.axisLeft(y))
// .attr("transform", "translate(0," + 200 +")")
// // Show the main vertical line
svg
.selectAll("vertLines")
.data(sumstat)
.enter()
.append("line")
//x(d3.format("%m/%d/%Y").parse(d.key))
//Problem: need to get date object from x(d.key) because D3 stores keys as strings
.attr("x1", function(d) {return x(parser(d.key)) })
.attr("x2", function(d) {return x(parser(d.key)) })
.attr("y1", function(d){return(y(d.value.min))})
.attr("y2", function(d){return(y(d.value.max))})
.attr("stroke", "black")
.style("width", 40)
// rectangle for the main box
var boxWidth = 80
svg
.selectAll("boxes")
.data(sumstat)
.enter()
.append("rect")
.attr("x", function(d){return(x(parser(d.key))-boxWidth/2)})
.attr("y", function(d){return(y(d.value.q3))})
.attr("height", function(d){return(y(d.value.q1)-y(d.value.q3))})
.attr("width", boxWidth )
.attr("stroke", "black")
.style("fill", " #4d79ff")
// Show the median
svg
.selectAll("medianLines")
.data(sumstat)
.enter()
.append("line")
.attr("x1", function(d){return(x(parser(d.key))-boxWidth/2) })
.attr("x2", function(d){return(x(parser(d.key))+boxWidth/2) })
.attr("y1", function(d){return(y(d.value.median))})
.attr("y2", function(d){return(y(d.value.median))})
.attr("stroke", "black")
.style("width", 80)
// Create tool tip with brands
var tooltip = d3.select("#boxplot")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("font-size", "20px")
.style("font-family", "Gill Sans MT")
.style("font-weight", "bold")
// Three functions which define tooltip use over 3 stages: mouseover, mousemove, mouseleave
var mouseover = function(d) {
tooltip
.transition()
.duration(200)
.style("opacity", 1)
tooltip
.html("<span style='color:#4d79ff'> Brand: </span>" + d.brand) // + d.Prior_disorder + "<br>" + "HR: " + d.HR)
.style("left", (d3.mouse(this)[0]+30) + "px")
.style("top", (d3.mouse(this)[1]+30) + "px")
}
var mousemove = function(d) {
tooltip
.style("left", (d3.mouse(this)[0]+30) + "px")
.style("top", (d3.mouse(this)[1]+30) + "px")
}
var mouseleave = function(d) {
tooltip
.transition()
.duration(200)
.style("opacity", 0)
}
console.log(function(d) {return x(parser(d.key)) - boxWidth/2})
// myval = function(d){return x(parser(d.key))} ,
// myval2 = function(d){return y(+d.price)} ;
// // Add individual points with jitter
var jitterWidth = 50
svg
.selectAll("indPoints")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {return(x(parser(d.date)) - jitterWidth/2 + Math.random()*jitterWidth )}) //x pos of date
.attr("cy", function(d) {return(y(+d.price))}) //ypos of a price
.attr("r", 4)
.style("fill", "#C6E9D5")
.attr("stroke", "#4d79ff")
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// function(d){return(x(parser(d.key))-boxWidth/2)})
})
</script>