forked from Shemahmforash/VisualData
/
energy_consumption_data_explorer.php
executable file
·195 lines (159 loc) · 7.82 KB
/
energy_consumption_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
require_once("data_explorer.php");
class EnergyConsumptionDataExplorer extends DataExplorer {
/**
* Subclass constructor calls parent class
* @param <String> $key The key of the spreadsheet
*/
function __construct($key="", $showAverage=false) {
error_reporting(E_ALL & ~E_NOTICE);
if ($key == "")
parent::__construct('pyj6tScZqmEd1G8qI4GpZQg', $showAverage);
else
parent::__construct($key, $showAverage);
}
/**
* Creates a graphic (png image) from the data and parameters chosen chosen
* @param <String> $file_name the name of the file
* @param <String> $title the title of the graphic (optional, if null it will get the title from the spreadsheet)
* @param <Int> $chartType the type of graphic (by default its 2, i.e., a line graph)
*/
public function createChart($file_name, $title="", $yAxisTitle="", $chartType=2, $yDataToUse=null, $xDataToUse=null) {
//gets the Y labels from the data
$yAxis = $this->getYAxisLabels();
/* Create and populate the pData object */
$MyData = new pData();
//adds the Y Data
$this->filterYData($MyData, $yAxis, $yDataToUse);
//defines the yAxis title (if it isn't passed in the method it uses the name of the default data)
if ($yAxisTitle == "")
$MyData->setAxisName(0, "Energy Consumption");
else
$MyData->setAxisName(0, $yAxisTitle);
//adds the X Data
//$MyData->addPoints($this->getXAxisLabels(), "Years");
$this->filterXData($MyData, $this->getXAxisLabels(), $xDataToUse);
$MyData->setSerieDescription("Years", "Years");
$MyData->setAbscissa("Years");
//DEFINO O EIXO DOS XX COMO DATAS
//$MyData->setXAxisDisplay(AXIS_FORMAT_DATE);
/* Create the pChart object */
$myPicture = new pImage(900, 430, $MyData);
/* Turn off Antialiasing */
$myPicture->Antialias = FALSE;
/* Add a border to the picture */
$myPicture->drawRectangle(0, 0, 899, 429, array("R" => 0, "G" => 0, "B" => 0));
/* Write the chart title */
$myPicture->setFontProperties(array("FontName" => "pchart/fonts/Forgotte.ttf", "FontSize" => 11));
//when the title isn't passed to the method, it will get the title directly from the spreadsheet
if ($title == "")
$title = $this->getTitle();
$myPicture->drawText(150, 35, $title, array("FontSize" => 20, "Align" => TEXT_ALIGN_BOTTOMMIDDLE));
/* Set the default font */
$myPicture->setFontProperties(array("FontName" => "pchart/fonts/pf_arma_five.ttf", "FontSize" => 6));
/* Define the chart area */
$myPicture->setGraphArea(60, 40, 850, 400);
/* Draw the scale */
$scaleSettings = array("XMargin" => 10, "YMargin" => 10, "Floating" => TRUE, "GridR" => 200, "GridG" => 200, "GridB" => 200, "DrawSubTicks" => TRUE, "CycleBackground" => TRUE);
$myPicture->drawScale($scaleSettings);
/* Turn on Antialiasing */
$myPicture->Antialias = TRUE;
/**
* Draws the graphic given its type (Note: myPicture is passed by reference here)
*/
$this->getChart($myPicture, $chartType);
/* $myPicture->setGraphArea(500, 60, 670, 190);
$myPicture->drawFilledRectangle(480, 170, 650, 200, array("R" => 255, "G" => 255, "B" => 255, "Surrounding" => -200, "Alpha" => 10));
$myPicture->drawScale(array("XMargin" => 10, "YMargin" => 10, "Pos" => SCALE_POS_LEFTRIGHT, "DrawSubTicks" => TRUE));
$myPicture->setShadow(TRUE, array("X" => -1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
$myPicture->drawLineChart();
$myPicture->setShadow(FALSE); */
/* Write the chart legend */
$myPicture->drawLegend(540, 20, array("Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
$myPicture->render("pictures/" . $file_name);
}
/**
* Filters the Y axis data (in the energy case, these are the countries)
* @param <pData> $data a pData object (passed by reference)
* @param <String[]> $yLabels
* @param <String[]> $filterData
*/
private function filterYData(&$data, $yLabels, $filterData=null) {
if (empty($filterData)) {
$data->addPoints($this->getDataRow(2), $yLabels[2]);
$data->addPoints($this->getDataRow(3), $yLabels[3]);
//$data->addPoints($this->getDataRow(4), $yLabels[4]);
if ($this->showAverage) {
//médias
for ($i = 0; $i < count($this->getDataRow(2)); $i++) {
$data->addPoints($data->getSerieAverage($yLabels[2]), "{$yLabels[2]} (Media)");
$data->addPoints($data->getSerieAverage($yLabels[3]), "{$yLabels[3]} (Media)");
//$data->addPoints($data->getSerieAverage($yLabels[4]), "Media {$yLabels[4]}");
}
}
} else {
/*
* Esta funcao serve para converter o array de strings para inteiros
* (Isto teve que ser feito por causa que a funcao quando recebe a
* string da problemas de memoria)
*/
$filterDataInt = array_map(
create_function('$value', 'return (int)$value;'),
$filterData
);
//adds the chosen data to the graphic
for ($i = 0; $i < count($filterDataInt); $i++) {
echo "array[$i] = " . $filterDataInt[$i] . "<br/>";
$data->addPoints($this->getDataRow($filterDataInt[$i]), $yLabels[$filterDataInt[$i]]);
if ($this->showAverage) {
//médias
for ($k = 0; $k < count($this->getDataRow($filterDataInt[$i])); $k++) {
$data->addPoints($data->getSerieAverage($yLabels[$filterDataInt[$i]]), "{$yLabels[$filterDataInt[$i]]} (Media)");
}
}
}
}
}
/**
* This function filters the data to use in the X axis (usually the years)
* @param <type> $data a pData object (passed by reference)
* @param <type> $xLabels the complete labels of the xAxis contained in the spreadsheet
* @param <type> $filterData the xLabels to be used in the graphic
*/
private function filterXData(&$data, $xLabels, $filterData=null) {
if (empty($filterData)) {
$data->addPoints($xLabels, "Years");
} else {
/*
* Esta funcao serve para converter o array de strings para inteiros
* (Isto teve que ser feito por causa que a funcao quando recebe a
* string da problemas de memoria)
*/
$filterDataInt = array_map(
create_function('$value', 'return (int)$value;'),
$filterData
);
$data->addPoints($filterDataInt, "Years");
}
}
/**
* Draws the chart by passing the object that draws and the type of chart
* @param <pImage> $mypic
* @param <integer> $chartType an integer that defines the type of chart (1->bar, 2->line, 3->plot)
* @return <type>
*/
private function getChart($mypic, $chartType) {
switch ($chartType) {
case "1": $gchart = $mypic->drawBarChart(array("DisplayPos" => LABEL_POS_INSIDE, "DisplayValues" => FALSE, "Rounded" => TRUE, "Surrounding" => 30));
break;
case "2": $gchart = $mypic->drawLineChart();
break;
case "3": $gchart = $mypic->drawPlotChart(array("PlotSize" => 1, "PlotBorder" => TRUE, "BorderSize" => 1));
;
break;
default: $gchart = $mypic->drawLineChart();
}
return $gchart;
}
}