-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers-topic-stack.html
129 lines (108 loc) · 3.53 KB
/
users-topic-stack.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
<!DOCTYPE html>
<html>
<style>
.axis .domain {
display: none;
}
.legend-text {
stroke: black;
font-size: 13px;
}
</style>
<body>
<svg width="960" height="750"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="colorbrewer.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 80},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom - 120,
g = svg.append("g").attr("transform", "translate(" + margin.left+ "," + margin.top + ")");
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.05)
.align(0.1);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(colorbrewer.Paired[9]);
d3.csv("../ABCi_Twitter_Analysis/diabetes/new_set/matrix_of_top_twitters_topics.csv", function(d, i, columns) {
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
d.total = t;
return d;
}, function(error, data) {
if (error) throw error;
var keys = data.columns.slice(1);
console.log(keys)
x.domain(data.map(function(d) { return d.Username; }));
y.domain([0, d3.max(data, function(d) { return d.total; }) + 30]).nice();
console.log(data.map(function(d) { return d.Username; }))
z.domain(keys);
g.append("g")
.selectAll("g")
.data(d3.stack().keys(keys)(data))
.enter().append("g")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.Username); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth())
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (height) + ")")
.call(d3.axisBottom(x))
.attr("font-size", 13)
.attr('stroke', 'bold')
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("fill", "#000")
.attr("transform", "rotate(60)")
.style("text-anchor", "start") ;
g.append("text") // text label for the x axis
.attr("transform", "translate("+ (width/2) +"," +(height + (margin.bottom*4.5)) + ")")
.attr("font-family", "sans-serif")
.attr("stroke", "black")
.style("text-anchor", "middle")
.text("Most active Users");
console.log(height + " " + width)
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.attr("font-size", 13)
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("font-size", 15)
.attr("transform", "translate(-50," + (height/1.7) + ") rotate(270)")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("Tweet Occurrences");
var legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 19)
.attr("width", 20)
.attr("height", 19)
.attr("fill", z);
legend.append("text")
.attr('class', 'legend-text')
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
});
</script>
</body>
</html>