Update chart to show sum

This commit is contained in:
James Cole
2016-12-23 07:20:47 +01:00
parent bf0744e03a
commit 1f1334a1fc
4 changed files with 60 additions and 9 deletions

View File

@@ -31,6 +31,8 @@ class ChartJsGenerator implements GeneratorInterface
* 0: [
* 'label' => 'label of set',
* 'type' => bar or line, optional
* 'yAxisID' => ID of yAxis, optional, will not be included when unused.
* 'fill' => if to fill a line? optional, will not be included when unused.
* 'entries' =>
* [
* 'label-of-entry' => 'value'
@@ -39,6 +41,8 @@ class ChartJsGenerator implements GeneratorInterface
* 1: [
* 'label' => 'label of another set',
* 'type' => bar or line, optional
* 'yAxisID' => ID of yAxis, optional, will not be included when unused.
* 'fill' => if to fill a line? optional, will not be included when unused.
* 'entries' =>
* [
* 'label-of-entry' => 'value'
@@ -64,11 +68,19 @@ class ChartJsGenerator implements GeneratorInterface
unset($first, $labels);
foreach ($data as $set) {
$chartData['datasets'][] = [
$currentSet = [
'label' => $set['label'],
'type' => $set['type'] ?? 'line',
'data' => array_values($set['entries']),
];
if (isset($set['yAxisID'])) {
$currentSet['yAxisID'] = $set['yAxisID'];
}
if (isset($set['fill'])) {
$currentSet['fill'] = $set['fill'];
}
$chartData['datasets'][] = $currentSet;
}
return $chartData;

View File

@@ -314,14 +314,33 @@ class CategoryReportController extends Controller
$chartData[$category->id . '-in'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.income'))) . ')',
'type' => 'bar',
'yAxisID' => 'y-axis-0',
'entries' => [],
];
$chartData[$category->id . '-out'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.expenses'))) . ')',
'type' => 'bar',
'yAxisID' => 'y-axis-0',
'entries' => [],
];
// total in, total out:
$chartData[$category->id . '-total-in'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.sum_of_income'))) . ')',
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
$chartData[$category->id . '-total-out'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.sum_of_expenses'))) . ')',
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
}
$sumOfIncome = [];
$sumOfExpense = [];
while ($currentStart < $end) {
$currentEnd = clone $currentStart;
@@ -332,15 +351,35 @@ class CategoryReportController extends Controller
/** @var Category $category */
foreach ($categories as $category) {
$labelIn = $category->id . '-in';
$labelOut = $category->id . '-out';
// get sum, and get label:
$chartData[$labelIn]['entries'][$label] = $income[$category->id] ?? '0';
$chartData[$labelOut]['entries'][$label] = $expenses[$category->id] ?? '0';
$labelIn = $category->id . '-in';
$labelOut = $category->id . '-out';
$labelSumIn = $category->id . '-total-in';
$labelSumOut = $category->id . '-total-out';
$currentIncome = $income[$category->id] ?? '0';
$currentExpense = $expenses[$category->id] ?? '0';
// add to sum:
$sumOfIncome[$category->id] = $sumOfIncome[$category->id] ?? '0';
$sumOfExpense[$category->id] = $sumOfExpense[$category->id] ?? '0';
$sumOfIncome[$category->id] = bcadd($sumOfIncome[$category->id], $currentIncome);
$sumOfExpense[$category->id] = bcadd($sumOfExpense[$category->id], $currentExpense);
// add to chart:
$chartData[$labelIn]['entries'][$label] = $currentIncome;
$chartData[$labelOut]['entries'][$label] = $currentExpense;
$chartData[$labelSumIn]['entries'][$label] = $sumOfIncome[$category->id];
$chartData[$labelSumOut]['entries'][$label] = $sumOfExpense[$category->id];
}
$currentStart = clone $currentEnd;
$currentStart->addDay();
}
// remove all empty entries to prevent cluttering:
foreach ($chartData as $key => $entry) {
if (array_sum($entry['entries']) == 0) {
unset($chartData[$key]);
}
}
$data = $this->generator->multiSet($chartData);
$cache->store($data);

View File

@@ -125,9 +125,9 @@ function doubleYChart(URI, container) {
];
options.stacked = true;
options.scales.xAxes[0].stacked = true;
console.log(options);
// console.log(options);
var chartType = 'line';
var chartType = 'bar';
drawAChart(URI, container, chartType, options, colorData);
}

View File

@@ -36,7 +36,7 @@ function drawChart() {
"use strict";
// month view:
stackedColumnChart(mainUri, 'in-out-chart');
doubleYChart(mainUri, 'in-out-chart');
// draw pie chart of income, depending on "show other transactions too":
redrawPieChart('categories-in-pie-chart', categoryIncomeUri);