Skip to content

Commit 2fad426

Browse files
committed
final tweaks
1 parent d185d2d commit 2fad426

File tree

9 files changed

+83
-17
lines changed

9 files changed

+83
-17
lines changed

src/Autocomplete/assets/src/controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { TPluginHash } from 'tom-select/dist/types/contrib/microplugin';
44
import { RecursivePartial, TomSettings, TomTemplates } from 'tom-select/dist/types/types';
55

66
export interface AutocompletePreConnectOptions {
7-
options: any
7+
options: any;
88
}
99
export interface AutocompleteConnectOptions {
10-
tomSelect: TomSelect
11-
options: any
10+
tomSelect: TomSelect;
11+
options: any;
1212
}
1313

1414
export default class extends Controller {
@@ -393,7 +393,7 @@ export default class extends Controller {
393393
}
394394

395395
if (changeDisabledState) {
396-
this.changeTomSelectDisabledState((this.formElement.disabled));
396+
this.changeTomSelectDisabledState(this.formElement.disabled);
397397
}
398398

399399
if (changePlaceholder) {

src/Chartjs/assets/dist/controller.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export default class extends Controller {
44
static values: {
55
view: ObjectConstructor;
66
};
7+
private chart;
78
connect(): void;
9+
viewValueChanged(): void;
810
private dispatchEvent;
911
}

src/Chartjs/assets/dist/controller.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { Controller } from '@hotwired/stimulus';
22
import Chart from 'chart.js/auto';
33

44
class default_1 extends Controller {
5+
constructor() {
6+
super(...arguments);
7+
this.chart = null;
8+
}
59
connect() {
610
if (!(this.element instanceof HTMLCanvasElement)) {
711
throw new Error('Invalid element');
@@ -15,8 +19,23 @@ class default_1 extends Controller {
1519
if (!canvasContext) {
1620
throw new Error('Could not getContext() from Element');
1721
}
18-
const chart = new Chart(canvasContext, payload);
19-
this.dispatchEvent('connect', { chart });
22+
this.chart = new Chart(canvasContext, payload);
23+
this.dispatchEvent('connect', { chart: this.chart });
24+
}
25+
viewValueChanged() {
26+
if (this.chart) {
27+
this.chart.data = this.viewValue.data;
28+
this.chart.options = this.viewValue.options;
29+
this.chart.update();
30+
const parentElement = this.element.parentElement;
31+
if (parentElement && this.chart.options.responsive) {
32+
const originalWidth = parentElement.style.width;
33+
parentElement.style.width = (parentElement.offsetWidth + 1) + 'px';
34+
setTimeout(() => {
35+
parentElement.style.width = originalWidth;
36+
}, 0);
37+
}
38+
}
2039
}
2140
dispatchEvent(name, payload) {
2241
this.dispatch(name, { detail: payload, prefix: 'chartjs' });

src/Chartjs/assets/src/controller.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default class extends Controller {
1919
view: Object,
2020
};
2121

22+
private chart: Chart | null = null;
23+
2224
connect() {
2325
if (!(this.element instanceof HTMLCanvasElement)) {
2426
throw new Error('Invalid element');
@@ -35,9 +37,32 @@ export default class extends Controller {
3537
if (!canvasContext) {
3638
throw new Error('Could not getContext() from Element');
3739
}
38-
const chart = new Chart(canvasContext, payload);
40+
this.chart = new Chart(canvasContext, payload);
41+
42+
this.dispatchEvent('connect', { chart: this.chart });
43+
}
3944

40-
this.dispatchEvent('connect', { chart });
45+
/**
46+
* If the underlying data or options change, let's update the chart!
47+
*/
48+
viewValueChanged(): void {
49+
if (this.chart) {
50+
this.chart.data = this.viewValue.data;
51+
this.chart.options = this.viewValue.options;
52+
this.chart.update();
53+
54+
// Updating the chart seems to sometimes result in a chart that is
55+
// much smaller than the canvas size. Resizing the parent element
56+
// triggers a mechanism in Chart.js that fixes the size.
57+
const parentElement = this.element.parentElement;
58+
if (parentElement && this.chart.options.responsive) {
59+
const originalWidth = parentElement.style.width;
60+
parentElement.style.width = parentElement.offsetWidth + 1 + 'px';
61+
setTimeout(() => {
62+
parentElement.style.width = originalWidth;
63+
}, 0);
64+
}
65+
}
4166
}
4267

4368
private dispatchEvent(name: string, payload: any) {

src/Chartjs/assets/test/controller.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,23 @@ describe('ChartjsController', () => {
7676

7777
expect(chart.options.showLines).toBe(false);
7878
});
79+
80+
it('will update when the view data changes', async () => {
81+
const { chart, canvas } = await startChartTest(`
82+
<canvas
83+
data-testid='canvas'
84+
data-controller='check chartjs'
85+
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;"
86+
></canvas>
87+
`);
88+
89+
expect(chart.options.showLines).toBe(false);
90+
const currentViewValue = JSON.parse((canvas.dataset.chartjsViewValue as string));
91+
currentViewValue.options.showLines = true;
92+
canvas.dataset.chartjsViewValue = JSON.stringify(currentViewValue);
93+
94+
await waitFor(() => {
95+
expect(chart.options.showLines).toBe(true);
96+
});
97+
});
7998
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use App\Form\TodoListForm;
88
use App\Repository\FoodRepository;
99
use App\Repository\TodoListRepository;
10-
use App\Service\DinoStatsService;
1110
use App\Service\LiveDemoRepository;
1211
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1312
use Symfony\Component\HttpFoundation\Request;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function fetchData(int $start, int $end, array $types): array
3030
$step = round(($start - $end) / $steps);
3131

3232
$labels = [];
33-
for ($i = 0; $i < $steps; $i++) {
33+
for ($i = 0; $i < $steps; ++$i) {
3434
$current = $start - ($i * $step);
3535
// generate a random rgb color
3636

@@ -52,7 +52,7 @@ public function fetchData(int $start, int $end, array $types): array
5252

5353
return [
5454
'labels' => $labels,
55-
'datasets' => $datasets
55+
'datasets' => $datasets,
5656
];
5757
}
5858

@@ -68,7 +68,7 @@ private function getRawData(): array
6868
private function getSpeciesCounts(int $start, int $steps, int $step, string $type): array
6969
{
7070
$counts = [];
71-
for ($i = 0; $i < $steps; $i++) {
71+
for ($i = 0; $i < $steps; ++$i) {
7272
$current = round($start - ($i * $step));
7373
$counts[] = $this->countSpeciesAt($current, $type);
7474
}
@@ -80,12 +80,12 @@ private function countSpeciesAt(int $currentYear, string $type): int
8080
{
8181
$count = 0;
8282
foreach ($this->getRawData() as $dino) {
83-
if (($type !== self::ALL_DINOS) && $dino['type'] !== $type) {
83+
if ((self::ALL_DINOS !== $type) && $dino['type'] !== $type) {
8484
continue;
8585
}
8686

8787
if ($dino['from'] >= $currentYear && $dino['to'] <= $currentYear) {
88-
$count++;
88+
++$count;
8989
}
9090
}
9191

ux.symfony.com/src/Twig/DinoChartComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class DinoChartComponent
2626
public function __construct(
2727
private DinoStatsService $dinoStatsService,
2828
private ChartBuilderInterface $chartBuilder,
29-
)
30-
{
29+
) {
3130
}
3231

3332
#[ExposeInTemplate]
@@ -52,6 +51,7 @@ public function getChart(): Chart
5251
),
5352
],
5453
],
54+
'maintainAspectRatio' => false,
5555
]);
5656

5757
return $chart;

ux.symfony.com/templates/components/dino_chart.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545

4646
<hr>
4747

48-
{{ render_chart(chart) }}
48+
<div style="min-height: 400px;">
49+
{{ render_chart(chart) }}
50+
</div>
4951

5052
<small>Source: <a href="https://www.nhm.ac.uk/">National History Museum</a> courtesy of https://github.com/kjanjua26/jurassic-park</small>
5153
</div>

0 commit comments

Comments
 (0)