PHP - Problema al usar google chart

 
Vista:
sin imagen de perfil

Problema al usar google chart

Publicado por José Vicente (3 intervenciones) el 05/11/2022 09:17:04
Hola, estoy intentando hacer una página web con php en la que intento mostrar los datos de una tabla postgres en un gráfico google chart pero no consigo que me muestre nada. Mi código es:
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
<!DOCTYPE html>
 
<?php
    $db_conexion = pg_connect("host=localhost dbname=base_datos user=usuario password=contraseña");
?>
 
<html>
    <head>
        <meta charset="UTF-8">
        <title> GRÁFICA WEB </title>
        <script src="https://www.google.com/jsapi"></script>
        <script src="https://www.gstatic.com/charts/loader.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script>
 google.load("visualization", "1", {packages:["corechart"]});
 google.setOnLoadCallback(drawChart);
 function drawChart() {
 var data = google.visualization.arrayToDataTable([
 
 ['fecha','sistolica','diastolica','pulsaciones','saturacion'],
 <?php
			$query = "SELECT * from VALORES";
 
			 $exec = Pg_query($db_conexiononexion,$query);
			 while($row = pg_fetch_array($exec))
                         {
 
 
			 echo "['".$row['fecha']."',".$row['sistolica']
                                 ."',".$row['diastolica']."',"
                                 .$row['pulsaciones']."',".$row['saturacion']."],";
                         }
 ?>
 ]);
 var options = {
          title: 'VALORES OBTENIDOS',
          hAxis: {title: 'FECHAS',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0}
        };
 
 var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
 }
 
    </script>
 
 
    <body>
        <div id="chart_div" style="width: 100%; height: 500px;"></div>
    </body>
</html>
¿Podeis echarme un cable? Gracias.
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
sin imagen de perfil

Problema al usar google chart

Publicado por José Vicente (3 intervenciones) el 07/11/2022 10:24:29
He cambiado algunas cosas del codigo para ver si me funcionaba y ahora me muestra el error:
Data column(s) for axis #0 cannot be of type string.
Si el llenado del array lo coloco al inicio del código compruebo que si que lo está haciendo, sin embargo, al colocarlo en su lugar me salta el error anterior.
Mi código actualizado es:
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
<?php
$db_conexion = pg_connect('host=localhost dbname=BD user=postgres password=password');
?>
<!DOCTYPE html>
<html>
<head>
 
    <meta charset="UTF-8">
    <title>GRÁFICA WEB</title>
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
 
</head>
<body>
 
<div id="chart_div" style="width: 100%; height: 500px;"></div>
 
<script src="https://www.google.com/jsapi"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
<script>
google.charts.load("visualization", "1.0", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart(){
 
    var data = new google.visualization.arrayToDataTable([
 
       ['fecha', 'sistolica', 'diastolica', 'pulsaciones', 'saturacion'],
 
        <?php
 
        $exec = Pg_query($db_conexion, 'SELECT * from VALORES;');
 
        while($row = pg_fetch_array($exec))
        {
            echo
            '[  "'.$row['fecha'].'",
                "'.$row['sistolica'].'",
                "'.$row['diastolica'].'",
                "'.$row['pulsaciones'].'",
                "'.$row['saturacion'].'"
            ],';
        }
 
         ?>
 
    ]);
 
    var options =
        {
        title: 'VALORES OBTENIDOS',
        isStacked: true,
        height: 300,
        legend: {position: 'bottom', maxLines: 1},
        hAxis: {title: 'FECHAS', titleTextStyle: {color: 'Black'}, format: "dd-MM-yyyy"},
        vAxis: {minValue: 0}
    };
 
    var chart_div = document.getElementById('chart_div');
    var chart = new google.visualization.AreaChart(chart_div);
    chart.draw(data, options);
 
}
</script>
 
</body>
</html>

la Bd es:
fecha-> date
sistolica-> numeric
diastolica-> numeric
pulsaciones-> numeric
saturacion-> numeric.

¿Podéis ayudarme con el error? Gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar