pData.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. pData - Simplifying data population for pChart
  4. Copyright (C) 2008 Jean-Damien POGOLOTTI
  5. Version 1.13 last updated on 08/17/08
  6. http://pchart.sourceforge.net
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 1,2,3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. Class initialisation :
  18. pData()
  19. Data populating methods :
  20. ImportFromCSV($FileName,$Delimiter=",",$DataColumns=-1,$HasHeader=FALSE,$DataName=-1)
  21. AddPoint($Value,$Serie="Serie1",$Description="")
  22. Series manipulation methods :
  23. AddSerie($SerieName="Serie1")
  24. AddAllSeries()
  25. RemoveSerie($SerieName="Serie1")
  26. SetAbsciseLabelSerie($SerieName = "Name")
  27. SetSerieName($Name,$SerieName="Serie1")
  28. + SetSerieSymbol($Name,$Symbol)
  29. SetXAxisName($Name="X Axis")
  30. SetYAxisName($Name="Y Axis")
  31. SetXAxisFormat($Format="number")
  32. SetYAxisFormat($Format="number")
  33. SetXAxisUnit($Unit="")
  34. SetYAxisUnit($Unit="")
  35. removeSerieName($SerieName)
  36. removeAllSeries()
  37. Data retrieval methods :
  38. GetData()
  39. GetDataDescription()
  40. */
  41. /* pData class definition */
  42. class pData
  43. {
  44. var $Data;
  45. var $DataDescription;
  46. function pData()
  47. {
  48. $this->Data = "";
  49. $this->DataDescription = "";
  50. $this->DataDescription["Position"] = "Name";
  51. $this->DataDescription["Format"]["X"] = "number";
  52. $this->DataDescription["Format"]["Y"] = "number";
  53. $this->DataDescription["Unit"]["X"] = NULL;
  54. $this->DataDescription["Unit"]["Y"] = NULL;
  55. }
  56. function ImportFromCSV($FileName,$Delimiter=",",$DataColumns=-1,$HasHeader=FALSE,$DataName=-1)
  57. {
  58. $handle = @fopen($FileName,"r");
  59. if ($handle)
  60. {
  61. $HeaderParsed = FALSE;
  62. while (!feof($handle))
  63. {
  64. $buffer = fgets($handle, 4096);
  65. $buffer = str_replace(chr(10),"",$buffer);
  66. $buffer = str_replace(chr(13),"",$buffer);
  67. $Values = split($Delimiter,$buffer);
  68. if ( $buffer != "" )
  69. {
  70. if ( $HasHeader == TRUE && $HeaderParsed == FALSE )
  71. {
  72. if ( $DataColumns == -1 )
  73. {
  74. $ID = 1;
  75. foreach($Values as $key => $Value)
  76. { $this->SetSerieName($Value,"Serie".$ID); $ID++; }
  77. }
  78. else
  79. {
  80. $SerieName = "";
  81. foreach($DataColumns as $key => $Value)
  82. $this->SetSerieName($Values[$Value],"Serie".$Value);
  83. }
  84. $HeaderParsed = TRUE;
  85. }
  86. else
  87. {
  88. if ( $DataColumns == -1 )
  89. {
  90. $ID = 1;
  91. foreach($Values as $key => $Value)
  92. { $this->AddPoint(intval($Value),"Serie".$ID); $ID++; }
  93. }
  94. else
  95. {
  96. $SerieName = "";
  97. if ( $DataName != -1 )
  98. $SerieName = $Values[$DataName];
  99. foreach($DataColumns as $key => $Value)
  100. $this->AddPoint($Values[$Value],"Serie".$Value,$SerieName);
  101. }
  102. }
  103. }
  104. }
  105. fclose($handle);
  106. }
  107. }
  108. function AddPoint($Value,$Serie="Serie1",$Description="") {
  109. if (is_array($Value) && count($Value) == 1)
  110. $Value = $Value[0];
  111. $ID = 0;
  112. for ($i=0;$i<=count($this->Data);$i++) {
  113. if(isset($this->Data[$i][$Serie])) { $ID = $i+1; }
  114. }
  115. if ( count($Value) == 1 ) {
  116. $this->Data[$ID][$Serie] = $Value;
  117. if ( $Description != "" ) $this->Data[$ID]["Name"] = $Description;
  118. elseif (!isset($this->Data[$ID]["Name"])) $this->Data[$ID]["Name"] = $ID;
  119. } else {
  120. foreach ($Value as $key => $Val) {
  121. $this->Data[$ID][$Serie] = $Val;
  122. if (!isset($this->Data[$ID]["Name"]))
  123. $this->Data[$ID]["Name"] = $ID;
  124. $ID++;
  125. }
  126. }
  127. }
  128. function AddSerie($SerieName="Serie1")
  129. {
  130. if ( !isset($this->DataDescription["Values"]) )
  131. {
  132. $this->DataDescription["Values"][] = $SerieName;
  133. }
  134. else
  135. {
  136. $Found = FALSE;
  137. foreach($this->DataDescription["Values"] as $key => $Value )
  138. if ( $Value == $SerieName ) { $Found = TRUE; }
  139. if ( !$Found )
  140. $this->DataDescription["Values"][] = $SerieName;
  141. }
  142. }
  143. function AddAllSeries()
  144. {
  145. unset($this->DataDescription["Values"]);
  146. if ( isset($this->Data[0]) )
  147. {
  148. foreach($this->Data[0] as $Key => $Value)
  149. {
  150. if ( $Key != "Name" )
  151. $this->DataDescription["Values"][] = $Key;
  152. }
  153. }
  154. }
  155. function RemoveSerie($SerieName="Serie1")
  156. {
  157. if ( !isset($this->DataDescription["Values"]) )
  158. return(0);
  159. $Found = FALSE;
  160. foreach($this->DataDescription["Values"] as $key => $Value )
  161. {
  162. if ( $Value == $SerieName )
  163. unset($this->DataDescription["Values"][$key]);
  164. }
  165. }
  166. function SetAbsciseLabelSerie($SerieName = "Name")
  167. {
  168. $this->DataDescription["Position"] = $SerieName;
  169. }
  170. function SetSerieName($Name,$SerieName="Serie1")
  171. {
  172. $this->DataDescription["Description"][$SerieName] = $Name;
  173. }
  174. function SetXAxisName($Name="X Axis")
  175. {
  176. $this->DataDescription["Axis"]["X"] = $Name;
  177. }
  178. function SetYAxisName($Name="Y Axis")
  179. {
  180. $this->DataDescription["Axis"]["Y"] = $Name;
  181. }
  182. function SetXAxisFormat($Format="number")
  183. {
  184. $this->DataDescription["Format"]["X"] = $Format;
  185. }
  186. function SetYAxisFormat($Format="number")
  187. {
  188. $this->DataDescription["Format"]["Y"] = $Format;
  189. }
  190. function SetXAxisUnit($Unit="")
  191. {
  192. $this->DataDescription["Unit"]["X"] = $Unit;
  193. }
  194. function SetYAxisUnit($Unit="")
  195. {
  196. $this->DataDescription["Unit"]["Y"] = $Unit;
  197. }
  198. function SetSerieSymbol($Name,$Symbol)
  199. {
  200. $this->DataDescription["Symbol"][$Name] = $Symbol;
  201. }
  202. function removeSerieName($SerieName)
  203. {
  204. if ( isset($this->DataDescription["Description"][$SerieName]) )
  205. unset($this->DataDescription["Description"][$SerieName]);
  206. }
  207. function removeAllSeries()
  208. {
  209. foreach($this->DataDescription["Values"] as $Key => $Value)
  210. unset($this->DataDescription["Values"][$Key]);
  211. }
  212. function GetData()
  213. {
  214. return($this->Data);
  215. }
  216. function GetDataDescription()
  217. {
  218. return($this->DataDescription);
  219. }
  220. }
  221. ?>