-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotEdge.js
More file actions
132 lines (113 loc) · 3.81 KB
/
plotEdge.js
File metadata and controls
132 lines (113 loc) · 3.81 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
// plot a histogram for the number of edges in public graphs, data in a .csv file
function parser1(d) {
d.pMPG = +d.Edges;
return d;
}
function mpghist1(csvdata) {
var binsize = 10;
var minbin = 0;
var maxofcsvdata = d3.max(csvdata, function(d){return d.pMPG;});
var maxbin = Math.ceil(maxofcsvdata + 0.01 * maxofcsvdata);
var numbins = Math.floor((maxbin - minbin) / binsize);
// whitespace on either side of the bars
var binmargin = .2;
var margin = {top: 10, right: 30, bottom: 50, left: 60};
var width = 1050 - margin.left - margin.right;
var height = 450 - margin.top - margin.bottom;
// Set the limits of the x axis
var xmin = minbin - 1;
var xmax = maxbin + 1;
histdata = new Array(numbins);
for (var i = 0; i < numbins; i++) {
histdata[i] = { numfill: 0, meta: "" };
}
// Fill histdata with y-axis values and meta data
csvdata.forEach(function(d) {
var bin = Math.floor((d.pMPG - minbin) / binsize);
if ((bin.toString() != "NaN") && (bin < histdata.length)) {
histdata[bin].numfill += 1;
histdata[bin].meta += "<tr><td>" + d.Name +
" " +
"</td><td>" +
d.pMPG.toFixed(0) + " edges</td></tr>";
}
});
// This scale is for determining the widths of the histogram bars
var x = d3.scale.linear()
.domain([0, (xmax - xmin)])
.range([0, width]);
// Scale for the placement of the bars
var x2 = d3.scale.linear()
.domain([xmin, xmax])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, d3.max(histdata, function(d) {
return d.numfill;
})])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x2)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.ticks(8)
.orient("left");
var tip = d3.tip()
.attr('class', 'd3-tip')
.direction('e')
.offset([0, 20])
.html(function(d) {
return '<table id="tiptable">' + d.meta + "</table>";
});
// put the graph in the "graphEdge" div
var svg = d3.select("#graphEdge").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.call(tip);
// set up the bars
var bar = svg.selectAll(".bar")
.data(histdata)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d, i) { return "translate(" +
x2(i * binsize + minbin) + "," + y(d.numfill) + ")"; })
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
// add rectangles of correct size at correct location
bar.append("rect")
.attr("x", x(binmargin))
.attr("width", x(binsize - 2 * binmargin))
.attr("height", function(d) { return height - y(d.numfill); });
// add the x axis and x-label
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("text")
.attr("class", "xlabel")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + margin.bottom)
.text("Number of Edges in Public Graphs (Move on the graph to see detail)");
// add the y axis and y-label
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(0,0)")
.call(yAxis);
svg.append("text")
.attr("class", "ylabel")
.attr("y", 0 - margin.left) // x and y switched due to rotation
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("transform", "rotate(-90)")
.style("text-anchor", "middle")
.text("Number of Graphs");
}
// Read in .csv data and make graph
d3.csv("TotalPublicEdges.csv", parser1,
function(error, csvdata) {
mpghist1(csvdata);
});