Skip to content

Firefly III: a personal finances manager

Expand some code to generate chartJS charts.

James Cole3e82d438079e
diff --git a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php
new file mode 100644
index 0000000000..c496d2fb49
--- /dev/null
+++ b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace FireflyIII\Generator\Chart\Account;
+
+use Carbon\Carbon;
+use Config;
+use FireflyIII\Models\Account;
+use Illuminate\Support\Collection;
+use Preferences;
+use Steam;
+
+/**
+ * Class ChartJsAccountChartGenerator
+ *
+ * @package FireflyIII\Generator\Chart\Account
+ */
+class ChartJsAccountChartGenerator implements AccountChartGenerator
+{
+
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function all(Collection $accounts, Carbon $start, Carbon $end)
+ {
+ return $this->frontpage($accounts, $start, $end);
+ }
+
+ /**
+ * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function frontpage(Collection $accounts, Carbon $start, Carbon $end)
+ {
+ // language:
+ $language = Preferences::get('language', 'en')->data;
+ $format = Config::get('firefly.monthAndDay.' . $language);
+ $data = [
+ 'count' => 0,
+ 'labels' => [],
+ 'datasets' => [],
+ ];
+ $current = clone $start;
+ while ($current <= $end) {
+ $data['labels'][] = $current->formatLocalized($format);
+ $current->addDay();
+ }
+
+ foreach ($accounts as $account) {
+ $set = [
+ 'label' => $account->name,
+ 'fillColor' => 'rgba(220,220,220,0.2)',
+ 'strokeColor' => 'rgba(220,220,220,1)',
+ 'pointColor' => 'rgba(220,220,220,1)',
+ 'pointStrokeColor' => '#fff',
+ 'pointHighlightFill' => '#fff',
+ 'pointHighlightStroke' => 'rgba(220,220,220,1)',
+ 'data' => [],
+ ];
+ $current = clone $start;
+ while ($current <= $end) {
+ $set['data'][] = Steam::balance($account, $current);
+ $current->addDay();
+ }
+ $data['datasets'][] = $set;
+ $data['count']++;
+ }
+
+ return $data;
+ }
+
+ /**
+ * @param Account $account
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function single(Account $account, Carbon $start, Carbon $end)
+ {
+ // TODO: Implement single() method.
+ //throw new NotImplementedException;
+ }
+}
\ No newline at end of file
diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
new file mode 100644
index 0000000000..d5cf392ba0
--- /dev/null
+++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace FireflyIII\Generator\Chart\Budget;
+
+
+use Illuminate\Support\Collection;
+
+class ChartJsBudgetChartGenerator implements BudgetChartGenerator
+{
+
+ /**
+ * @param Collection $entries
+ *
+ * @return array
+ */
+ public function budget(Collection $entries)
+ {
+ }
+
+ /**
+ * @param Collection $entries
+ *
+ * @return array
+ */
+ public function budgetLimit(Collection $entries)
+ {
+ }
+
+ /**
+ * @param Collection $entries
+ *
+ * @return array
+ */
+ public function frontpage(Collection $entries)
+ {
+ }
+
+ /**
+ * @param Collection $budgets
+ * @param Collection $entries
+ *
+ * @return array
+ */
+ public function year(Collection $budgets, Collection $entries)
+ {
+ }
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php
index 3d92c8ef1d..c8b7834bc5 100644
--- a/app/Http/Controllers/Chart/AccountController.php
+++ b/app/Http/Controllers/Chart/AccountController.php
@@ -90,8 +90,8 @@ class AccountController extends Controller
public function frontpage(AccountRepositoryInterface $repository)
{
$frontPage = Preferences::get('frontPageAccounts', []);
- $start = Session::get('start', Carbon::now()->startOfMonth());
- $end = Session::get('end', Carbon::now()->endOfMonth());
+ $start = clone Session::get('start', Carbon::now()->startOfMonth());
+ $end = clone Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
// chart properties for cache:
@@ -101,7 +101,7 @@ class AccountController extends Controller
$cache->addProperty('frontpage');
$cache->addProperty('accounts');
if ($cache->has()) {
- return Response::json($cache->get()); // @codeCoverageIgnore
+ //return Response::json($cache->get()); // @codeCoverageIgnore
}
$data = $this->generator->frontpage($accounts, $start, $end);