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