Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Sankey Visualization of Rank Choice Voting #71

Merged
merged 7 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commands/slash/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func Vote() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discor
Data: &discordgo.InteractionResponseData{
Content: func() string {
response := fmt.Sprintf("Voting submitted for: **%s**\n", title)
response += "Results:\n"
response += "Ranking:\n"
for _, option := range ranking {
response += fmt.Sprintf("- **%s**\n", option)
}
Expand Down
127 changes: 84 additions & 43 deletions structs/rankChoiceVote.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,64 +108,105 @@ func (r RankChoiceVote) String() string {
// ConvertToSanKeyRows converts a map of rounds to a SanKeyRowss
func (r RankChoiceVote) HTML() string {
HTML := `
<!DOCTYPE html>
<html>
<head>
<title>%s</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-image: url("https://ritsec.club/assets/images/bg-dark.svg");
text-align: center;
}
.header {
background-color: #0d0d0f;
color: white;
padding: 10px 0;
margin-bottom: 20px;
}
.container {
width: 1000px;
margin: 20px auto;
margin-top: 150px;
padding: 30px;
color: white;
background-color: #222222;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>

<div class="header">
<h1>%s</h1>
</div>

<div class="container">
<h1>Winner: %s</h1>
<div id="sankey_multiple"></div>
</div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["sankey"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'From');
data.addColumn('string', 'To');
data.addColumn('number', 'Weight');
data.addRows(%s);

var options = {
width: 1000,
sankey: {
node: {
colors: ['#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613', '#ff7613'],

label: {
color: '#ffffff',
fontSize: 14,
},
},
link: {
colors: ['#ff7613'],

},
},
};

var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple'));
chart.draw(data, options);
}
</script>

<div id="sankey_multiple" style="width: 900px; height: 300px;"></div>

<script type="text/javascript">
google.charts.load("current", {packages:["sankey"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'From');
data.addColumn('string', 'To');
data.addColumn('number', 'Weight');
data.addRows(%v);

// Set chart options
var options = {
width: 600,
};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple'));
chart.draw(data, options);
}
</script>
</body>
</html>`

var rows SanKeyRows

for i, round := range r.Rounds {
if i == 0 {
if i == len(r.Rounds)-1 {
continue
}
for _, choice := range round.Choices {
if choice.Selection == r.Eliminations[i-1] {
fromRound := Round{}
toRound := Round{}

fromRound.Choices = make([]Choice, len(r.Rounds[i-1].Choices))
copy(fromRound.Choices, r.Rounds[i-1].Choices)

toRound.Choices = make([]Choice, len(r.Rounds[i].Choices))
copy(toRound.Choices, r.Rounds[i].Choices)

rows.Rows = append(rows.Rows, createFlows(fromRound, toRound, choice.Selection, i)...)
} else {
rows.Rows = append(rows.Rows, SanKeyRow{
From: fmt.Sprintf("%v - Round %d", choice.Selection, i-1),
To: fmt.Sprintf("%v - Round %d", choice.Selection, i),
Weight: choice.Votes,
})
if choice.Selection == r.Eliminations[i] {
continue
}

rows.Rows = append(rows.Rows, SanKeyRow{
From: fmt.Sprintf("%v - Round %d", choice.Selection, i),
To: fmt.Sprintf("%v - Round %d", choice.Selection, i+1),
Weight: choice.Votes,
})
}

rows.Rows = append(rows.Rows, createFlows(r.Rounds[i], r.Rounds[i+1], r.Eliminations[i], i)...)
}

return fmt.Sprintf(HTML, rows.String())
return fmt.Sprintf(HTML, r.Title, r.Title, r.Winner, rows.String())
}

// createFlows creates the flows between rounds
Expand All @@ -177,8 +218,8 @@ func createFlows(fromRound Round, toRound Round, eliminated string, round int) [
if from.Selection == to.Selection {
if from.Votes < to.Votes {
rows = append(rows, SanKeyRow{
From: fmt.Sprintf("%v - Round %d", eliminated, round-1),
To: fmt.Sprintf("%v - Round %d", to.Selection, round),
From: fmt.Sprintf("%v - Round %d", eliminated, round),
To: fmt.Sprintf("%v - Round %d", to.Selection, round+1),
Weight: to.Votes - from.Votes,
})
}
Expand Down
Loading