2017-07-02 00:13:04 +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:30:12 +02:00
|
|
|
import * as moment from 'moment';
|
|
|
|
import * as c3 from 'c3';
|
2017-07-02 00:13:04 +02:00
|
|
|
|
2017-07-27 14:30:12 +02:00
|
|
|
import * as angular from 'angular';
|
2017-07-02 00:13:04 +02:00
|
|
|
|
2017-07-06 10:35:15 +02:00
|
|
|
module.exports = angular.module('categoryChartModule', [
|
2017-07-02 00:13:04 +02:00
|
|
|
])
|
|
|
|
|
2017-07-05 21:48:20 +02:00
|
|
|
.component('categoryChart', {
|
|
|
|
template: '<div></div>',
|
|
|
|
bindings: {
|
|
|
|
minDate: '<',
|
2017-07-29 16:32:07 +02:00
|
|
|
maxDate: '<',
|
|
|
|
account: '<'
|
2017-07-05 21:48:20 +02:00
|
|
|
},
|
2017-07-29 16:34:24 +02:00
|
|
|
controller: function($element, categoryService) {
|
2017-07-05 21:48:20 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2017-07-29 16:32:07 +02:00
|
|
|
vm.loadData = function(account: Account) {
|
|
|
|
categoryService.query(
|
2017-07-29 16:34:24 +02:00
|
|
|
account.id,
|
2017-07-29 16:32:07 +02:00
|
|
|
vm.minDate ? moment(vm.minDate).format('YYYY-MM-DD') : null,
|
|
|
|
vm.maxDate ? moment(vm.maxDate).format('YYYY-MM-DD') : null
|
|
|
|
).subscribe((results) => {
|
2017-07-05 21:48:20 +02:00
|
|
|
var expenses=[],
|
|
|
|
revenues=[],
|
|
|
|
colors={},
|
|
|
|
names={};
|
|
|
|
|
|
|
|
var revenuesColor = 'green',
|
|
|
|
expensesColor = 'orange';
|
|
|
|
|
|
|
|
angular.forEach(results, function(result) {
|
|
|
|
|
2017-07-05 23:19:24 +02:00
|
|
|
if(result.revenues > 0) {
|
|
|
|
var revenuesName = 'revenues-' + result.category;
|
2017-07-05 21:48:20 +02:00
|
|
|
|
2017-07-05 23:19:24 +02:00
|
|
|
revenues.push([revenuesName, result.revenues]);
|
|
|
|
names[revenuesName] = result.category;
|
|
|
|
colors[revenuesName] = revenuesColor;
|
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
|
2017-07-05 23:19:24 +02:00
|
|
|
if(result.expenses < 0) {
|
|
|
|
var expensesName = 'expenses-' + result.category;
|
|
|
|
|
|
|
|
expenses.splice(0, 0, [expensesName, -result.expenses]);
|
|
|
|
names[expensesName] = result.category;
|
|
|
|
colors[expensesName] = expensesColor;
|
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
vm.chart.unload();
|
|
|
|
|
|
|
|
vm.chart.load({
|
|
|
|
columns: revenues.concat(expenses),
|
|
|
|
names: names,
|
|
|
|
colors: colors
|
|
|
|
});
|
2017-07-02 00:13:04 +02:00
|
|
|
});
|
2017-07-05 21:48:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
vm.$onInit = function() {
|
|
|
|
vm.chart = c3.generate({
|
|
|
|
bindto: $element[0].children[0],
|
|
|
|
data: {
|
|
|
|
columns: [],
|
|
|
|
type: 'donut',
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
format: {
|
|
|
|
value: function(value, ratio, id, index) {
|
|
|
|
return value + '€';
|
|
|
|
}
|
2017-07-02 00:13:04 +02:00
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
},
|
|
|
|
donut: {
|
|
|
|
label: {
|
|
|
|
format: function(value) {
|
|
|
|
return value + '€';
|
|
|
|
}
|
2017-07-02 00:13:04 +02:00
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
show: false
|
2017-07-02 00:13:04 +02:00
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-29 16:32:07 +02:00
|
|
|
vm.$onChanges = function(changes) {
|
|
|
|
if('account' in changes && changes.account.currentValue) {
|
|
|
|
vm.loadData(changes.account.currentValue);
|
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
};
|
2017-07-02 00:13:04 +02:00
|
|
|
}
|
2017-07-05 21:48:20 +02:00
|
|
|
})
|
|
|
|
|
2017-07-06 10:35:15 +02:00
|
|
|
.name;
|