PHP - Graficas Con pChart con PHP y MySQL (no me importa otra libreria de grafica)

 
Vista:
sin imagen de perfil

Graficas Con pChart con PHP y MySQL (no me importa otra libreria de grafica)

Publicado por miguel (1 intervención) el 01/04/2015 12:55:15
Estoy usando la librería de pChart para conseguir hacer una gráfica, pero no tengo mucha idea de como generarla.

No me importaría usar otra librería para generar la gráfica.

La estructura de la base de datos es la siguiente
sentencia SQL:
select Fecha, Cantidad from frecuencia where Fecha between '2015-03-30 13:38:33' and '2015-03-30 13:48:34'"

y el resultado seria el siguiente:
| 2015-03-30 16:37:30 | 1.88 |
| 2015-03-30 16:38:30 | 13.16 |
| 2015-03-30 16:39:50 | 12.94 |
| 2015-03-30 16:40:39 | 11.94 |
| 2015-03-30 16:41:29 | 11.96 |

Quiero meter ese resultado en una gráfica de linea pero no me funciona nada :(


Lo que he conseguido por ahora es que si hago una consulta especifica me funciona la grafica:

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
<?php
 
/* Include all the classes */
include("/var/www/graficas/pChart/class/pData.class.php");
include("/var/www/graficas/pChart/class/pDraw.class.php");
include("/var/www/graficas/pChart/class/pImage.class.php");
 
/* Create your dataset object */
$myData = new pData();
 
mysql_connect("localhost","root","Redinsa2015");
mysql_select_db("base");
$sql2="select Fecha as Time, Cantidad as Temp_F from frecuencia where Fecha between '2015-03-30 13:38:33' and '2015-03-30 13:48:34'";
 
$resultado=mysql_query($sql2);
/* First Y axis will be dedicated to the temperatures */
//while($rows=mysql_fetch_array($resultado)){
while($row = mysql_fetch_array($resultado))
{
//$Sample_Number = $row["Sample_Number"]; //Not using this data
//$myData->addPoints($Sample_Number,"Sample_Number");
 
$Time = $row["Time"];
$myData->addPoints($Time,"Time");
$Temp_F = $row["Temp_F"];
$myData->addPoints($Temp_F,"Temp_F");
}
 
$myData-> setSerieOnAxis("Temp_F", 0); //assigns the data to the frist axis
$myData-> setAxisName(0, "Frecuencia (Hz)"); //adds the label to the first axis
 
//$myData-> setSerieOnAxis("LDR_V", 1);
//$myData-> setAxisName(1, "LDR");
 
$myData-> setAxisPosition(1,AXIS_POSITION_LEFT); //moves the second axis to the far left
 
$myData->setAbscissa("Time"); //sets the time data set as the x axis label
 
 
$myPicture = new pImage(1100,300,$myData); /* Create a pChart object and associate your dataset */
 
$myPicture->setFontProperties(array("FontName"=>"/var/www/graficas/pChart/fonts/pf_arma_five.ttf","FontSize"=>6)); /* Choose a nice font */
 
$myPicture->setGraphArea(80,40,1000,200); /* Define the boundaries of the graph area */
 
$Settings = array("R"=>250, "G"=>250, "B"=>250, "Dash"=>1, "DashR"=>0, "DashG"=>0, "DashB"=>0);
 
$myPicture->drawScale(array("LabelRotation"=>320)); /* Draw the scale, keep everything automatic */
 
/*The combination makes a cool looking graph*/
$myPicture->drawPlotChart();
$myPicture->drawLineChart();
 
 
$myPicture->drawLegend(90,20); //adds the legend
 
$myPicture->autoOutput(); /* Build the PNG file and send it to the web browser */
 
?>


Pero si lo hago desde una peticion GET no me da la grafica:

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
<html>
<html>
<head>
<title>Graficas</title>
</head>
 
<body>
 
<?php
$pais=$_GET['pais'];
$canton=$_GET['canton'];
$parroquia=$_GET['parroquia'];
 
 
// Me conecto a la base de datos
mysql_connect("localhost","root","Redinsa2015");
mysql_select_db("base");
// Declaro la variable $paisant que es la que me va a indicar si hay que volver a cargar los datos de las provincias
//$paisant=$pais;
//$cantonant=$canton;
 
// if para verificar si se ha presionado el boton enviar
 
if (!empty($_GET['button'])){
// en caso de que se ha presionado el boton enviar, procesar el formulario
$pais=$_GET['pais'];
$canton=$_GET['canton'];
$parroquia=$_GET['parroquia'];
 
include("/var/www/graficas/pChart/class/pData.class.php");
include("/var/www/graficas/pChart/class/pDraw.class.php");
include("/var/www/graficas/pChart/class/pImage.class.php");
 
/* Create your dataset object */
$myData = new pData();
 
mysql_connect("localhost","root","Redinsa2015");
mysql_select_db("base");
//$sql2="select Fecha as Time, Cantidad as Temp_F from frecuencia where Fecha between '2015-03-30 13:38:33' and '2015-03-30 13:48:34'";
$sql2="select Fecha as Time,Cantidad as Temp_F from $pais where Fecha between '$canton' and '$parroquia'";
 
$resultado=mysql_query($sql2);
/* First Y axis will be dedicated to the temperatures */
//while($rows=mysql_fetch_array($resultado)){
while($row = mysql_fetch_array($resultado))
{
//$Sample_Number = $row["Sample_Number"]; //Not using this data
//$myData->addPoints($Sample_Number,"Sample_Number");
 
$Time = $row["Time"];
$myData->addPoints($Time,"Time");
 
$Temp_F = $row["Temp_F"];
$myData->addPoints($Temp_F,"Temp_F");
 
}
 
$myData-> setSerieOnAxis("Temp_F", 0); //assigns the data to the frist axis
$myData-> setAxisName(0, "Frecuencia (Hz)"); //adds the label to the first axis
 
//$myData-> setSerieOnAxis("LDR_V", 1);
//$myData-> setAxisName(1, "LDR");
 
$myData-> setAxisPosition(1,AXIS_POSITION_LEFT); //moves the second axis to the far left
 
$myData->setAbscissa("Time"); //sets the time data set as the x axis label
 
$myPicture = new pImage(1100,300,$myData); /* Create a pChart object and associate your dataset */
 
$myPicture->setFontProperties(array("FontName"=>"/var/www/graficas/pChart/fonts/pf_arma_five.ttf","FontSize"=>6)); /* Choose a nice font */
 
$myPicture->setGraphArea(80,40,1000,200); /* Define the boundaries of the graph area */
 
$Settings = array("R"=>250, "G"=>250, "B"=>250, "Dash"=>1, "DashR"=>0, "DashG"=>0, "DashB"=>0);
 
$myPicture->drawScale(array("LabelRotation"=>320)); /* Draw the scale, keep everything automatic */
 
/*The combination makes a cool looking graph*/
$myPicture->drawPlotChart();
$myPicture->drawLineChart();
 
$myPicture->drawLegend(90,20); //adds the legend
 
$myPicture->autoOutput(); /* Build the PNG file and send it to the web browser */
 
 
 
 
}
else
{
// caso contrario volver a general el formulario
// echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">\n\n";
?>
<form method="get" action="index.php">
<table width="70%" border="0" align="center">
<?php
 
 
 
print ("
<input type=\"hidden\" name=\"paisant\" value=\"$paisant\">
<input type=\"hidden\" name=\"cantonant\" value=\"$cantonant\">
 
<tr>
<td><div align=\"right\"><strong>Servicio:</strong></div></td>
 
<td><select name=\"pais\" onchange=\"submit();\">
");
//Muestra el combobox de las provincias una vez que se haya elegido el país, no antes
if (!isset($pais)){
print ("<option selected>Seleccione el servicio</option>");
$pais="0";
}

$sql="SHOW FULL TABLES FROM base";
$res=mysql_query($sql);

while($fila=mysql_fetch_array($res)){
print("<option value=\"$fila[Tables_in_base]\"");
if ($fila[Tables_in_base] == $pais) {
print ("selected");
}
print(">$fila[Tables_in_base]</option>\n");
}
print("</select></td></tr>");
// combo cantones
// if para mostrar los cantones
// if ($pais!="0"){
print ("

<tr>
<td><div align=\"right\"><strong>Fecha de inicio:</strong></div></td>
<td><select name=\"canton\" onchange=\"submit();\">
");
print ("<option selected>Seleccione la fecha de inicio</option>");
//Muestra el combobox de las parroquias una vez que se haya elegido el canton, no antes
if (!isset($canton)){
$canton="0";
}

$sqlcan="select Fecha from $pais where Tabla='$pais'";
$res=mysql_query($sqlcan);
 
while($fila=mysql_fetch_array($res)){
print("<option value=\"$fila[Fecha]\"");
if ($fila[Fecha] == $canton) {
print ("selected");
}
print(">$fila[Fecha]</option>\n");
}
print("</select></td></tr>");
 
// combo parroquias
// if para mostrar las parroquias
//if ($canton!="0"){
print("
<tr>
<td><div align=\"right\"><strong>Fecha de final:</strong></div></td>
<td><select name=\"parroquia\">
");

print ("<option selected>Seleccione la fecha final</option>");
if (!isset($parroquia)){

$parroquia="0";
}
$sqlparr="select Fecha from $pais where Tabla='$pais'";
$resparr=mysql_query($sqlparr);
 
while($filaprov=mysql_fetch_array($resparr)){
print("<option value=\"$filaprov[Fecha]\">$filaprov[Fecha]</option>");
}
print("
</select>
</td>
</tr>
");
// } // if canton
// } // if pais
?>
<tr>
<td><div align="right"><input name="button" type="submit" value="Enviar"></div></td>
<td><input name="reset" type="reset" value="Borrar"></td>
</tr>
</table>
</form>
<?php
} // cierro if principal del boton enviar
?>
</body>
</html>
 
Si alguien me echa una mano se lo agradeceria.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder