-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathVotesChart.vue
46 lines (41 loc) · 1.06 KB
/
VotesChart.vue
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
<script>
import { Bar } from 'vue-chartjs'
const TOP_N_VOTERS_TO_SHOW_ON_CHART = 10
// Used to sort by votes value - from bigger to smaller (desc)
function sortByStartDate(nextUser, currentUser) {
const nextVoteValue = nextUser.value
const currentVoteValue = currentUser.value
return currentVoteValue - nextVoteValue
}
export default {
extends: Bar,
props: { data: Array },
watch: {
data: async function (newVal) {
const sortedVotes = Array.from(newVal).sort(sortByStartDate).slice(0, TOP_N_VOTERS_TO_SHOW_ON_CHART)
this.labels = sortedVotes.map(user => user.id)
this.values = sortedVotes.map(user => user.value)
this.renderChart({
labels: this.labels,
datasets: [
{
label: 'Pizza Lovers',
backgroundColor: '#f87979',
data: this.values,
}
]
}, {
scales: {
yAxes: [{
ticks: {
stepSize: 1,
min: 0,
max: this.values[0]
}
}]
}
})
}
}
}
</script>