accountant-ui/src/operations/category-chart.component.js

138 lines
4.3 KiB
JavaScript

// 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';
var moment = require('moment'),
c3 = require('c3');
var angular = require('angular');
var ngResource = require('angular-resource');
module.exports = angular.module('categoryChartModule', [
ngResource
])
.component('categoryChart', {
template: '<div></div>',
bindings: {
minDate: '<',
maxDate: '<'
},
controller: function($routeParams, $element, Categories, Incomes) {
var vm = this;
vm.loadData = function() {
Categories.query({
id: $routeParams.accountId,
begin: vm.minDate ? moment(vm.minDate).format('YYYY-MM-DD') : null,
end: vm.maxDate ? moment(vm.maxDate).format('YYYY-MM-DD') : null
}, function(results) {
var expenses=[],
revenues=[],
colors={},
names={};
var revenuesColor = 'green',
expensesColor = 'orange';
angular.forEach(results, function(result) {
if(result.revenues > 0) {
var revenuesName = 'revenues-' + result.category;
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;
}
});
vm.chart.unload();
vm.chart.load({
columns: revenues.concat(expenses),
names: names,
colors: colors
});
});
};
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 + '€';
}
}
},
donut: {
label: {
format: function(value) {
return value + '€';
}
}
},
legend: {
show: false
}
});
//vm.loadData();
};
vm.$onChanges = function() {
vm.loadData();
};
}
})
.factory('Categories', function($resource) {
return $resource(
'/api/account/:id/category', {
id: '@id'
}
);
})
.factory('Incomes', function($resource) {
return $resource(
'/api/account/:id/income', {
id: '@id'
}
);
})
.name;