PHPackages                             onemoreahmad/laravel-easy-metrics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. onemoreahmad/laravel-easy-metrics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

onemoreahmad/laravel-easy-metrics
=================================

Generate metrics with ease and precision.

02PHP

Since Mar 31Pushed 1y agoCompare

[ Source](https://github.com/onemoreahmad/laravel-easy-metrics)[ Packagist](https://packagist.org/packages/onemoreahmad/laravel-easy-metrics)[ RSS](/packages/onemoreahmad-laravel-easy-metrics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Easy metrics banner](./art/banner.png)](./art/banner.png)

 [![Workflow status](https://camo.githubusercontent.com/849f90bb748fe39087d8bb663f38bb6a7946a500a05b7698af1189944ee9612a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73616b616e6a6f2f6c61726176656c2d656173792d6d6574726963732f74657374732e796d6c3f7374796c653d666f722d7468652d6261646765)](https://github.com/sakanjo/laravel-easy-metrics/actions) [![Laravel v10.x](https://camo.githubusercontent.com/bfea501efe5759f52cfebee79ebb907199c9e88ae0bd65948d40ba67a1ad73fa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d7631302e782d4646324432303f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)](https://laravel.com) [![PHP 8.0](https://camo.githubusercontent.com/2091ef06611f32d698bc7cc9a7331556f251f26d41ee5820639e6acabbe77ad0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e302d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870)](https://php.net)

🔥 Easy metrics
==============

[](#-easy-metrics)

Easily create metrics for your application.

> ✨ Help support the maintenance of this package by [sponsoring me](https://github.com/sponsors/sakanjo).

> Designed to work with **Laravel**, **Filament**, [Easy enum](https://github.com/sakanjo/laravel-easy-enum), and more.

[![Preview](./art/preview.png)](./art/preview.png)

🚀 Supported metrics
-------------------

[](#-supported-metrics)

- **Bar** metric
- **Doughnut** metric
- **Line** metric
- **Pie** metric
- **Polar** metric
- **Trend** metric
- **Value** metric

Table of contents
=================

[](#table-of-contents)

- [Install](#-install)
- [Usage](#-usage)
    - [Value metric](#value-metric)
    - [Doughnut metric](#doughnut-metric)
    - [Trend metric](#trend-metric)
    - [Other metrics](#other-metrics)
- [Ranges](#ranges)
    - [Available custom ranges](#available-custom-ranges)
- [Growth rates](#growth-rates)
    - [Using value metric](#using-value-metric)
    - [Using trend metric](#using-trend-metric)
    - [Using doughnut metric](#using-doughnut-metric)
    - [Available growth rate types](#available-growth-rate-types)
- [Tips](#-tips)
    - [using getLabel](#using-getLabel)
- [Practical examples](#-practical-examples)
    - [Filamentphp v3 widgets](#filamentphp-v3-widgets)
- [Support the development](#-support-the-development)
- [Credits](#%EF%B8%8F-credits)
- [License](#-license)

📦 Install
---------

[](#-install)

```
composer require sakanjo/laravel-easy-metrics

```

🦄 Usage
-------

[](#-usage)

### Value metric

[](#value-metric)

```
use SaKanjo\EasyMetrics\Metrics\Value;
use App\Models\User;

$data = Value::make(User::class)
    ->count();
```

#### Query types

[](#query-types)

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

##### Min

[](#min)

```
Value::make(User::class)
    ->min('age');
```

##### Max

[](#max)

```
Value::make(User::class)
    ->max('age');
```

##### Sum

[](#sum)

```
Value::make(User::class)
    ->sum('age');
```

##### Average

[](#average)

```
Value::make(User::class)
    ->average('age');
```

##### Count

[](#count)

```
Value::make(User::class)
    ->count();
```

### Doughnut metric

[](#doughnut-metric)

```
use SaKanjo\EasyMetrics\Metrics\Doughnut;
use App\Models\User;

[$labels, $data] = Doughnut::make(User::class)
    ->count('gender');
```

#### Query types

[](#query-types-1)

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

##### Min

[](#min-1)

```
Doughnut::make(User::class)
    ->min('age', 'gender');
```

##### Max

[](#max-1)

```
Doughnut::make(User::class)
    ->max('age', 'gender');
```

##### Sum

[](#sum-1)

```
Doughnut::make(User::class)
    ->sum('age', 'gender');
```

##### Average

[](#average-1)

```
Doughnut::make(User::class)
    ->average('age', 'gender');
```

##### Count

[](#count-1)

```
Doughnut::make(User::class)
    ->count('gender');
```

### Trend metric

[](#trend-metric)

```
use SaKanjo\EasyMetrics\Metrics\Trend;
use App\Models\User;

[$labels, $data] = Trend::make(User::class)
    ->countByMonths();
```

#### Query types

[](#query-types-2)

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

##### Min

[](#min-2)

```
$trend->minByYears('age');
$trend->minByMonths('age');
$trend->minByWeeks('age');
$trend->minByDays('age');
$trend->minByHours('age');
$trend->minByMinutes('age');
```

##### Max

[](#max-2)

```
$trend->maxByYears('age');
$trend->maxByMonths('age');
$trend->maxByWeeks('age');
$trend->maxByDays('age');
$trend->maxByHours('age');
$trend->maxByMinutes('age');
```

##### Sum

[](#sum-2)

```
$trend->sumByYears('age');
$trend->sumByMonths('age');
$trend->sumByWeeks('age');
$trend->sumByDays('age');
$trend->sumByHours('age');
$trend->sumByMinutes('age');
```

##### Average

[](#average-2)

```
$trend->averageByYears('age');
$trend->averageByMonths('age');
$trend->averageByWeeks('age');
$trend->averageByDays('age');
$trend->averageByHours('age');
$trend->averageByMinutes('age');
```

##### Count

[](#count-2)

```
$trend->countByYears();
$trend->countByMonths();
$trend->countByWeeks();
$trend->countByDays();
$trend->countByHours();
$trend->countByMinutes();
```

### Other metrics

[](#other-metrics)

- `Bar extends Trend`
- `Line extends Trend`
- `Doughnut extends Pie`
- `Polar extends Pie`

Ranges
------

[](#ranges)

Every metric class contains a ranges method, that will determine the range of the results based on it's date column.

```
use SaKanjo\EasyMetrics\Metrics\Trend;
use SaKanjo\EasyMetrics\Metrics\Enums\Range;
use App\Models\User;

Value::make(User::class)
    ->range(30)
    ->ranges([
        15, 30, 365,
        Range::TODAY, // Or 'TODAY'
    ]);
```

### Available custom ranges

[](#available-custom-ranges)

- `Range::TODAY`
- `Range::YESTERDAY`
- `Range::MTD`
- `Range::QTD`
- `Range::YTD`
- `Range::ALL`

Growth rates
------------

[](#growth-rates)

Growth rate, expressed as both a **value** and a **percentage**, measures the change in a quantity over **time**, showing the speed of its expansion or contraction in both absolute and relative terms.

### Using **Value** metric

[](#using-value-metric)

```
use SaKanjo\EasyMetrics\Metrics\Value;
use SaKanjo\EasyMetrics\Enums\GrowthRateType;
use App\Models\User;

[$value, $growth] = Value::make(User::class)
    ->withGrowthRate()
    ->growthRateType(GrowthRateType::Value) // default is `Percentage`
    ->count();
```

### Using **Trend** metric

[](#using-trend-metric)

```
use SaKanjo\EasyMetrics\Metrics\Trend;
use App\Models\User;

[$labels, $data, $growth] = Trend::make(User::class)
    ->withGrowthRate()
    ->countByYears();
```

### Using **Doughnut** metric

[](#using-doughnut-metric)

```
use SaKanjo\EasyMetrics\Metrics\Doughnut;
use App\Models\User;

[$labels, $data, $growth] = Doughnut::make(User::class)
    ->withGrowthRate()
    ->count('gender');
```

### Available growth rate types

[](#available-growth-rate-types)

- `GrowthRateType::Value`
- `GrowthRateType::Percentage`

🔥 Tips
------

[](#-tips)

#### Using getLabel

[](#using-getlabel)

You can use the `getLabel` method to customize the retreived data labels, for example:

```
