-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiecharts_basic.html
More file actions
195 lines (174 loc) · 11 KB
/
piecharts_basic.html
File metadata and controls
195 lines (174 loc) · 11 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
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
190
191
192
193
194
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>ConTracker v1.1</title>
<script src='https://d3js.org/d3.v3.js'></script>
<style type="text/css">
.slice text {
font-size: 12pt;
font-family: Arial;
}
</style>
</head>
<body>
<script type="text/javascript">
</script>
<button onclick="switchSortMode(0)">Party</button>
<button onclick="switchSortMode(1)">Total</button>
<button onclick="switchSortMode(2)">Initials</button>
<br>
<br>
<br>
<div id="pieCharts01" width=100vw height=100vh style="display:block"></div>
<div id="pieCharts0-1" width=100vw height=100vh style="display:none"></div>
<div id="pieCharts11" width=100vw height=100vh style="display:none"></div>
<div id="pieCharts1-1" width=100vw height=100vh style="display:none"></div>
<div id="pieCharts21" width=100vw height=100vh style="display:none"></div>
<div id="pieCharts2-1" width=100vw height=100vh style="display:none"></div>
<script type="text/javascript">
// Adapted from http://bl.ocks.org/enjalot/1203641
var color = ["#98AFC7", "#B6B6B4", "#FB8885"],
currentSortMode = 0,
currentAscending = 1,
data = [{"percentages": [{"value":55.9041545415},{"value":0.49319112139},{"value":43.6026543371}],"total":51460843.27,"initials":"AL"},
{"percentages": [{"value":7.62092272691},{"value":54.0299072381},{"value":38.349170035}],"total":2738951.01,"initials":"AK"},
{"percentages": [{"value":53.5404001662},{"value":1.12370169042},{"value":45.3358981433}],"total":69546864.32,"initials":"AZ"},
{"percentages": [{"value":30.077329908},{"value":0.012180654035},{"value":69.910489438}],"total":9205499.12,"initials":"AR"},
{"percentages": [{"value":74.7100775453},{"value":0.693065924937},{"value":24.5968565297}],"total":247669267.56,"initials":"CA"},
{"percentages": [{"value":65.6110152562},{"value":0.66442164402},{"value":33.7245630997}],"total":22154725.29,"initials":"CO"},
{"percentages": [{"value":93.4852001608},{"value":3.5527136788e-15},{"value":6.51479983922}],"total":21643057.42,"initials":"CT"},
//{"percentages": [{"value":98.3985729733},{"value":-1.7763568394e-15},{"value":1.6014270267}],"total":537957.7,"initials":"DC"},
{"percentages": [{"value":83.6636343978},{"value":1.06581410364e-14},{"value":16.3363656022}],"total":5306939.2,"initials":"DE"},
{"percentages": [{"value":41.0955611873},{"value":0.78206800592},{"value":58.1223708067}],"total":181200896.76,"initials":"FL"},
{"percentages": [{"value":64.859008481},{"value":0.0557061077614},{"value":35.0852854112}],"total":72068345.13,"initials":"GA"},
{"percentages": [{"value":98.0356795254},{"value":5.3290705182e-15},{"value":1.96432047458}],"total":7311657.23,"initials":"HI"},
{"percentages": [{"value":5.20537488163},{"value":0.0647751655889},{"value":94.7298499528}],"total":2968884.73,"initials":"ID"},
{"percentages": [{"value":66.8196599433},{"value":0.465251925966},{"value":32.7150881308}],"total":70300102.32,"initials":"IL"},
{"percentages": [{"value":39.1480800026},{"value":0.117117467355},{"value":60.7348025301}],"total":60663188.51,"initials":"IN"},
{"percentages": [{"value":68.4826762539},{"value":-1.06581410364e-14},{"value":31.5173237461}],"total":21447446.06,"initials":"IA"},
{"percentages": [{"value":54.2379935057},{"value":-7.1054273576e-15},{"value":45.7620064943}],"total":22309624.56,"initials":"KS"},
{"percentages": [{"value":54.2292187019},{"value":-3.5527136788e-14},{"value":45.7707812981}],"total":20361383.17,"initials":"KY"},
{"percentages": [{"value":13.5121669917},{"value":0.198935450679},{"value":86.2888975576}],"total":17413754.0,"initials":"LA"},
{"percentages": [{"value":40.2298983708},{"value":29.9479737494},{"value":29.8221278799}],"total":16963417.3,"initials":"ME"},
{"percentages": [{"value":88.1325705085},{"value":4.54753204009},{"value":7.31989745138}],"total":40913660.06,"initials":"MD"},
{"percentages": [{"value":74.584181154},{"value":7.05379502057},{"value":18.3620238254}],"total":71231522.54,"initials":"MA"},
{"percentages": [{"value":56.4414945434},{"value":0.314272145889},{"value":43.2442333108}],"total":76518696.66,"initials":"MI"},
{"percentages": [{"value":41.330608044},{"value":26.5115157254},{"value":32.1578762306}],"total":56839596.71,"initials":"MN"},
{"percentages": [{"value":29.0600804575},{"value":0.0},{"value":70.9399195425}],"total":19964860.45,"initials":"MS"},
{"percentages": [{"value":61.8446953847},{"value":1.0209642809},{"value":37.1343403344}],"total":61758020.51,"initials":"MO"},
{"percentages": [{"value":62.443641143},{"value":0.0419080184924},{"value":37.5144508385}],"total":46706312.31,"initials":"MT"},
{"percentages": [{"value":37.2005855619},{"value":-2.13162820728e-14},{"value":62.7994144381}],"total":14190096.85,"initials":"NE"},
{"percentages": [{"value":65.1251898999},{"value":0.00244456586083},{"value":34.8723655343}],"total":49088470.85,"initials":"NV"},
{"percentages": [{"value":70.8371607616},{"value":-7.1054273576e-15},{"value":29.1628392384}],"total":11521716.19,"initials":"NH"},
{"percentages": [{"value":52.17731251},{"value":0.0563036917992},{"value":47.7663837982}],"total":92834836.1,"initials":"NJ"},
{"percentages": [{"value":80.2767005768},{"value":7.92893690647},{"value":11.7943625167}],"total":21282451.48,"initials":"NM"},
{"percentages": [{"value":76.5181369716},{"value":0.466080199727},{"value":23.0157828287}],"total":115945753.61,"initials":"NY"},
{"percentages": [{"value":42.1745397861},{"value":0.0855009566177},{"value":57.7399592572}],"total":37619836.4,"initials":"NC"},
{"percentages": [{"value":75.7082442824},{"value":-3.5527136788e-15},{"value":24.2917557176}],"total":34650498.58,"initials":"ND"},
{"percentages": [{"value":56.5762581954},{"value":0.0385231720993},{"value":43.3852186325}],"total":81644808.27,"initials":"OH"},
{"percentages": [{"value":15.9959526867},{"value":0.120764599093},{"value":83.8832827142}],"total":11218395.21,"initials":"OK"},
{"percentages": [{"value":58.1827653554},{"value":7.1054273576e-15},{"value":41.8172346446}],"total":12743272.35,"initials":"OR"},
{"percentages": [{"value":65.4158636857},{"value":0.267200285115},{"value":34.3169360292}],"total":107603036.38,"initials":"PA"},
{"percentages": [{"value":84.6122495137},{"value":0.17095326849},{"value":15.2167972178}],"total":8987783.7,"initials":"RI"},
{"percentages": [{"value":34.2704005045},{"value":0.266839071538},{"value":65.4627604239}],"total":19934486.99,"initials":"SC"},
{"percentages": [{"value":24.8506876265},{"value":0.577592738932},{"value":74.5717196346}],"total":3225456.06,"initials":"SD"},
{"percentages": [{"value":38.2898855789},{"value":0.0161754333179},{"value":61.6939389878}],"total":53978584.86,"initials":"TN"},
{"percentages": [{"value":56.9841384612},{"value":1.10755770233},{"value":41.9083038365}],"total":215729915.92,"initials":"TX"},
{"percentages": [{"value":24.5671472198},{"value":3.74965489829},{"value":71.6831978819}],"total":21082588.97,"initials":"UT"},
{"percentages": [{"value":8.46535296136},{"value":90.1697229852},{"value":1.36492405343}],"total":10264711.04,"initials":"VT"},
{"percentages": [{"value":69.7657612226},{"value":0.119466600381},{"value":30.1147721771}],"total":72939566.14,"initials":"VA"},
{"percentages": [{"value":71.8925427429},{"value":0.284749486776},{"value":27.8227077703}],"total":53979847.95,"initials":"WA"},
{"percentages": [{"value":43.780063275},{"value":14.1436017636},{"value":42.0763349615}],"total":29465196.77,"initials":"WV"},
{"percentages": [{"value":67.3623698422},{"value":0.00746587330634},{"value":32.6301642845}],"total":64640796.89,"initials":"WI"},
{"percentages": [{"value":9.52099101069},{"value":0.00322280652816},{"value":90.4757861828}],"total":9308656.83,"initials":"WY"}];
drawPieDisplay(0,1);
drawPieDisplay(0,-1);
drawPieDisplay(1,1);
drawPieDisplay(1,-1);
drawPieDisplay(2,1);
drawPieDisplay(2,-1);
function drawPieDisplay(sortMode, ascending) {
if(sortMode == 0) sortByDominance(data, ascending);
if(sortMode == 1) sortByTotals(data, ascending);
if(sortMode == 2) sortByInitials(data, ascending);
// display the pie charts
var state;
for(state = 0; state < data.length; state++) {
var r = 0.006 * Math.sqrt(data[state].total),
w = 2*r,
h = 2*r;
var vis = d3.select("#pieCharts" + sortMode + "" + ascending)
.append("svg:svg")
.data([data[state].percentages])
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + r + "," + r + ")")
var arc = d3.svg.arc()
.outerRadius(r);
var pie = d3.layout.pie()
.value(function(d) { return d.value; }).sort(null);
var arcs = vis.selectAll("g.slice")
.data(pie)
.enter()
.append("svg:g")
.attr("class", "slice");
arcs.append("svg:path")
.attr("fill", function(d, i) { return color[i]; } )
.attr("d", arc);
vis.append("svg:text")
.attr("text-anchor", "middle")
.attr("transform", "translate(" + 0 + "," + 0.35*r + ")")
.attr("font-family", "Times")
.attr("font-size", function(d,i) {
return 2*(Math.floor(0.4*r))+"pt";
})
.text(function(d, i) { return data[state].initials; });
}
}
function sortByDominance(data, ascending) {
// sort the data by rep, dem
data.sort(function(a,b) {
var aDemDominance = a.percentages[0].value - a.percentages[2].value;
var bDemDominance = b.percentages[0].value - b.percentages[2].value;
return ascending*(aDemDominance - bDemDominance);
});
}
function sortByInitials(data, ascending) {
data.sort(function(a,b) {
// from https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript
if (a.initials < b.initials)
return -1 * ascending;
if ( a.initials > b.initials)
return 1 * ascending;
return 0 * ascending;
});
}
function sortByTotals(data, ascending) {
data.sort(function(a,b) { return ascending * (a.total - b.total); });
}
function switchSortMode(newSortMode){
if(newSortMode != currentSortMode) setCurrentSorting(newSortMode, 1);
else toggleAscending();
}
function toggleAscending() {
setCurrentSorting(currentSortMode, currentAscending * -1);
}
function setCurrentSorting(newSortMode, newAscending) {
hideCurrent();
currentSortMode = newSortMode;
currentAscending = newAscending;
showCurrent();
}
function hideCurrent() {
var pieCharts = document.getElementById("pieCharts"+ currentSortMode + "" + currentAscending);
pieCharts.style.display = "none";
}
function showCurrent() {
var pieCharts = document.getElementById("pieCharts"+ currentSortMode + "" + currentAscending);
pieCharts.style.display = "block";
}
</script>
</body>
</html>