Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/charts custom timespan #144

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,8 @@
"@schematics/angular:component": {
"styleext": "scss"
}
},
"cli": {
"analytics": false
}
}
}
33 changes: 17 additions & 16 deletions src/app/components/co2-bar-chart/co2-bar-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,51 @@ import { Component, OnInit } from '@angular/core';
import { ChartDataSets } from 'chart.js';
import { DataService } from 'src/app/services/data.service';
import { RouteService } from 'src/app/services/route.service';
import { CalcService } from 'src/app/services/calc.service';

@Component({
selector: 'app-co2-bar-chart',
templateUrl: './co2-bar-chart.component.html',
styleUrls: ['./co2-bar-chart.component.css']
})
export class Co2BarChartComponent implements OnInit {


constructor(private dataService: DataService, private calcService: CalcService, private routeService: RouteService) { }

co2BarChartLabels: string[] = this.getCo2BarChartLabels();

co2BarChartData: ChartDataSets[] = [
{ data: this.dataService.getAvgCo2PerDayLast6MonthsArr(this.routeService), label: 'CO₂ (avg kg/day)' },
{ data: this.calcService.getAvgCo2PerDayLast6MonthsArr(this.routeService), label: 'CO₂ (avg kg/day)' },
];

co2BarChartColors = [
{
backgroundColor: "#ff6961",
{
backgroundColor: '#ff6961',
},
];

showDetails = false;

constructor(private dataService: DataService, private routeService: RouteService) { }
showDetails = false;

ngOnInit(): void {

}

getCo2BarChartLabels(): string[] {
const labels = [];
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const today = new Date;
const today = new Date();
let index: number = (today.getMonth() - 5);
while (index < 0) {
index += 12;
}
for (let i=1; i<=6; i++) {
labels.push(monthNames[index])
}
for (let i = 1; i <= 6; i++) {
labels.push(monthNames[index]);
index = index + 1;
while (index > 11) {
index -= 12;
}
}
return labels
}
}
return labels;
}

}
38 changes: 19 additions & 19 deletions src/app/components/co2-pie-chart/co2-pie-chart.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { formatNumber } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { Alert } from 'src/app/models/alert';
import { DataService } from 'src/app/services/data.service';
import { RouteService } from 'src/app/services/route.service';
import { CalcService } from 'src/app/services/calc.service';

@Component({
selector: 'app-co2-pie-chart',
Expand All @@ -11,30 +10,33 @@ import { RouteService } from 'src/app/services/route.service';
})
export class Co2PieChartComponent implements OnInit {

constructor(private calcService: CalcService) { }

// average co2-emissions [kg] per day caused by transport (excluding air travel) by a German
avgTranspCO2German: number = 4.4;
avgTranspCO2German = 4.4;

// average co2 emissions per day in kg over the last 30 days from different means of transport
currentCo2Car = this.dataService.getCo2Last30DaysByVehicle(this.routeService, 'car')/30;
currentCo2Car = this.calcService.getCo2Last30DaysByVehicle('car') / 30;

currentCo2Motorcycle = this.calcService.getCo2Last30DaysByVehicle('motorcycle') / 30;

currentCo2Motorcycle = this.dataService.getCo2Last30DaysByVehicle(this.routeService, 'motorcycle')/30;
currentCo2PublicTransport = this.calcService.getCo2Last30DaysByVehicle('train') / 30;

// average co2 emissions per day in kg over the last 30 days from different means of transport
currentCo2Data: number[] = [this.currentCo2Car, this.currentCo2Motorcycle, this.currentCo2PublicTransport];

currentCo2PublicTransport = this.dataService.getCo2Last30DaysByVehicle(this.routeService, 'train')/30;

currentCo2Data: number[] = [this.currentCo2Car,this.currentCo2Motorcycle, this.currentCo2PublicTransport]; // average co2 emissions per day in kg over the last 30 days from different means of transport
currentCo2SumPerDay = this.calcService.getTotalCo2Last30Days() / 30;

currentCo2SumPerDay = this.dataService.getTotalCo2Last30Days(this.routeService)/30;
currentCo2RestBudget: number = this.avgTranspCO2German - this.currentCo2SumPerDay;

currentCo2RestBudget: number = this.avgTranspCO2German - this.currentCo2SumPerDay;

// Properties: General Co2-PieChart (All co2 summed up)
co2PieChartData: number[] = this.getCo2PieChartData();
co2PieChartData: number[] = this.getCo2PieChartData();

co2PieChartLabels: string[] = this.getCo2PieChartLabels();

co2PieChartColors: object[] = [
{
backgroundColor: ["#ff6961", '#f0f0f0'],
backgroundColor: ['#ff6961', '#f0f0f0'],
borderColor: '#fff',
pointBackgroundColor: '#fff',
pointBorderColor: '#fff'
Expand All @@ -48,7 +50,7 @@ export class Co2PieChartComponent implements OnInit {

co2PieChartDetailsColors: object[] = [
{
backgroundColor: ["#ff6961", "#ffb447", "#efed86", '#f0f0f0'],
backgroundColor: ['#ff6961', '#ffb447', '#efed86', '#f0f0f0'],
borderColor: '#fff',
pointBackgroundColor: '#fff',
pointBorderColor: '#fff'
Expand All @@ -60,17 +62,15 @@ export class Co2PieChartComponent implements OnInit {
type: this.getCo2AlertType(),
message: this.getCo2AlertMessage()
};

// Flag for Co2-Alert
showCo2Alert = true;

// Flag for Details-View
showDetails = false;

constructor(private dataService: DataService, private routeService: RouteService) { }

ngOnInit(): void {
}
}

getBudgetPercent(): string {
const value = this.currentCo2SumPerDay * 100 / this.avgTranspCO2German;
Expand All @@ -86,7 +86,7 @@ export class Co2PieChartComponent implements OnInit {
else {
return [this.currentCo2SumPerDay];
}
}
}

getCo2PieChartLabels(): string[] {
if (this.currentCo2RestBudget > 0) {
Expand Down
27 changes: 14 additions & 13 deletions src/app/components/km-pie-chart/km-pie-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Component, OnInit } from '@angular/core';
import { ChartOptions } from 'chart.js';
import { DataService } from 'src/app/services/data.service';
import { RouteService } from 'src/app/services/route.service';
import { CalcService } from 'src/app/services/calc.service';

@Component({
selector: 'app-km-pie-chart',
Expand All @@ -11,41 +12,41 @@ import { RouteService } from 'src/app/services/route.service';
})
export class KmPieChartComponent implements OnInit {

kmCar = this.dataService.getDistanceLast30DaysByVehicle(this.routeService, 'car');
kmCar = this.calcService.getDistanceLast30DaysByVehicle(this.routeService, 'car');

kmMotorcycle = this.dataService.getDistanceLast30DaysByVehicle(this.routeService, 'motorcycle');
kmMotorcycle = this.calcService.getDistanceLast30DaysByVehicle(this.routeService, 'motorcycle');

kmPublicTransport = this.dataService.getDistanceLast30DaysByVehicle(this.routeService, 'train');
kmPublicTransport = this.calcService.getDistanceLast30DaysByVehicle(this.routeService, 'train');

kmBicycle = this.dataService.getDistanceLast30DaysByVehicle(this.routeService, 'bicycle');
kmBicycle = this.calcService.getDistanceLast30DaysByVehicle(this.routeService, 'bicycle');

kmWalking = this.dataService.getDistanceLast30DaysByVehicle(this.routeService, 'walking');
kmWalking = this.calcService.getDistanceLast30DaysByVehicle(this.routeService, 'walking');

kmPieChartData: number[] = [this.kmCar, this.kmMotorcycle, this.kmPublicTransport, this.kmBicycle, this.kmWalking];

kmSum: number = this.kmPieChartData.reduce((pv, cv) => pv + cv, 0);

emptyPieChartData: number[] = [1];

kmPieChartLabels: string[] = ['Car', 'Motorbike', 'Public Transport', 'Bicycle', 'By Foot'];

emptyPieChartLabels: string[] = ['No routes in the last 30 days'];

kmPieChartColors: object[] = [
{
backgroundColor: ["#ff6961", "#ffb447", "#efed86", "#b0c988", "#85bb88"],
backgroundColor: ['#ff6961', '#ffb447', '#efed86', '#b0c988', '#85bb88'],
borderColor: '#fff',
pointBackgroundColor: '#fff',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#9ffcb',
pointHoverBorderColor: '#52f75d'
}
}
];

emptyPieChartColors: object[] = [
emptyPieChartColors: object[] = [
{
backgroundColor: '#f0f0f0',
}
}
];

kmPieChartOptions: ChartOptions = {
Expand All @@ -72,10 +73,10 @@ export class KmPieChartComponent implements OnInit {
plugins: {},
maintainAspectRatio: false
};

showDetails = false;

constructor(private dataService: DataService, private routeService: RouteService) { }
constructor(private dataService: DataService, private calcService: CalcService, private routeService: RouteService) { }

ngOnInit(): void {
}
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/calc.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { CalcService } from './calc.service';

describe('CalcService', () => {
let service: CalcService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(CalcService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Loading