marshmallow / statistics-numerics
此部分将包含 marshmallow 核心的所有统计逻辑。
Requires
- php: ^7.1|^8.0
- marshmallow/commands: ^1.0
This package is auto-updated.
Last update: 2024-09-05 10:54:12 UTC
README
Marshmallow 统计
使用 Marshmallow 统计包,可以快速轻松地生成 JSON 输出,适用于 Numerics 应用。此包默认与 Numerics 的 Basic Auth 版本配合使用,以保护 JSON 文件。
安装
composer require marshmallow/statistics-numerics
当 composer 准备就绪后,创建一个 Basic Auth 用户,使其可以通过以下 artisan 命令在线查看这些 JSON 文件。
php artisan statistics:install
检查是否正常工作
完成安装后,您可以直接查看以下示例图表。如果您访问以下 URL 之一,您需要使用在安装过程中创建的 Basic Auth 凭证进行登录。
- https://marshmallow.dev/statistics/demo/toplist
- https://marshmallow.dev/statistics/demo/timer
- https://marshmallow.dev/statistics/demo/piechart
- https://marshmallow.dev/statistics/demo/numberanddifference
- https://marshmallow.dev/statistics/demo/namedlinegraph
- https://marshmallow.dev/statistics/demo/linegraph
- https://marshmallow.dev/statistics/demo/hourdensity
- https://marshmallow.dev/statistics/demo/gauge
- https://marshmallow.dev/statistics/demo/daydensity
- https://marshmallow.dev/statistics/demo/number
- https://marshmallow.dev/statistics/demo/label
颜色选项
您可以给所有图表添加颜色。这是 Numerics 应用中图表的背景颜色。以下是当前可用的所有颜色列表。
(new Number)->title('Number')
->red()
->blue()
->green()
->purple()
->orange()
->midnightBlue()
->coffee()
->burgundy()
->wintergreen()
->value(123);
您还可以调用 ->color('red')
,如果这样更方便。如果您在此方法中传入一个不存在的颜色,将会抛出 Exception
。
创建您自己的统计
为了创建自己的统计,您需要创建一个控制器来收集统计信息。使用 php artisan make:controller MyOwnStatisticsController
创建控制器。请确保此控制器扩展了此包的 BaseController,以便使用 Basic Auth 功能。
然后,在新的控制器中创建第一个方法,例如 salesToday()
,并返回一个 Number
图表。
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Marshmallow\Statistics\Http\Statistics\Controllers\StatisticsController as StatisticsBaseController;
class MyOwnStatisticsController extends StatisticsBaseController
{
public function salesToday ()
{
return (new \Marshmallow\Statistics\Statistics\Number)->title('Verkoop vandaag')
->color('green')
->value(Orders::get()->count());
}
}
完成此操作后,创建一个路由,就像您在 Laravel 中所做的那样,以使这些数据可用。
Route::get('/stats/sales-today', 'MyOwnStatisticsController@salesToday');
现在,您可以通过将 URL {{jouw domein}}/stats/sales-today
添加到应用中,在 Numerics 应用中查看这些数据。请注意,在应用中选择与您在方法中使用相同的图表。在此示例中,我们使用了 Number
,因此必须在 Numerics 应用中选择 Basic Widgets - Number from authenticated JSON data
在 Custom JSON - Basic Auth
类别中。
不同的图表
以下是此包中可用的不同图表列表。点击查看如何调用它们以及有哪些选项。