This repository has been archived on 2023-08-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
FoodLoop-Web/src/app/dashboard/more-graphs-and-tables.component.ts

390 lines
10 KiB
TypeScript
Raw Normal View History

2019-07-15 12:38:06 +01:00
import {Component, OnInit, Input, Output, EventEmitter, ViewChild, ElementRef} from '@angular/core';
2019-07-15 01:17:01 +01:00
import {ApiService} from '../providers/api-service';
2019-07-15 12:38:06 +01:00
import {BaseChartDirective, Color} from 'ng2-charts';
2019-07-15 01:17:01 +01:00
import {CurrencyPipe} from '@angular/common';
2019-07-15 12:38:06 +01:00
import {ChartType} from "chart.js";
2019-08-14 12:07:05 +01:00
import * as moment from 'moment';
import { NgModel } from '@angular/forms';
@Component({
templateUrl: 'more-graphs-and-tables.component.html',
})
2019-07-11 15:18:33 +01:00
export class MoreStuffComponent implements OnInit {
@Output() public onClick = new EventEmitter();
@Input() public categories: any;
2019-08-16 16:58:35 +01:00
lineChartBegin: any;
lineChartEnd: any;
2019-08-14 12:07:05 +01:00
bubbleChartBegin: any;
bubbleChartEnd: any;
cached_graph_data: any;
isBubbleChartLoaded:boolean = false;
isLineChartLoaded:boolean = false;
isSupplierChartLoaded:boolean = false;
constructor(
private api: ApiService,
private currencyPipe: CurrencyPipe,
2019-07-15 01:17:01 +01:00
) {
2019-08-14 12:07:05 +01:00
this.bubbleChartBegin = moment().format('YYYY-MM-DD');
this.bubbleChartEnd = moment().format('YYYY-MM-DD');
2019-08-16 12:27:05 +01:00
this.lineChartBegin = moment().format('YYYY-MM-DD');
this.lineChartEnd = moment().format('YYYY-MM-DD');
2019-07-15 01:17:01 +01:00
}
ngOnInit(): void {
this.loadYearSpend();
this.loadSupplierBubble(false, ('0'), ('0'));
2019-07-15 12:38:06 +01:00
this.loadSupplierHistory();
}
2019-07-15 01:17:01 +01:00
public showLegend = true;
public sampleColours: Color[] = [
{
backgroundColor: [
'red',
'green',
2019-08-20 16:47:20 +01:00
'#52afed',
2019-07-15 01:17:01 +01:00
'purple',
'yellow',
'brown',
'magenta',
'cyan',
'orange',
'pink'
]
}
];
2019-07-15 01:17:01 +01:00
/*
* Supplier Bubble Chart Setup
*/
2019-07-15 12:38:06 +01:00
private formatGraphData(passed_graph_data: any, useRange : boolean, start_range: string, end_range: string) : any[] {
let graph_data = [];
if (useRange == true) {
passed_graph_data.data.map(item=> {
2019-08-16 11:32:45 +01:00
let is_item_in_range = (new Date(item.date.substring(0, 10)) >= new Date(start_range) && new Date(item.date.substring(0, 10)) <= new Date(end_range));
2019-08-20 16:47:20 +01:00
if (is_item_in_range) {
graph_data.push({
2019-08-16 11:32:45 +01:00
t: new Date(item.date.substring(0, 10)),
r: item.value > 1000000 ? (item.value / 200000) : (item.value / 100000) + 5,
supplier: item.seller,
y: item.count,
value: item.value,
count: item.count,
2019-07-15 12:38:06 +01:00
});
}
});
return graph_data;
} else {
passed_graph_data.data.map(item => {
graph_data.push({
t: item.date,
2019-08-20 16:47:20 +01:00
r: item.value > 1000000 ? (item.value / 200000) : (item.value / 100000) + 5,
supplier: item.seller,
y: item.count,
value: item.value,
count: item.count,
});
});
2019-08-15 12:09:46 +01:00
return graph_data;
}
}
private loadSupplierBubble(useRange: boolean, start_range : string, end_range : string) {
this.isBubbleChartLoaded = false;
// console.log("Fetching data for bubble chart... this will take a while. custom range = " + useRange);
2019-08-16 10:51:47 +01:00
var is_cached = false;
try {
2019-08-16 12:13:43 +01:00
if (this.cached_graph_data) {
2019-08-16 10:51:47 +01:00
is_cached = true;
}
} catch {
// not cached
}
if (is_cached) {
2019-08-16 15:24:49 +01:00
// console.log("Using cached data of " + this.cached_graph_data.length + " items.");
this.supplierBubbleChartData[0].data = this.formatGraphData(this.cached_graph_data, useRange, start_range, end_range);
this.isBubbleChartLoaded = true;
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
}
else {
2019-08-16 15:24:49 +01:00
// console.log("Not using cached data.");
this.api.loadMiscUrl('organisation/external/supplier_count').subscribe(
result => {
this.cached_graph_data = result;
2019-08-16 16:58:35 +01:00
2019-08-16 10:51:47 +01:00
this.supplierBubbleChartData[0].data = this.formatGraphData(result, useRange, start_range, end_range);
2019-08-16 15:24:49 +01:00
// console.log("Graph fetched with " + this.supplierBubbleChartData[0].data.length + " items.");
this.isBubbleChartLoaded = true;
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-08-15 12:09:46 +01:00
}
)
}
2019-08-15 12:09:46 +01:00
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-07-15 12:38:06 +01:00
}
public supplierBubbleChartType: ChartType = 'bubble';
2019-07-15 01:17:01 +01:00
public supplierBubbleChartData: any[] = [
2019-07-11 15:18:33 +01:00
{
2019-07-15 12:38:06 +01:00
data: [],
label: ["Spend"],
2019-07-15 01:17:01 +01:00
borderColor: 'blue',
hoverBorderColor: 'black',
radius: 5,
2019-07-15 01:17:01 +01:00
},
2019-07-11 15:18:33 +01:00
];
2019-07-15 01:17:01 +01:00
public supplierBubbleChartLabels: string[] = [];
public supplierBubbleChartOptions: any = {
2019-07-11 15:18:33 +01:00
responsive: true,
scales: {
2019-07-15 01:17:01 +01:00
xAxes: [{
type: 'time',
time: {
unit: 'month'
},
scaleLabel:{
display:true,
labelString:'Date'
2019-07-15 01:17:01 +01:00
}
}],
yAxes: [{
scaleLabel: {
display:true,
labelString:'Number of purchases'
}
}]
2019-07-15 01:17:01 +01:00
},
tooltips: {
callbacks: {
label: (tooltip, data) => {
2019-07-15 01:17:01 +01:00
return this.bubbleTooltipCallback(tooltip, data);
},
},
},
2019-07-15 01:17:01 +01:00
};
2019-07-15 01:17:01 +01:00
private bubbleTooltipCallback(tooltipItem: any, data: any) {
let dataset = data.datasets[tooltipItem.datasetIndex];
let value = dataset.data[tooltipItem.index];
2019-07-15 12:38:06 +01:00
return `${value.supplier}: ${this.currencyPipe.transform(value.value, 'GBP', 'symbol', '1.2-2')} over ${value.count} purchases`;
}
private loadYearSpend() {
this.api.loadMiscUrl('organisation/external/year_spend').subscribe(
result => {
let value_data = [];
let count_data = [];
2019-08-16 15:24:49 +01:00
console.log("The server is UP");
2019-07-15 12:38:06 +01:00
result.data.map(item => {
value_data.push({
t: item.date,
y: item.value,
});
count_data.push({
t: item.date,
y: item.count,
});
});
this.yearSpendChartData[0].data = value_data;
this.yearSpendChartData[1].data = count_data;
}
)
}
2019-08-14 12:07:05 +01:00
bubbleChartUpdate() {
2019-08-16 15:24:49 +01:00
// console.log("start_range input box: " + this.bubbleChartBegin);
// console.log("end_range input box: " + this.bubbleChartEnd);
2019-08-15 14:51:10 +01:00
// this is called when daterange is changed
this.loadSupplierBubble(true, (this.bubbleChartBegin), (this.bubbleChartEnd));
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
/*
bubbleChartBegin: any;
bubbleChartEnd: any;
*/
2019-08-14 12:07:05 +01:00
}
2019-07-15 01:17:01 +01:00
public yearSpendChartData: any[] = [
{
2019-07-15 12:38:06 +01:00
data: [],
label: ["Value £"],
2019-07-15 01:17:01 +01:00
fill: false,
borderColor: 'red',
hoverBackgroundColor: '#ffa1b5',
2019-07-15 01:17:01 +01:00
hoverBorderColor: 'red',
yAxisID: 'y-value',
},
{
2019-07-15 12:38:06 +01:00
data: [],
2019-07-15 01:17:01 +01:00
label: ["Count"],
fill: false,
borderColor: 'blue',
hoverBackgroundColor: '#52afed',
2019-07-15 01:17:01 +01:00
hoverBorderColor: 'blue',
yAxisID: 'y-count',
},
];
public yearSpendChartOptions: any = {
responsive: true,
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'month'
},
scaleLabel: {
display:true,
labelString: 'Date'
2019-07-15 01:17:01 +01:00
}
}],
yAxes: [
{id: 'y-value', position: 'left', beginAtZero: true},
{id: 'y-count', position: 'right', beginAtZero: true},
]
},
};
public yearSpendChartColors: Color[] = [
2019-07-10 12:49:36 +01:00
{
backgroundColor: [
'#ffa1b5',
'#52afed'
2019-07-10 12:49:36 +01:00
]
}
];
2019-07-15 01:17:01 +01:00
public yearSpendChartLabels: string[] = [];
2019-07-15 12:38:06 +01:00
public yearSpendChartType: ChartType = 'line';
2019-07-15 01:17:01 +01:00
randomData() {
return Math.random();
}
2019-08-16 12:27:05 +01:00
lineChartUpdate() {
this.loadYearSpend();
2019-08-16 12:27:05 +01:00
}
2019-07-15 12:38:06 +01:00
@ViewChild('supplierChart', {read: BaseChartDirective, static: false}) supplierChart: BaseChartDirective;
private loadSupplierHistory() {
this.api.loadMiscUrl('organisation/external/supplier_history').subscribe(
result => {
let labels = [];
let year = [];
let half = [];
let quarter = [];
result.data.map(item => {
labels.push(item.name);
year.push(item.year_total);
half.push(item.half_total);
quarter.push(item.quarter_total);
});
this.supplierMonthChartData[0].data = quarter.slice(0,15);
this.supplierMonthChartData[1].data = half.slice(0,15);
this.supplierMonthChartData[2].data = year.slice(0,15);
this.supplierMonthChartLabels = labels.slice(0,15);
2019-07-15 12:38:06 +01:00
}
)
this.isSupplierChartLoaded = true;
2019-07-15 12:38:06 +01:00
}
private supplierChartNext() {
result => {
let labels = [];
let year = [];
let half = [];
let quarter = [];
result.data.map(item => {
labels.push(item.name);
year.push(item.year_total);
half.push(item.half_total);
quarter.push(item.quarter_total);
});
this.supplierMonthChartData[0].data = quarter.slice(16,30);
this.supplierMonthChartData[1].data = half.slice(16,30);
this.supplierMonthChartData[2].data = year.slice(16,30);
this.supplierMonthChartLabels = labels.slice(16,30);
}
this.isSupplierChartLoaded = true;
}
private supplierChartPrevious() {
result => {
let labels = [];
let year = [];
let half = [];
let quarter = [];
result.data.map(item => {
labels.push(item.name);
year.push(item.year_total);
half.push(item.half_total);
quarter.push(item.quarter_total);
});
this.supplierMonthChartData[0].data = quarter.slice(0,15);
this.supplierMonthChartData[1].data = half.slice(0,15);
this.supplierMonthChartData[2].data = year.slice(0,15);
this.supplierMonthChartLabels = labels.slice(0,15);
}
this.isSupplierChartLoaded = true;
}
2019-07-15 01:17:01 +01:00
public supplierMonthChartData: any[] = [
{
2019-07-15 12:38:06 +01:00
data: [],
2019-07-15 01:17:01 +01:00
label: ["3 Month"],
fill: false,
borderColor: 'red',
hoverBorderColor: 'red',
hoverBackgroundColor: 'red',
},
{
2019-07-15 12:38:06 +01:00
data: [],
2019-07-15 01:17:01 +01:00
label: ["6 Month"],
fill: false,
borderColor: 'blue',
hoverBorderColor: 'blue',
hoverBackgroundColor: 'blue',
2019-07-15 01:17:01 +01:00
},
{
2019-07-15 12:38:06 +01:00
data: [],
2019-07-15 01:17:01 +01:00
label: ["12 Month"],
fill: false,
borderColor: 'orange',
hoverBorderColor: 'orange',
hoverBackgroundColor: 'orange',
2019-07-15 01:17:01 +01:00
},
];
public supplierMonthChartOptions: any = {
2019-07-15 12:38:06 +01:00
//maintainAspectRatio: false,
2019-07-15 01:17:01 +01:00
responsive: true,
scales: {
xAxes: [{
scaleLabel: {
display:true,
labelString: 'Spend amount £'
}
}],
yAxes: [{
scaleLabel: {
display:true,
labelString: 'Supplier Names'
}
}]
2019-07-15 01:17:01 +01:00
},
};
2019-07-15 12:38:06 +01:00
public supplierMonthChartLabels: string[] = [];
public supplierMonthChartType: ChartType = 'horizontalBar';
}