From f7283956036aaab654ca38f9767a8cd1db3e7d36 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 27 Jun 2015 17:05:39 +0200 Subject: [PATCH] Added some more charts --- .../Budget/ChartJsBudgetChartGenerator.php | 44 +++++++++++++++++++ .../Controllers/Chart/BudgetController.php | 11 ++--- app/Providers/FireflyServiceProvider.php | 15 ++++++- public/js/charts.js | 37 ++++++++++++++-- resources/twig/index.twig | 9 +++- 5 files changed, 103 insertions(+), 13 deletions(-) diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php index d5cf392ba0..2c0a3e0f2e 100644 --- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -5,6 +5,11 @@ namespace FireflyIII\Generator\Chart\Budget; use Illuminate\Support\Collection; +/** + * Class ChartJsBudgetChartGenerator + * + * @package FireflyIII\Generator\Chart\Budget + */ class ChartJsBudgetChartGenerator implements BudgetChartGenerator { @@ -33,6 +38,45 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator */ public function frontpage(Collection $entries) { + $data = [ + 'count' => 2, + 'labels' => [], + 'datasets' => [], + ]; + foreach ($entries as $entry) { + if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) { + $data['labels'][] = $entry[0]; + } + } + // dataset: left + // dataset: spent + // dataset: overspent + $left = []; + $spent = []; + $overspent = []; + $amount = []; + $expenses = []; + foreach ($entries as $entry) { + if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) { + $left[] = round($entry[1], 2); + $spent[] = round($entry[2], 2); + $overspent[] = round($entry[3], 2); + $amount[] = round($entry[4], 2); + $expenses[] = round($entry[5], 2); + //$data['count']++; + } + } + + $data['datasets'][] = [ + 'label' => 'Amount', + 'data' => $amount, + ]; + $data['datasets'][] = [ + 'label' => 'Spent', + 'data' => $expenses, + ]; + + return $data; } /** diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 4793ce3c92..edccfd042f 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -149,7 +149,7 @@ class BudgetController extends Controller $cache->addProperty('budget'); $cache->addProperty('all'); if ($cache->has()) { - return Response::json($cache->get()); // @codeCoverageIgnore + //return Response::json($cache->get()); // @codeCoverageIgnore } bcscale(2); @@ -159,7 +159,7 @@ class BudgetController extends Controller $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end); if ($repetitions->count() == 0) { $expenses = $repository->spentInPeriodCorrected($budget, $start, $end, true); - $allEntries->push([$budget->name, 0, 0, $expenses]); + $allEntries->push([$budget->name, 0, 0, $expenses, 0, 0]); continue; } /** @var LimitRepetition $repetition */ @@ -171,18 +171,19 @@ class BudgetController extends Controller $left = max(bcsub($repetition->amount, $expenses), 0); // limited at zero. $overspent = max(bcsub($expenses, $repetition->amount), 0); // limited at zero. $date = $repetition->startdate->formatLocalized($this->monthAndDayFormat); - $name = $budget->name . ' (' . $date . ')'; + //$name = $budget->name . ' (' . $date . ')'; + $name = $budget->name; // $spent is maxed to the repetition amount: $spent = $expenses > $repetition->amount ? $repetition->amount : $expenses; - $allEntries->push([$name, $left, $spent, $overspent]); + $allEntries->push([$name, $left, $spent, $overspent, $repetition->amount, $expenses]); } } $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end) * -1; - $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses]); + $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]); $data = $this->generator->frontpage($allEntries); $cache->store($data); diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index b05e112dec..627f01631b 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -92,12 +92,23 @@ class FireflyServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); // make Google charts: - $this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator'); + //$this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\ChartJsAccountChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Bill\BillChartGenerator', 'FireflyIII\Generator\Chart\Bill\GoogleBillChartGenerator'); - $this->app->bind('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator', 'FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator'); + //$this->app->bind('FireflyIII\Generator\Chart\Bill\BillChartGenerator', 'FireflyIII\Generator\Chart\Bill\GoogleBillChartGenerator'); + + //$this->app->bind('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator', 'FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator', 'FireflyIII\Generator\Chart\Budget\ChartJsBudgetChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Category\CategoryChartGenerator', 'FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator'); + //$this->app->bind('FireflyIII\Generator\Chart\Category\CategoryChartGenerator', 'FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGenerator', 'FireflyIII\Generator\Chart\PiggyBank\GooglePiggyBankChartGenerator'); + //$this->app->bind('FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGenerator', 'FireflyIII\Generator\Chart\PiggyBank\GooglePiggyBankChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Report\ReportChartGenerator', 'FireflyIII\Generator\Chart\Report\GoogleReportChartGenerator'); + //$this->app->bind('FireflyIII\Generator\Chart\Report\ReportChartGenerator', 'FireflyIII\Generator\Chart\Report\GoogleReportChartGenerator'); $this->app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help'); diff --git a/public/js/charts.js b/public/js/charts.js index d998bab31d..63fe8c56c1 100644 --- a/public/js/charts.js +++ b/public/js/charts.js @@ -3,6 +3,18 @@ /* Make some colours: */ +/* +#555299 +#4285f4 +#db4437 +#f4b400 +#0f9d58 +#ab47bc +#00acc1 +#ff7043 +#9e9d24 +#5c6bc0", "#f06292", "#00796b", "#c2185b"], + */ var colourSet = [ [53, 124, 165], [0, 141, 76], @@ -35,8 +47,23 @@ var defaultAreaOptions = { multiTooltipTemplate: "<%=datasetLabel%>: <%= '" + currencyCode + " ' + Number(value).toFixed(2).replace('.', ',') %>" }; -var defaultLineOptions = defaultAreaOptions; -defaultLineOptions.datasetFill = false; +var defaultLineOptions = { + scaleShowGridLines: false, + pointDotRadius: 2, + datasetStrokeWidth: 1, + pointHitDetectionRadius: 5, + datasetFill: false, + scaleFontSize: 10, + responsive: true, + scaleLabel: "<%= '" + currencyCode + " ' + Number(value).toFixed(2).replace('.', ',') %>", + tooltipFillColor: "rgba(0,0,0,0.5)", + multiTooltipTemplate: "<%=datasetLabel%>: <%= '" + currencyCode + " ' + Number(value).toFixed(2).replace('.', ',') %>" +}; + +var defaultColumnOptions = { + multiTooltipTemplate: "<%=datasetLabel%>: <%= '" + currencyCode + " ' + Number(value).toFixed(2).replace('.', ',') %>" + +}; /** * Function to draw a line chart: @@ -116,8 +143,9 @@ function areaChart(URL, container, options) { */ function columnChart(URL, container, options) { "use strict"; - + options = options || defaultColumnOptions; $.getJSON(URL).success(function (data) { + var ctx = document.getElementById(container).getContext("2d"); var newData = {}; newData.datasets = []; @@ -133,7 +161,8 @@ function columnChart(URL, container, options) { dataset.pointHighlightStroke = strokePointHighColors[i]; newData.datasets.push(dataset); } - new Chart(ctx).Column(newData, options); + console.log(newData); + new Chart(ctx).Bar(newData, options); }).fail(function () { $('#' + container).addClass('google-chart-error'); diff --git a/resources/twig/index.twig b/resources/twig/index.twig index bd8dc6f34f..08fea67bc8 100644 --- a/resources/twig/index.twig +++ b/resources/twig/index.twig @@ -45,7 +45,7 @@
{% endif %} {% if Config.get('firefly.chart') == 'chartjs' %} - + {% endif %} @@ -59,7 +59,12 @@
-
+ {% if Config.get('firefly.chart') == 'google' %} +
+ {% endif %} + {% if Config.get('firefly.chart') == 'chartjs' %} + + {% endif %}