PHPackages                             bggardner/google-visualization-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. bggardner/google-visualization-php

ActiveLibrary[API Development](/categories/api)

bggardner/google-visualization-php
==================================

Google Visualization Data Source for PHP

976[6 issues](https://github.com/bggardner/google-visualization-php/issues)PHP

Since Nov 23Pushed 2y ago4 watchersCompare

[ Source](https://github.com/bggardner/google-visualization-php)[ Packagist](https://packagist.org/packages/bggardner/google-visualization-php)[ RSS](/packages/bggardner-google-visualization-php/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Google Visualization Data Source for PHP
========================================

[](#google-visualization-data-source-for-php)

This is a near literal translation of [google-visualization-java](https://github.com/google/google-visualization-java) into PHP. The `QueryParser` class was not translated, but written from scratch. While its main purpose is to generate data formatted for Google Charts, it can also be used as an abstraction layer for accessing data from a variety of sources using a SQL-like query. Thorough testing has not been performed, so bug reports are encouraged. Enjoy!

Features
--------

[](#features)

- A PHP implementation of the [Google Chart Tools Datasource Protocol](https://developers.google.com/chart/interactive/docs/dev/implementing_data_source) (V0.6)
- Parses a [Google Visualization Query](https://developers.google.com/chart/interactive/docs/querylanguage) into a PHP object
- Executes the query on an existing `DataTable` or retrieves one from a database using a `Util\xxxDataSourceHelper` class, which performs automatic type casting:
    - PDO:
        - PostgreSQL
        - MS SQL Server / SQL Azure
        - MySQL
        - SQLite
    - MySQLi
- Outputs the result in the requested format
    - `csv` - Comma Separated Values
    - `html` - HyperText Markup Language
    - `json` - JavaScript Object Notation
    - `jsonp` - JSON with Padding
    - `php` - Serialized PHP object with class: DataTable (success) or ResponseStatus (error)
    - `tsv-excel` - Tab Separated Values for Excel
- Complete support of the [Google Visualization Query Language](https://developers.google.com/chart/interactive/docs/querylanguage) (V0.7), with some additional functions:
    - `ABS(number)` - absolute value
    - `CONCAT(string1, string2, ...)` - concatenate strings
    - `CONCAT_WS(separator, string1, string2, ...)` - concatenate strings with separator
    - `LEFT(string, length)` - left-most characters of a string
    - `RIGHT(string, length)` - right-most characters of a string
    - `ROUND(number, precision)` - round a number to a digit of precision

Dependencies
------------

[](#dependencies)

- PHP 7.1+
    - intl extension
    - PDO extension (optional, required for `Util\PdoDataSourceHelper` classes)
        - PDO database-specific driver extensions (required for each driver you need to use)
    - mysqli extension (optional, required for `Util\MysqliDataSourceHelper` class)
- ICU (optional, to compile additional resource bundles)
    - See the [ICU ReadMe](http://source.icu-project.org/repos/icu/icu/trunk/readme.html) if `genrb` is not installed on your system

Installation
------------

[](#installation)

1. Clone/extract repository; or via [Composer](https://getcomposer.org/) requiring `"bggardner/google-visualization-php": "dev-master"`
2. (Optional, to add languages/locales for error messages) Use the ICU tool `genrb` to compile each `*.txt` file in `Base\ErrorMessages`
    - This will compile `*.txt` into `*.res` (default locale resource bundle):

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

Usage
-----

[](#usage)

The usage is nearly similar to that of the [java library](https://developers.google.com/chart/interactive/docs/dev/dsl_about) (see that for further usage help).

- Include all the files in the path or use an autoloader, such as Composer.
- For usage with Google Charts:
    - Create a class that extends the `DataSource` class
    - Instantiate the class in a file that accepts the HTTP GET request from the Google Chart
- Useful stand-alone functions if using as an abstraction layer:
    - `DataSourceHelper::parseQuery($string)` - Returns a `Query` object from $string
    - `Util\Pdo\MySqlPdoDataSourceHelper::executeQuery(Query $query, PDO $pdo, $tableNmae)` - Returns a `DataTable` object by applying the query to a MySQL table
    - `DataSourceHelper::applyQuery(Query $query, DataTable $dataTable, $locale)` - Returns a `DataTable` object by applying the query to an exsiting `DataTable`
- Optionally the resource bundle can be kept in a folder outside of the repository. In that case call `Google\Visualization\DataSource\Base\LocaleUtil::setResourceBundleDir($pathToResources);` where `ErrorMessages` is a sub-folder of `$pathToResources`.

Examples
--------

[](#examples)

Query a table named "mytable" from a SQL database, using Composer's autoload:

```
