PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Classes/Phaux-extras/WHPlot.php

http://phaux.googlecode.com/
PHP | 173 lines | 139 code | 25 blank | 9 comment | 3 complexity | a0bb1efb5e9212a9067205a065c3a5d8 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Phaux-extras
  4. */
  5. class WHPlot extends WHComponent {
  6. protected $id;
  7. protected $type = 'bar';
  8. protected $width = '100%';
  9. protected $height = '200px';
  10. protected $data = array();
  11. protected $options = array();
  12. protected $labels = array();
  13. public function __construct(){
  14. parent::__construct();
  15. if(!is_int($this->classVarNamed('id'))){
  16. $this->setClassVarNamed('id',0);
  17. }else{
  18. $this->setClassVarNamed('id',$this->classVarNamed('id')+1);
  19. }
  20. $this->id = $this->classVarNamed('id');
  21. }
  22. /*
  23. ** dataName is a unique name for this data
  24. ** anArrayOfData is in the format of array(x=>y,...)
  25. ** where x and y are both numbers
  26. */
  27. public function setDataNamed($dataName,$anArrayOfData){
  28. $this->data[$dataName] = $anArrayOfData;
  29. return $this;
  30. }
  31. public function getDataNamed($dataName){
  32. return $this->data[$dataName];
  33. }
  34. public function removeDataNamed($dataName){
  35. unset($this->data[$dataName]);
  36. return $this;
  37. }
  38. public function setLabelForTick($aNumber,$aString){
  39. $this->labels[$aNumber] = $aString;
  40. $this->createOptionForLabels();
  41. return $this;
  42. }
  43. public function labelForTick($aNumber){
  44. return $this->labels[$aNumber];
  45. }
  46. public function resetLabels(){
  47. $this->labels = array();
  48. }
  49. public function createOptionForLabels(){
  50. $option = '[';
  51. $first = true;
  52. foreach($this->labels as $tick => $value){
  53. if($first){
  54. $first = FALSE;
  55. }else{
  56. $option .= ' , ';
  57. }
  58. $option .= "{v:$tick, label:\"$value\"}";
  59. }
  60. $this->setOptionNamed('xTicks',$option.']');
  61. return $this;
  62. }
  63. public function setOptionNamed($optionName,$optionValue){
  64. $this->options['"'.$optionName.'"'] = $optionValue;
  65. return $this;
  66. }
  67. public function optionNamed($optionName){
  68. return $this->options['"'.$optionName.'"'];
  69. }
  70. public function width(){
  71. return $this->width;
  72. }
  73. public function setWidth($aNumber){
  74. $this->width = $aNumber;
  75. return $this;
  76. }
  77. public function height(){
  78. return $this->height;
  79. }
  80. public function setHeight($aNumber){
  81. $this->height = $aNumber;
  82. return $this;
  83. }
  84. public function validTypes(){
  85. return array('pie','line','bar');
  86. }
  87. public function type(){
  88. return $this->type;
  89. }
  90. public function setType($aValidType){
  91. if(!in_array($aValidType,$this->validTypes())){
  92. $this->error($aValidType.' is not a valid type.
  93. Valid types are '.implode(', ',$this->validTtpes()));
  94. }
  95. $this->type = $aValidType;
  96. return $this;
  97. }
  98. public function renderCanvasOn($html){
  99. $id = 'whplot-'.$this->id;
  100. return $html->div()->id('parent-'.$id)->
  101. width($this->width)->
  102. height($this->height)->
  103. with(
  104. $html->div()->class('whplot-plot')->
  105. width($this->width)->
  106. height($this->height)->
  107. id($id)
  108. );
  109. }
  110. public function renderScriptOn($html){
  111. $jscript = 'var hasCanvas = CanvasRenderer.isSupported();';
  112. $jscript .= 'var opts = {'.
  113. Object::implodeArrayWithKey(',',':',$this->options).
  114. '};'."\n";
  115. $dataNames = array();
  116. foreach($this->data as $dataName => $dataArray){
  117. $jscript .= 'var '.$dataName.' = [['.
  118. Object::implodeArrayWithKey('], [',', ',$dataArray).
  119. ']];'."\n";
  120. $dataNames[] = $dataName;
  121. }
  122. $dataStringToPass = '['.implode(',',$dataNames).']';
  123. $jscript .= 'if (hasCanvas) {';
  124. $jscript .= 'var plot'.$this->id.' = new EasyPlot("'.$this->type.'",opts,$("whplot-'.$this->id.'"),'.$dataStringToPass.');}';
  125. //$jscript .= 'addLoadEvent( function (){'.$jscript.'});';
  126. $jscript = '
  127. function resizePlot'.$this->id.' (){
  128. plotParent = elementDimensions("parent-whplot-'.$this->id.'");
  129. plotDiv = $("whplot-'.$this->id.'");
  130. plotDiv.setAttribute("width",plotParent.w+"px");
  131. $("whplot-'.$this->id.'").innerHTML = "";
  132. '.$jscript.'
  133. }
  134. addToCallStack(window,"onresize",resizePlot'.$this->id.');
  135. addLoadEvent(resizePlot'.$this->id.');
  136. ';
  137. return $html->script()->with($jscript);
  138. }
  139. public function renderContentOn($html){
  140. return $this->renderCanvasOn($html).$this->renderScriptOn($html);
  141. }
  142. public function updateRoot($anHtmlRoot){
  143. parent::updateRoot($anHtmlRoot);
  144. $anHtmlRoot->needsScript('mochikit/MochiKit.js');
  145. $anHtmlRoot->needsScript('plotkit/excanvas.js');
  146. $anHtmlRoot->needsScript('plotkit/PlotKit_Packed.js');
  147. }
  148. }