Line Chart

You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.

Provides the ability to create a line chart for metric

namespace MoonShine\Resources;
 
use MoonShine\Metrics\LineChartMetric;
 
class PostResource extends Resource
{
//...
 
public function metrics(): array
{
return [
LineChartMetric::make('Orders')
->line([
'Profit' => Order::query()
->selectRaw('SUM(price) as sum, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('sum','date')
->toArray()
])
->line([
'Avg' => Order::query()
->selectRaw('AVG(price) as avg, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('avg','date')
->toArray()
], '#EC4176'),
];
}
 
//...
}

or using one line method

namespace MoonShine\Resources;
 
use MoonShine\Metrics\LineChartMetric;
 
class PostResource extends Resource
{
//...
 
public function metrics(): array
{
return [
LineChartMetric::make('Orders')
->line([
'Profit' => Order::query()
->selectRaw('SUM(price) as sum, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('sum','date')
->toArray(),
'Avg' => Order::query()
->selectRaw('AVG(price) as avg, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('avg','date')
->toArray()
],[
'red', 'blue'
])
];
}
 
//...
}

By default, LineChart graphics sort keys in ascending order. This feature can be used with the usual withoutSortKeys() method.

LineChartMetric::make('Orders')
->line([
'Profit' => Order::query()
->selectRaw('SUM(price) as sum, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('sum','date')
->toArray()
])
->withoutSortKeys(),