2019-12-17 13:29:26 +03:00
|
|
|
/* global Chart */
|
|
|
|
import Component from '@ember/component';
|
2020-07-06 18:53:25 +03:00
|
|
|
import moment from 'moment';
|
2020-05-22 19:58:01 +03:00
|
|
|
import {action} from '@ember/object';
|
2019-12-17 13:29:26 +03:00
|
|
|
import {computed, get} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-22 19:58:01 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2019-12-17 13:29:26 +03:00
|
|
|
|
2020-07-06 18:53:25 +03:00
|
|
|
const DATE_FORMAT = 'D MMM YYYY';
|
|
|
|
|
2019-12-17 13:29:26 +03:00
|
|
|
export default Component.extend({
|
2020-05-22 19:58:01 +03:00
|
|
|
ajax: service(),
|
2020-05-26 19:17:35 +03:00
|
|
|
membersStats: service(),
|
2020-05-22 19:58:01 +03:00
|
|
|
|
|
|
|
// public attrs
|
|
|
|
nightShift: false,
|
|
|
|
|
|
|
|
stats: null,
|
|
|
|
chartData: null,
|
|
|
|
chartOptions: null,
|
2020-09-22 10:51:29 +03:00
|
|
|
|
|
|
|
startDateLabel: computed('membersStats.stats', function () {
|
|
|
|
if (!this.membersStats?.stats?.total_on_date) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
let firstDate = Object.keys(this.membersStats.stats.total_on_date)[0];
|
|
|
|
return moment(firstDate).format(DATE_FORMAT);
|
|
|
|
}),
|
2020-05-22 19:58:01 +03:00
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
selectedRange: computed('membersStats.days', function () {
|
2020-05-22 19:58:01 +03:00
|
|
|
const availableRanges = this.availableRanges;
|
2020-06-01 17:48:46 +03:00
|
|
|
return availableRanges.findBy('days', this.membersStats.days);
|
2019-12-17 13:29:26 +03:00
|
|
|
}),
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
availableRanges: computed(function () {
|
|
|
|
return [{
|
|
|
|
name: '30 days',
|
|
|
|
days: '30'
|
|
|
|
}, {
|
|
|
|
name: '90 days',
|
|
|
|
days: '90'
|
|
|
|
}, {
|
|
|
|
name: '365 days',
|
|
|
|
days: '365'
|
|
|
|
}, {
|
|
|
|
name: 'All time',
|
|
|
|
days: 'all-time'
|
|
|
|
}];
|
2019-12-17 13:29:26 +03:00
|
|
|
}),
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
// Lifecycle ---------------------------------------------------------------
|
|
|
|
|
2019-12-17 13:29:26 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.setChartJSDefaults();
|
|
|
|
},
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
if (this._lastNightShift !== undefined && this.nightShift !== this._lastNightShift) {
|
|
|
|
this.setChartOptions();
|
2019-12-17 13:29:26 +03:00
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
this._lastNightShift = this.nightShift;
|
2019-12-17 13:29:26 +03:00
|
|
|
},
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
// Actions -----------------------------------------------------------------
|
2019-12-17 13:29:26 +03:00
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
changeDateRange: action(function (range) {
|
2020-06-01 17:48:46 +03:00
|
|
|
this.membersStats.days = get(range, 'days');
|
2020-05-22 19:58:01 +03:00
|
|
|
this.fetchStatsTask.perform();
|
|
|
|
}),
|
2019-12-17 13:29:26 +03:00
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
// Tasks -------------------------------------------------------------------
|
|
|
|
|
|
|
|
fetchStatsTask: task(function* () {
|
2020-05-27 18:12:13 +03:00
|
|
|
this.set('stats', null);
|
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
let stats = yield this.membersStats.fetch();
|
2020-05-22 19:58:01 +03:00
|
|
|
|
2020-05-27 18:12:13 +03:00
|
|
|
if (stats) {
|
|
|
|
this.set('stats', stats);
|
2020-05-22 19:58:01 +03:00
|
|
|
|
2020-05-27 18:12:13 +03:00
|
|
|
this.setChartOptions({
|
|
|
|
rangeInDays: Object.keys(stats.total_on_date).length
|
|
|
|
});
|
2020-05-22 19:58:01 +03:00
|
|
|
|
2020-05-27 18:12:13 +03:00
|
|
|
this.setChartData({
|
|
|
|
dateLabels: Object.keys(stats.total_on_date),
|
|
|
|
dateValues: Object.values(stats.total_on_date)
|
|
|
|
});
|
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
// Internal ----------------------------------------------------------------
|
|
|
|
|
|
|
|
setChartData({dateLabels, dateValues}) {
|
|
|
|
this.set('chartData', {
|
|
|
|
labels: dateLabels,
|
|
|
|
datasets: [{
|
|
|
|
label: 'Total members',
|
|
|
|
cubicInterpolationMode: 'monotone',
|
|
|
|
data: dateValues,
|
|
|
|
fill: false,
|
2021-02-03 22:08:01 +03:00
|
|
|
backgroundColor: 'rgba(181,255,24,1.0)',
|
2020-05-22 19:58:01 +03:00
|
|
|
pointRadius: 0,
|
|
|
|
pointHitRadius: 10,
|
2021-02-03 22:08:01 +03:00
|
|
|
borderColor: 'rgba(181,255,24,1.0)',
|
|
|
|
borderJoinStyle: 'miter'
|
2020-05-22 19:58:01 +03:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setChartOptions({rangeInDays}) {
|
|
|
|
let maxTicksAllowed = this.getTicksForRange(rangeInDays);
|
|
|
|
|
|
|
|
this.setChartJSDefaults();
|
|
|
|
|
|
|
|
this.set('chartOptions', {
|
|
|
|
responsive: true,
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
top: 5, // Needed otherwise the top dot is cut
|
|
|
|
right: 10,
|
|
|
|
bottom: 5,
|
|
|
|
left: 10
|
2019-12-17 13:29:26 +03:00
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
},
|
|
|
|
title: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
intersect: false,
|
|
|
|
mode: 'index',
|
|
|
|
displayColors: false,
|
|
|
|
backgroundColor: '#343f44',
|
|
|
|
xPadding: 7,
|
|
|
|
yPadding: 7,
|
|
|
|
cornerRadius: 5,
|
|
|
|
caretSize: 7,
|
|
|
|
caretPadding: 5,
|
|
|
|
bodyFontSize: 13,
|
|
|
|
titleFontStyle: 'normal',
|
|
|
|
titleFontColor: 'rgba(255, 255, 255, 0.7)',
|
2020-07-06 18:11:52 +03:00
|
|
|
titleMarginBottom: 4,
|
|
|
|
callbacks: {
|
|
|
|
label: function (tooltipItems, data) {
|
|
|
|
return data.datasets[0].label + `: ` + data.datasets[0].data[tooltipItems.index].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
2020-07-06 18:53:25 +03:00
|
|
|
},
|
|
|
|
title: function (tooltipItems) {
|
|
|
|
return moment(tooltipItems[0].xLabel).format(DATE_FORMAT);
|
2020-07-06 18:11:52 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
},
|
|
|
|
hover: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
|
|
|
animationDuration: 120
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
xAxes: [{
|
|
|
|
labelString: 'Date',
|
|
|
|
gridLines: {
|
|
|
|
drawTicks: false,
|
2021-02-03 22:08:01 +03:00
|
|
|
color: (this.nightShift ? '#333F44' : '#3E3F41'),
|
|
|
|
zeroLineColor: (this.nightShift ? '#333F44' : '#3E3F41')
|
2020-05-22 19:58:01 +03:00
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: false,
|
|
|
|
maxRotation: 0,
|
|
|
|
minRotation: 0,
|
|
|
|
padding: 6,
|
|
|
|
autoSkip: false,
|
|
|
|
maxTicksLimit: 10,
|
|
|
|
callback: function (value, index, values) {
|
|
|
|
let step = (values.length - 1) / (maxTicksAllowed);
|
|
|
|
let steps = [];
|
|
|
|
for (let i = 0; i < maxTicksAllowed; i++) {
|
|
|
|
steps.push(Math.round(i * step));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (index === (values.length - 1)) {
|
|
|
|
return 'Today';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (steps.includes(index)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
yAxes: [{
|
|
|
|
gridLines: {
|
|
|
|
drawTicks: false,
|
|
|
|
display: false,
|
|
|
|
drawBorder: false
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
maxTicksLimit: 5,
|
|
|
|
fontColor: '#9baeb8',
|
|
|
|
padding: 8,
|
2020-07-06 18:11:52 +03:00
|
|
|
precision: 0,
|
|
|
|
callback: function (value) {
|
|
|
|
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
}
|
|
|
|
}]
|
2019-12-17 13:29:26 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getTicksForRange(rangeInDays) {
|
|
|
|
if (rangeInDays <= 30) {
|
|
|
|
return 6;
|
|
|
|
} else if (rangeInDays <= 90) {
|
|
|
|
return 18;
|
|
|
|
} else {
|
|
|
|
return 24;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
setChartJSDefaults() {
|
|
|
|
Chart.defaults.LineWithLine = Chart.defaults.line;
|
|
|
|
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
|
|
|
|
draw: function (ease) {
|
|
|
|
Chart.controllers.line.prototype.draw.call(this, ease);
|
|
|
|
|
|
|
|
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
|
|
|
|
let activePoint = this.chart.tooltip._active[0];
|
|
|
|
let ctx = this.chart.ctx;
|
|
|
|
let x = activePoint.tooltipPosition().x;
|
|
|
|
let topY = this.chart.scales['y-axis-0'].top;
|
|
|
|
let bottomY = this.chart.scales['y-axis-0'].bottom;
|
|
|
|
|
|
|
|
// draw line
|
|
|
|
ctx.save();
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(x, topY);
|
|
|
|
ctx.lineTo(x, bottomY);
|
|
|
|
ctx.lineWidth = 1;
|
2021-02-03 22:08:01 +03:00
|
|
|
ctx.strokeStyle = (this.nightShift ? 'rgba(62, 176, 239, 0.65)' : 'rgba(62, 176, 239, 0.1)');
|
2020-05-22 19:58:01 +03:00
|
|
|
ctx.stroke();
|
|
|
|
ctx.restore();
|
2019-12-17 13:29:26 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 19:58:01 +03:00
|
|
|
});
|
2019-12-17 13:29:26 +03:00
|
|
|
}
|
|
|
|
});
|