GoogleChart.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2008 by Denis M. Gabaidulin                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     class GoogleChart implements Stringable
00016     {
00017         const BASE_URL = 'http://chart.apis.google.com/chart?';
00018         
00019         protected $color    = null;
00020         protected $size     = null;
00021         protected $type     = null;
00022         protected $label    = null;
00023         protected $data     = null;
00024         protected $legend   = null;
00025         protected $title    = null;
00026         protected $fillers  = null;
00027         
00031         public static function create()
00032         {
00033             return new self;
00034         }
00035         
00036         public function __construct()
00037         {
00038             $this->fillers = GoogleChartSolidFillCollection::create();
00039         }
00040         
00044         public function setColor(GoogleChartColor $color)
00045         {
00046             $this->color = $color;
00047             
00048             return $this;
00049         }
00050         
00054         public function setSize(GoogleChartSize $size)
00055         {
00056             $this->size = $size;
00057             
00058             return $this;
00059         }
00060         
00064         public function setType(GoogleChartType $type)
00065         {
00066             $this->type = $type;
00067             
00068             return $this;
00069         }
00070         
00074         public function setLabel(GoogleChartLabel $label)
00075         {
00076             $this->label = $label;
00077             
00078             return $this;
00079         }
00080         
00084         public function setData(GoogleChartData $data)
00085         {
00086             $this->data = $data;
00087             
00088             return $this;
00089         }
00090         
00091         public function getData()
00092         {
00093             return $this->data;
00094         }
00095         
00099         public function setLegend(GoogleChartLegend $legend)
00100         {
00101             $this->legend = $legend;
00102             
00103             return $this;
00104         }
00105         
00109         public function setTitle($title)
00110         {
00111             $this->title = GoogleChartTitle::create($title);
00112             
00113             return $this;
00114         }
00115         
00116         public function addChartAreaFiller(Color $color)
00117         {
00118             $this->fillers->addFiller(
00119                 GoogleChartSolidFillType::create(
00120                     GoogleChartSolidFillType::CHART_AREA
00121                 ),
00122                 $color
00123             );
00124             
00125             return $this;
00126         }
00127         
00128         public function addBackgroundFiller(Color $color)
00129         {
00130             $this->fillers->addFiller(
00131                 GoogleChartSolidFillType::create(
00132                     GoogleChartSolidFillType::BACKGROUND
00133                 ),
00134                 $color
00135             );
00136             
00137             return $this;
00138         }
00139         
00140         public function addTransparencyFiller(Color $color)
00141         {
00142             $this->fillers->addFiller(
00143                 GoogleChartSolidFillType::create(
00144                     GoogleChartSolidFillType::TRANSPARENCY
00145                 ),
00146                 $color
00147             );
00148             
00149             return $this;
00150         }
00151         
00152         public function toString()
00153         {
00154             $url = self::BASE_URL;
00155             
00156             Assert::isNotNull($this->type);
00157             $parameters[] = $this->type->toString();
00158             
00159             Assert::isNotNull($this->size);
00160             $parameters[] = $this->size->toString();
00161             
00162             Assert::isNotNull($this->color);
00163             $parameters[] = $this->color->toString();
00164             
00165             Assert::isNotNull($this->data);
00166             $parameters[] = $this->data->toString();
00167             
00168             if ($this->legend)
00169                 $parameters[] = $this->legend->toString();
00170             
00171             if ($this->label)
00172                 $parameters[] = $this->label->toString();
00173             
00174             if ($this->title)
00175                 $parameters[] = $this->title->toString();
00176             
00177             if ($this->fillers->hasFillers())
00178                 $parameters[] = $this->fillers->toString();
00179             
00180             $url .= implode('&', $parameters);
00181             
00182             return $url;
00183         }
00184     }
00185 ?>