UserController slightly refactored.

This commit is contained in:
Alexey Skobkin 2017-01-13 03:00:20 +03:00
parent da2a27dd63
commit eb5aa80430

View file

@ -52,7 +52,6 @@ class UserController extends Controller
$eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay(); $eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay();
return $this->render('@SkobkinPointTools/User/top.html.twig', [ return $this->render('@SkobkinPointTools/User/top.html.twig', [
//'top_users' => $topUsers,
'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay), 'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay),
'top_chart' => $this->createTopUsersGraph($topUsers), 'top_chart' => $this->createTopUsersGraph($topUsers),
]); ]);
@ -65,38 +64,13 @@ class UserController extends Controller
*/ */
private function createEventsDynamicChart(array $eventsByDay = []): Highchart private function createEventsDynamicChart(array $eventsByDay = []): Highchart
{ {
$translator = $this->get('translator'); $data = [];
$chartData = [ foreach ($eventsByDay as $dailyEvents) {
'titles' => [], $data[$dailyEvents->getDate()->format('d.m')] = $dailyEvents->getEventsCount();
'events' => [],
];
foreach ($eventsByDay as $day) {
$chartData['titles'][] = $day->getDate()->format('d.m');
$chartData['events'][] = $day->getEventsCount();
} }
$series = [[ return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount');
'name' => $translator->trans('Events count'),
'data' => $chartData['events'],
]];
$ob = new Highchart();
$ob->chart->renderTo('eventschart');
$ob->chart->type('line');
$ob->title->text($translator->trans('Events by day'));
$ob->xAxis->title(['text' => null]);
$ob->xAxis->categories($chartData['titles']);
$ob->yAxis->title(['text' => $translator->trans('amount')]);
$ob->plotOptions->bar([
'dataLabels' => [
'enabled' => true,
]
]);
$ob->series($series);
return $ob;
} }
/** /**
@ -105,34 +79,45 @@ class UserController extends Controller
* @param TopUserDTO[] $topUsers * @param TopUserDTO[] $topUsers
*/ */
private function createTopUsersGraph(array $topUsers = []): Highchart private function createTopUsersGraph(array $topUsers = []): Highchart
{
$data = [];
foreach ($topUsers as $topUser) {
$data[$topUser->getLogin()] = $topUser->getSubscribersCount();
}
return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount');
}
private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart
{ {
$translator = $this->get('translator'); $translator = $this->get('translator');
$chartData = [ $chartData = [
'titles' => [], 'keys' => [],
'subscribers' => [], 'values' => [],
]; ];
// Preparing chart data // Preparing chart data
foreach ($topUsers as $user) { foreach ($data as $key => $value) {
$chartData['titles'][] = $user->getLogin(); $chartData['keys'][] = $key;
$chartData['subscribers'][] = $user->getSubscribersCount(); $chartData['values'][] = $value;
} }
// Chart // Chart
$series = [[ $series = [[
'name' => $translator->trans('Subscribers'), 'name' => $translator->trans($amountLabel),
'data' => $chartData['subscribers'], 'data' => $chartData['values'],
]]; ]];
// Initializing chart // Initializing chart
$ob = new Highchart(); $ob = new Highchart();
$ob->chart->renderTo('topchart'); $ob->chart->renderTo($blockId);
$ob->chart->type('bar'); $ob->chart->type($type);
$ob->title->text($translator->trans('Top users')); $ob->title->text($translator->trans($bottomLabel));
$ob->xAxis->title(['text' => null]); $ob->xAxis->title(['text' => null]);
$ob->xAxis->categories($chartData['titles']); $ob->xAxis->categories($chartData['keys']);
$ob->yAxis->title(['text' => $translator->trans('amount')]); $ob->yAxis->title(['text' => $translator->trans($amountLabel)]);
$ob->plotOptions->bar([ $ob->plotOptions->bar([
'dataLabels' => [ 'dataLabels' => [
'enabled' => true 'enabled' => true