2018-12-29 01:27:37 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { get, computed } from '@ember/object';
|
|
|
|
import { isArray } from '@ember/array';
|
|
|
|
import { isPresent } from '@ember/utils';
|
2017-08-11 16:39:36 +02:00
|
|
|
import moment from 'moment';
|
2016-03-17 13:32:53 +01:00
|
|
|
|
|
|
|
const addArrays = function() {
|
2017-08-11 16:39:36 +02:00
|
|
|
let args = Array.prototype.slice.call(arguments);
|
|
|
|
let basis = args.shift();
|
|
|
|
if (!isArray(basis)) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-03-17 13:32:53 +01:00
|
|
|
|
|
|
|
args.forEach(function(array) {
|
|
|
|
array.forEach(function(value, index) {
|
2016-08-02 01:55:48 +02:00
|
|
|
if (isPresent(value)) {
|
2016-03-17 13:32:53 +01:00
|
|
|
basis[index] = basis[index] + value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return basis;
|
|
|
|
};
|
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
export default Component.extend({
|
2018-12-29 01:27:37 +01:00
|
|
|
i18n: service(),
|
2016-08-12 22:04:09 +02:00
|
|
|
type: 'bar',
|
2018-12-29 01:27:37 +01:00
|
|
|
data: computed('users.[]', 'options.{[],each.title}', 'currentLocale', function() {
|
2017-07-31 16:52:27 +02:00
|
|
|
let labels = this.get('options').map((option) => {
|
2017-08-11 16:39:36 +02:00
|
|
|
let value = get(option, 'title');
|
|
|
|
if (!get(this, 'isFindADate')) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasTime = value.length > 10; // 'YYYY-MM-DD'.length === 10
|
|
|
|
let momentFormat = hasTime ? 'LLLL' : get(this, 'momentLongDayFormat');
|
|
|
|
let timezone = get(this, 'timezone');
|
|
|
|
let date = hasTime && isPresent(timezone) ? moment.tz(value, timezone) : moment(value);
|
|
|
|
date.locale(get(this, 'currentLocale'));
|
|
|
|
return date.format(momentFormat);
|
2017-07-31 16:52:27 +02:00
|
|
|
});
|
2016-03-20 15:30:54 +01:00
|
|
|
|
2016-03-17 13:32:53 +01:00
|
|
|
let datasets = [];
|
2017-08-11 16:39:36 +02:00
|
|
|
let participants = this.get('users.length');
|
2016-08-12 22:04:09 +02:00
|
|
|
|
2017-08-11 16:39:36 +02:00
|
|
|
let yes = this.get('users').map((user) => {
|
2016-08-12 22:04:09 +02:00
|
|
|
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') {
|
2017-08-11 16:39:36 +02:00
|
|
|
let maybe = this.get('users').map((user) => {
|
2016-03-17 13:32:53 +01:00
|
|
|
return user.get('selections').map((selection) => {
|
|
|
|
return selection.get('type') === 'maybe' ? 1 : 0;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
datasets.push({
|
|
|
|
label: this.get('i18n').t('answerTypes.maybe.label').toString(),
|
2016-08-12 22:04:09 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}),
|
2018-12-29 01:27:37 +01:00
|
|
|
chartOptions: computed(function () {
|
|
|
|
return {
|
|
|
|
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) {
|
|
|
|
let { datasets } = data;
|
|
|
|
let { datasetIndex } = tooltipItem;
|
|
|
|
let { label } = datasets[datasetIndex];
|
|
|
|
let value = tooltipItem.yLabel;
|
|
|
|
return `${label}: ${value} %`;
|
|
|
|
}
|
2016-08-12 22:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 01:27:37 +01:00
|
|
|
}),
|
2016-03-17 13:32:53 +01:00
|
|
|
});
|