-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Orbitale/chart
Start working on charts system
- Loading branch information
Showing
12 changed files
with
303 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import '../css/app.css'; | ||
import * as Highcharts from "highcharts"; | ||
|
||
// import $ from 'jquery'; | ||
require('../css/app.css'); | ||
|
||
console.info('App startup'); | ||
global.Highcharts = Highcharts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Compotes package. | ||
* | ||
* (c) Alex "Pierstoval" Rock <[email protected]>. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Controller; | ||
|
||
use App\Highcharts\Chart\TagUsageChart; | ||
use App\Repository\OperationRepository; | ||
use App\Repository\TagRepository; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Twig\Environment; | ||
|
||
class AnalyticsController | ||
{ | ||
private Environment $twig; | ||
private TagRepository $tagRepository; | ||
private OperationRepository $operationRepository; | ||
|
||
public function __construct( | ||
Environment $twig, | ||
TagRepository $tagRepository, | ||
OperationRepository $operationRepository | ||
) { | ||
$this->twig = $twig; | ||
$this->tagRepository = $tagRepository; | ||
$this->operationRepository = $operationRepository; | ||
} | ||
|
||
/** | ||
* @Route("/admin/analytics", name="analytics") | ||
*/ | ||
public function analytics(): Response | ||
{ | ||
$operations = $this->operationRepository->findWithTags(); | ||
|
||
return new Response($this->twig->render('analytics.html.twig', [ | ||
'charts' => [ | ||
TagUsageChart::create($operations), | ||
], | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Compotes package. | ||
* | ||
* (c) Alex "Pierstoval" Rock <[email protected]>. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Highcharts\Chart; | ||
|
||
abstract class AbstractChart implements ChartInterface | ||
{ | ||
public function getConfig(): array | ||
{ | ||
return $this->getOptions() + ['series' => $this->getSeries()]; | ||
} | ||
|
||
abstract protected function getSeries(): array; | ||
|
||
abstract protected function getOptions(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Compotes package. | ||
* | ||
* (c) Alex "Pierstoval" Rock <[email protected]>. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Highcharts\Chart; | ||
|
||
interface ChartInterface | ||
{ | ||
public function getName(): string; | ||
|
||
/** | ||
* This corresponds to the options sent to the Highcharts js object. | ||
* | ||
* Config has to be convertible to JSON or JS. | ||
* If your config contains closures, it will be rendered as a string (for now). | ||
* | ||
* @see https://api.highcharts.com/highcharts/ | ||
*/ | ||
public function getConfig(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Compotes package. | ||
* | ||
* (c) Alex "Pierstoval" Rock <[email protected]>. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Highcharts\Chart; | ||
|
||
use App\Entity\Operation; | ||
|
||
class TagUsageChart extends AbstractChart | ||
{ | ||
/** * @var Operation[] */ | ||
private array $operations = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'Tags'; | ||
} | ||
|
||
public static function create($operations): self | ||
{ | ||
$self = new self(); | ||
|
||
foreach ($operations as $operation) { | ||
$self->addOperation($operation); | ||
} | ||
|
||
return $self; | ||
} | ||
|
||
protected function getOptions(): array | ||
{ | ||
return [ | ||
'chart' => [ | ||
'type' => $type = 'bar', | ||
'height' => 500, | ||
], | ||
'legend' => [ | ||
'align' => 'right', | ||
'layout' => 'vertical', | ||
], | ||
'title' => ['text' => 'Tags usage'], | ||
'xAxis' => [ | ||
'categories' => ['Tags'], | ||
], | ||
'yAxis' => [ | ||
'title' => ['text' => 'Number of operations with these tags'], | ||
], | ||
'plotOptions' => [ | ||
$type => [ | ||
'pointWidth' => 10, | ||
'borderWidth' => 0, | ||
'groupPadding' => 0.01, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
protected function getSeries(): array | ||
{ | ||
$series = []; | ||
|
||
foreach ($this->operations as $operation) { | ||
foreach ($operation->getTags() as $tag) { | ||
$tagName = $tag->getName(); | ||
if (!isset($series[$tagName])) { | ||
$series[$tagName] = [ | ||
'name' => $tagName, | ||
'data' => [0], | ||
]; | ||
} | ||
|
||
$series[$tagName]['data'][0]++; | ||
} | ||
} | ||
|
||
\ksort($series); | ||
|
||
return \array_values($series); | ||
} | ||
|
||
private function addOperation(Operation $operation): void | ||
{ | ||
$this->operations[] = $operation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Compotes package. | ||
* | ||
* (c) Alex "Pierstoval" Rock <[email protected]>. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Twig; | ||
|
||
use Symfony\Component\String\Slugger\SluggerInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class SlugifyExtension extends AbstractExtension | ||
{ | ||
private SluggerInterface $slugger; | ||
|
||
public function __construct(SluggerInterface $slugger) | ||
{ | ||
$this->slugger = $slugger; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('slug', [$this, 'slugify']), | ||
]; | ||
} | ||
|
||
public function slugify($string): string | ||
{ | ||
if (\is_object($string)) { | ||
$string = (string) $string; | ||
} | ||
|
||
return $this->slugger->slug($string)->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends '@EasyAdmin/default/layout.html.twig' %} | ||
|
||
{% block content_title %} | ||
Analytics | ||
{% endblock %} | ||
|
||
{% block main %} | ||
{% for chart in charts %} | ||
<div id="{{ chart.name|slug }}"></div> | ||
{% endfor %} | ||
{% endblock %} | ||
|
||
{% block body_custom_javascript %} | ||
{{ parent() }} | ||
<script type="text/javascript" async> | ||
{% for chart in charts %} | ||
Highcharts.chart("{{ chart.name|slug }}", {{ chart.config|json_encode|raw }}); | ||
{% endfor %} | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters