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

Adding a Barchart display for voting #257

Merged
merged 5 commits into from
Oct 6, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Wire up charjs chart
  • Loading branch information
benrick committed Oct 6, 2018
commit 8db27bbb2f18b30f13b6401228f40ba87bd0312f
82 changes: 34 additions & 48 deletions src/DevChatter.Bot.Web/wwwroot/js/voting.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,51 @@
var voting = (function () {
var options = [];
var votes = [];
var chart = {};
var data = {};

var optionsAnimation = {
scales: {
xAxes: [{
ticks: {
beginAtZero: true
}
}]
}
};

var voteEnd = function (ctx) {
ctx.clearRect(0, 0, 1920, 1080);
options = [];
votes = [];
chart = {};
data = {};
};

var voteReceived = function (ctx, voteInfo) {
votes = voteInfo.voteTotals;
let voteReceived = function (ctx, voteInfo) {
// TODO: Announce the vote.
displayVoteOverlay(ctx);
};

var voteStart = function (ctx, choices) {
options = choices;
votes = Array(choices.length).fill(0);
displayChart(ctx);
var dataSet = data["datasets"][0]["data"];
voteInfo.voteTotals.forEach(function(voteCount) {
dataSet.push(voteCount);
dataSet.shift();
});
chart.update();
};

function displayChart(ctx) {
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: options,
datasets: [{
var voteStart = function (ctx, choices) {
data = {
labels: choices,
datasets: [
{
label: '# of Votes',
data: votes,
data: Array(choices.length).fill(0),
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
]
};
chart = new Chart(ctx, {
type: 'horizontalBar',
data: data,
options: optionsAnimation
});
}

function displayVoteOverlay(ctx) {
var optionDisplays = options.map((x, i) => (i + 1) + " ) " + x + " - " + (votes[i] || '0'));
ctx.clearRect(0, 0, 1920, 1080);
ctx.fillStyle = "#cbcbcb";
var height = 20 + (40 * optionDisplays.length);
ctx.font = "30px Arial";
var textWidth = Math.max(...optionDisplays.map(x => ctx.measureText(x).width));
ctx.fillRect(0, 0, textWidth + 20, height);
displayChoices(ctx, optionDisplays);
}

function displayChoices(ctx, optionDisplays) {
ctx.fillStyle = "#000000";
for (var i = 0; i < optionDisplays.length; i++) {
ctx.fillText(optionDisplays[i], 10, 40 + 40*i);
}
}
};

return {
voteStart: voteStart,
Expand Down