forked from Shemahmforash/VisualData
/
data_explorer.php
executable file
·127 lines (107 loc) · 3.85 KB
/
data_explorer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// Load Zend Class Loader and Zend GData Classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_Docs');
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
// DataExplorer Class
class DataExplorer {
private $key = null;
private $spreadSheetService = null;
protected $showAverage;
/**
*
* @param <type> $spreadSheetKey
*/
function __construct($spreadSheetKey, $showAverage = false) {
$this->key = $spreadSheetKey;
$this->spreadSheetService = new Zend_Gdata_Spreadsheets(new Zend_Http_Client());
$this->showAverage = $showAverage;
}
/**
*
* @param <type> $minCol
* @param <type> $maxCol
* @param <type> $minRow
* @param <type> $maxRow
* @return <type>
*/
private function _getSpreadSheetData($minCol=null, $maxCol=null, $minRow=null, $maxRow=null) {
$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query->setSpreadsheetKey($this->key);
// this is needed for public spreadsheets
$query->setVisibility('public');
$query->setProjection('values');
// range of data to get
if (is_integer($minCol))
$query->setMinCol($minCol);
if (is_integer($maxCol))
$query->setMaxCol($maxCol);
if (is_integer($minRow))
$query->setMinRow($minRow);
if (is_integer($maxRow))
$query->setMaxRow($maxRow);
// get data into a 2 dimensional array
$feed = $this->spreadSheetService->getCellFeed($query);
foreach ($feed as $cellEntry) {
$row = $cellEntry->cell->getRow();
$col = $cellEntry->cell->getColumn();
$val = $cellEntry->cell->getText();
$data[$row][$col] = $val;
}
return $data;
}
// The following functions assume the spreadsheet data is in the following format:
//
// |----------------+----------------+----------------+-----+----------------|
// | Title | X Axis Label 1 | X Axis Label 2 | ... | X Axis Label n |
// |----------------+----------------+----------------+-----+----------------|
// | Y Axis Label 1 | value 1,1 | value 1,2 | ... | value 1,n |
// | Y Axis Label 2 | values 2,1 | value 2,2 | ... | values 2,n |
// | ... | ... | ... | ... | ... |
// | Y Axis Label n | value n,1 | value n,2 | ... | value n,3 |
// |----------------+----------------+----------------+-----+----------------|
//
/**
* Gets the title of the spreadsheet from row1, collumn1
* @return <type>
*/
public function getTitle() {
$data = $this->_getSpreadSheetData(1, 1, 1, 1);
return $data[1][1];
}
/**
* Gets the X axis Labels, which are present in the first row (from the 2nd collumn on)
* @return <type>
*/
public function getXAxisLabels() {
$data = $this->_getSpreadSheetData(2, null, 1, 1);
return $data[1];
}
/**
*
* @return <type>
*/
public function getYAxisLabels() {
$data = $this->_getSpreadSheetData(1, 1, 2, null);
// transform a column into an array of labels and make shure the key matches
// the row number on the spreadsheet
foreach ($data as $key => $row) {
$labels[$key] = $row[1];
}
return $labels;
}
/**
*
* @param <type> $row_number
* @return <type>
*/
public function getDataRow($row_number) {
$data = $this->_getSpreadSheetData(2, null, $row_number, $row_number);
return $data[$row_number];
}
}