bggardner/google-visualization-php

PHP的Google Visualization数据源

dev-master 2023-11-23 17:25 UTC

This package is auto-updated.

Last update: 2024-09-23 19:11:21 UTC


README

这是对google-visualization-java的近乎直译到PHP。QueryParser类没有翻译,而是从头编写。虽然其主要目的是生成用于Google Charts的数据格式,但它也可以用作一个抽象层,通过类似SQL的查询访问各种来源的数据。尚未进行彻底测试,因此鼓励提交错误报告。祝您享受!

功能

  • PHP实现Google Chart Tools Datasource Protocol (V0.6)
  • Google Visualization Query解析为PHP对象
  • 在现有的DataTable上执行查询或从数据库中检索一个,使用Util\xxxDataSourceHelper类,该类执行自动类型转换
    • PDO
      • PostgreSQL
      • MS SQL Server / SQL Azure
      • MySQL
      • SQLite
    • MySQLi
  • 以请求的格式输出结果
    • csv - 逗号分隔值
    • html - 超文本标记语言
    • json - JavaScript对象表示法
    • jsonp - 带填充的JSON
    • php - 序列化的PHP对象,类为DataTable(成功)或ResponseStatus(错误)
    • tsv-excel - Excel的制表符分隔值
  • 完全支持Google Visualization Query Language (V0.7),并增加了某些附加功能
    • ABS(number) - 绝对值
    • CONCAT(string1, string2, ...) - 连接字符串
    • CONCAT_WS(separator, string1, string2, ...) - 使用分隔符连接字符串
    • LEFT(string, length) - 字符串的左侧字符
    • RIGHT(string, length) - 字符串的右侧字符
    • ROUND(number, precision) - 将数字四舍五入到指定的精度位

依赖项

  • PHP 7.1+
    • intl扩展
    • PDO扩展(可选,用于Util\PdoDataSourceHelper类)
      • PDO数据库特定驱动程序扩展(需要使用每个所需的驱动程序)
    • mysqli扩展(可选,用于Util\MysqliDataSourceHelper类)
  • ICU(可选,用于编译额外的资源包)
    • 如果您的系统未安装genrb,请参阅ICU ReadMe

安装

  1. 克隆/提取仓库;或者通过Composer需要"bggardner/google-visualization-php": "dev-master"
  2. (可选,为错误消息添加语言/区域) 使用ICU工具genrb编译Base\ErrorMessages中的每个*.txt文件
    • 这将编译*.txt*.res(默认区域资源包)

user@localhost [/path/to/google-visualization-php/src/Base/ErrorMessages]# genrb *.txt

使用方法

用法几乎与java库相同(参阅该链接以获取进一步的帮助)。

  • 包括路径中的所有文件或使用自动加载程序,例如Composer。
  • 与Google Charts一起使用
    • 创建一个扩展DataSource类的类
    • 在接收来自Google图表的HTTP GET请求的文件中实例化类
  • 作为抽象层使用时,有用的独立函数
    • DataSourceHelper::parseQuery($string) - 从 $string 返回一个 Query 对象
    • Util\Pdo\MySqlPdoDataSourceHelper::executeQuery(Query $query, PDO $pdo, $tableNmae) - 通过将查询应用于MySQL表,返回一个 DataTable 对象
    • DataSourceHelper::applyQuery(Query $query, DataTable $dataTable, $locale) - 通过将查询应用于现有的 DataTable,返回一个 DataTable 对象
  • 可选地,资源包可以保存在存储库之外的文件夹中。在这种情况下,调用 Google\Visualization\DataSource\Base\LocaleUtil::setResourceBundleDir($pathToResources);,其中 ErrorMessages$pathToResources 的子文件夹。

示例

使用Composer的自动加载,从SQL数据库查询名为 "mytable" 的表

<?php

require "vendor/autoload.php";

// The custom class that defines how the data is generated
class MyDataSource extends Google\Visualization\DataSource\DataSource
{
    public function getCapabilities() { return Google\Visualization\DataSource\Capabilities::SQL; }

    public function generateDataTable(Google\Visualization\DataSource\Query\Query $query)
    {
        // MySQL
        $pdo = new PDO("mysql:host=xxx;port=xxx;dbname=xxx", "username", "password");
        return Google\Visualization\DataSource\Util\Pdo\MysqlPdoDataSourceHelper::executeQuery($query, $pdo, "mytable");

        // MS SQL Server / SQL Azure
        $pdo = new PDO("sqlsrv:Server=xxx;Database=xxx", "username", "password");
        return Google\Visualization\DataSource\Util\Pdo\MssqlserverPdoDataSourceHelper::executeQuery($query, $pdo, "mytable");

        // PostgreSQL
        $pdo = new PDO("pgsql:host=xxx;port=xxx;dbname=xxx", "username", "password");
        return Google\Visualization\DataSource\Util\Pdo\PostgresqlPdoDataSourceHelper::executeQuery($query, $pdo, "mytable");

        // SQLite
        $pdo = new PDO("sqlite:/path/to/xxx.db");
        return Google\Visualization\DataSource\Util\Pdo\SqlitePdoDataSourceHelper::executeQuery($query, $pdo, "mytable");

        // MySQLi
        $db = new mysqli("host", "username", "password");
        return Google\Visualization\DataSource\Util\MysqliDataSourceHelper::executeQuery($query, $db, "mytable");
    }

    public function isRestrictedAccessMode() { return FALSE; }
}

// Instantiating the class parses the 'tq' and 'tqx' HTTP request parameters and outputs the resulting data
new MyDataSource();

使用 spl_autoload_register 查询CSV文件(具有已知列顺序和数据类型)

<?php

spl_autoload_register(function($class) {
    $class = str_replace('Google\\Visualization\\DataSource\\', '', $class);
    require_once '/path/to/google-visualization-php/src/' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});

class MyDataSource extends Google\Visualization\DataSource\DataSource
{
    public function getCapabilities() { return Google\Visualization\DataSource\Capabilities::NONE; }

    public function generateDataTable(Google\Visualization\DataSource\Query\Query $query = NULL)
    {
        // Since Capabilities are NONE, the $query argument will be NULL as the data will be processed by DataSourceHelper

        // Create the DataTable and configure the columns (name and data type)
        $dataTable = new Google\Visualization\DataSource\DataTable\DataTable();
        $columnDescriptions = array();
        $columnDescriptions[] = new Google\Visualization\DataSource\DataTable\ColumnDescription("x", Google\Visualization\DataSource\DataTable\Value\ValueType::NUMBER, "x");
        $columnDescriptions[] = new Google\Visualization\DataSource\DataTable\ColumnDescription("y", Google\Visualization\DataSource\DataTable\Value\ValueType::NUMBER, "y");
        $dataTable->addColumns($columnDescriptions);

        // Populate the DataTable
        $fh = fopen('data.csv', 'r');
        while (($data = fgetcsv($fh)) !== FALSE) {
            $tableRow = new Google\Visualization\DataSource\DataTable\TableRow();
            foreach ($data as $datum) {
                $value = new Google\Visualization\DataSource\DataTable\Value\NumberValue($datum);
                $tableCell = new Google\Visualization\DataSource\DataTable\TableCell($value);
                $tableRow->addCell($tableCell);
            }
            $dataTable->addRow($tableRow);
        }
      return $dataTable;
    }

    public function isRestrictedAccessMode() { return FALSE; }
}

new MyDataSource();