-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wow, I did my first chart and it works!
- Loading branch information
1 parent
de95454
commit f74f931
Showing
13 changed files
with
296 additions
and
112 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,42 +1,6 @@ | ||
import '../css/app.css'; | ||
import * as Highcharts from "highcharts"; | ||
|
||
// import $ from 'jquery'; | ||
require('../css/app.css'); | ||
|
||
console.info('App startup'); | ||
|
||
(async function () { | ||
|
||
const chartsContainer = document.getElementById('charts'); | ||
|
||
if (chartsContainer) { | ||
try { | ||
const filtersUrl = chartsContainer.getAttribute('data-filters-url'); | ||
|
||
const url = new URL(filtersUrl); | ||
url.searchParams.append('filters', '...'); | ||
|
||
const result = await fetch(url); | ||
|
||
if (result.status !== 200) { | ||
throw new Error('Filters request returned an error.'); | ||
} | ||
|
||
const json = await result.json(); | ||
|
||
console.info('Filters fetch result: ', json); | ||
|
||
if (!json.highcharts) { | ||
throw new Error('Highcharts field is not defined in response.'); | ||
} | ||
|
||
const myChart = Highcharts.chart('charts', json.highcharts); | ||
|
||
console.info('Chart: ', myChart); | ||
} catch (e) { | ||
console.error('Cannot fetch filters: '+e.message); | ||
} | ||
|
||
} | ||
|
||
})(); | ||
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.