Ver Mensaje Individual
  #12 (permalink)  
Antiguo 27/03/2014, 13:03
a18327
 
Fecha de Ingreso: noviembre-2012
Mensajes: 74
Antigüedad: 11 años, 5 meses
Puntos: 3
Respuesta: Reportes Graficos con PHP

Se puede pasar el array de php a un array de javascript y pasar el array de javascript a la grafica
Código PHP:
Ver original
  1. <?php
  2.     $mysqli = new mysqli("localhost","root","","kosys");
  3.  
  4.     //Esta es la query
  5.     $sql = $mysqli->query("SELECT rubros.nombre as nombre, sum(productos.ventas) AS venta_rubro FROM productos INNER JOIN rubros ON productos.rubro = rubros.id GROUP BY productos.rubro");
  6.     $datos=array();//Nueva Linea
  7.     while($row = $sql->fetch_array(MYSQLI_NUM))
  8.     {                  
  9.         $nombre = $row[0];
  10.         $venta = $row[1];
  11.         $datos[]=array($nombre,$venta);//Nueva Linea
  12.     }
  13. ?>
  14. <!DOCTYPE HTML>
  15. <html>
  16.     <head>
  17.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  18.         <script type="text/javascript" src="jquery-ui-1.8.20\jquery-1.7.2.js"></script>
  19.         <script type="text/javascript">
  20.             $(function () {
  21.                 var datos=$.parseJSON(<?php echo json_encode($datos); ?>);
  22.                 $('#container').highcharts({
  23.                     chart: {
  24.                         plotBackgroundColor: null,
  25.                         plotBorderWidth: null,
  26.                         plotShadow: false
  27.                     },
  28.                     title: {
  29.                         text: 'Venta por Rubros'
  30.                     },
  31.                     tooltip: {
  32.                         pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
  33.                     },
  34.                     plotOptions: {
  35.                         pie: {
  36.                             allowPointSelect: true,
  37.                             cursor: 'pointer',
  38.                             dataLabels: {
  39.                                 enabled: true,
  40.                                 color: '#000000',
  41.                                 connectorColor: '#000000',
  42.                                 format: '<b>{point.name}</b>: {point.percentage:.1f} %'
  43.                             }
  44.                         }
  45.                     },
  46.                     series: [{
  47.                         type: 'pie',
  48.                         name: 'Venta por Rubros',
  49.                         data: datos,
  50.                     }]
  51.                 });
  52.             });
  53.         </script>
  54.     </head>
  55.     <body>
  56.         <script src="Highcharts\Highcharts-3.0.10\js\highcharts.js"></script>
  57.         <script src="Highcharts\Highcharts-3.0.10\js\modules\exporting.js"></script>
  58.         <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
  59.     </body>
  60. </html>

json_encode no necesita de librerias, ya viene con php.
Lo que genera json_encode es un string que representa un array de php, en javascript se toma ese string y se convierte de nuevo a un array con la funcion parseJSON(que es una función de jquery) y ese array se le pasa a la grafica.

Última edición por a18327; 27/03/2014 a las 13:19