-
Notifications
You must be signed in to change notification settings - Fork 3
/
time-series.js
95 lines (77 loc) · 2.59 KB
/
time-series.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
function ts_plot(counts, raw_days){
// convert date format
days = [];
for (d in raw_days){
days.push(new Date(raw_days[d]));
}
var margin = {top: 10, right: 30, bottom: 50, left: 50},
width = 480 - margin.left - margin.right,
height = 220 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(Math.min.apply(null,days)), new Date(Math.max.apply(null,days))])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, Math.max.apply(null,counts)])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(""));
var line = d3.svg.line()
.x(function(d) {return x(new Date(d[0])); })
.y(function(d) { return y(d[1]); })
var svg = d3.select("#tooltip").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 + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll('text')
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.attr("x", 9)
.attr("y", 0)
.style("text-anchor", "start")
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("x", -90)
.attr("y", -45)
.attr("dy", ".35em")
.attr("transform", "rotate(270)")
.style("text-anchor", "middle")
.text("Count of Crawled Ads");
svg.append("g")
.append("text")
.attr("class", "label")
.attr("x", width/2 - 5)
.attr("y", 10)
.style("text-anchor", "middle")
.style("font-weight", "bold")
.style("font-size", "11pt")
.text("Trend");
sorted_data = []
for (j in days){
sorted_data.push([days[j], counts[j]])
}
function lineData(x_counts){
return x_counts.map(function(x,i){
return [x[0], x[1]]
})
}
var ld = lineData(sorted_data)
svg.append("path")
.datum(ld)
.attr("class", "line")
.attr("d", line);
document.getElementById("tooltip").innerHTML += '<div style="y: 1000px; vertical-align: bottom; x: 250px; position: absolute;">\
*Kernel density estimates have been artificially inflated for demo purposes</div>'
}