igraal/stats-table

该包已被废弃且不再维护。未建议替代包。

处理具有多个格式化程序的统计表的辅助工具

v1.0.0 2018-03-29 15:14 UTC

This package is not auto-updated.

Last update: 2024-02-17 11:57:16 UTC


README

PHP 库,用于处理统计表和 CSV、JSON 和 Excel 导出。 构建状态

摘要

此库帮助您根据某些数据创建统计表。您提供数据、表头以及您希望在页脚行中显示的内容,然后您可以将其表导出为 JSON、CSV 或 Excel 文件。

这对于在 HTML 前端显示大量您想要查看的表格非常有用,并且当您想要添加将此数据导出为 CSV 或 Excel 文件的功能时。

安装

使用 composer

使用 composer,只需将以下 require 添加到您的 composer.json 中

"require": {
    ... ,
    "igraal/stats-table": "dev-master"
}

用法

使用 StatsTable 类

StatsTable 是用于保存数据的类。它接受一个必需参数和四个可选参数。创建新表的简单方法是将数据本身及其表头传递(即使表头是可选的)。

use IgraalOSL\StatsTable\StatsTable;

$data = [
    ['date' => '2014-01-01', 'hits' => 32500],
    ['date' => '2014-01-02', 'hits' => 48650],
];
$headers = ['date' => 'Date', 'hits' => 'Number of hits'];
$statsTable = new StatsTable($data, $headers);

导出表格

目前支持三种格式:Excel、CSV 和 JSON。因此,您可以使用同一表格进行 AJAX 调用或下载。

首先,创建您的导出器,然后导出您的数据。

use IgraalOSL\StatsTable\Dumper\Excel\ExcelDumper;

$excelDumper = new ExcelDumper();
$excelContents = $excelDumper->dump($statsTable);

header('Content-type: application/vnd.ms-excel');
echo $excelContents

使用 stats table builder

为了帮助您构建表格,您可以使用 StatsTableBuilder 类。它帮助您合并多个表中的数据,并可以创建自动计算的列。它还帮助您构建聚合(即页脚行),具有多种可能性:比率、总和、平均值或静态内容。

use IgraalOSL\StatsTable\StatsTableBuilder;

$data = [
    '2014-01-01' => ['hits' => 32500],
    '2014-01-02' => ['hits' => 48650],
];

$statsTableBuilder = new StatsTableBuilder(
    $data,
    ['hits' => 'Number of hits']
);
$statsTableBuilder->addIndexesAsColumn('date', 'Date');

$statsTable = $statsTableBuilder->build();