2022-04-28 18:40:19 +03:00
|
|
|
/* globals Chart */
|
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import moment from 'moment';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
2022-05-09 13:25:38 +03:00
|
|
|
const DATE_FORMAT = 'D MMM, YYYY';
|
2022-04-20 15:43:11 +03:00
|
|
|
|
2022-04-28 18:40:19 +03:00
|
|
|
// custom ChartJS draw function
|
|
|
|
Chart.defaults.hoverBar = Chart.defaults.bar;
|
|
|
|
Chart.controllers.hoverBar = Chart.controllers.bar.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],
|
|
|
|
ctx = this.chart.ctx,
|
|
|
|
x = activePoint.tooltipPosition().x,
|
|
|
|
topY = this.chart.legend.bottom,
|
|
|
|
bottomY = this.chart.chartArea.bottom;
|
|
|
|
|
|
|
|
// draw line
|
|
|
|
ctx.save();
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(x, topY);
|
|
|
|
ctx.lineTo(x, bottomY);
|
|
|
|
ctx.setLineDash([3, 4]);
|
|
|
|
ctx.lineWidth = 1;
|
|
|
|
ctx.strokeStyle = '#7C8B9A';
|
|
|
|
ctx.stroke();
|
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
export default class PaidBreakdown extends Component {
|
|
|
|
@service dashboardStats;
|
|
|
|
@service feature;
|
|
|
|
|
|
|
|
@action
|
|
|
|
loadCharts() {
|
2022-04-27 17:11:01 +03:00
|
|
|
this.dashboardStats.loadSubscriptionCountStats();
|
2022-04-20 15:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get loading() {
|
2022-04-27 17:11:01 +03:00
|
|
|
return this.dashboardStats.subscriptionCountStats === null;
|
2022-04-20 15:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get chartTitle() {
|
|
|
|
return 'Paid subscribers';
|
|
|
|
}
|
|
|
|
|
|
|
|
get chartType() {
|
2022-04-28 18:40:19 +03:00
|
|
|
return 'hoverBar';
|
2022-04-20 15:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get chartData() {
|
2022-04-27 17:11:01 +03:00
|
|
|
const stats = this.dashboardStats.filledSubscriptionCountStats;
|
2022-04-20 15:43:11 +03:00
|
|
|
const labels = stats.map(stat => stat.date);
|
2022-04-27 17:11:01 +03:00
|
|
|
const newData = stats.map(stat => stat.positiveDelta);
|
|
|
|
const canceledData = stats.map(stat => -stat.negativeDelta);
|
2022-05-10 18:40:28 +03:00
|
|
|
let barThickness = 5;
|
|
|
|
|
2022-05-11 13:20:20 +03:00
|
|
|
if (newData.length > 30 + 1) {
|
2022-05-10 18:40:28 +03:00
|
|
|
barThickness = 2;
|
2022-05-11 13:20:20 +03:00
|
|
|
} else if (newData.length > 90 + 1) {
|
2022-05-10 18:40:28 +03:00
|
|
|
barThickness = 1;
|
|
|
|
}
|
2022-04-20 15:43:11 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
data: newData,
|
|
|
|
backgroundColor: '#8E42FF',
|
|
|
|
cubicInterpolationMode: 'monotone',
|
|
|
|
barThickness: barThickness,
|
|
|
|
minBarLength: 3
|
|
|
|
}, {
|
|
|
|
data: canceledData,
|
|
|
|
backgroundColor: '#FB76B4',
|
|
|
|
cubicInterpolationMode: 'monotone',
|
|
|
|
barThickness: barThickness,
|
|
|
|
minBarLength: 3
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get chartOptions() {
|
2022-04-26 20:54:52 +03:00
|
|
|
const barColor = this.feature.nightShift ? 'rgba(200, 204, 217, 0.25)' : 'rgba(200, 204, 217, 0.65)';
|
2022-04-20 15:43:11 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
responsive: true,
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
title: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
2022-04-22 16:49:03 +03:00
|
|
|
layout: {
|
|
|
|
padding: {
|
2022-04-27 16:40:38 +03:00
|
|
|
top: 24,
|
2022-04-22 16:49:03 +03:00
|
|
|
bottom: 0,
|
2022-04-27 16:40:38 +03:00
|
|
|
left: 0,
|
|
|
|
right: 0
|
2022-04-22 16:49:03 +03:00
|
|
|
}
|
|
|
|
},
|
2022-04-20 15:43:11 +03:00
|
|
|
hover: {
|
|
|
|
onHover: function (e) {
|
|
|
|
e.target.style.cursor = 'pointer';
|
|
|
|
}
|
|
|
|
},
|
2022-04-26 16:52:46 +03:00
|
|
|
animation: {
|
|
|
|
duration: 0
|
|
|
|
},
|
|
|
|
responsiveAnimationDuration: 0,
|
2022-04-20 15:43:11 +03:00
|
|
|
tooltips: {
|
2022-04-26 20:54:52 +03:00
|
|
|
enabled: false,
|
2022-04-20 15:43:11 +03:00
|
|
|
intersect: false,
|
|
|
|
mode: 'index',
|
2022-04-26 20:54:52 +03:00
|
|
|
custom: function (tooltip) {
|
|
|
|
// get tooltip element
|
|
|
|
const tooltipEl = document.getElementById('gh-dashboard5-breakdown-tooltip');
|
2022-05-11 11:51:32 +03:00
|
|
|
const tooltipWidth = tooltipEl.offsetWidth;
|
2022-04-20 15:43:11 +03:00
|
|
|
|
2022-04-26 20:54:52 +03:00
|
|
|
// only show tooltip when active
|
|
|
|
if (tooltip.opacity === 0) {
|
|
|
|
tooltipEl.style.display = 'none';
|
|
|
|
tooltipEl.style.opacity = 0;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 15:43:11 +03:00
|
|
|
|
2022-04-26 20:54:52 +03:00
|
|
|
// update tooltip styles
|
|
|
|
tooltipEl.style.display = 'block';
|
|
|
|
tooltipEl.style.opacity = 1;
|
|
|
|
tooltipEl.style.position = 'absolute';
|
2022-05-11 11:51:32 +03:00
|
|
|
tooltipEl.style.left = tooltip.x - (tooltipWidth / 2) + 'px';
|
2022-04-28 18:40:19 +03:00
|
|
|
tooltipEl.style.top = '70px';
|
2022-04-26 20:54:52 +03:00
|
|
|
},
|
|
|
|
callbacks: {
|
|
|
|
label: (tooltipItems, data) => {
|
2022-04-28 18:40:19 +03:00
|
|
|
// new data
|
|
|
|
let newValue = parseInt(data.datasets[0].data[tooltipItems.index].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
|
2022-05-11 08:54:36 +03:00
|
|
|
document.querySelector('#gh-dashboard5-breakdown-tooltip .gh-dashboard5-tooltip-value-1 .value').innerHTML = `${newValue}`;
|
2022-04-20 15:43:11 +03:00
|
|
|
|
2022-04-28 18:40:19 +03:00
|
|
|
// canceld data
|
|
|
|
let canceledValue = Math.abs(parseInt(data.datasets[1].data[tooltipItems.index].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')));
|
2022-05-11 08:54:36 +03:00
|
|
|
document.querySelector('#gh-dashboard5-breakdown-tooltip .gh-dashboard5-tooltip-value-2 .value').innerHTML = `${canceledValue}`;
|
2022-04-20 15:43:11 +03:00
|
|
|
},
|
|
|
|
title: (tooltipItems) => {
|
2022-04-26 20:54:52 +03:00
|
|
|
const value = moment(tooltipItems[0].xLabel).format(DATE_FORMAT);
|
|
|
|
document.querySelector('#gh-dashboard5-breakdown-tooltip .gh-dashboard5-tooltip-label').innerHTML = value;
|
2022-04-20 15:43:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
|
|
|
offset: false,
|
|
|
|
gridLines: {
|
|
|
|
drawTicks: false,
|
|
|
|
display: true,
|
|
|
|
drawBorder: false,
|
|
|
|
color: 'rgba(255, 255, 255, 0.1)',
|
|
|
|
lineWidth: 0,
|
|
|
|
zeroLineColor: barColor,
|
|
|
|
zeroLineWidth: 1
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: false,
|
|
|
|
fontColor: '#7C8B9A',
|
|
|
|
padding: 8,
|
|
|
|
precision: 0
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
xAxes: [{
|
|
|
|
offset: true,
|
|
|
|
stacked: true,
|
|
|
|
gridLines: {
|
|
|
|
color: barColor,
|
|
|
|
borderDash: [4,4],
|
|
|
|
display: false,
|
|
|
|
drawBorder: false,
|
|
|
|
drawTicks: false,
|
|
|
|
zeroLineWidth: 1,
|
|
|
|
zeroLineColor: barColor,
|
|
|
|
zeroLineBorderDash: [4,4]
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: false
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|