From 1f1334a1fc85019b27bcf7fae09cc383749908ac Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 23 Dec 2016 07:20:47 +0100 Subject: [PATCH] Update chart to show sum --- .../Chart/Basic/ChartJsGenerator.php | 14 +++++- .../Chart/CategoryReportController.php | 49 +++++++++++++++++-- public/js/ff/charts.js | 4 +- public/js/ff/reports/category/month.js | 2 +- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/Generator/Chart/Basic/ChartJsGenerator.php b/app/Generator/Chart/Basic/ChartJsGenerator.php index c9b13a873c..3f5eb8145c 100644 --- a/app/Generator/Chart/Basic/ChartJsGenerator.php +++ b/app/Generator/Chart/Basic/ChartJsGenerator.php @@ -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; diff --git a/app/Http/Controllers/Chart/CategoryReportController.php b/app/Http/Controllers/Chart/CategoryReportController.php index d241d27ef8..af6c2c4a75 100644 --- a/app/Http/Controllers/Chart/CategoryReportController.php +++ b/app/Http/Controllers/Chart/CategoryReportController.php @@ -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); diff --git a/public/js/ff/charts.js b/public/js/ff/charts.js index e8e3afd674..f49a5cc4a2 100644 --- a/public/js/ff/charts.js +++ b/public/js/ff/charts.js @@ -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); } diff --git a/public/js/ff/reports/category/month.js b/public/js/ff/reports/category/month.js index cdbe5420b2..607d635205 100644 --- a/public/js/ff/reports/category/month.js +++ b/public/js/ff/reports/category/month.js @@ -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);