2017-06-18 00:46:20 +02:00
|
|
|
// vim: set tw=80 ts=4 sw=4 sts=4:
|
|
|
|
/*
|
|
|
|
This file is part of Accountant.
|
|
|
|
|
|
|
|
Accountant is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Accountant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/* jshint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
import * as moment from 'moment';
|
|
|
|
import * as c3 from 'c3';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Component, ElementRef,
|
|
|
|
Inject, Input, Output, EventEmitter,
|
|
|
|
OnInit, OnChanges
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { Logger } from '@nsalaun/ng-logger';
|
|
|
|
|
|
|
|
import { Account } from '../accounts/account';
|
|
|
|
import { DailyBalanceService } from '../accounts/dailyBalance.service';
|
|
|
|
|
|
|
|
class DateRange {
|
|
|
|
minDate: Date;
|
|
|
|
maxDate: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
class xAxis {
|
|
|
|
balances: Date[];
|
|
|
|
expenses: Date[];
|
|
|
|
revenues: Date[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'balance-chart',
|
|
|
|
template: '<div></div>'
|
|
|
|
})
|
|
|
|
export class BalanceChartComponent implements OnInit, OnChanges {
|
|
|
|
@Input() account: Account;
|
|
|
|
@Output() onUpdate: EventEmitter<DateRange> = new EventEmitter<DateRange>();
|
|
|
|
|
|
|
|
private chart: c3.ChartAPI;
|
|
|
|
private balances: number[];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private elementRef: ElementRef,
|
|
|
|
private logger: Logger,
|
|
|
|
private dailyBalanceService: DailyBalanceService,
|
|
|
|
@Inject('accountIdService') private accountIdService,
|
|
|
|
) {
|
|
|
|
this.logger.log("Constructor BalanceChartComponent")
|
|
|
|
}
|
|
|
|
|
|
|
|
loadData() {
|
|
|
|
this.dailyBalanceService.query(
|
|
|
|
this.accountIdService.get()
|
|
|
|
).subscribe((results) => {
|
|
|
|
var headers: any[][] = [['date', 'balances', 'expenses', 'revenues']];
|
2017-07-05 21:48:20 +02:00
|
|
|
|
|
|
|
var rows = results.map(function(result) {
|
|
|
|
return [
|
|
|
|
result.operation_date,
|
|
|
|
result.balance,
|
|
|
|
result.expenses,
|
|
|
|
result.revenues
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
this.chart.unload();
|
2017-07-05 21:48:20 +02:00
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
this.chart.load({
|
2017-07-05 21:48:20 +02:00
|
|
|
rows: headers.concat(rows)
|
|
|
|
});
|
2017-06-18 00:46:20 +02:00
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
var x: any;
|
|
|
|
|
|
|
|
x = this.chart.x();
|
|
|
|
|
|
|
|
this.logger.log("x", x);
|
|
|
|
|
2017-07-05 21:48:20 +02:00
|
|
|
var balances = x.balances;
|
2017-07-04 20:35:53 +02:00
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
this.onUpdate.emit({
|
|
|
|
minDate: balances[0],
|
|
|
|
maxDate: balances[balances.length - 1]
|
|
|
|
});
|
2017-07-01 14:50:17 +02:00
|
|
|
});
|
2017-07-05 21:48:20 +02:00
|
|
|
};
|
2017-07-02 00:11:02 +02:00
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
$onInit() {
|
|
|
|
this.ngOnInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2017-07-05 21:48:20 +02:00
|
|
|
var tomorrow = moment().endOf('day').valueOf();
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
this.chart = c3.generate({
|
|
|
|
bindto: this.elementRef.nativeElement.children[0],
|
2017-07-05 21:48:20 +02:00
|
|
|
size: {
|
|
|
|
height: 450,
|
2017-07-01 14:50:17 +02:00
|
|
|
},
|
2017-07-05 21:48:20 +02:00
|
|
|
data: {
|
|
|
|
x: 'date',
|
|
|
|
rows: [],
|
|
|
|
axes: {
|
|
|
|
expenses: 'y2',
|
|
|
|
revenues: 'y2'
|
|
|
|
},
|
|
|
|
type: 'bar',
|
|
|
|
types: {
|
|
|
|
balances: 'area'
|
|
|
|
},
|
|
|
|
groups: [
|
|
|
|
['expenses', 'revenues']
|
|
|
|
],
|
|
|
|
// Disable for the moment because there is an issue when
|
|
|
|
// using subchart line is not refreshed after subset
|
|
|
|
// selection.
|
|
|
|
//regions: {
|
|
|
|
// balances: [{
|
|
|
|
// start: tomorrow,
|
|
|
|
// style: 'dashed'
|
|
|
|
// }]
|
|
|
|
//}
|
2017-07-01 14:50:17 +02:00
|
|
|
},
|
2017-07-05 21:48:20 +02:00
|
|
|
regions: [{
|
|
|
|
start: tomorrow,
|
|
|
|
}],
|
|
|
|
axis: {
|
|
|
|
x: {
|
|
|
|
type: 'timeseries',
|
|
|
|
tick: {
|
|
|
|
format: '%Y-%m-%d',
|
|
|
|
rotate: 50,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
label: {
|
|
|
|
text: 'Amount',
|
|
|
|
position: 'outer-middle'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
y2: {
|
|
|
|
show: true,
|
|
|
|
label: {
|
|
|
|
text: 'Amount',
|
|
|
|
position: 'outer-middle'
|
|
|
|
}
|
2017-07-01 14:50:17 +02:00
|
|
|
}
|
|
|
|
},
|
2017-07-05 21:48:20 +02:00
|
|
|
grid: {
|
|
|
|
x: {
|
|
|
|
show: true,
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
show: true,
|
2017-07-01 14:50:17 +02:00
|
|
|
}
|
|
|
|
},
|
2017-07-05 21:48:20 +02:00
|
|
|
tooltip: {
|
|
|
|
format: {
|
|
|
|
value: function(value, ratio, id, index) {
|
|
|
|
return value + '€';
|
|
|
|
}
|
2017-07-01 14:50:17 +02:00
|
|
|
}
|
|
|
|
},
|
2017-07-05 21:48:20 +02:00
|
|
|
subchart: {
|
2017-07-01 14:50:17 +02:00
|
|
|
show: true,
|
2017-07-05 21:48:20 +02:00
|
|
|
onbrush: function(domain) {
|
2017-07-27 14:03:39 +02:00
|
|
|
this.onUpdate.emit({minDate: domain[0], maxDate: domain[1]});
|
2017-07-01 14:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-04 21:13:08 +02:00
|
|
|
});
|
2017-07-05 21:48:20 +02:00
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
this.loadData();
|
2017-07-05 21:48:20 +02:00
|
|
|
};
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
setLines(account: Account) {
|
|
|
|
if(this.chart) {
|
|
|
|
this.chart.ygrids([
|
2017-07-05 21:48:20 +02:00
|
|
|
{ value: 0, axis: 'y2' },
|
|
|
|
{ value: 0, axis: 'y', class: 'zeroline'},
|
|
|
|
]);
|
|
|
|
|
2017-07-14 10:33:20 +02:00
|
|
|
if(account) {
|
2017-07-27 14:03:39 +02:00
|
|
|
this.chart.ygrids.add({
|
2017-07-14 10:33:20 +02:00
|
|
|
value: account.authorized_overdraft,
|
|
|
|
axis: 'y',
|
|
|
|
class: 'overdraft'
|
|
|
|
});
|
|
|
|
}
|
2017-07-05 21:36:23 +02:00
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
};
|
|
|
|
|
2017-07-27 14:03:39 +02:00
|
|
|
$onChanges(changes) {
|
|
|
|
this.ngOnChanges(changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes) {
|
2017-07-05 21:48:20 +02:00
|
|
|
if('account' in changes) {
|
2017-07-27 14:03:39 +02:00
|
|
|
this.setLines(changes.account.currentValue);
|
2017-07-14 10:33:20 +02:00
|
|
|
} else {
|
2017-07-27 14:03:39 +02:00
|
|
|
this.setLines(this.account);
|
2017-07-05 21:48:20 +02:00
|
|
|
}
|
|
|
|
};
|
2017-07-27 14:03:39 +02:00
|
|
|
}
|