Skip to content
Snippets Groups Projects
finanz.js 1.98 KiB
Newer Older
chrissi^'s avatar
chrissi^ committed
$(function () {
    $.get('/s0_finanz.json', function (ans) {
        let types = [];
        let bound = 0;
        ans.appropriations.forEach(function (appr) {
            if (appr.subject.indexOf('Freifunk') > -1) {
                types = types.concat(appr.types);
                bound += parseFloat(appr.amount);
            }
        });
        $('#finanz_total').text(bound.toString());
        const typeIds = types.map(function (typ) {
            return ans.types.findIndex(function (t) { return t.indexOf(typ) > -1; });
        });
Kasalehlia's avatar
Kasalehlia committed
        const income = [];
        const expense = [];
        const labels = [];
        for (let i = 1; i <= 12; i++) {
            labels.unshift(ans.months[i].yearmonth);
            for (let j = 0; j < typeIds.length; j++) {
                ((j%2) === 0 ? income : expense).unshift(Math.abs(parseFloat(ans.months[i].typeTotals[typeIds[j]])));
            }
chrissi^'s avatar
chrissi^ committed
        }
Kasalehlia's avatar
Kasalehlia committed
        const ctx = document.getElementById('graph').getContext('2d');
        const chart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels,
                datasets: [{
                    backgroundColor: '#90ee90',
                    label: 'Einnahmen',
                    data: income
                }, {
                    backgroundColor: '#ff6363',
                    label: 'Ausgaben',
                    data: expense
                }],
            },
            options: {
                scales: {
                    yAxes: [{
                        scaleLabel: {
                            display: true,
                            labelString: 'EUR',
                        },
                        ticks: {
                            beginAtZero: true,
                        },
Kasalehlia's avatar
Kasalehlia committed
                    }],
                },
                tooltips: {
                    callbacks: {
                        label: function (item) { return item.yLabel+''; },
                    },
                },
            },
chrissi^'s avatar
chrissi^ committed
        });
    });
});