decide.nolog.cz/app/components/poll-evaluation-chart.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-03-17 13:32:53 +01:00
import Ember from 'ember';
const { Component, computed, get, inject, isPresent } = Ember;
2016-03-17 13:32:53 +01:00
const addArrays = function() {
const args = Array.prototype.slice.call(arguments);
const basis = args.shift();
args.forEach(function(array) {
array.forEach(function(value, index) {
if (isPresent(value)) {
2016-03-17 13:32:53 +01:00
basis[index] = basis[index] + value;
}
});
});
return basis;
};
export default Component.extend({
i18n: inject.service(),
type: 'bar',
data: computed('users.[]', 'dates.[]', 'dates.@each.formatted', 'pollOptions.[]', 'pollOptions.@each.title', 'i18n.locale', function() {
2016-03-20 15:30:54 +01:00
let labels;
if (this.get('isFindADate')) {
labels = this.get('dates').map((date) => {
return get(date, 'formatted');
2016-03-20 15:30:54 +01:00
});
} else {
labels = this.get('pollOptions').map((pollOption) => {
return get(pollOption, 'title');
2016-03-20 15:30:54 +01:00
});
}
2016-03-17 13:32:53 +01:00
let datasets = [];
const participants = this.get('users.length');
const yes = this.get('users').map((user) => {
return user.get('selections').map((selection) => {
return selection.get('type') === 'yes' ? 1 : 0;
});
});
datasets.push({
label: this.get('i18n').t('answerTypes.yes.label').toString(),
backgroundColor: 'rgba(151,187,205,0.5)',
borderColor: 'rgba(151,187,205,0.8)',
hoverBackgroundColor: 'rgba(151,187,205,0.75)',
hoverBorderColor: 'rgba(151,187,205,1)',
data: addArrays.apply(this, yes).map((value) => Math.round(value / participants * 100))
});
2016-03-17 13:32:53 +01:00
if (this.get('answerType') === 'YesNoMaybe') {
const maybe = this.get('users').map((user) => {
return user.get('selections').map((selection) => {
return selection.get('type') === 'maybe' ? 1 : 0;
});
});
datasets.push({
label: this.get('i18n').t('answerTypes.maybe.label').toString(),
backgroundColor: 'rgba(220,220,220,0.5)',
borderColor: 'rgba(220,220,220,0.8)',
hoverBackgroundColor: 'rgba(220,220,220,0.75)',
hoverBorderColor: 'rgba(220,220,220,1)',
2016-03-17 13:32:53 +01:00
data: addArrays.apply(this, maybe).map((value) => Math.round(value / participants * 100))
});
}
return {
datasets,
labels
};
}),
options: {
legend: {
display: false
},
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true,
ticks: {
callback(value) {
return `${value} %`;
},
max: 100,
min: 0
}
}]
},
tooltips: {
mode: 'label',
callbacks: {
label(tooltipItem, data) {
const { datasets } = data;
const { datasetIndex } = tooltipItem;
const { label } = datasets[datasetIndex];
const value = tooltipItem.yLabel;
return `${label}: ${value} %`;
}
}
}
}
2016-03-17 13:32:53 +01:00
});