Ver Mensaje Individual
  #5 (permalink)  
Antiguo 02/05/2016, 14:23
novo34
 
Fecha de Ingreso: enero-2015
Ubicación: Ecuador
Mensajes: 9
Antigüedad: 9 años, 3 meses
Puntos: 0
Respuesta: Array en Reporte Grafico de ventas

Cita:
Iniciado por xerifandtomas Ver Mensaje
Prueba algo asi:
Código PHP:
Ver original
  1. <?php
  2. $arrayMeses = array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
  3.  
  4. $connection = mysqli_connect("localhost","root","","db") or die("Error " . mysqli_error($connection));
  5. //fetch department names from the department table
  6. $sql = "SELECT Month(created_at) AS Meses, Sum(total) AS total_Mes FROM sell WHERE Year(created_at)=2016 GROUP BY Meses ORDER BY Meses asc";
  7. $result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));
  8.  
  9. $valores  = array();
  10. while($row = mysqli_fetch_array($result))
  11. {
  12.     $valores["total_mes"][]=$row[1];
  13.     $valores["meses"][]=$arrayMeses[date($row[0])-1];
  14. }
  15. $valores_json= json_encode($valores);
  16. mysqli_close($connection);
  17.  
  18. /*  Optienes algo como esto:
  19.  
  20. {"total_mes":[8,130,166,112],"meses":["Enero","Febrero","Marzo","Abril"]}
  21.  
  22. */
  23.  
  24. ?>
  25. <!DOCTYPE HTML>
  26. <html>
  27.     <head>
  28.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  29.         <title>Highcharts Example</title>
  30.  
  31.         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  32.         <style type="text/css">
  33. ${demo.css}
  34.         </style>
  35.         <script type="text/javascript">
  36. $(function () {
  37.     var valores =  eval('('+'<?php echo $valores_json; ?>'+')');
  38.    
  39.     $('#container').highcharts({
  40.         chart: {
  41.             type: 'column'
  42.         },
  43.         title: {
  44.             text: 'Ventas mensuales'
  45.         },
  46.         subtitle: {
  47.             text: '2016'
  48.         },
  49.         xAxis: {
  50.             categories: valores["meses"],    
  51.             crosshair: true
  52.         },
  53.         yAxis: {
  54.             min: 0,
  55.             title: {
  56.                 text: 'Dolares (USD)'
  57.             }
  58.         },
  59.         tooltip: {
  60.             headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
  61.             pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
  62.                 '<td style="padding:0"><b>{point.y:.1f} usd</b></td></tr>',
  63.             footerFormat: '</table>',
  64.             shared: true,
  65.             useHTML: true
  66.         },
  67.         plotOptions: {
  68.             column: {
  69.                 pointPadding: 0.2,
  70.                 borderWidth: 0
  71.             }
  72.         },
  73.         series: [{
  74.             name: 'Total Mes',
  75.             data: valores["total_mes"]
  76.  
  77.         }, {
  78.             name: 'Gastos',
  79.             data: valores["gastos"]
  80.  
  81.         }, {
  82.             name: 'Devoluciones',
  83.             data: valores["devoluciones"]
  84.  
  85.         }]
  86.     });
  87. });
  88.         </script>
  89. </head>
  90. <body>
  91. <script src="https://code.highcharts.com/highcharts.js"></script>
  92. <script src="https://code.highcharts.com/modules/exporting.js"></script>
  93. <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
  94.  
  95. </body>
  96. </html>
Muchas muchas gracias me funcionó perfecto, solo le tuve que cambiar una linea de código ya que no me salían los valores.
esta:
Código PHP:
Ver original
  1. $valores_json= json_encode($valores);

por esta:
Código PHP:
Ver original
  1. $valores_json= preg_replace( "/\"(\d+)\"/", '$1', json_encode($valores, JSON_NUMERIC_CHECK));

saludos.