import Component from '@glimmer/component'; import {action} from '@ember/object'; import {inject as service} from '@ember/service'; import {tracked} from '@glimmer/tracking'; const MODE_OPTIONS = [{ name: 'Cadence', value: 'cadence' }, { name: 'Tiers', value: 'tiers' }]; export default class ChartPaidMix extends Component { @service dashboardStats; /** * Call this method when you need to fetch new data from the server. In this component, it will get called * when the days parameter changes and on initialisation. */ @action loadCharts() { // The dashboard stats service will take care or reusing and limiting API-requests between charts if (this.mode === 'cadence') { this.dashboardStats.loadPaidMembersByCadence(); } else { this.dashboardStats.loadPaidMembersByTier(); } } @tracked mode = 'cadence'; modeOptions = MODE_OPTIONS; get selectedModeOption() { return this.modeOptions.find(option => option.value === this.mode); } @action onSwitchMode(selected) { this.mode = selected.value; if (this.loading) { // We don't have the data yet for the newly selected mode this.loadCharts(); } } get loading() { if (this.mode === 'cadence') { return this.dashboardStats.paidMembersByCadence === null; } return this.dashboardStats.paidMembersByTier === null; } get chartType() { return 'pie'; } get chartData() { if (this.mode === 'cadence') { return { labels: ['Monthly', 'Annual'], datasets: [{ data: [this.dashboardStats.paidMembersByCadence.monthly, this.dashboardStats.paidMembersByCadence.annual], fill: false, backgroundColor: ['#14b8ff'], tension: 0.1 }] }; } const labels = this.dashboardStats.paidMembersByTier.map(stat => stat.tier.name); const data = this.dashboardStats.paidMembersByTier.map(stat => stat.members); return { labels, datasets: [{ data, fill: false, backgroundColor: ['#14b8ff'], tension: 0.1 }] }; } get chartOptions() { return { legend: { display: false } }; } get chartHeight() { return 100; } }