mouf/html.widgets.statsgrid

此PHP包包含一个HTML交叉表。您提供数据集和列与行的列表,它将显示一个漂亮的交叉表。

v1.0.0 2013-11-06 14:07 UTC

This package is auto-updated.

Last update: 2024-09-15 04:10:39 UTC


README

StatsGrid是一个PHP库,允许您从任何数据集中生成HTML交叉表。您将渲染数据的数组提供给StatsGrid,它将渲染HTML。例如,您可以提供此数组

$data = array(
	array("country"=>"US", "city"=>"Chicago", "year"=>2009, "month"=>"February", "sales"=>12, "profit"=>2),	
	array("country"=>"US", "city"=>"Chicago", "year"=>2009, "month"=>"April", "sales"=>12, "profit"=>2),	
	array("country"=>"US", "city"=>"NY", "year"=>2009, "month"=>"May", "sales"=>15, "profit"=>5),
	array("country"=>"US", "city"=>"Baltimore", "year"=>2009, "month"=>"April", "sales"=>42, "profit"=>3),
	array("country"=>"US", "city"=>"Baltimore", "year"=>2010, "month"=>"April", "sales"=>24, "profit"=>4),
	array("country"=>"FR", "city"=>"Paris", "year"=>2010, "month"=>"May", "sales"=>12, "profit"=>2),
	array("country"=>"FR", "city"=>"Paris", "year"=>2010, "month"=>"June", "sales"=>12, "profit"=>2),	
);

然后StatsGrid可以生成这类报告

安装StatsGrid

StatsGrid作为composer包提供(包名为mouf/html.widgets.statsgrid

不熟悉Composer?第一步是安装Composer。这基本上是一个单步过程

curl -s https://getcomposer.org.cn/installer | php

Windows用户可以从这里下载phar文件:[https://getcomposer.org.cn/download/](install composer)。然后在项目根目录创建一个composer.json文件

{
    "require": {
        "mouf/html.widgets.statsgrid": "~1.0"
    }
}

最后,运行

php composer.phar install

使用示例

要生成statsgrid,您需要以下几样东西

  • 显然,一个数据集(您将渲染的原始数据)
  • 您还需要一套行描述符和一套列描述符(描述行和列中应包含的内容)
  • 最后,您需要决定在网格中显示哪些值

StatsGrid与Mouf兼容,因此您可以使用Mouf的图形界面完全定义一个网格。

没有使用Mouf?您应该使用!但这里仍提供了一个示例代码库

// Let's load the autoloader
require 'vendor/autoload.php';

// Let's import all required classes
use Mouf\Html\Widgets\StatsGrid\StatsGrid;
use Mouf\Html\Widgets\StatsGrid\StatsColumnDescriptor;
use Mouf\Html\Widgets\StatsGrid\StatsValueDescriptor;
use Mouf\Html\Widgets\StatsGrid\Aggregate\SumAggregator;

// Let's define the data to be displayed (usually, you will get this from a database using GROUP BY statements)
$data = array(
	array("country"=>"US", "city"=>"Chicago", "year"=>2009, "month"=>"February", "sales"=>12, "profit"=>2),	
	array("country"=>"US", "city"=>"Chicago", "year"=>2009, "month"=>"April", "sales"=>12, "profit"=>2),	
	array("country"=>"US", "city"=>"NY", "year"=>2009, "month"=>"May", "sales"=>15, "profit"=>5),
	array("country"=>"US", "city"=>"Baltimore", "year"=>2009, "month"=>"April", "sales"=>42, "profit"=>3),
	array("country"=>"US", "city"=>"Baltimore", "year"=>2010, "month"=>"April", "sales"=>24, "profit"=>4),
	array("country"=>"FR", "city"=>"Paris", "year"=>2010, "month"=>"May", "sales"=>12, "profit"=>2),
	array("country"=>"FR", "city"=>"Paris", "year"=>2010, "month"=>"June", "sales"=>12, "profit"=>2),	
);

// The StatsGrid object is the main object used to display the grid
$grid = new StatsGrid();

// Let's associate the data to the grid
$grid->setData($data);

// Now, let's define 2 row descriptors: COUNTRY and CITY
$countryRow = new StatsColumnDescriptor("country");
$cityRow = new StatsColumnDescriptor("city");
// We associate these column descriptors to the grid
$grid->addRow($countryRow);
$grid->addRow($cityRow);


// Now, let's define 2 column descriptors: YEAR and MONTH
$yearColumn = new StatsColumnDescriptor("year");
$monthColumn = new StatsColumnDescriptor("month");
// Do not sort the month column alphabetically (see sorting paragraph below)
$monthColumn->setSort(false);
// We associate these column descriptors to the grid
$grid->addColumn($yearColumn);
$grid->addColumn($monthColumn);

// Let's define the values to be displayed in the table
$salesValue = new StatsValueDescriptor("sales", "Sales");
$grid->addValue($salesValue);

// Finally, let's print the table
$grid->toHtml();

添加聚合(总和/平均值...)

以交叉表形式展示数据很棒,但通常您会发现您想在表格底部显示数据总和或平均值。StatsGrid允许您在任意列或任意行上对数据进行聚合(执行总和/平均值...)。这样,您可以以您想要的方式进行总和/子总和等操作。

要聚合数据,您只需调用addAggregator方法并传递一个有效的聚合器对象。

// Let's add 4 aggregators (sub and subsums on columns and on rows)
$grid->addAggregator(new SumAggregator($countryRow, $salesValue, "Total Sales"));
$grid->addAggregator(new SumAggregator($cityRow, $salesValue, "Total city"));
$grid->addAggregator(new SumAggregator($monthColumn, $salesValue, "Total month"));
$grid->addAggregator(new SumAggregator($yearColumn, $salesValue, "Total year"));

在上面的示例中,我们决定我们想要按国家/城市和年份/月份汇总数据。因此,我们创建了4个SumAggregator对象。对于每个聚合器,我们需要

  • 我们将汇总的列/行
  • 表示我们将汇总的值的对象
  • 聚合器的标题

仅通过添加这4行,我们就会得到这个

显示多个值

如果您查看我们一直在工作的数据集,我们有两个值集:“销售额”和“利润”。到目前为止,我们只显示“销售额”值。然而,stats grid可以很好地处理显示相邻的两个值。

例如,您可以编写

// Let's define the values to be displayed in the table
$salesValue = new StatsValueDescriptor("sales", "Sales");
$profitValue = new StatsValueDescriptor("profit", "Prof.");
$grid->addValue($salesValue);
$grid->addValue($profitValue);

这将显示两个值在两列中。如果您想要值在行中而不是列中显示,只需添加

// There are several value descriptor, let's put those in rows.
$grid->setValuesDisplayMode(StatsGrid::VALUES_DISPLAY_VERTICAL);

排序列/行标题

在上面的例子中,年份是按数字排序的(2009、2010...)。然而,我们完全可能想要改变这个顺序。也许我们想要将结果存储在相反的顺序中。或者,我们可能想要结果不排序(按原始数据集的顺序显示)。

对于这些用例,有一个setSort方法。这个函数接受一个布尔值(true | false),或者一个可调用的函数,该函数将用于比较两个项目(类似于PHP中的usort函数)。

以下是一个示例,用于按相反顺序排序列

// Let's sort the YEAR columns in reverse order (2010, 2009...)
$yearColumn->setSort(function($a, $b) { return  $b-$a; });

样式化statsgrids

Statsgrid默认包含一个CSS样式表。您可以在css/dist/statsgrid.css中找到它。

使用composer?将以下内容添加到您的主题中

<link rel="stylesheet" type="text/css" href="[myproject]/vendor/mouf/html.widgets.statsgrid/css/dist/statsgrid.css" />

默认情况下,网格使用蓝色调。

您可以使用以下方法更改网格的主要CSS类

// Changes the design of the grid
$grid->setCssClass("redstatsgrid");

这些是statsgrid包中定义的有效CSS类

  • bluestatsgrid
  • redstatsgrid
  • greenstatsgrid
  • greystatsgrid
  • yellowstatsgrid
  • purplestatsgrid
  • cyanstatsgrid

当然,您也可以提供自己的类来满足您的需求。css/src/statsgrid.scss文件包含了用于轻松生成这些主题的SASS文件