Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

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