-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
143 lines (135 loc) · 4.43 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
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body {
font: 12px "Comic Sans MS";
}
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
div.tooltip {
position: absolute;
text-align: center;
width: 100px;
height: 70px;
padding: 2px;
font: 12px "Comic Sans MS";
background: lightsteelblue;
border: 0px;
border-radius: 3px;
pointer-events: none;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 60, right: 90, bottom: 60, left: 80},
width = 1200 - margin.left - margin.right,
height = 540 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var formatTime = d3.time.format("%e %B");
var x = d3.time.scale().range([0, width]);
var y0 = d3.scale.linear().range([height, 0]);
var y1 = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(10);
var yAxisLeft = d3.svg.axis().scale(y0)
.orient("left").ticks(10);
var yAxisRight = d3.svg.axis().scale(y1)
.orient("right").ticks(10);
var valueline2 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y1(d.search); });
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y0(d.price); });
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body")
.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 + ")");
d3.csv("price_and_search_data.csv", function(error, data) {
data.forEach(function(d) {
console.log(d);
d.unformat = d.date;
d.date = parseDate(d.date);
d.search = +d.search;
d.price = +d.price;
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y0.domain([0, d3.max(data, function(d) {
return Math.max(d.price); })]);
y1.domain([0, d3.max(data, function(d) {
return Math.max(d.search); })]);
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
svg.append("path")
.style("stroke", "red")
.attr("d", valueline2(data));
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 1.8)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y0(d.price); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .98);
div .html(d.unformat + "<br/>$" + d.price + "<br/>Search interest: " + d.search + "/100")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
svg.append("g")
.attr("class", "y axis")
.style("fill", "steelblue")
.call(yAxisLeft);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + " ,0)")
.style("fill", "red")
.call(yAxisRight);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
padding = -120;
svg.append("text")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("transform", "translate("+ (padding/2) +","+(height/2)+")rotate(-90)")
.text("Bitcoin price in USD");
svg.append("text")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("transform", "translate("+ (width - (padding/2)) +","+(height/2)+")rotate(90)")
.text("Google search");
svg.append("text")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("transform", "translate("+ (width/2) +","+(height-(padding/3))+")")
.text("Date");
});
</script>
</body>