Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/11/2013, 15:16
MrBay88
 
Fecha de Ingreso: noviembre-2013
Mensajes: 8
Antigüedad: 10 años, 5 meses
Puntos: 0
Json y librería highcharts

He buscado por el foro (y por google), pero no encuentro la solución, y si la he leído, no la he entendido.

No soy capaz de dibujar ninguna gráfica. Si yo le inserto los datos manualmente sí, pero si intento que los lea a través del json, no.

He creado un formulario que me manda los datos a una base de datos MYSQL. Bien. Luego, convierto esos datos a json con un json_encode.

Os dejo el Php:

Código:
<?php
$con = mysql_connect("url", "user", "password");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("nombre_base_datos", $con);

$result = mysql_query("SELECT Mes, Compras FROM nombre_de_la_tabla");

$rows = array();
while($r = mysql_fetch_array($result)) {
$row['Mes'] = $r[0];
$row['Compras'] = $r[1];
array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

mysql_close($con);
?>
Que me devuelve en este formato:
[["Enero",3],["Febrero",8]]

A continuación intento dibujar la gráfica con el json que me proporciona este php.

Os dejo el código:

Código:
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highcharts Pie Chart</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Web Sales & Marketing Efforts'
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                }
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        }

        $.getJSON("nombre_archivo.php", function(json) {
            options.series[0].data = json;
            chart = new Highcharts.Chart(options);
        });



    });   
    </script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
Creo que el problema está o en el formato del json o cuando llamo al json. El ejemplo que sigo es de esta web: http://blueflame-software.com/blog/highcharts-pie-chart-php-mysql-example/

Me da igual el tipo de gráfica, ya sea una "pie chart" o una gráfica de barras.

Mi intención es crear una encuesta y mostrar los resultados al momento. Si conocéis otra manera de hacerlo, o ayudarme/guiarme os lo agradecería también. Ando un poco desesperado ya. Cualquier mínima ayuda sería de gran utilidad.

Muchas gracias por adelantado.