import { Directive, Component, OnInit } from '@angular/core'; import { Http, Response } from '@angular/http'; import { ApiService } from '../providers/api-service'; import { Router } from '@angular/router'; import { GraphWidget } from '../widgets/graph-widget.component'; import { DataType } from '../shared/data-types.enum'; @Component({ templateUrl: 'dashboard.component.html' }) export class DashboardComponent implements OnInit { // Snippet data public customersThisMonth: number; public moneySpentThisMonth: number; public pointsTotal: number; public averageTransactionToday: number; shuffledArray: any; public widgetList = [ { type: 'graph', name: 'customers_last_7_days', icon: 'icon-people', title: 'Customers Last 7 Days', }, { type: 'graph', name: 'customers_last_30_days', icon: 'icon-people', title: 'Customers Last 30 Days', }, { type: 'graph', name: 'sales_last_7_days', icon: 'icon-diamond', title: 'Sales Last 7 Days', dataType: DataType.currency, }, { type: 'graph', name: 'sales_last_30_days', icon: 'icon-diamond', title: 'Sales Last 30 Days', dataType: DataType.currency, }, { type: 'graph', name: 'purchases_last_7_days', title: 'Purchases Last 7 Days', dataType: DataType.currency, }, { type: 'graph', name: 'purchases_last_30_days', title: 'Purchases Last 30 Days', dataType: DataType.currency, }, ]; constructor( private http: Http, private api: ApiService, ) { this.shuffle = this.shuffledArray; this.api.breadcrumb_data(undefined) .subscribe( result => { console.log(result); this.customersThisMonth = result.customersthismonth; this.moneySpentThisMonth = result.moneyspentthismonth; this.pointsTotal = result.pointstotal; this.averageTransactionToday = result.averagetransactiontoday; } ); } // Fisher-Yates shuffle function public shuffle(array) { return new Promise(resolve => { let counter = array.length; // While there are elements in the array while (counter > 0) { // Pick a random index const index = Math.floor(Math.random() * counter); // Decrease counter by 1 counter--; // And swap the last element with it const temp = array[counter]; array[counter] = array[index]; array[index] = temp; } this.shuffledArray = array; resolve(true); }); } public brandSuccess = '#4dbd74'; public brandInfo = '#63c2de'; public brandDanger = '#f86c6b'; // convert Hex to RGBA public convertHex(hex: string, opacity: number) { hex = hex.replace('#', ''); const r = parseInt(hex.substring(0, 2), 16); const g = parseInt(hex.substring(2, 4), 16); const b = parseInt(hex.substring(4, 6), 16); const rgba = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + opacity / 100 + ')'; return rgba; } // events public chartClicked(e: any): void { console.log(e); } public chartHovered(e: any): void { console.log(e); } // mainChart public random(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1) + min); } public mainChartElements = 27; public mainChartData1: Array = []; public mainChartData2: Array = []; public mainChartData3: Array = []; public mainChartData: Array = [ { data: this.mainChartData1, label: 'Current' }, { data: this.mainChartData2, label: 'Previous' }, { data: this.mainChartData3, label: 'BEP' } ]; /* tslint:disable:max-line-length */ public mainChartLabels: Array = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Thursday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; /* tslint:enable:max-line-length */ public mainChartOptions: any = { responsive: true, maintainAspectRatio: false, scales: { xAxes: [{ gridLines: { drawOnChartArea: false, }, ticks: { callback: function(value: any) { return value.charAt(0); } } }], yAxes: [{ ticks: { beginAtZero: true, maxTicksLimit: 5, stepSize: Math.ceil(250 / 5), max: 250 } }] }, elements: { line: { borderWidth: 2 }, point: { radius: 0, hitRadius: 10, hoverRadius: 4, hoverBorderWidth: 3, } }, legend: { display: false } }; public mainChartColours: Array = [ { // brandInfo backgroundColor: this.convertHex(this.brandInfo, 10), borderColor: this.brandInfo, pointHoverBackgroundColor: '#fff' }, { // brandSuccess backgroundColor: 'transparent', borderColor: this.brandSuccess, pointHoverBackgroundColor: '#fff' }, { // brandDanger backgroundColor: 'transparent', borderColor: this.brandDanger, pointHoverBackgroundColor: '#fff', borderWidth: 1, borderDash: [8, 5] } ]; public mainChartLegend = false; public mainChartType = 'line'; ngOnInit(): void { // generate random values for mainChart for (let i = 0; i <= this.mainChartElements; i++) { this.mainChartData1.push(this.random(50, 200)); this.mainChartData2.push(this.random(80, 100)); this.mainChartData3.push(this.random(50, 200)); } } }