PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/components/GraphComponent.php

https://bitbucket.org/kramx7/dc-portal
PHP | 317 lines | 214 code | 77 blank | 26 comment | 37 complexity | 61781c27533446b15ba4e71ab8febea4 MD5 | raw file
  1. <?php
  2. //ini_set("memory_limit", "2048M");
  3. //ini_set("max_execution_time", "0");
  4. /**
  5. * UserIdentity represents the data needed to identity a user.
  6. * It contains the authentication method that checks if the provided
  7. * data can identity the user.
  8. */
  9. Yii::import('application.vendors.jpgraph.*');
  10. class GraphComponent extends CComponent {
  11. public $directory = '';
  12. public $filename = '';
  13. public $title = '';
  14. public $subtitle = '';
  15. public $width = 800;
  16. public $height = 600;
  17. public $xdata = array();
  18. public $ydata = array();
  19. public $num_of_results = 0;
  20. public $num_of_yaxis = 0;
  21. public $from_date;
  22. public $to_date;
  23. public $date_format = 'timestamp';
  24. public $type;
  25. public $target_id = 0;
  26. public function setDateLine2Graph($display = false, $filename = 'images/dateline2.png') {
  27. require_once('jpgraph.php');
  28. require_once('jpgraph_line.php');
  29. function DateTimeFormat($aVal) {
  30. return Date('Y-m-d G:i', $aVal);
  31. }
  32. // Some (random) data
  33. $ydata = array(170, 150, 180, 140, 170, 150, 180, 160, 190);
  34. $ydata2 = array(340, 340, 340, 340, 340, 340, 340, 340, 340);
  35. $ynum = count($ydata);
  36. $xdata = array(
  37. strtotime('2013-05-23 01:41'),
  38. strtotime('2013-05-23 03:21'),
  39. strtotime('2013-05-23 05:06'),
  40. strtotime('2013-05-23 06:46'),
  41. strtotime('2013-05-23 08:36'),
  42. strtotime('2013-05-23 10:15'),
  43. strtotime('2013-05-23 11:56'),
  44. strtotime('2013-05-23 13:36'),
  45. strtotime('2013-05-23 15:16')
  46. );
  47. $xnum = count($xdata);
  48. // Size of the overall graph
  49. $width = $this->width;
  50. $height = $this->height;
  51. // Create the graph and set a scale.
  52. // These two calls are always required
  53. $graph = new Graph($width, $height);
  54. $graph->SetMargin(100, 100, 50, 120);
  55. $graph->title->Set($this->title);
  56. $graph->subtitle->Set($this->subtitle);
  57. $graph->SetAlphaBlending();
  58. //$graph->SetScale('intlin');
  59. // Setup a manual x-scale (We leave the sentinels for the
  60. // Y-axis at 0 which will then autoscale the Y-axis.)
  61. // We could also use autoscaling for the x-axis but then it
  62. // probably will start a little bit earlier than the first value
  63. // to make the first value an even number as it sees the timestamp
  64. // as an normal integer value.
  65. $graph->SetScale("intlin", 0, 450, $xdata[0], $xdata[$xnum - 1]);
  66. $graph->xaxis->SetLabelFormatCallback('DateTimeFormat');
  67. $graph->xaxis->SetLabelAngle(45);
  68. $graph->yaxis->title->Set('KW');
  69. $graph->yaxis->title->SetMargin(30);
  70. //$graph->xaxis->title->Set('Time');
  71. $graph->xaxis->SetTitle('Time', 'center');
  72. $graph->xaxis->title->SetMargin(80);
  73. $graph->xaxis->title->SetPos('center', 'bottom');
  74. // Create the linear plot
  75. $lineplot = new LinePlot($ydata, $xdata);
  76. $lineplot->SetColor("blue");
  77. // Add the plot to the graph
  78. $graph->Add($lineplot);
  79. $lineplot2 = new LinePlot($ydata2, $xdata);
  80. $lineplot2->SetColor("red");
  81. // Add the plot to the graph
  82. $graph->Add($lineplot2);
  83. $lineplot->SetLegend('Load');
  84. $lineplot2->SetLegend('Capacity');
  85. $graph->legend->SetAbsPos(10, 10, 'right', 'top');
  86. $graph->legend->SetLayout(LEGEND_VERT);
  87. $graph->legend->SetLineWeight(2);
  88. $graph->legend->SetShadow();
  89. // Display the graph
  90. if ($display) {
  91. $graph->Stroke();
  92. } else {
  93. $graph->Stroke($filename);
  94. $this->filename = $filename;
  95. }
  96. }
  97. public function setData($data, $from, $to, $type, $target_id = 0) {
  98. $this->type = $type;
  99. $this->from_date = $from;
  100. $this->to_date = $to;
  101. $this->target_id = $target_id;
  102. // set ydata and xdata var
  103. $this->num_of_results = count($data);
  104. if ($this->num_of_results > 0) {
  105. $num_of_fields = count($data[0]);
  106. } else {
  107. $num_of_fields = 0;
  108. }
  109. $this->num_of_yaxis = $num_of_fields - 1;
  110. for ($i = 0; $i < $this->num_of_yaxis; $i++) {
  111. $this->ydata[$i] = array();
  112. }
  113. for ($i = 0; $i < $this->num_of_results; $i++) {
  114. $row = $data[$i];
  115. array_push($this->xdata, $row[0]);
  116. for ($j = 0; $j < $this->num_of_yaxis; $j++) {
  117. array_push($this->ydata[$j], $row[$j + 1]);
  118. }
  119. }
  120. $timezone = new DateTimeZone("Australia/Sydney");
  121. $offset = $timezone->getOffset(new DateTime("now")); // Offset in seconds
  122. $xtemp = 0;
  123. for ($i = 0; $i < $this->num_of_results; $i++) {
  124. $xtemp = $this->xdata[$i] + $offset;
  125. $this->xdata[$i] = $xtemp;
  126. }
  127. }
  128. public function setTitle($title) {
  129. $from_date = date('d/m/Y G:i:s', $this->from_date);
  130. $to_date = date('d/m/Y G:i:s', $this->to_date);
  131. if (($this->type == "kW") || ($this->type == "kWh")) {
  132. $type = ($this->type == "kWh")?"kW-h":$this->type;
  133. if ($this->target_id) {
  134. $this->title = $type . " for " . $title . "\n" . $from_date . " to " . $to_date;
  135. } else {
  136. $this->title = $type . " Total\n" . $from_date . " to " . $to_date;
  137. }
  138. } else {
  139. $this->title = $this->type . " for " . $title . "\n" . $from_date . " to " . $to_date;
  140. }
  141. }
  142. public function setSubTitle($client_name, $facility_name) {
  143. $this->subtitle = $facility_name . " \n " . $client_name;
  144. }
  145. public function plotGraph() {
  146. require_once('jpgraph.php');
  147. require_once('jpgraph_line.php');
  148. require_once('jpgraph_date.php');
  149. // inner function for array walk callback
  150. if(!function_exists('formatDate')) {
  151. function formatDate(&$aVal) {
  152. $aVal = date('d-m-Y H:i', $aVal);
  153. }
  154. }
  155. $xmin = min($this->xdata);
  156. $xmax = max($this->xdata);
  157. $this->from_date = date('d/m/Y-H:i:s', $xmin);
  158. $this->to_date = date('d/m/Y-H:i:s', $xmax);
  159. array_walk($this->xdata, 'formatDate');
  160. $ymax = 0;
  161. for ($i = 0; $i < $this->num_of_yaxis; $i++) {
  162. if ($ymax < max($this->ydata[$i])) {
  163. $ymax = max($this->ydata[$i]);
  164. }
  165. }
  166. if ($ymax == 0) {
  167. $ymax = 1;
  168. for ($i = 0; $i < $this->num_of_yaxis; $i++) {
  169. for ($j = 0; $j < $this->num_of_results; $j++) {
  170. $this->ydata[$i][$j] = 0.003;
  171. }
  172. }
  173. } else {
  174. $ymax = $ymax * 1.1;
  175. }
  176. $date = date("YmdGis");
  177. $this->filename = 'images/jpgraph/jpgraph' . $date . '.png';
  178. if ($this->directory == '') {
  179. $this->directory = dirname(Yii::app()->getBasePath()) . '/' . $this->filename;
  180. } else {
  181. $this->directory .= $this->filename;
  182. }
  183. $graph = new Graph($this->width, $this->height);
  184. $graph->title->Set($this->title);
  185. $graph->subtitle->Set($this->subtitle);
  186. $graph->SetScale('datlin');
  187. $graph->SetMarginColor('green@0.95');
  188. $graph->SetMargin(100, 20, 70, 200);
  189. $graph->xaxis->SetTickSide(SIDE_BOTTOM);
  190. $graph->yaxis->SetTickSide(SIDE_LEFT);
  191. $graph->yaxis->scale->SetAutoMin(0);
  192. $lineplots = array();
  193. for ($i = 0; $i < $this->num_of_yaxis; $i++) {
  194. $lineplots[$i] = new LinePlot($this->ydata[$i]);
  195. $graph->Add($lineplots[$i]);
  196. }
  197. for ($i = 0; $i < $this->num_of_yaxis; $i++) {
  198. if ($this->num_of_yaxis > 1) {
  199. if ($i == 0) {
  200. $lineplots[$i]->SetBarCenter();
  201. $lineplots[$i]->SetColor("blue");
  202. $lineplots[$i]->SetLegend("Load");
  203. }
  204. if ($i == 1) {
  205. $lineplots[$i]->SetBarCenter();
  206. $lineplots[$i]->SetColor("red");
  207. $lineplots[$i]->SetLegend("Capacity");
  208. }
  209. }
  210. $lineplots[$i]->SetFillFromYMin();
  211. $lineplots[$i]->SetStyle("solid");
  212. }
  213. $graph->legend->SetReverse();
  214. $graph->xaxis->SetTickLabels($this->xdata);
  215. $graph->xaxis->SetTextLabelInterval(2);
  216. $graph->xaxis->SetLabelAngle(45);
  217. $graph->xaxis->HideFirstTickLabel();
  218. $graph->xaxis->HideLastTickLabel();
  219. $graph->xaxis->SetTitle('Date', 'middle');
  220. $graph->xaxis->title->SetMargin(70);
  221. $graph->yaxis->scale->SetAutoMax($ymax);
  222. $graph->yaxis->title->Set($this->type);
  223. $graph->yaxis->title->SetMargin(30);
  224. $graph->yaxis->HideZeroLabel();
  225. $graph->legend->SetShadow('gray@0.4', 5);
  226. $graph->legend->SetPos(0, 0, 'right', 'right');
  227. $graph->legend->SetColumns(1);
  228. //echo $this->directory;
  229. $graph->Stroke($this->directory);
  230. $this->deleteOldGraphs();
  231. }
  232. public function deleteOldGraphs() {
  233. foreach (glob(Yii::app()->params['RootDirectory'] . 'images/jpgraph/*') as $filename) {
  234. $file_age = time() - filemtime($filename);
  235. if ($file_age > Yii::app()->params['TimeLimit']) {
  236. unlink($filename);
  237. }
  238. }
  239. }
  240. }