-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·190 lines (153 loc) · 4.1 KB
/
index.html
File metadata and controls
executable file
·190 lines (153 loc) · 4.1 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<style>
svg {
font: 10px sans-serif;
}
path {
fill: blue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
<script>
var margin = {
top: 10,
right: 10,
bottom: 100,
left: 40
},
margin2 = {
top: 50,
right: 10,
bottom: 20,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var brushLoc = [0, 0];
var newLoc = [0, 0];
var count = 0;
var brushes = [];
var parseDate = d3.time.format("%b %Y").parse;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom");
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) {
return x2(d.date);
})
.y0(height2)
.y1(function(d) {
return y2(d.price);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var context = svg.append("g")
.attr("class", "xplane")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.csv("sp500.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
});
x.domain(d3.extent(data.map(function(d) {
return d.date;
})));
y.domain([0, d3.max(data.map(function(d) {
return d.price;
}))]);
x2.domain(x.domain());
y2.domain(y.domain());
context.append("path")
.datum(data)
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
brush = newBrush();
$(".xplane").click(click);
});
function click(e) {
var newLoc = getMousePos(e, $(".xplane"));
if (window.event.metaKey) {
if (newLoc < brushLoc[0] || newLoc > brushLoc[1]) {
setPtr("none");
brush = newBrush();
} else {
setPtr("all");
}
} else {
setPtr("all");
}
}
function setPtr(ev) {
$('#brush' + count).css("pointer-events", ev);
}
function newBrush() {
count++;
var brush = d3.svg.brush()
.x(x2)
.on("brushend", brushend);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("id", "brush")
.attr("height", height2 + 7);
brushes.push(brush);
return brush;
}
function brushend() {
brushLoc = getBrushLoc(this);
setPtr("none");
printData();
}
function printData() {
var view = $('.extentView');
view.empty();
var count = 1;
brushes.forEach(function(br){
view.append('<p>Brush '+count+': '+br.extent()[0]+' to '+br.extent()[1]+'</p>');
count++;
});
}
function getBrushLoc(br) {
var brusha = $(br);
var x = parseInt(brusha.find('.extent').attr('x'), 10);
var x1 = x + parseInt(brusha.find('.extent').attr('width'), 10);
return [x, x1];
}
function getMousePos(event, element) {
var offset = element.offset();
var x = event.pageX - offset.left;
return x;
}
</script>
<div class="extentView">
</body>