firefly-iii/app/Generator/Chart/Basic/ChartJsGenerator.php

155 lines
4.1 KiB
PHP
Raw Normal View History

2016-12-11 09:02:04 -06:00
<?php
/**
* ChartJsGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2016-12-11 09:02:04 -06:00
namespace FireflyIII\Generator\Chart\Basic;
2016-12-11 10:05:48 -06:00
use FireflyIII\Support\ChartColour;
2017-02-11 08:52:55 -06:00
use Steam;
2016-12-11 10:05:48 -06:00
2016-12-11 09:02:04 -06:00
/**
* Class ChartJsGenerator
*
* @package FireflyIII\Generator\Chart\Basic
*/
class ChartJsGenerator implements GeneratorInterface
{
/**
2016-12-15 02:49:35 -06:00
* Will generate a Chart JS compatible array from the given input. Expects this format
*
* Will take labels for all from first set.
2016-12-11 09:02:04 -06:00
*
* 0: [
* 'label' => 'label of set',
2016-12-11 10:46:30 -06:00
* 'type' => bar or line, optional
2016-12-23 00:20:47 -06:00
* 'yAxisID' => ID of yAxis, optional, will not be included when unused.
* 'fill' => if to fill a line? optional, will not be included when unused.
2016-12-11 09:02:04 -06:00
* 'entries' =>
* [
* 'label-of-entry' => 'value'
* ]
* ]
* 1: [
* 'label' => 'label of another set',
2016-12-11 10:46:30 -06:00
* 'type' => bar or line, optional
2016-12-23 00:20:47 -06:00
* 'yAxisID' => ID of yAxis, optional, will not be included when unused.
* 'fill' => if to fill a line? optional, will not be included when unused.
2016-12-11 09:02:04 -06:00
* 'entries' =>
* [
* 'label-of-entry' => 'value'
* ]
* ]
*
2017-01-02 13:42:29 -06:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five.
2016-12-11 09:02:04 -06:00
*
* @param array $data
*
* @return array
*/
public function multiSet(array $data): array
{
2016-12-15 06:47:28 -06:00
reset($data);
$first = current($data);
2016-12-30 06:45:02 -06:00
$labels = is_array($first['entries']) ? array_keys($first['entries']) : [];
2016-12-15 06:47:28 -06:00
2016-12-11 09:02:04 -06:00
$chartData = [
'count' => count($data),
2016-12-15 06:47:28 -06:00
'labels' => $labels, // take ALL labels from the first set.
2016-12-11 09:02:04 -06:00
'datasets' => [],
];
2016-12-15 06:47:28 -06:00
unset($first, $labels);
2016-12-11 09:02:04 -06:00
foreach ($data as $set) {
2016-12-23 00:20:47 -06:00
$currentSet = [
2016-12-11 09:02:04 -06:00
'label' => $set['label'],
2016-12-11 10:46:30 -06:00
'type' => $set['type'] ?? 'line',
2016-12-11 09:02:04 -06:00
'data' => array_values($set['entries']),
];
2016-12-23 00:20:47 -06:00
if (isset($set['yAxisID'])) {
$currentSet['yAxisID'] = $set['yAxisID'];
}
if (isset($set['fill'])) {
$currentSet['fill'] = $set['fill'];
}
if (isset($set['currency_symbol'])) {
$currentSet['currency_symbol'] = $set['currency_symbol'];
}
2016-12-23 00:20:47 -06:00
$chartData['datasets'][] = $currentSet;
2016-12-11 09:02:04 -06:00
}
return $chartData;
}
2016-12-11 09:25:25 -06:00
2016-12-11 10:05:48 -06:00
/**
* Expects data as:
*
* key => value
*
* @param array $data
*
* @return array
*/
public function pieChart(array $data): array
{
2016-12-11 10:46:30 -06:00
$chartData = [
2016-12-11 10:05:48 -06:00
'datasets' => [
0 => [],
],
'labels' => [],
];
2017-04-22 00:05:44 -05:00
// sort by value, keep keys.
asort($data);
2017-06-05 04:12:50 -05:00
$index = 0;
2016-12-11 10:05:48 -06:00
foreach ($data as $key => $value) {
// make larger than 0
2017-02-11 08:52:55 -06:00
$chartData['datasets'][0]['data'][] = floatval(Steam::positive($value));
2016-12-11 10:05:48 -06:00
$chartData['datasets'][0]['backgroundColor'][] = ChartColour::getColour($index);
$chartData['labels'][] = $key;
$index++;
}
return $chartData;
}
2016-12-11 10:46:30 -06:00
/**
* Will generate a (ChartJS) compatible array from the given input. Expects this format:
*
* 'label-of-entry' => value
* 'label-of-entry' => value
*
* @param string $setLabel
* @param array $data
*
* @return array
*/
public function singleSet(string $setLabel, array $data): array
{
$chartData = [
'count' => 1,
'labels' => array_keys($data), // take ALL labels from the first set.
'datasets' => [
[
'label' => $setLabel,
'data' => array_values($data),
],
],
];
return $chartData;
}
}