-
Notifications
You must be signed in to change notification settings - Fork 0
/
th-poll-donut.html
189 lines (163 loc) · 6.88 KB
/
th-poll-donut.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
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
178
179
180
181
182
183
184
185
186
187
188
189
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../th-d3-chart/th-d3-chart.html">
<link rel="import" href="th-poll-pair.html">
<!--
@group Thelma Charts
@element th-poll-donut
@blurb Charts the data retreived from th-poll-pair in a donut chart.
@status alpha
@homepage http://github.com/thelmanews/thelma-charts/blob/master/th-poll-donut.html
-->
<polymer-element name="th-poll-donut" extends="th-d3-chart" attributes=" question color1 color2">
<template>
<core-style ref="theme"></core-style>
<style>
:host {
display: inline-block;
position: relative;
width: 250px;
height: 250px;
}
</style>
<svg id="chart"></svg>
<th-poll-pair firebaseKey="{{_pairingKey}}" data="{{pollData}}"></th-poll-pair>
</template>
<script>
Polymer('th-poll-donut', {
question: "Will Donald Trump win the 2016 Republican nomination?",
init: function(){
var self = this;
this.questionChanged();
this.addEventListener('data-change', function(){
self.dataChanged();
});
this._setupDimensions();
this._setupElements();
},
_setupElements: function(){
var self = this;
// Arc svg
self.arc = d3.svg.arc()
.startAngle(0)
.innerRadius(self.dims.innerRadius)
.outerRadius(self.dims.outerRadius);
// Donut container
var svg = d3.select(self.$.chart);
var chart = svg.attr("width", self.width)
.attr("height", self.height)
.append("g")
.attr("transform", "translate(" +(self.width / 2) + "," + (self.dims.labelSize + self.dims.outerRadius) + ")");
// Donut group for paths
var meter = chart.append("g")
.attr("class", "progress-meter");
// Donut background
self.background = meter.append("path")
.attr("class", "fill-foreground2")
.attr("d", self.arc.endAngle(2 * Math.PI))
.style("stroke", this.color2)
.style("fill", this.color2);
// Donut foreground
self.foreground = meter.append("path")
.attr("class", "data0")
.style("stroke", this.color1)
.style("fill", this.color1);
// Data labels
self.topLabel = svg.append("text")
.attr('class','percent data0 label no-stroke')
.style('font-size', self.dims.labelSize*0.5 + 'px')
.attr("text-anchor", "middle")
.attr("transform", "translate(" +(self.width / 2 ) + "," + self.dims.labelSize/2 + ")")
.style("fill", this.color1)
.style("opacity", 0);
self.bottomLabel = svg.append("text")
.attr('class','percent fill-foreground2 label no-stroke')
.style('font-size', self.dims.labelSize*0.5 + 'px')
.attr("text-anchor", "middle")
.attr("transform", "translate(" +(self.width / 2 ) + "," + (self.dims.donutSize + self.dims.labelSize + self.dims.labelSize/2) + ")")
.style("fill", this.color2)
.style("opacity", 0);
},
_setupDimensions: function(){
var self = this;
self.width = self.computeWidth();
self.height = self.computeHeight();
self._prevProgress = 0;
self.dims = {
minLabelHeight: 20,
padding: 1
};
self.dims.labelSize = Math.min(60, Math.max(self.dims.minLabelHeight, Math.min(self.height * 0.12, self.width * 0.12)));
self.dims.donutSize = Math.min(self.width, self.height, (self.height - 2 * self.dims.labelSize));
self.dims.outerRadius = (self.dims.donutSize) * 0.49 - (self.dims.padding*2);
self.dims.innerRadius = (self.dims.donutSize) * 0.32;
},
animate: function(){
var self = this;
if (!self.pollData || !self.pollData.data || !self.pollData.data[0]._percent && self.pollData.data[0]._percent !== 0){
// Calculate percentages from data
self.updateChartdata();
} else {
// Display labels
self.topLabel
.text(self.pollData.data[0].label + ' ' + self.pollData.data[0]._percent + '%')
.transition().duration(500)
.style('opacity', 1);
self.bottomLabel
.text(self.pollData.data[1].label + ' ' + self.pollData.data[1]._percent + '%')
.transition().duration(500)
.style('opacity', 1);
// Designate the final progress level
var progress = self.pollData.data[0]._percent;
if(self._prevProgress == progress) {
self._prevProgress = 0;
}
var total = 100;
var i = d3.interpolate(self._prevProgress/total, progress/total);
// Animate the foreground to reach the final progress
self.foreground.transition().duration(500).tween("progress", function() {
return function(t) {
var currentProgress = i(t);
self.foreground.attr("d", self.arc.endAngle(Math.PI*2 * currentProgress));
};
});
self._prevProgress = progress;
}
},
reset: function(){
var self = this;
self.foreground.attr("d", self.arc.endAngle(0));
self.bottomLabel.style('opacity', 0);
self.topLabel.style('opacity', 0);
self._prevProgress = 0;
},
// PRIVATE METHODS
_createUniqueKey: function(str){
var hash, i, chr, len;
if (str.length == 0) return str;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
},
questionChanged: function(){
this._pairingKey = this._createUniqueKey(this.question);
},
dataChanged: function(){
if (this.pollData && this.pollData.data && this.pollData.data.length){
this.updateChartdata();
this.animate();
}
},
updateChartdata: function(){
var self = this;
if (this.pollData){
this.pollData.data.forEach(function(obj, i){
obj._percent = parseInt((obj._votes/self.pollData.total)*100);
})
}
}
});
</script>
</polymer-element>