-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab9.js
129 lines (114 loc) · 3.78 KB
/
lab9.js
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
//Render an interactive scatterplot of the data:
//– Dimensions for x-axis and y-axis can be selected.
//– Filtering on MPG is supported.
//– Hovered car name is shown as the h4 header.
$(document).ready(function() {
var draw = function(xKey, yKey) {
var xValue = function(d) { return d[xKey]; }, yValue = function(d) { return d[yKey]; };
var xMap = function(d) { return x(xValue(d));}, yMap = function(d) { return y(yValue(d));}
var margin = {top: 30, right: 20, bottom: 60, left: 30},
width = 400 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// Set the ranges
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d[xKey]); })
.y(function(d) { return y(d[yKey]); });
// Adds the svg canvas
var canvas = d3.select(".plot")
.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("#hovered").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Get the data
d3.csv("car.csv", function(error, data) {
data.forEach(function(d) {
// csv data are input one row at a time
var key = Object.keys(d)
for(var i = 0; i < key.length; i++) {
// change string into number format
if (!isNaN(d[key[i]])){
d[key[i]] = +d[key[i]];}
}
});
// Try to assign values to axis x and axis y
var selx = document.getElementById("sel-x");
var options = Object.keys(data[0])
for(var i = 1; i < options.length-1; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
selx.appendChild(el);
}
var sely = document.getElementById("sel-y");
for(var i = 1; i < options.length-1; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
sely.appendChild(el);
}
// Scale the range of the data
x.domain([d3.min(data, xValue)-10, d3.max(data, xValue)+10]);
y.domain([d3.min(data, yValue)-5, d3.max(data, yValue)+5]);
// Add the scatterplot
var scatterplot = canvas.selectAll("dot").data(data);
scatterplot.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap)
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.name)
.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);
});
scatterplot.exit().remove();
// Add the X Axis
canvas.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "x label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xKey);
// Add the Y Axis
canvas.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "y label")
.attr("x", 30)
.attr("y", -6)
.attr("transform", "rotate(90)")
.style("text-anchor", "end")
.text(yKey);
})
}
draw('displacement', 'mpg');
});