First attempt at functional category chart.

This commit is contained in:
James Cole
2015-12-16 13:08:26 +01:00
parent 6a8bf0aa62
commit 1b3d208540
12 changed files with 390 additions and 28 deletions

View File

@@ -19,6 +19,13 @@ interface CategoryChartGenerator
*/
public function all(Collection $entries);
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries);
/**
* @param Collection $entries
*

View File

@@ -158,4 +158,37 @@ class ChartJsCategoryChartGenerator implements CategoryChartGenerator
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries)
{
// dataset:
$data = [
'count' => 0,
'labels' => [],
'datasets' => [],
];
// get labels from one of the categories (assuming there's at least one):
$first = $entries->first();
foreach ($first['spent'] as $year => $noInterest) {
$data['labels'][] = strval($year);
}
// then, loop all entries and create datasets:
foreach ($entries as $entry) {
$name = $entry['name'];
$spent = $entry['spent'];
$earned = $entry['earned'];
$data['datasets'][] = ['label' => 'Spent in category ' . $name, 'data' => array_values($spent)];
$data['datasets'][] = ['label' => 'Earned in category ' . $name, 'data' => array_values($earned)];
}
$data['count'] = count($data['datasets']);
return $data;
}
}