devhereco/custom-chart

一个Laravel包,可以从任何模型中提取数据以实现自定义图表。

2.2 2024-06-17 10:44 UTC

This package is auto-updated.

Last update: 2024-09-17 11:22:20 UTC


README

CustomChart 是一个Laravel包,允许您从Eloquent模型中生成可定制的图表数据。它支持各种聚合函数和灵活的日期筛选选项。

stars contributors releases issues

简单用法

1. 生成图表数据

此函数生成您需要用于任何图表的数据。

控制器:

use Devhereco\CustomChart\CustomChart;

// ...

$chart = CustomChart::create(
    User::class, 
    'Registered Users Per Day', 
    'count', 
    'id', 
    7,
    'days'
);

return $chart;

示例输出

{
    "name": "Registered Users Per Day",
    "data": {
        "2022-12-19": 12,
        "2022-12-20": 15,
        "2022-12-21": 23,
        "2022-12-22": 52,
        "2022-12-23": 41,
        "2022-12-24": 12,
        "2022-12-25": 15,
        "2022-12-26": 73
    },
    "this_month": 243,
    "last_month": 200,
    "this_year": 2430,
    "last_year": 2000,
    "percentage_change": {
        "this_month": {
            "value": "21.50%",
            "status": "positive"
        },
        "last_month": {
            "value": "10.00%",
            "status": "positive"
        },
        "this_year": {
            "value": "21.50%",
            "status": "positive"
        }
    },
    "total": 243
}

安装

1. 使用Composer要求

composer require devhereco/custom-chart

2. 添加服务提供者(Laravel 5.4及以下版本)

最新版本的Laravel具有自动发现功能并自动添加服务提供者。如果您使用的是Laravel 5.4.x及以下版本,请记住将其添加到/app/config/app.php中的提供者数组中。

// ...
Devhereco\CustomChart\ServiceProvider::class,

可用报告和选项

该包目前支持各种类型的图表/报告

注意:从Laravel 8开始,所有模型都放在名为Models的文件夹中(例如,App\Models\...)。

使用所有选项的示例

控制器

use Devhereco\CustomChart\CustomChart;

// ...

$chart = CustomChart::create(
    User::class, 
    'Registered Users Per Day', 
    'count', 
    'id', 
    7, // Show last 7 days/weeks/months/years
    'days', // Interval type
    true, // Show total
    'Y-m-d'
);

return $chart;

示例输出

{
    "name": "Registered Users Per Day",
    "data": {
        "2022-12-19": 12,
        "2022-12-20": 15,
        "2022-12-21": 23,
        "2022-12-22": 52,
        "2022-12-23": 41,
        "2022-12-24": 12,
        "2022-12-25": 15,
        "2022-12-26": 73
    },
    "this_month": 243,
    "last_month": 200,
    "this_year": 2430,
    "last_year": 2000,
    "percentage_change": {
        "this_month": {
            "value": "21.50%",
            "status": "positive"
        },
        "last_month": {
            "value": "10.00%",
            "status": "positive"
        },
        "this_year": {
            "value": "21.50%",
            "status": "positive"
        }
    },
    "total": 243
}