-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathChartWidget.php
More file actions
139 lines (109 loc) · 2.92 KB
/
ChartWidget.php
File metadata and controls
139 lines (109 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Filament\Widgets;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Support\RawJs;
use Illuminate\Contracts\Support\Htmlable;
use Livewire\Attributes\Locked;
abstract class ChartWidget extends Widget implements HasSchemas, HasActions
{
use Concerns\CanPoll;
use ChartWidget\Concerns\HasEmptyState;
use InteractsWithActions;
use InteractsWithSchemas;
/**
* @var array<string, mixed> | null
*/
protected ?array $cachedData = null;
#[Locked]
public ?string $dataChecksum = null;
public ?string $filter = null;
protected string $color = 'primary';
protected ?string $heading = null;
protected ?string $description = null;
protected ?string $maxHeight = null;
/**
* @var array<string, mixed> | null
*/
protected ?array $options = null;
protected bool $isCollapsible = false;
/**
* @var view-string
*/
protected string $view = 'filament-widgets::chart-widget';
public function mount(): void
{
$this->dataChecksum = $this->generateDataChecksum();
}
abstract protected function getType(): string;
protected function generateDataChecksum(): string
{
return md5(json_encode($this->getCachedData()));
}
/**
* @return array<string, mixed>
*/
protected function getCachedData(): array
{
return $this->cachedData ??= $this->getData();
}
/**
* @return array<string, mixed>
*/
protected function getData(): array
{
return [];
}
/**
* @return array<scalar, scalar> | null
*/
protected function getFilters(): ?array
{
return null;
}
public function getHeading(): string | Htmlable | null
{
return $this->heading;
}
public function getDescription(): string | Htmlable | null
{
return $this->description;
}
protected function getMaxHeight(): ?string
{
return $this->maxHeight;
}
/**
* @return array<string, mixed> | RawJs | null
*/
protected function getOptions(): array | RawJs | null
{
return $this->options;
}
public function updateChartData(): void
{
$newDataChecksum = $this->generateDataChecksum();
if ($newDataChecksum !== $this->dataChecksum) {
$this->dataChecksum = $newDataChecksum;
$this->dispatch('updateChartData', data: $this->getCachedData());
}
}
public function rendering(): void
{
$this->updateChartData();
}
public function getColor(): string
{
return $this->color;
}
public function isCollapsible(): bool
{
return $this->isCollapsible;
}
public function isEmpty(): bool
{
return empty($this->getCachedData());
}
}