-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
79 lines (69 loc) · 3.17 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="style.css">
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<style>
circle {fill: lightblue; stroke: black;}
div.tooltip {
opacity: 0;
position: absolute;
text-align: center;
width: 250px;
height: auto;
background: lightgray;
border: 0px;
pointer-events: none;
}
</style>
<body onload='init()'>
<h1>Average City MPG vs. Average Highway MPG</h1>
<p fill="black">This graph shows a relationship between City and Highway MPG by respective car brands in 2017; the size of the dots indicates the number of cylinders.</p>
<p>(Hover over the dots to reveal additional information.)</p>
<svg width="1200" height="670">
</svg>
<p>
<a href="tester.html">Pie Chart</a>
<a href="cars.html">Bar Chart</a>
</p>
<script>
async function init() {
const data = await d3.csv('https://flunky.github.io/cars2017.csv')
var x = d3.scaleLog().base(10).domain([10,150]).range([0, 1000]);
var y = d3.scaleLog().base(10).domain([10,150]).range([570, 0]);
var svg = d3.select("svg")
.append("g").attr("transform", "translate("+50+","+50+")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.select("g")
.selectAll("circle").data(data)
.enter().append("circle")
.attr("cx", function(d,i) {return x(d.AverageCityMPG);})
.attr("cy", function(d,i) {return y(d.AverageHighwayMPG);})
.attr("r", function(d) {return (Number(d.EngineCylinders)+2);})
.on ("mouseover", function(i,d) {
tooltip.style("opacity", 1)
.style("left",(event.pageX)+"px")
.style("top",(event.pageY)+"px")
.html("Make: "+d.EngineCylinders+"-Cylinder "+d.Make+"; City MPG: "+d.AverageCityMPG+"; Highway MPG: "+d.AverageHighwayMPG+"; Fuel-type: "+d.Fuel);
console.log(d)
})
.on ("mouseout", function(i,d) {
tooltip.style("opacity", 0)
})
d3.select("svg").append("g").attr("transform","translate("+50+","+50+")")
.call(d3.axisLeft(y)
.tickValues([10,20,50,100]).tickFormat(d3.format("~s")));
d3.select("svg").append("g").attr("transform","translate("+50+","+620+")")
.call(d3.axisBottom(x)
.tickValues([10,20,50,100]).tickFormat(d3.format("~s")));
}
</script>
</body>
</html>