Repository navigation
Expand file tree
/
Copy pathprac.js
More file actions
120 lines (97 loc) · 4.36 KB
/
Copy pathprac.js
File metadata and controls
120 lines (97 loc) · 4.36 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
// globals:
var title = d3.select("svg").append("text").text("EBOLA").classed("title",true).attr("x",288).attr("y",20)
var margin = {top: 36, right: 20, bottom: 100, left: 100},
width = 560 - margin.left - margin.right,
height = 420 - margin.top - margin.bottom;
// function to transform a domain of date objects into a range of screen coordinates
var x = d3.scale.linear()
.range([0, width]);
// function to transform a domain of numeric values into a range of screen coordinates
var y = d3.scale.linear()
.range([height, 0]);
// function that thakes an input and transforms it into a specified range.
var size_scale = d3.scale.linear()
.range([3,31])
// function that returns one of 10 colors depending on the integer input
var color = d3.scale.category10();
// d3.svg.axis is a predefined and you can change the options
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// d3.svg.axis is a predefined and you can change the options
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
// adds an svg element to the DOM
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g") // the 'g' element is just a container; also note: the indent is intentional
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // repositions the svg to the right 'margin.left' units and down 'margin.top' units
// MAIN
// scatter plot builder
// note: build_graph() takes one argument: the dataset
function build_graph(tweetData){
// print statment often used to test code
console.log("building scatterplot.")
// format strings to date objects and make counts numeric
tweetData.forEach(function(d) {
//d.timeStamp = parseDate(d.timeStamp);
d.timeStamp = new Date(d.timeStamp);
d.followersCount = +d.followersCount;
d.followingCount = +d.followingCount;
d.listedCount = +d.listedCount;
d.statusesCount = +d.statusesCount;
});
console.log("tweetData:",tweetData)
// we get the min/max values from our dataset to define the domain of various functions
size_scale.domain(d3.extent(tweetData,function(d) {return d.followersCount}));
console.log(d3.extent(tweetData, function(d) { return d.followersCount; }));
console.log(d3.extent(tweetData, function(d) { return d.followingCount; }));
x.domain(d3.extent(tweetData, function(d) { return d.followingCount; }));
y.domain(d3.extent(tweetData, function(d) { return d.followersCount; }));
// adds the x axis
svg.append("g")
.attr("class", "x axis") // look at the .axis setings in style.css
.attr("transform", "translate(0," + height + ")")
.call(xAxis) // calls the function that we created to use d3.svg.axis
.append("text")
.text("Following")
.attr("x",width)
.attr("dy", "-.5em")
.style("stroke","black")
.style("text-anchor", "end");
// changes the x axis css that does not include the "Following" label
d3.selectAll(".x.axis .tick.major text")
.style("text-anchor", "end")
.attr("dx", "-0.497664em")
.attr("dy", ".1em")
.attr("transform", function(d) {
return "rotate(-65)"
});
// adds the y axis and some added css
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end") // additional css (http://mzl.la/1rC7zya)
.text("Followers")
.style("stroke","black"); // we can override the css here
// joins the data, creates the circles, classes the circles as 'dot'
svg.selectAll(".dot")
.data(tweetData)
.enter().append("circle") // data binding (http://bost.ocks.org/mike/join/)
.classed("dot",true)
.attr("r", 3)
.attr("cx", function(d) { return x(d.followingCount); })
.attr("cy", function(d) { return y(d.followersCount); });
console.log("built scatterplot")
}
console.log("loading data")
d3.csv("data/sample1000_withHeader_zerosEdited.csv", function(error, rawData) {
console.log("loaded data")
build_graph(rawData);
});