This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
FoodLoop-Web/src/app/dashboard/dashboard.component.ts

205 lines
5.1 KiB
TypeScript
Raw Normal View History

2017-07-05 17:08:23 +00:00
import { Directive, Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { GraphWidget } from '../widgets/graph-widget.component';
import { OrgBarSnippetComponent } from '../snippets/org-snippet-bar.component';
import { DataType } from '../shared/data-types.enum';
@Component({
templateUrl: 'dashboard.component.html'
})
export class DashboardComponent implements OnInit {
2017-09-07 12:58:51 +00:00
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,
},
];
2017-07-04 14:44:13 +00:00
constructor(
2017-08-30 11:41:28 +00:00
) {
this.shuffle = this.shuffledArray;
}
2017-08-30 11:41:28 +00:00
2017-07-04 14:44:13 +00:00
// 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
2017-08-30 11:41:28 +00:00
const index = Math.floor(Math.random() * counter);
2017-07-04 14:44:13 +00:00
// Decrease counter by 1
counter--;
2017-07-04 14:44:13 +00:00
// And swap the last element with it
2017-08-30 11:41:28 +00:00
const temp = array[counter];
2017-07-04 14:44:13 +00:00
array[counter] = array[index];
array[index] = temp;
}
this.shuffledArray = array;
resolve(true);
});
}
2017-08-30 11:41:28 +00:00
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<number> = [];
public mainChartData2: Array<number> = [];
public mainChartData3: Array<number> = [];
public mainChartData: Array<any> = [
{
data: this.mainChartData1,
label: 'Current'
},
{
data: this.mainChartData2,
label: 'Previous'
},
{
data: this.mainChartData3,
label: 'BEP'
}
];
/* tslint:disable:max-line-length */
public mainChartLabels: Array<any> = ['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<any> = [
{ // 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));
2017-05-12 16:07:18 +00:00
this.mainChartData3.push(this.random(50, 200));
}
}
}