1<?php 2/** 3 * pData - Simplifying data population for pChart 4 * 5 * @copyright 2008 Jean-Damien POGOLOTTI 6 * @version 2.0 7 * @copyright 2010 Tim Martin 8 * 9 * http://pchart.sourceforge.net 10 * 11 * This program is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation, either version 1,2,3 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25/** 26 * @brief Imports data from CSV files into a pData object 27 * 28 * All the methods on this class are static, it's debatable whether it 29 * really needs to be a class, or whether it should be a namespace. 30 */ 31class CSVImporter { 32 /** 33 * @brief Import data into the specified dataobject from a CSV 34 * file 35 * 36 * @param $FileName Name of the file (with path if necessary) 37 * @param $DataColumns If specified, this should be an array of 38 * strings that give the names that will be suffixed to "Serie" 39 * to name the series of data. If this is left to the default, 40 * numbers will be used instead. 41 */ 42 static public function importFromCSV(pData $data, $FileName, $Delimiter = ",", $DataColumns = -1, $HasHeader = FALSE, $DataName = -1) { 43 $handle = @fopen($FileName, "r"); 44 45 if($handle == null) { 46 throw new Exception("Failed to open file"); 47 } 48 49 $HeaderParsed = FALSE; 50 while(!feof($handle)) { 51 $buffer = fgets($handle, 4096); 52 53 if($buffer != "") { 54 if($HasHeader && !$HeaderParsed) { 55 self::importHeaderFromCSV($data, $buffer, $Delimiter, $DataColumns); 56 $HeaderParsed = true; 57 } else { 58 self::importChunkFromCSV($data, $buffer, $Delimiter, $DataColumns, $DataName); 59 } 60 } 61 } 62 fclose($handle); 63 } 64 65 static private function importHeaderFromCSV(pData $data, $buffer, $Delimiter, $DataColumns) { 66 $buffer = str_replace(chr(10), "", $buffer); 67 $buffer = str_replace(chr(13), "", $buffer); 68 $Values = explode($Delimiter, $buffer); 69 70 if($DataColumns == -1) { 71 $ID = 1; 72 foreach($Values as $key => $Value) { 73 $data->SetSeriesName($Value, "Serie".$ID); 74 $ID++; 75 } 76 } else { 77 foreach($DataColumns as $key => $Value) 78 $data->SetSeriesName($Values [$Value], "Serie".$Value); 79 } 80 } 81 82 /** 83 * @brief Import CSV data from a partial file chunk 84 */ 85 static private function importChunkFromCSV(pData $data, $buffer, $Delimiter, $DataColumns, $DataName) { 86 $buffer = str_replace(chr(10), "", $buffer); 87 $buffer = str_replace(chr(13), "", $buffer); 88 $Values = explode($Delimiter, $buffer); 89 90 if($DataColumns == -1) { 91 $ID = 1; 92 foreach($Values as $key => $Value) { 93 $data->AddPoint(intval($Value), "Serie".$ID); 94 $ID++; 95 } 96 } else { 97 $SerieName = ""; 98 if($DataName != -1) 99 $SerieName = $Values [$DataName]; 100 101 foreach($DataColumns as $key => $Value) 102 $data->AddPoint($Values [$Value], "Serie".$Value, $SerieName); 103 } 104 } 105}