PHP code example of happenv-com / filament-enhanced-charts
1. Go to this page and download the library: Download happenv-com/filament-enhanced-charts library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
happenv-com / filament-enhanced-charts example snippets
class OrdersChart extends EnhancedChartWidget
{
protected static ?string $heading = 'Orders per day';
protected function getOption(): Option
{
return Option::cartesian()
->xAxis(CategoryAxis::make()->data(['Mon', 'Tue', 'Wed']))
->series(LineSeries::make()->name('Orders')->smooth()->data([12, 20, 15]));
}
}
use Happenv\FilamentEnhancedCharts\FilamentEnhancedChartsPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
FilamentEnhancedChartsPlugin::make(),
]);
}
use Flowframe\Trend\Trend;
use Happenv\FilamentEnhancedCharts\Data\ChartData;
$data = ChartData::fromTimeSeries(
Trend::query($this->scopedOrders())
->between(now()->subDays(30), now())
->perDay()
->sum('total'),
);
Option::cartesian()
->xAxis(CategoryAxis::make()->data($data->labels()))
->series(LineSeries::make()->smooth()->data($data->values()));
use Happenv\FilamentEnhancedCharts\Concerns\HasChartData;
use Happenv\FilamentEnhancedCharts\Data\ChartData;
use Happenv\FilamentEnhancedCharts\Enums\ChartType;
use Happenv\FilamentEnhancedCharts\Widgets\EnhancedChartWidget;
class SalesByChannelChart extends EnhancedChartWidget
{
use HasChartData;
protected static ?string $heading = 'Sales by channel';
protected function getData(): ChartData
{
return ChartData::fromPairs(Order::query()->pluck('total', 'channel'));
}
protected function chartType(): ChartType
{
return ChartType::Bar; // Line (default), Area, Bar, or Pie
}
}
use Filament\Support\Colors\Color;
BarSeries::make()
->color(Color::Emerald) // 500 shade
->data($values);
LineSeries::make()
->color(Color::Amber[600]) // an explicit shade
->data($values);
protected static bool $scrollable = false;
Option::make()->scrollable(false);
protected static ?string $heading = 'Revenue'; // or override getHeading()
protected static ?string $subheading = 'Last 30 days'; // or getSubheading()
protected static bool $isCollapsible = true; // or isCollapsible()
protected static int $contentHeight = 400; // px, default 300; or getContentHeight()
protected static ?string $footer = 'Updated hourly'; // or getFooter() (string|Htmlable|View)
protected static string $chartId = 'revenueChart'; // stable DOM id, optional
protected static string $renderer = 'canvas'; // or 'svg'; or getRenderer()
protected int | string | array $columnSpan = 'full';
protected static bool $deferLoading = true;
protected function getOption(): Option
{
if (! $this->readyToLoad) {
return Option::make();
}
return Option::cartesian()->series(/* the expensive part */);
}
protected static ?string $loadingIndicator = 'Loading…'; // or getLoadingIndicator() returning a View
use Filament\Forms\Components\DatePicker;
use Filament\Schemas\Schema;
use Filament\Widgets\ChartWidget\Concerns\HasFiltersSchema;
use Happenv\FilamentEnhancedCharts\Contracts\HasFiltersSchema as HasFiltersSchemaContract;
use Happenv\FilamentEnhancedCharts\Widgets\EnhancedChartWidget;
class BlogPostsChart extends EnhancedChartWidget implements HasFiltersSchemaContract
{
use HasFiltersSchema;
public function filtersSchema(Schema $schema): Schema
{
return $schema->components([
DatePicker::make('date_start')->default(now()->subMonth()),
DatePicker::make('date_end')->default(now()),
]);
}
public function updatedInteractsWithSchemas(string $statePath): void
{
parent::updatedInteractsWithSchemas($statePath);
$this->updateOptions();
}
protected function getOption(): Option
{
$start = $this->filters['date_start'] ?? null;
$end = $this->filters['date_end'] ?? null;
// ...
}
}
use Happenv\FilamentEnhancedCharts\Option\Component\VisualMap;
use Happenv\FilamentEnhancedCharts\Option\Series\MapSeries;
/** @return array<string, string> */
public function getMaps(): array
{
return ['world' => asset('geo/world.json')];
}
protected function getOption(): Option
{
return Option::make()
->visualMap(VisualMap::continuous()->min(0)->max(100)->calculable())
->series(MapSeries::make()->map('world')->roam()->data($rows));
}
use Happenv\FilamentEnhancedCharts\Columns\EnhancedChartColumn;
EnhancedChartColumn::make('trend')
->sparkline(fn (Product $record): array => $record->daily_sales)
->fill(), // area fill; ->bars() for bar sparklines
EnhancedChartColumn::make('ohlc')
->width(160) // px, '100%', or a Closure; height(int) too
->candles(fn (Product $record): array => $record->ohlc_rows), // [open, close, low, high] rows
EnhancedChartColumn::make('mix')
->pie(fn (Order $record): array => ['B2B' => 60, 'B2C' => 40]), // donut
EnhancedChartColumn::make('custom')
->chart(fn (Server $record): ?Option => Option::make()
->series(GaugeSeries::make()->data([DataPoint::make($record->cpu)]))),
use function Pest\Livewire\livewire;
livewire(RevenueChart::class)
->assertChartSeriesCount(2)
->assertChartHasSeries('line')
->assertChartOptions(fn (array $options): bool => $options['series'][0]['smooth'] === true);