Rewrote category chart.

This commit is contained in:
Alexis Lahouze 2018-06-11 00:25:01 +02:00
parent 9b9a64fb52
commit 280d5b8bb8

View File

@ -1,9 +1,10 @@
// vim: set tw=80 ts=2 sw=2 sts=2 : // vim: set tw=80 ts=2 sw=2 sts=2 :
import { Component, OnInit } from '@angular/core'; import { Component, ViewChild, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router'; import { ActivatedRoute, ParamMap } from '@angular/router';
import { Logger } from '@nsalaun/ng-logger'; import { Logger } from '@nsalaun/ng-logger';
import { jqxChartComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxchart';
import * as _ from 'underscore'; import * as _ from 'underscore';
@ -13,29 +14,69 @@ import { CategoryService } from './category.service';
@Component({ @Component({
selector: 'category-chart', selector: 'category-chart',
template: `<ngx-charts-bar-horizontal-2d template: `
[results]="data" <jqxChart #categoryChart
[xAxis]="showXAxis" [width]="'100%'"
[yAxis]="showYAxis" [height]="400"
[showXAxisLabel]="showXAxisLabel" [title]="'Categories'"
[showYAxisLabel]="showYAxisLabel" [description]="''"
barPadding="1"> [showLegend]="false"
</ngx-charts-bar-horizontal-2d>` [seriesGroups]="seriesGroups">
</jqxChart>
`
}) })
export class CategoryChartComponent implements OnInit { export class CategoryChartComponent implements OnInit {
public data; @ViewChild('categoryChart') chart: jqxChartComponent;
public showXAxis = true; public seriesGroups: any = [{
public showYAxis = true; type: 'donut',
public showXAxisLabel = false; source: [],
public showYAxisLabel = false; //showLabels: true,
series: [{
dataField: 'value',
displayText: 'category',
initialAngle: 90,
radius: 130,
innerRadius: 90,
formatSettings: { sufix: '€', decimalPlaces: 2 },
radiusDataField: 'category',
colorFunction: (value, itemIndex, series, group) => {
if(group.source[itemIndex].type === 'expenses') {
return 'tomato';
}
return 'yellowgreen';
},
opacity: 0.5
}]
}, {
type: 'donut',
source: [],
showLabels: true,
series: [{
dataField: 'value',
displayText: 'name',
initialAngle: 90,
labelRadius: 50,
radius: 85,
innerRadius: 75,
formatSettings: { sufix: '€', decimalPlaces: 2 },
colorFunction: (value, itemIndex, series, group) => {
if(group.source[itemIndex].name === 'Expenses') {
return 'tomato';
}
return 'yellowgreen';
}
}]
}];
constructor( constructor(
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
private logger: Logger, private logger: Logger,
private categoryService: CategoryService, private categoryService: CategoryService,
) { ) {
this.data = []; //this.data = [];
} }
loadData() { loadData() {
@ -47,27 +88,54 @@ export class CategoryChartComponent implements OnInit {
+accountId, +accountId,
fromDay, fromDay,
toDay toDay
).map((results: Category[]) => { ).subscribe((results: Category[]) => {
return _.sortBy(results, 'income').reverse(); let expenses = _.filter(results, function(item: Category) {
}).map((results: Category[]) => { return item.expenses < 0;
return results.map((result: Category) => { }).map(function(item: Category) {
return { return {
'name': result.category, category: item.category,
'series': [{ value: -item.expenses,
'name': 'expenses', type: 'expenses'
'value': -Number(result.expenses)
}, {
'name': 'revenues',
'value': Number(result.revenues)
}]
}; };
}); });
}).subscribe((results) => {
this.data = results; expenses = _.sortBy(expenses, 'value').reverse();
let revenues = _.filter(results, function(item: Category) {
return item.revenues > 0;
}).map(function(item: Category) {
return {
category: item.category,
value: item.revenues,
type: 'revenues'
};
});
revenues = _.sortBy(revenues, 'value');
this.seriesGroups[0].source = expenses.concat(revenues);
let totals = [
{name: 'Expenses', value: 0},
{name: 'Revenues', value: 0}
];
results.forEach(function(item: Category) {
totals[0].value -= item.expenses;
totals[1].value += item.revenues;
});
this.seriesGroups[1].source = totals;
if(this.chart && this.chart.host) {
this.chart.refresh();
}
}); });
} }
ngOnInit() { ngOnInit() {
this.activatedRoute.queryParamMap.subscribe(() => {this.loadData();}); this.activatedRoute.queryParamMap.subscribe(() => {this.loadData();});
this.loadData();
} }
} }