-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
258 lines (217 loc) · 7.33 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* global d3, ga */
var _ = require('lodash')
var voters = require('./example-voters.js').map(function (voter) {
return Object.assign({}, voter, {
name: voter.uid,
})
})
var votersByUid = _.keyBy(voters, 'uid')
var links = voters.map(function (voter) {
return {
source: voter.name,
target: voter.delegate,
type: 'delegation',
}
})
var nodes = {}
var width = 900
var height = 600
// Convert links to weird d3 nodes object
links.forEach(function (link) {
link.source = nodes[link.source] || (nodes[link.source] = {
name: link.source,
full_name: votersByUid[link.source].full_name,
vote: votersByUid[link.source].vote,
isDelegated: votersByUid[link.source].isDelegated,
})
link.target = nodes[link.target] || (nodes[link.target] = {
name: link.target,
full_name: votersByUid[link.target].full_name,
vote: votersByUid[link.target].vote,
isDelegated: votersByUid[link.target].isDelegated,
})
})
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(60)
.charge(-300)
.on('tick', tick) // eslint-disable-line no-use-before-define
.start()
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height)
// Delegation arrows
svg.append('defs').selectAll('marker')
.data(['delegation'])
.enter()
.append('marker')
.attr('id', function (d) { return d })
.attr('viewBox', '0 -5 10 10')
.attr('refX', 20)
.attr('refY', -1.5)
.attr('markerWidth', 6)
.attr('markerHeight', 6)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M0,-5L10,0L0,5')
.attr('class', function () { return 'arrow' })
// Delegation lines
var path = svg.append('g').selectAll('path')
.data(force.links())
.enter()
.append('path')
.attr('class', 'link')
.attr('marker-end', function (d) { return 'url(#' + d.type + ')' })
// Vote nodes
var circle = svg.append('g').selectAll('circle')
.data(force.nodes())
.enter()
.append('circle')
.attr('r', 10)
.attr('class', function (d) { return 'vote ' + d.vote })
.on('click', function (d) { clickVoter(d.name) }) // eslint-disable-line no-use-before-define
.call(force.drag)
// Background rectangle for names
var rect = svg.append('g').selectAll('rect')
.data(force.nodes())
.enter()
.append('rect')
.attr('x', 13)
.attr('y', '-0.4em')
.attr('width', function (d) { return d.full_name.length * 5 })
.attr('height', 11)
.classed('name-labels', true)
// Name labels
var text = svg.append('g').selectAll('text')
.data(force.nodes())
.enter()
.append('text')
.attr('x', 13)
.attr('y', '.31em')
.text(function (d) { return d.full_name })
function tick() {
path.attr('d', function linkArc(d) {
var dx = d.target.x - d.source.x
var dy = d.target.y - d.source.y
var dr = Math.sqrt(dx * dx + dy * dy) // eslint-disable-line no-mixed-operators
return 'M' + d.source.x + ',' + d.source.y + 'A' + dr + ',' + dr + ' 0 0,1 ' + d.target.x + ',' + d.target.y
})
circle.attr('transform', function (d) { return 'translate(' + d.x + ',' + d.y + ')' })
text.attr('transform', function (d) { return 'translate(' + d.x + ',' + d.y + ')' })
rect.attr('transform', function (d) { return 'translate(' + d.x + ',' + d.y + ')' })
}
var generateRandomVotes = require('./generate-random-votes.js')
// declare cycleState so resolveIndividualsPosition has access
var cycleState
// Given a voter and the record of all votes,
// return that individual's voter position (recursive)
function resolveIndividualsPosition(voter, votesByVoterUid) {
// Did the voter explicitly vote?
if (votesByVoterUid.hasOwnProperty(voter.uid)) {
return votesByVoterUid[voter.uid].position
}
// Protect against endless cycle of no-show votes
cycleState.hare = votersByUid[cycleState.hare.delegate]
if (!votesByVoterUid.hasOwnProperty(cycleState.hare.uid)) {
cycleState.hare = votersByUid[cycleState.hare.delegate]
if (!votesByVoterUid.hasOwnProperty(cycleState.hare.uid)) {
cycleState.tortoise = votersByUid[cycleState.tortoise.delegate]
if (cycleState.hare === cycleState.tortoise) {
return 'no_vote'
}
}
}
// Otherwise inherit their delegate's position
var delegate = votersByUid[voter.delegate]
return resolveIndividualsPosition(delegate, votesByVoterUid)
}
function tallyVotes(votesByVoterUid) {
var bill = {
uid: 'exampleItem',
name: 'Example Item',
author: 'e', // voter_uid of 'Eva Ernst'
body: '',
date_introduced: new Date('Mon Sep 12 2016 04:34:21 GMT-0700 (PDT)'),
date_of_vote: new Date('Fri Sep 16 2016 17:00:00 GMT-0700 (PDT)'),
votes_yea: 0, // these tally values all default to 0
votes_yea_from_delegate: 0,
votes_nay: 0,
votes_nay_from_delegate: 0,
votes_no_vote: 0,
}
// Tally up the votes by iterating through each voter
voters.forEach(function (voter) {
// reset cycleState to implement Floyd's Cycle-Finding Algorithm
cycleState = {
tortoise: voter,
hare: voter,
}
var position = resolveIndividualsPosition(voter, votesByVoterUid)
var isDelegated = !votesByVoterUid.hasOwnProperty(voter.uid) && position !== 'no_vote'
// Increment tally counter for the appropriate key
var tallyKey = 'votes_' + position
if (isDelegated) {
tallyKey += '_from_delegate'
}
bill[tallyKey]++
// console.log(voter.full_name, tallyKey)
// Update the node
nodes[voter.uid].vote = position
nodes[voter.uid].isDelegated = isDelegated
})
circle
.data(force.nodes())
.attr('class', function (d) { return 'vote ' + d.vote + (d.isDelegated ? ' isDelegated' : '') })
var totalYea = bill.votes_yea + bill.votes_yea_from_delegate
var totalNay = bill.votes_nay + bill.votes_nay_from_delegate
var outcome = 'ties'
if (totalYea > totalNay) {
outcome = 'passes'
}
if (totalYea < totalNay) {
outcome = 'fails'
}
document.getElementById('yea-count').innerText = totalYea
document.getElementById('nay-count').innerText = totalNay
document.getElementById('outcome').innerText = outcome
document.getElementById('outcome').className = outcome
path
.data(force.links())
.attr('class', function (d) { return 'link' + (d.source.isDelegated ? ' isDelegated' : '') })
}
var votes
var votesByVoterUid = {}
document.getElementById('simulate').onclick = function () {
// clear existing votes
voters.forEach(function (voter) {
nodes[voter.uid].vote = undefined
})
votes = generateRandomVotes(voters)
votesByVoterUid = _.keyBy(votes, 'voter_uid') // Create index for quick lookups
tallyVotes(votesByVoterUid)
ga('send', 'event', 'user action', 'simulate button', 'clicked simulate button')
}
function clickVoter(voterUid) {
var positions = ['yea', 'nay', 'no_vote']
var newPosition
if (!votesByVoterUid[voterUid]) {
newPosition = 'yea'
votesByVoterUid[voterUid] = {
voter_uid: voterUid,
}
} else {
var oldPosition = votesByVoterUid[voterUid].position
newPosition = positions[positions.indexOf(oldPosition) + 1]
}
if (newPosition === 'no_vote') {
delete votesByVoterUid[voterUid]
} else {
votesByVoterUid[voterUid].position = newPosition
}
tallyVotes(votesByVoterUid)
ga('send', 'event', 'user action', 'clicked voter', 'clicked voter node')
}
// Start simulated
// document.getElementById('simulate').click()