Dashboards and widgets#
A widget is a card computed on the server for a date range. Widgets sit on dashboards, or above a resource’s list.
final class RevenueStat extends VectorWidget
{
public function __construct(private readonly SqlLink $db) {}
public function type(): string { return self::STAT; }
public function cacheSeconds(): int { return 60; }
public function calculate(WidgetRequest $request): WidgetResult
{
// one query for $request->from .. $request->to
return WidgetResult::stat($current, $previous, 'money:EUR');
}
}
Widget types#
Type |
Shows |
|---|---|
|
One figure, compared with the previous period. |
|
A series over time. |
|
A breakdown into slices, such as orders by status. |
|
A figure against a target. |
|
A few rows, optionally linking to the full list. |
|
The queue dashboard’s card: jobs waiting and failed, linking to the Queues page. Only |
A value’s format is a number, money:EUR, percent or duration.
An application can replace a built-in type’s card in the UI, through
vector.widgets in its app.config or window.Vector.defineWidget()
in a runtime extension (Customizing the UI); the data keeps that
type’s shape.
Extension widget types#
type() may also return an extension type: a lower-case kebab-case name
that is none of the six above, such as sparkline. Any other type is
refused when the registry compiles. Vector ships no card for an
extension type. The application registers the card for it in its UI,
either in its runtime extension with
window.Vector.defineWidget(type, component) or in its app.config
under vector.widgets (Customizing the UI). A card whose type
has no renderer says that none is registered for the type, instead of
guessing at a shape.
calculate() answers such a widget with WidgetResult::extension():
public function type(): string { return 'sparkline'; }
public function calculate(WidgetRequest $request): WidgetResult
{
return WidgetResult::extension('sparkline', ['samples' => [3, 1, 4]]);
}
The card is a runtime extension, listed in VECTOR_UI_EXTENSIONS
(hosted) or runtimeConfig.public.vector.extensions (standalone)
(Customizing the UI); it receives the answer as data and the
widget’s descriptor as widget:
export default function ({ vue, vuetify, defineWidget }) {
defineWidget('sparkline', {
props: { data: {}, widget: {} },
setup: props => () => vue.h(vuetify.VChip, { size: 'small' }, () => props.data.samples.join(', ')),
})
}
Alternatively, map the type to a globally registered component name in
the app’s app.config: vector: { widgets: { sparkline: 'MySparkline' } }.
The data reaches the card as the widget wrote it, beside the type,
range and cached that Vector sets on every answer, so it cannot use
those three keys; a built-in type is refused too, since its card reads
the shape its own factory builds. None of the built-in shapes applies.
Ranges, caching, canView() and translated labels work as for any
widget.
Shaping a widget#
Method |
Effect |
|---|---|
|
The ranges offered, as key => label, the first the default; |
|
3, 4, 6 or 12 columns of the grid. |
|
The URL segment (by default the class name without |
|
Who sees it, on top of the dashboard’s or resource’s own visibility. |
|
Above zero, memoizes the result in a bounded per-worker cache, keyed by widget, range and the account it was computed for. |
|
Shares one cached result between all admins, for figures that are the same for everyone. |
Dashboards#
#[Dashboard]
final class MainDashboard extends VectorDashboard
{
public function widgets(): array { return [RevenueStat::class, OrdersTrend::class]; }
}
A dashboard’s name is its class name without Dashboard, in kebab-case
(MainDashboard is main), unless name() says otherwise. The
dashboard named main (or, without one, the first) is the admin’s
home page; the others appear in the navigation drawer under their own
labels. canView() hides a whole dashboard from a user.
Resource widgets#
A resource’s widgets() appear above its list. A widget runs its own
query: when the resource’s scope() narrows what a user may see, narrow
the widget’s query the same way, and leave cacheShared() false so one
admin is never shown another’s figures.
See also#
Resources: badges, the other at-a-glance count.
Customizing the UI: a renderer of your own for a widget type.