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 /* pData class definition */
48 class pData
49  {
50   var $Data;
51   var $DataDescription;
52
53   function pData()
54    {
55     $this->Data                           = "";
56     $this->DataDescription                = "";
57     $this->DataDescription["Position"]    = "Name";
58     $this->DataDescription["Format"]["X"] = "number";
59     $this->DataDescription["Format"]["Y"] = "number";
60     $this->DataDescription["Unit"]["X"]   = NULL;
61     $this->DataDescription["Unit"]["Y"]   = NULL;
62    }
63
64   function ImportFromCSV($FileName,$Delimiter=",",$DataColumns=-1,$HasHeader=FALSE,$DataName=-1)
65    {
66     $handle = @fopen($FileName,"r");
67     if ($handle)
68      {
69       $HeaderParsed = FALSE;
70       while (!feof($handle))
71        {
72         $buffer = fgets($handle, 4096);
73         $buffer = str_replace(chr(10),"",$buffer);
74         $buffer = str_replace(chr(13),"",$buffer);
75         $Values = split($Delimiter,$buffer);
76
77         if ( $buffer != "" )
78          {
79           if ( $HasHeader == TRUE && $HeaderParsed == FALSE )
80            {
81             if ( $DataColumns == -1 )
82              {
83               $ID = 1;
84               foreach($Values as $key => $Value)
85                { $this->SetSerieName($Value,"Serie".$ID); $ID++; }
86              }
87             else
88              {
89               $SerieName = "";
90
91               foreach($DataColumns as $key => $Value)
92                $this->SetSerieName($Values[$Value],"Serie".$Value);
93              }
94             $HeaderParsed = TRUE;
95            }
96           else
97            {
98             if ( $DataColumns == -1 )
99              {
100               $ID = 1;
101               foreach($Values as $key => $Value)
102                { $this->AddPoint(intval($Value),"Serie".$ID); $ID++; }
103              }
104             else
105              {
106               $SerieName = "";
107               if ( $DataName != -1 )
108                $SerieName = $Values[$DataName];
109
110               foreach($DataColumns as $key => $Value)
111                $this->AddPoint($Values[$Value],"Serie".$Value,$SerieName);
112              }
113            }
114          }
115        }
116       fclose($handle);
117      }
118    }
119
120   function AddPoint($Value,$Serie="Serie1",$Description="")
121    {
122     if (is_array($Value) && count($Value) == 1)
123      $Value = $Value[0];
124
125     $ID = 0;
126     for($i=0;$i<=count($this->Data);$i++)
127      { if(isset($this->Data[$i][$Serie])) { $ID = $i+1; } }
128
129     if ( count($Value) == 1 )
130      {
131       $this->Data[$ID][$Serie] = $Value;
132       if ( $Description != "" )
133        $this->Data[$ID]["Name"] = $Description;
134       elseif (!isset($this->Data[$ID]["Name"]))
135        $this->Data[$ID]["Name"] = $ID;
136      }
137     else
138      {
139       foreach($Value as $key => $Val)
140        {
141         $this->Data[$ID][$Serie] = $Val;
142         if (!isset($this->Data[$ID]["Name"]))
143          $this->Data[$ID]["Name"] = $ID;
144         $ID++;
145        }
146      }
147    }
148
149   function AddSerie($SerieName="Serie1")
150    {
151     if ( !isset($this->DataDescription["Values"]) )
152      {
153       $this->DataDescription["Values"][] = $SerieName;
154      }
155     else
156      {
157       $Found = FALSE;
158       foreach($this->DataDescription["Values"] as $key => $Value )
159        if ( $Value == $SerieName ) { $Found = TRUE; }
160
161       if ( !$Found )
162        $this->DataDescription["Values"][] = $SerieName;
163      }
164    }
165
166   function AddAllSeries()
167    {
168     unset($this->DataDescription["Values"]);
169
170     if ( isset($this->Data[0]) )
171      {
172       foreach($this->Data[0] as $Key => $Value)
173        {
174         if ( $Key != "Name" )
175          $this->DataDescription["Values"][] = $Key;
176        }
177      }
178    }
179
180   function RemoveSerie($SerieName="Serie1")
181    {
182     if ( !isset($this->DataDescription["Values"]) )
183      return(0);
184
185     $Found = FALSE;
186     foreach($this->DataDescription["Values"] as $key => $Value )
187      {
188       if ( $Value == $SerieName )
189        unset($this->DataDescription["Values"][$key]);
190      }
191    }
192
193   function SetAbsciseLabelSerie($SerieName = "Name")
194    {
195     $this->DataDescription["Position"] = $SerieName;
196    }
197
198   function SetSerieName($Name,$SerieName="Serie1")
199    {
200     $this->DataDescription["Description"][$SerieName] = $Name;
201    }
202
203   function SetXAxisName($Name="X Axis")
204    {
205     $this->DataDescription["Axis"]["X"] = $Name;
206    }
207
208   function SetYAxisName($Name="Y Axis")
209    {
210     $this->DataDescription["Axis"]["Y"] = $Name;
211    }
212
213   function SetXAxisFormat($Format="number")
214    {
215     $this->DataDescription["Format"]["X"] = $Format;
216    }
217
218   function SetYAxisFormat($Format="number")
219    {
220     $this->DataDescription["Format"]["Y"] = $Format;
221    }
222
223   function SetXAxisUnit($Unit="")
224    {
225     $this->DataDescription["Unit"]["X"] = $Unit;
226    }
227
228   function SetYAxisUnit($Unit="")
229    {
230     $this->DataDescription["Unit"]["Y"] = $Unit;
231    }
232
233   function SetSerieSymbol($Name,$Symbol)
234    {
235     $this->DataDescription["Symbol"][$Name] = $Symbol;
236    }
237
238   function removeSerieName($SerieName)
239    {
240     if ( isset($this->DataDescription["Description"][$SerieName]) )
241      unset($this->DataDescription["Description"][$SerieName]);
242    }
243
244   function removeAllSeries()
245    {
246     foreach($this->DataDescription["Values"] as $Key => $Value)
247      unset($this->DataDescription["Values"][$Key]);
248    }
249
250   function GetData()
251    {
252     return($this->Data);
253    }
254
255   function GetDataDescription()
256    {
257     return($this->DataDescription);
258    }
259  }
260?>