Zend Framework: Zend_Service_Gchart Component Proposal
| Proposed Component Name | Zend_Service_Gchart |
|---|---|
| Developer Notes | http://framework.zend.com/wiki/display/ZFDEV/Zend_Service_Gchart |
| Proposers | Jeremy Giberson |
| Zend Liaison | TBD |
| Revision | 1.0 - 1 January 2008: Initial Draft. (wiki revision: 6) |
Table of Contents
1. Overview
Zend_Service_Gchart provides an interface for Googles GChart API.
2. References
3. Component Requirements, Constraints, and Acceptance Criteria
- This component will provide chart type independent object instances.
- Each object instance of this component will accept every GChart option.
- The object will determine what options to accept and send with GChart API Request.
- This component will provide minimal error checking.
- This component may support using POST request to GChart's Api service to avoid natural url size limit of GET requests. This depends on Gcharts support of POST requests.
4. Dependencies on Other Framework Components
- Zend_Cache
- Zend_Exception
- Zend_Service
5. Theory of Operation
You may statically configure a Zend_Cache object for Gchart. If so, you must specify a valid directory with write access for image generation.
The component is instantiated using Zend_Service_Chart with out any fore-knowledge of the chart type.
At minimum you will provide the gchart instance with a Zend_Service_Gchart_Parameter_Data object, and a Zend_Service_Gchart_Parameter_Size object.
You can add a single instance of any|all other Zend_Service_Gchart_Parameter_ objects.
To generate the image url you will specify the type of gchart using one of the provided Zend_Service_Gchart::CHART_TYPE_ constant values.
If you have configured Zend_Service_Gchart for cache, during request url generation it will check the cache for pre-existing gchart api response. If a previous response exists, it will use the raw image data to create a temporary image file in the specified image directory, and return the url to this file. If data does not exist in the cache for the request a new request will be sent to Gchart API using GET|POST and the raw image data from the response will be saved in the cache.
If cache has not been configured for Zend_Service_Gchart, then a gchart request uri will be constructed and returned.
How request parameters are generated:
Each parameter of the instance will be iterated through and asked if compatible with the chart type specified for the request.
If the the parameter is compatible it will be asked for an array of $key=>$value pairs to be included in the request.
The request is serialized using the google chart api URI and request pairs.
You will use the uri as the image source like so:
<img src='$request_uri'/>
6. Milestones / Tasks
- Milestone 1: [DONE] Class layout will be developed to facilitate the goals of this component
- Milestone 2: Support for all current chart options will be completed
- Milestone 3: Integrate Zend_Cache support for saving requests
- Milestone 4: Component is reviewed and accepted for the Zend Framework Incubator
- Milestone 5: Working prototype checked into incubator svn
- Milestone 6: Initial documentation exists
7. Class Index
[DONE]
- Zend_Service_Gchart
- Zend_Service_Gchart_Parameter_Interface
- Zend_Service_Gchart_Parameter_Colors
- Zend_Service_Gchart_Parameter_Data_Interface
- Zend_Service_Gchart_Parameter_Data_Abstract
- Zend_Service_Gchart_Parameter_Data_Extended
- Zend_Service_Gchart_Parameter_Data_Simple
- Zend_Service_Gchart_Parameter_Data_Text
- Zend_Service_Gchart_Parameter_Legend
- Zend_Service_Gchart_Parameter_PieLabels
- Zend_Service_Gchart_Parameter_Size
- Zend_Service_Gchart_Parameter_Title
[TODO] - Zend_Service_Gchart_Parameter_* (remaining gchart options)
8. Use Cases
| UC-01 |
|---|
$gc = new Zend_Service_Gchart();
// size
$size = new Zend_Service_Gchart_Parameter_Size('320', '200');
// data
$data = new Zend_Service_Gchart_Parameter_Data_Text();
$sid = $data->addDataSeries(array(20, 22, 15, 45, 10));
$sid2 = $data->addDataSeries(array(21, 11, 24, 10, 55));
$data->setScaled(true);
// title
$title = new Zend_Service_Gchart_Parameter_Title("Performance Comparison\nAwesomeness vs. Uberness");
// legend
$legend = new Zend_Service_Gchart_Parameter_Legend($data);
$legend->setDataLabel($sid, 'Awesomeness');
$legend->setDataLabel($sid2, 'Uberness');
$legend->setPosition( Zend_Service_Gchart_Parameter_Legend::LEGEND_POSITION_BOTTOM );
// colors
$colors = new Zend_Service_Gchart_Parameter_Colors($data);
$colors->setSeriesColor($sid2, '0000ff');
$colors->addColors(array('ff0000', '00ff00', 'ffff00', '00ffff'));
// required parameters
$gc->setData($data);
$gc->setSize($size);
// optional parameters
$gc->addParameter($legend);
$gc->addParameter($colors);
$gc->addParameter($title);
// view usage
<br/>
<img src='<?=$this->gc->getImageUrl(Zend_Service_Gchart::CHART_TYPE_PIE);?>'>
<br/>
<img src='<?=$this->gc->getImageUrl(Zend_Service_Gchart::CHART_TYPE_PIE_3D);?>'>
<br/>
<img src='<?=$this->gc->getImageUrl(Zend_Service_Gchart::CHART_TYPE_LINE_SPARK);?>'>
<br/>
<img src='<?=$this->gc->getImageUrl(Zend_Service_Gchart::CHART_TYPE_BAR_HORIZONTAL_GROUPED);?>'>
<br/>
<img src='<?=$this->gc->getImageUrl(Zend_Service_Gchart::CHART_TYPE_SCATTER_PLOT);?>'>
<br/>
// OUTPUT:



// NOTE
You'll notice the pie charts only exhibit one data series. Since Pie charts only expect one series, it ignores other series in the data set.
I'm thinking it might be nice to be able to set a primary series with a data set that way you can specify which series (of many) is chosen for chart types that only support one series. This way, you can pack all your data into a single chart object, and in the view switch primary series and render charts for the series. Or render bar, line, etc using all the data.
9. Class Skeletons
These are the current skeletons for the GChart, Parameter Interface, and Parameter_Data. Please see the attached source for more classes and full function definitions. Each class has documentation in it.
| Name | Size | Creator (Last Modifier) | Creation Date | Last Mod Date | Comment | ||
|---|---|---|---|---|---|---|---|
| 10 kb | Jeremy Giberson | Jul 15, 2008 | Jul 15, 2008 | Current Source of component |
If you are wondering why there is an interface class and abstract class for data, it is because the abstract class has several functions implemented that wont change between data subclasses and the sub classes need to be interfaces to require the provide correct accessors.
Nice, I need this component ^^, maybe you can check GphpChart (http://www.malaiac.com/GphpChart/) seems like covers the whole API and I like the idea of use Zend_Cache.
ZF Home Page
Code Browser
Wiki Dashboard
I've still got to do a few things to finalize the GChart class, but the layout and theory of the class structure is pretty solid I think. What remains to be done is a parameter implementation for all the supported options in GCharts.
I'm also thinking it may be nice to add helper methods to the Zend_Service_Gchart class so you dont have to create all the parameters from scratch using their full name. Something like:
$gc = new Zend_Service_Gchart();
$sid = $gc->addDataSeries([1,23,42,12,15,27]);
$gc->setSeriesColor($sid, '00ff00');
$gc->addColors(['0000ff', 'ff0000');
$gc->addDataSeries([28,202,24,450]);
$gc->setSize(400, 300);
Where each of these methods would internally create the correct parameter object. The decision to do that, I'll leave to general consensus.