-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
173 lines (157 loc) · 5.61 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
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
<html>
<head>
<title>Lab 9</title>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="lab9.js"></script>
<link rel=stylesheet type="text/css" href="style.css"/>
</head>
<body>
<p id="demo"></p>
<h4 id="hovered"></h4>
<div class="plot"></div>
<div class="ui">
<div>
<label>X-Axis</label>
<select id="sel-x" onchange ="updateData()">
</select>
<label>Y-Axis</label>
<select id="sel-y" onchange ="updateData()">
</select>
</div>
<div>
<input id="mpg-min" type="text" value="0" size="10">
<input id="mpg-max" type="text" value="30" size="10">
<button id="update" onclick="QueryMPG()">Query MPG</button>
</div>
</div>
<script>
function QueryMPG() {
var mpg_min = document.getElementById("mpg-min").value;
var mpg_max = document.getElementById("mpg-max").value;
var xKey = document.getElementById("sel-x").value;
var yKey = document.getElementById("sel-y").value;
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);
// Adding constrain to data mapping so as to filter MPG
var xValue = function(d) { if((+d.mpg)<mpg_max && (+d.mpg)>mpg_min){return d[xKey];}};
var yValue = function(d) { if((+d.mpg)<mpg_max && (+d.mpg)>mpg_min){return d[yKey];}};
var xMap = function(d) {if (!isNaN(x(xValue(d)))){return x(xValue(d));}}
var yMap = function(d) {if (!isNaN(y(yValue(d)))){return y(yValue(d));}}
// Get the data again
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]];}
}
});
// Scale the range of the data again
x.domain([d3.min(data, xValue)-10, d3.max(data, xValue)+10]);
y.domain([d3.min(data, yValue)-5, d3.max(data, yValue)+5]);
// Select the section we want to apply our changes to
var svg = d3.select(".plot").transition();
// Make the changes
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".x.label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xKey);
svg.select(".y.label")
.attr("x", 30)
.attr("y", -6)
.attr("transform", "rotate(90)")
.style("text-anchor", "end")
.text(yKey);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);
svg.selectAll(".dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap);
});
}
function updateData() {
var xKey = document.getElementById("sel-x").value;
var yKey = document.getElementById("sel-y").value;
var xValue = function(d) { return d[xKey]; };
var yValue = function(d) { return d[yKey]; };
var xMap = function(d) { return x(xValue(d));}
var 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]); });
// Get the data again
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-1; i++) {
// change string into number format
if (!isNaN(d[key[i]])){
d[key[i]] = +d[key[i]];}
}
});
// Scale the range of the data again
x.domain([d3.min(data, xValue)-10, d3.max(data, xValue)+10]);
y.domain([d3.min(data, yValue)-5, d3.max(data, yValue)+5]);
// Select the section we want to apply our changes to
var svg = d3.select(".plot").transition();
// Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".x.label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xKey);
svg.select(".y.label")
.attr("x", 30)
.attr("y", -6)
.attr("transform", "rotate(90)")
.style("text-anchor", "end")
.text(yKey);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);
svg.selectAll(".dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap);
});
}
</script>
</body>
</html>