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: [ * 0: [
* 'label' => 'label of set', * 'label' => 'label of set',
* 'type' => bar or line, optional * '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' => * 'entries' =>
* [ * [
* 'label-of-entry' => 'value' * 'label-of-entry' => 'value'
@@ -39,6 +41,8 @@ class ChartJsGenerator implements GeneratorInterface
* 1: [ * 1: [
* 'label' => 'label of another set', * 'label' => 'label of another set',
* 'type' => bar or line, optional * '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' => * 'entries' =>
* [ * [
* 'label-of-entry' => 'value' * 'label-of-entry' => 'value'
@@ -64,11 +68,19 @@ class ChartJsGenerator implements GeneratorInterface
unset($first, $labels); unset($first, $labels);
foreach ($data as $set) { foreach ($data as $set) {
$chartData['datasets'][] = [ $currentSet = [
'label' => $set['label'], 'label' => $set['label'],
'type' => $set['type'] ?? 'line', 'type' => $set['type'] ?? 'line',
'data' => array_values($set['entries']), '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; return $chartData;

View File

@@ -314,14 +314,33 @@ class CategoryReportController extends Controller
$chartData[$category->id . '-in'] = [ $chartData[$category->id . '-in'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.income'))) . ')', 'label' => $category->name . ' (' . strtolower(strval(trans('firefly.income'))) . ')',
'type' => 'bar', 'type' => 'bar',
'yAxisID' => 'y-axis-0',
'entries' => [], 'entries' => [],
]; ];
$chartData[$category->id . '-out'] = [ $chartData[$category->id . '-out'] = [
'label' => $category->name . ' (' . strtolower(strval(trans('firefly.expenses'))) . ')', 'label' => $category->name . ' (' . strtolower(strval(trans('firefly.expenses'))) . ')',
'type' => 'bar', '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' => [], 'entries' => [],
]; ];
} }
$sumOfIncome = [];
$sumOfExpense = [];
while ($currentStart < $end) { while ($currentStart < $end) {
$currentEnd = clone $currentStart; $currentEnd = clone $currentStart;
@@ -334,13 +353,33 @@ class CategoryReportController extends Controller
foreach ($categories as $category) { foreach ($categories as $category) {
$labelIn = $category->id . '-in'; $labelIn = $category->id . '-in';
$labelOut = $category->id . '-out'; $labelOut = $category->id . '-out';
// get sum, and get label: $labelSumIn = $category->id . '-total-in';
$chartData[$labelIn]['entries'][$label] = $income[$category->id] ?? '0'; $labelSumOut = $category->id . '-total-out';
$chartData[$labelOut]['entries'][$label] = $expenses[$category->id] ?? '0'; $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 = clone $currentEnd;
$currentStart->addDay(); $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); $data = $this->generator->multiSet($chartData);
$cache->store($data); $cache->store($data);

View File

@@ -125,9 +125,9 @@ function doubleYChart(URI, container) {
]; ];
options.stacked = true; options.stacked = true;
options.scales.xAxes[0].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); drawAChart(URI, container, chartType, options, colorData);
} }

View File

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