Skip to content

Commit 7155def

Browse files
committed
[Site] Adding a new LiveComponent + Chartjs demo!
1 parent 233df73 commit 7155def

File tree

8 files changed

+2103
-1
lines changed

8 files changed

+2103
-1
lines changed

ux.symfony.com/src/Controller/LiveComponentDemoController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Form\TodoListForm;
88
use App\Repository\FoodRepository;
99
use App\Repository\TodoListRepository;
10+
use App\Service\DinoStatsService;
1011
use App\Service\LiveDemoRepository;
1112
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1213
use Symfony\Component\HttpFoundation\Request;
@@ -75,4 +76,12 @@ public function inlineEdit(LiveDemoRepository $liveDemoRepository, FoodRepositor
7576
'food' => $foodRepository->findOneBy([]),
7677
]);
7778
}
79+
80+
#[Route('/chartjs', name: 'app_live_components_demo_chartjs')]
81+
public function chartJs(LiveDemoRepository $liveDemoRepository): Response
82+
{
83+
return $this->render('live_component_demo/chartjs.html.twig', parameters: [
84+
'demo' => $liveDemoRepository->find('chartjs_updating'),
85+
]);
86+
}
7887
}

ux.symfony.com/src/Form/MealPlannerForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function configureOptions(OptionsResolver $resolver)
6868
private function getAvailableFoodChoices(string $meal): array
6969
{
7070
$foods = match ($meal) {
71-
self::MEAL_BREAKFAST => ['Eggs 🍳', 'Bacon 🥓', 'Strawberries 🍓', 'Croissant 🥐'],
71+
self::MEAL_BREAKFAST => ['Eggs 🍳', 'Bacon 🥓', 'Strawberries 🍓', 'Croissant 🥐', 'Other', 'AnOther'],
7272
self::MEAL_SECOND_BREAKFAST => ['Bagel 🥯', 'Kiwi 🥝', 'Avocado 🥑', 'Waffles 🧇'],
7373
self::MEAL_ELEVENSES => ['Pancakes 🥞', 'Salad 🥙', 'Tea ☕️'],
7474
self::MEAL_LUNCH => ['Sandwich 🥪', 'Cheese 🧀', 'Sushi 🍱'],
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
class DinoStatsService
6+
{
7+
private ?array $rawData = null;
8+
9+
private const ALL_DINOS = 'all';
10+
11+
public static function getAllTypes(): array
12+
{
13+
return [
14+
self::ALL_DINOS,
15+
'sauropod',
16+
'large theropod',
17+
'small theropod',
18+
'ceratopsian',
19+
'euornithopod',
20+
'armoured dinosaur',
21+
];
22+
}
23+
24+
public function fetchData(int $start, int $end, array $types): array
25+
{
26+
$start = abs($start);
27+
$end = abs($end);
28+
29+
$steps = 10;
30+
$step = round(($start - $end) / $steps);
31+
32+
$labels = [];
33+
for ($i = 0; $i < $steps; $i++) {
34+
$current = $start - ($i * $step);
35+
// generate a random rgb color
36+
37+
$labels[] = $current.' mya';
38+
}
39+
40+
$datasets = [];
41+
42+
foreach ($types as $type) {
43+
$color = sprintf('rgb(%d, %d, %d, .4)', mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
44+
45+
$datasets[] = [
46+
'label' => ucwords($type),
47+
'data' => $this->getSpeciesCounts($start, $steps, $step, $type),
48+
'borderColor' => $color,
49+
'backgroundColor' => $color,
50+
];
51+
}
52+
53+
return [
54+
'labels' => $labels,
55+
'datasets' => $datasets
56+
];
57+
}
58+
59+
private function getRawData(): array
60+
{
61+
if (null === $this->rawData) {
62+
$this->rawData = json_decode(file_get_contents(__DIR__.'/data/dino-stats.json'), true);
63+
}
64+
65+
return $this->rawData;
66+
}
67+
68+
private function getSpeciesCounts(int $start, int $steps, int $step, string $type): array
69+
{
70+
$counts = [];
71+
for ($i = 0; $i < $steps; $i++) {
72+
$current = round($start - ($i * $step));
73+
$counts[] = $this->countSpeciesAt($current, $type);
74+
}
75+
76+
return $counts;
77+
}
78+
79+
private function countSpeciesAt(int $currentYear, string $type): int
80+
{
81+
$count = 0;
82+
foreach ($this->getRawData() as $dino) {
83+
if (($type !== self::ALL_DINOS) && $dino['type'] !== $type) {
84+
continue;
85+
}
86+
87+
if ($dino['from'] >= $currentYear && $dino['to'] <= $currentYear) {
88+
$count++;
89+
}
90+
}
91+
92+
return $count;
93+
}
94+
}

ux.symfony.com/src/Service/LiveDemoRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public function findAll(): array
4242
description: 'Activate an inline editing form with validation.',
4343
route: 'app_live_components_demo_inline_edit',
4444
),
45+
new LiveDemo(
46+
'chartjs_updating',
47+
name: 'Auto-Updating Chart',
48+
description: 'Render & Update a Chart.js chart in real-time.',
49+
route: 'app_live_components_demo_chartjs',
50+
),
4551
];
4652
}
4753

0 commit comments

Comments
 (0)