-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpc.js
177 lines (153 loc) · 4.16 KB
/
pc.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
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
function pc(){
var self = this; // for internal d3 functions
var votes = [];
var pcDiv = $("#pc");
//initialize tooltip
var tooltip = d3.select("body")
.append("div")
.style("backgroundColor", "white")
.style("opacity", 0);
//Initialize color scale
var colors = d3.scale.category20();
var margin = [30, 10, 30, 30],
width = 960 - margin[1] - margin[3],
height = 400 - margin[0] - margin[2];
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {},
max;
var selMuns = [];
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
foreground,
g;
var svg = d3.select("#pc").append("svg:svg")
.attr("width", width + margin[1] + margin[3])
.attr("height", height + margin[0] + margin[2])
.append("svg:g")
.attr("transform", "translate(" + margin[3] + "," + margin[0] + ")");
function update(muns){
//Clear lines
if(typeof foreground !== 'undefined'){
foreground.remove();
}
//Clear scales
if(typeof g !== 'undefined'){
g.remove();
}
//Get data
self.data = barchart1.getData();
//Filter data
self.data = self.data.filter(function(d){
for(var i=0; i<muns.length; i++){
if(d.region_name === muns[i]){
return d;
}
}
});
//Extract votes
votes = [];
for(var j=0; j<self.data.length; j++){
var points = [];
points.push(muns[j]);
for(var k=0; k<self.data[j].info.length; k++){
points.push(self.data[j].info[k].votes);
}
votes.push(points);
}
// Create Dimensions
dimensions = ["Län", "Moderaterna", "Centerpartiet", "Folkpartiet", "Kristdemokraterna", "Miljöpartiet", "Socialdemokraterna", "Vänsterpartiet", "Sverigedemokraterna", "övriga partier"];
x.domain(dimensions);
//Add scale
y[dimensions[0]] = d3.scale.ordinal()
.domain(muns)
.rangePoints([height, 0]);
//Find max vote value for normalization
if(typeof votes[0] !== 'undefined'){
max = votes[0][1];
for(var i=0; i<votes.length; i++){
for(j=1; j< votes[i].length; j++){
if(max < votes[i][j]){
max = votes[i][j];
}
}
}
}
else{
max = 1;
}
for(var i=1; i<10; i++){
y[dimensions[i]] = d3.scale.linear()
.domain([0, max*100])
.range([height, 0]);
}
draw();
}
function draw(){
// Add lines
foreground = svg.append("svg:g")
.attr("class", "foreground")
.selectAll("path")
//add the data and append the path
.data(votes)
.enter()
.append("path")
.attr("d", path)
.style("fill", "none")
.style("stroke", function(d){ return colors(d[0]); })
.on("mouseover", function(d){
d3.select(this)
.style("stroke", "blue")
.style("stroke-width", 4);
return tooltip
.style("opacity", 1)
.text(d[0]);
})
.on("mousemove", function (d) {
return tooltip
.style("top", (d3.event.pageY + 16) + "px")
.style("left", (d3.event.pageX + 16) + "px");
})
.on("mouseout", function(d){
d3.select(this)
.style("stroke", function(d){ return colors(d[0]); })
.style("stroke-width", 1);
return tooltip.style("opacity", 0);
});
// Add a group element for each dimension.
g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("svg:g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; });
// Add an axis and title.
g.append("svg:g")
.attr("class", "axis")
//add scale
.each(function(d) {
d3.select(this).call(axis.scale(y[d]));
})
.append("svg:text")
.attr("text-anchor", "middle")
.attr("y", height + 15)
.text(String);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p, i) {
if(p === "Län"){
return [x(p), y[p](d[0])];
}
else{
return [x(p), -d[i]/max*height+height];
}
}));
}
this.setSelectedMuns = function(value){
selMuns = value;
update(value);
};
this.updateGraph = function(){
update(selMuns);
}
update([""]);
}