PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/webcore/ext/google/webcore.google.visualization.php

#
PHP | 332 lines | 200 code | 47 blank | 85 comment | 4 complexity | c84d8cde2b2f04e7a991c70a16d156f1 MD5 | raw file
  1. <?php
  2. /**
  3. * Represents a common control in a Google Visualization
  4. *
  5. * @package WebCore
  6. * @subpackage Google
  7. */
  8. class GVizControl extends ControlModelBase implements IRootModel, IBindingTarget
  9. {
  10. const TYPE_IMAGESPARKLINE = 'imagesparkline';
  11. const TYPE_AREACHART = 'areachart';
  12. const TYPE_PIECHART = 'piechart';
  13. const TYPE_COLUMNCHART = 'columnchart';
  14. const TYPE_LINECHART = 'linechart';
  15. const TYPE_BARCHART = 'barchart';
  16. const OPTION_IS_STACKED = 'isStacked';
  17. const OPTION_IS_3D = 'is3D';
  18. const OPTION_SHOW_VALUE_LABELS = 'showValueLabels';
  19. const OPTION_ENABLE_TOOLTIP = 'enableTooltip';
  20. const OPTION_SHOW_CATEGORIES = 'showCategories';
  21. const OPTION_WIDTH = 'width';
  22. const OPTION_HEIGHT = 'height';
  23. protected $caption;
  24. protected $type;
  25. protected $columns;
  26. protected $dataSource;
  27. protected $options;
  28. public function __construct($name, $caption, $type = 'barchart')
  29. {
  30. parent::__construct($name, true);
  31. $this->caption = $caption;
  32. $this->type = $type;
  33. $this->options = new KeyedCollection();
  34. $this->setOption(self::OPTION_HEIGHT, 320);
  35. $this->setOption(self::OPTION_WIDTH, 500);
  36. $this->setOption(self::OPTION_IS_STACKED, true);
  37. $this->setOption(self::OPTION_IS_3D, true);
  38. }
  39. /**
  40. * Get visualization type
  41. *
  42. * @return string
  43. */
  44. public function getVizType()
  45. {
  46. return $this->type;
  47. }
  48. public function getColumns()
  49. {
  50. return $this->columns;
  51. }
  52. public function getDataSource()
  53. {
  54. return $this->dataSource;
  55. }
  56. /**
  57. * Gets caption
  58. *
  59. * @return string
  60. */
  61. public function getCaption()
  62. {
  63. return $this->caption;
  64. }
  65. public function setCaption($value)
  66. {
  67. $this->caption = $value;
  68. }
  69. /**
  70. * Sets an option for the Chart
  71. *
  72. * @param string $option. One of the OPTION_* prefixed constants
  73. * @param bool $value
  74. */
  75. public function setOption($option, $value)
  76. {
  77. $this->options->setValue($option, $value);
  78. }
  79. /**
  80. * Gets the options
  81. */
  82. public function getOptions()
  83. {
  84. return $this->options;
  85. }
  86. /**
  87. * @param IndexedCollection $dataSource
  88. */
  89. public function dataBind(&$dataSource)
  90. {
  91. if ($dataSource->isEmpty())
  92. return;
  93. $this->columns = array();
  94. foreach ($dataSource->getItem(0) as $key => $value)
  95. {
  96. if (is_object($value))
  97. $scalar = $value->getValue();
  98. else
  99. $scalar = $value;
  100. if (is_float($scalar) || is_int($scalar))
  101. $this->columns[] = array(
  102. 'id' => $key,
  103. 'type' => 'number'
  104. );
  105. else
  106. $this->columns[] = array(
  107. 'id' => $key,
  108. 'type' => 'string'
  109. );
  110. }
  111. foreach ($dataSource as $item)
  112. {
  113. $arr = array();
  114. foreach ($item as $key => $value)
  115. {
  116. $scalar = is_object($value) ? $value->getValue() : $value;
  117. $arr[] = $scalar;
  118. }
  119. $this->dataSource[] = $arr;
  120. }
  121. }
  122. /**
  123. * Creates a default instance of this class
  124. *
  125. * @return GVizControl
  126. */
  127. public static function createInstance()
  128. {
  129. return new GVizControl('ISerializable', 'ISerializable');
  130. }
  131. }
  132. /**
  133. * Represents a Google Chart view
  134. *
  135. * @package WebCore
  136. * @subpackage Google
  137. */
  138. class GChartView extends HtmlViewBase
  139. {
  140. /**
  141. * Creates a new instance of this class based on a Google Visualization model
  142. *
  143. * @param GVizControl $model
  144. */
  145. public function __construct(&$model)
  146. {
  147. parent::__construct($model);
  148. $this->cssClass = 'vizview';
  149. $this->frameWidth = "auto";
  150. $this->isAsynchronous = true;
  151. $callbacks =& $this->renderCallbacks->getArrayReference();
  152. // Setup the callbacks for each renderable model
  153. $callbacks['GVizControl'] = array(
  154. 'GVizRenderCallbacks',
  155. 'renderGChartControl'
  156. );
  157. $this->registerDependencies();
  158. }
  159. /**
  160. * Registers model resources and dependencies on the client-side
  161. *
  162. */
  163. protected function registerDependencies()
  164. {
  165. //self::registerCommonDependecies();
  166. }
  167. }
  168. /**
  169. * Represents a Google Visualization view
  170. *
  171. * @package WebCore
  172. * @subpackage Google
  173. */
  174. class GVizView extends HtmlViewBase
  175. {
  176. /**
  177. * Creates a new instance of this class based on a Google Visualization model
  178. *
  179. * @param GVizControl $model
  180. */
  181. public function __construct(&$model)
  182. {
  183. parent::__construct($model);
  184. $this->cssClass = 'vizview';
  185. $this->frameWidth = "auto";
  186. $this->isAsynchronous = true;
  187. $callbacks =& $this->renderCallbacks->getArrayReference();
  188. // Setup the callbacks for each renderable model
  189. $callbacks['GVizControl'] = array(
  190. 'GVizRenderCallbacks',
  191. 'renderGVizControl'
  192. );
  193. $this->registerDependencies();
  194. }
  195. /**
  196. * Registers model resources and dependencies on the client-side
  197. *
  198. */
  199. protected function registerDependencies()
  200. {
  201. self::registerCommonDependecies();
  202. $formviewPath = HttpContext::getLibraryRoot() . 'ext/google/google.vizview.js';
  203. $cssPath = HttpContext::getLibraryRoot() . 'ext/google/google.vizview.css';
  204. HtmlViewManager::registerDependency(HtmlDependency::TYPE_JS_FILE, __CLASS__, 'HtmlVizView.Js', $formviewPath);
  205. HtmlViewManager::registerDependency(HtmlDependency::TYPE_CSS_FILE, __CLASS__, 'HtmlVizView.Css', $cssPath);
  206. $googleAPIPath = 'http://www.google.com/jsapi';
  207. HtmlViewManager::registerDependency(HtmlDependency::TYPE_JS_FILE, __CLASS__, 'GoogleAPI.Js', $googleAPIPath);
  208. $jsViz = "gVizView_" . $this->model->getName();
  209. $jsVizContainer = HtmlViewBase::getHtmlId($this->model) . "_content";
  210. $jsData = json_encode($this->model->getDataSource());
  211. $jsColumns = json_encode($this->model->getColumns());
  212. $jsVizType = $this->model->getVizType();
  213. $jsVizOptions = json_encode($this->model->getOptions()->getArrayReference());
  214. $javascript = "var $jsViz = new GVizView('$jsVizContainer', '$jsVizType', $jsVizOptions); {$jsViz}.setColumns('$jsColumns'); {$jsViz}.setData('$jsData'); {$jsViz}.draw();";
  215. HtmlViewManager::registerDependency(HtmlDependency::TYPE_JS_BLOCK, $this->model->getName(), 'gViz_' . $this->model->getName(), $javascript);
  216. }
  217. }
  218. /**
  219. * Contains static callback methods to render Google Visualization
  220. *
  221. * @package WebCore
  222. * @subpackage Google
  223. */
  224. class GVizRenderCallbacks extends HtmlRenderCallbacks
  225. {
  226. /**
  227. * Renders the main control as static chart
  228. *
  229. * @param GVizControl $model
  230. * @param GVizView $view
  231. */
  232. public static function renderGChartControl(&$model, &$view)
  233. {
  234. $tw = HtmlWriter::getInstance();
  235. $legends = array();
  236. $data = array();
  237. foreach ($model->getDataSource() as $item)
  238. {
  239. $legends[] = $item[0];
  240. $data[] = $item[1];
  241. }
  242. $url = "http://chart.apis.google.com/chart?";
  243. $url .= "cht=p3"; // Change for type
  244. $url .= "&chs=600x500";
  245. $url .= "&chl=" . implode("|", $legends);
  246. $url .= "&chd=t:" . implode(",", $data);
  247. $tw->openImg();
  248. $tw->addAttribute('src', $url);
  249. $tw->closeImg();
  250. }
  251. /**
  252. * Renders the main control
  253. *
  254. * @param GVizControl $model
  255. * @param GVizView $view
  256. */
  257. public static function renderGVizControl(&$model, &$view)
  258. {
  259. $tw = HtmlWriter::getInstance();
  260. $tw->openForm();
  261. $tw->addAttribute('name', $model->getName());
  262. $tw->addAttribute('action', HttpContext::getInfo()->getScriptVirtualPath());
  263. $tw->addAttribute('method', 'post');
  264. $tw->addAttribute('id', HtmlViewBase::getHtmlId($model));
  265. $tw->addAttribute('class', $view->getCssClass());
  266. $tw->openDiv();
  267. $tw->addAttribute('class', $view->getMasterCssClass() . '-caption');
  268. $tw->writeContent($model->getCaption());
  269. $tw->closeDiv();
  270. $tw->openDiv();
  271. $tw->addAttribute('id', HtmlViewBase::getHtmlId($model) . '_content');
  272. $tw->addAttribute('class', $view->getCssClass() . '-content');
  273. $tw->openDiv();
  274. $tw->addAttribute('id', $model->getName() . '_viz');
  275. $tw->addAttribute('type', $model->getVizType());
  276. $tw->addAttribute('class', $view->getCssClass() . '-viz');
  277. $tw->writeContent('');
  278. $tw->closeDiv();
  279. self::renderPostBackFlag($model, $view);
  280. $tw->closeDiv();
  281. $tw->closeForm();
  282. //self::renderInitializationScript($javascript);
  283. }
  284. }
  285. ?>