Stats and dashboards
Stat tiles show a figure with its trend and a sparkline. With a tile layout, a chart and a grid, they make a dashboard.
php
<?php
use Caelrik\Ui\Components\Chart;
use Caelrik\Ui\Components\Grid;
use Caelrik\Ui\Components\Stat;
use Caelrik\Ui\Components\Stats;
use Caelrik\Ui\Components\TileLayout;
return [
Stats::make('kpis')->label('This month')->columns(4)->add(
Stat::make('kpi-revenue')->label('Revenue')->value('$12,400')
->trend(12.5, 'vs last month')->sparkline([8, 9, 12, 11, 14, 13, 16]),
Stat::make('kpi-orders')->label('Orders')->value(312)
->trend(-3, 'vs last month')->sparkline([30, 34, 31, 29, 28]),
Stat::make('kpi-churn')->label('Churn')->value('2.1%')->goodWhen('down')
->trend(-0.4, 'vs last month', ' pts')->sparkline([2.6, 2.4, 2.3, 2.1])->color('success'),
Stat::make('kpi-tickets')->label('Open tickets')->value(18)->trend(0)->description('Answered within a day.'),
),
TileLayout::make('widgets')->columns(3)
->tile('sales', 'Sales by month', Chart::make('sales-chart')->type('line')->title('Sales by month', false)
->labels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
->dataset('Sales', [120, 190, 150, 210, 240, 230]), cols: 2, rows: 2)
->tile('visits', 'Visits', Stat::make('tile-visits')->label('Visits today')->value(1204)
->trend(8, 'vs yesterday')->sparkline([900, 1010, 980, 1100, 1204]))
->tile('orders', 'Latest orders', Grid::make('orders-grid')->caption('Three most recent orders')
->column('id', 'Order')->column('total', 'Total')
->rows([
['id' => 'A-1', 'total' => '$40'],
['id' => 'A-2', 'total' => '$95'],
['id' => 'A-3', 'total' => '$12'],
]))
->reorderable()->resizable(),
];Stat tiles
php
Stat::make('revenue')
->label('Revenue')
->value('$12,400') // a string, or a number: 1204 shows "1,204"
->trend(12.5, 'vs last month') // +12.5%; trend(-0.4, '', ' pts') for points
->goodWhen('down') // for costs, churn, waiting times
->sparkline([8, 9, 12, 11, 14, 13, 16])
->color('primary') // the sparkline's color
->description('Paid invoices only.')
->link('/reports/revenue'); // the label links, its click area covers the tile- The trend is said in words: "▲ Up 12.5% vs last month". The arrow is hidden from screen readers, and the color (green when good, red when bad) only repeats what the words say.
- The sparkline is an SVG drawn by PHP: no JavaScript, no inline styles. It is an image named after its values: "Revenue, last 7 values: from 8 to 16, lowest 8, highest 16." Its colors keep a 3:1 contrast in light and dark modes; in forced colors it uses the system's text color.
labels(['up' => ..., 'down' => ..., 'flat' => ..., 'sparkline' => ...])translates them.
A row of stats
php
Stats::make('kpis')->label('This month')->columns(4)->add($revenue, $orders, $churn, $tickets);A list named by its label: screen readers say "This month, list, 4 items". It has 1 to 6 columns on large screens, two on tablets and one on phones.
Dashboard widgets
A dashboard is a tile layout whose tiles hold any component: a Chart, a Grid, a Stat, a RecordView.
php
TileLayout::make('widgets')->columns(3)
->tile('sales', 'Sales by month', $chart, cols: 2, rows: 2)
->tile('visits', 'Visits', $visitsStat)
->tile('orders', 'Latest orders', $ordersGrid)
->reorderable()->resizable()->remember();- Charts follow their tile: when a tile is resized, its chart redraws at the new size.
- A stat in a tile loses its own border, so tiles are not boxes in boxes.