Change category graph type.

This commit is contained in:
Alexis Lahouze 2018-06-09 23:34:03 +02:00
parent d63c5dbffe
commit 9dd489e6c4
2 changed files with 44 additions and 70 deletions

View File

@ -1,101 +1,73 @@
// vim: set tw=80 ts=2 sw=2 sts=2 : // vim: set tw=80 ts=2 sw=2 sts=2 :
import * as c3 from 'c3';
import { import {
Component, ElementRef, Component, ElementRef,
Inject, Input, Output, Inject, Input, Output,
OnInit, OnChanges OnChanges
} from '@angular/core'; } from '@angular/core';
import { Logger } from '@nsalaun/ng-logger';
import * as _ from 'underscore';
import { Account } from '../accounts/account'; import { Account } from '../accounts/account';
import { Category } from './category';
import { CategoryService } from './category.service'; import { CategoryService } from './category.service';
@Component({ @Component({
selector: 'category-chart', selector: 'category-chart',
template: '<div></div>' template: `<ngx-charts-bar-horizontal-2d
[results]="data"
[xAxis]="showXAxis"
[yAxis]="showYAxis"
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
barPadding="1">
</ngx-charts-bar-horizontal-2d>`
}) })
export class CategoryChartComponent implements OnInit, OnChanges { export class CategoryChartComponent implements OnChanges {
@Input() minDate: Date; @Input() minDate: Date;
@Input() maxDate: Date; @Input() maxDate: Date;
@Input() account: Account; @Input() account: Account;
chart: c3.ChartAPI; public data;
public showXAxis = true;
public showYAxis = true;
public showXAxisLabel = false;
public showYAxisLabel = false;
constructor( constructor(
private elementRef: ElementRef,
private categoryService: CategoryService, private categoryService: CategoryService,
) {} ) {
this.data = [];
}
loadData(account: Account) { loadData(account: Account) {
this.categoryService.query( this.categoryService.query(
account.id, account.id,
this.minDate, this.minDate,
this.maxDate this.maxDate
).subscribe((results) => { ).map((results: Category[]) => {
var expenses=[], return _.sortBy(results, 'income').reverse();
revenues=[], }).map((results: Category[]) => {
colors={}, return results.map((result: Category) => {
names={}; return {
'name': result.category,
var revenuesColor = 'green', 'series': [{
expensesColor = 'orange'; 'name': 'expenses',
'value': -Number(result.expenses)
for(let result of results) { }, {
if(result.revenues > 0) { 'name': 'revenues',
var revenuesName = 'revenues-' + result.category; 'value': Number(result.revenues)
}]
revenues.push([revenuesName, result.revenues]); };
names[revenuesName] = result.category;
colors[revenuesName] = revenuesColor;
}
if(result.expenses < 0) {
var expensesName = 'expenses-' + result.category;
expenses.splice(0, 0, [expensesName, -result.expenses]);
names[expensesName] = result.category;
colors[expensesName] = expensesColor;
}
};
this.chart.unload();
this.chart.load({
columns: revenues.concat(expenses),
names: names,
colors: colors
}); });
}).subscribe((results) => {
this.data = results;
}); });
}; }
ngOnInit() {
this.chart = c3.generate({
bindto: this.elementRef.nativeElement.children[0],
data: {
columns: [],
type: 'donut',
order: null,
},
tooltip: {
format: {
value: function(value, ratio, id, index) {
return value + '€';
}
}
},
donut: {
label: {
format: function(value) {
return value + '€';
}
}
},
legend: {
show: false
}
});
};
ngOnChanges(changes) { ngOnChanges(changes) {
if('account' in changes && changes.account.currentValue) { if('account' in changes && changes.account.currentValue) {

View File

@ -8,6 +8,7 @@ import { RouterModule } from '@angular/router';
import { NgLoggerModule, Level } from '@nsalaun/ng-logger'; import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { ToastrModule } from 'ngx-toastr'; import { ToastrModule } from 'ngx-toastr';
import { TextMaskModule } from 'angular2-text-mask'; import { TextMaskModule } from 'angular2-text-mask';
@ -33,6 +34,7 @@ import { OperationListState } from './operation.states';
NgLoggerModule, NgLoggerModule,
ToastrModule, ToastrModule,
NgbModule, NgbModule,
NgxChartsModule,
TextMaskModule TextMaskModule
], ],
providers: [ providers: [