Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Array en Reporte Grafico de ventas

Estas en el tema de Array en Reporte Grafico de ventas en el foro de PHP en Foros del Web. Hola, tengo un problema que no he podido solucionar... Si alguien me ayuda le estaría muy agradecido. tengo la siguiente consulta: Código PHP: <?php     $arrayMeses  ...
  #1 (permalink)  
Antiguo 01/05/2016, 15:10
 
Fecha de Ingreso: enero-2015
Ubicación: Ecuador
Mensajes: 9
Antigüedad: 9 años, 3 meses
Puntos: 0
Array en Reporte Grafico de ventas

Hola, tengo un problema que no he podido solucionar... Si alguien me ayuda le estaría muy agradecido.

tengo la siguiente consulta:
Código PHP:
<?php
    $arrayMeses 
= array('Enero''Febrero''Marzo''Abril''Mayo''Junio''Julio''Agosto''Septiembre''Octubre''Noviembre''Diciembre');


    
$connection mysqli_connect("localhost","root","","db") or die("Error " mysqli_error($connection));

    
//fetch department names from the department table
    
$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";
    
$result mysqli_query($connection$sql) or die("Error " mysqli_error($connection));

    
$Meses1  = array();
    while(
$row mysqli_fetch_array($result))
    {
        
$Datos = array("Meses" => $arrayMeses[date($row[0])-1], "total_Mes" => $row[1]);
        
array_push($Meses1 $Datos);
        
    }
    echo 
json_encode($Meses1 );

    
mysqli_close($connection);
?>

Que me da como resultado lo siguiente:

Código HTML:
[{"Meses":"Enero","total_Mes":"48"},{"Meses":"Febrero","total_Mes":"54"},{"Meses":"Marzo","total_Mes":"683.3000000000001"},{"Meses":"Abril","total_Mes":"689.0999999999999"}]

lo que necesito es ponerlo en el siguiente gráfico:

Código HTML:
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <style type="text/css">
${demo.css}
        </style>
        <script type="text/javascript">
$(function () {
    var Nombre=<?php echo preg_replace( "/\"(\d+)\"/", '$1', json_encode($Meses1, JSON_NUMERIC_CHECK)); ?>;
    
    
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Ventas mensuales'
        },
        subtitle: {
            text: '2016'
        },
        xAxis: {
            categories: [Nombre],    
            crosshair: true
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Dolares (USD)'
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} usd</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [{
            name: 'Ventas',
            data: [Nombre]

        }, {
            name: 'Gastos',
            data: []

        }, {
            name: 'Devoluciones',
            data: []

        }]
    });
});
        </script>
    </head>
    <body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
</html> 
  #2 (permalink)  
Antiguo 01/05/2016, 17:18
Avatar de NueveReinas  
Fecha de Ingreso: septiembre-2013
Ubicación: No tan Buenos Aires
Mensajes: 1.101
Antigüedad: 10 años, 8 meses
Puntos: 145
Respuesta: Array en Reporte Grafico de ventas

Revisa este tema, quizá te ayude: http://www.forosdelweb.com/f18/consu...ltado-1149610/

Yo también uso Highcharts.
__________________
¿Te sirvió la respuesta? Deja un +1
  #3 (permalink)  
Antiguo 02/05/2016, 08:36
 
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 NueveReinas Ver Mensaje
Revisa este tema, quizá te ayude: [url]http://www.forosdelweb.com/f18/consulta-arrastra-resultado-1149610/[/url]

Yo también uso Highcharts.
Gracias ya lo vi pero no me sirve de mucho ya que mi problema es como ponerlo en el gráfico... teniendo en cuenta que soy nuevo en programación y lo que he aprendido es a base de tutoriales.

Saludos.
  #4 (permalink)  
Antiguo 02/05/2016, 12:41
 
Fecha de Ingreso: octubre-2010
Ubicación: España
Mensajes: 1.007
Antigüedad: 13 años, 6 meses
Puntos: 123
Respuesta: Array en Reporte Grafico de ventas

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>
__________________
Unset($vida['malRollo']);

Última edición por xerifandtomas; 02/05/2016 a las 12:47
  #5 (permalink)  
Antiguo 02/05/2016, 14:23
 
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.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:02.