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);
|
|
|
|
|
|
|
|
namespace FireflyIII\Generator\Chart\Basic;
|
|
|
|
|
2016-12-11 10:05:48 -06:00
|
|
|
use FireflyIII\Support\ChartColour;
|
|
|
|
|
2016-12-11 09:02:04 -06:00
|
|
|
/**
|
|
|
|
* Class ChartJsGenerator
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Generator\Chart\Basic
|
|
|
|
*/
|
|
|
|
class ChartJsGenerator implements GeneratorInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will generate a Chart JS compatible array from the given input. Expects this format:
|
|
|
|
*
|
|
|
|
* 0: [
|
|
|
|
* 'label' => 'label of set',
|
2016-12-11 10:46:30 -06:00
|
|
|
* 'type' => bar or line, optional
|
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-11 09:02:04 -06:00
|
|
|
* 'entries' =>
|
|
|
|
* [
|
|
|
|
* 'label-of-entry' => 'value'
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function multiSet(array $data): array
|
|
|
|
{
|
|
|
|
$chartData = [
|
|
|
|
'count' => count($data),
|
|
|
|
'labels' => array_keys($data[0]['entries']), // take ALL labels from the first set.
|
|
|
|
'datasets' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($data as $set) {
|
|
|
|
$chartData['datasets'][] = [
|
|
|
|
'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']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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' => [],
|
|
|
|
];
|
2016-12-11 10:46:30 -06:00
|
|
|
$index = 0;
|
2016-12-11 10:05:48 -06:00
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
|
|
|
// make larger than 0
|
|
|
|
if (bccomp($value, '0') === -1) {
|
|
|
|
$value = bcmul($value, '-1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$chartData['datasets'][0]['data'][] = round($value, 2);
|
|
|
|
$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;
|
|
|
|
}
|
2016-12-11 09:02:04 -06:00
|
|
|
}
|