Ver Mensaje Individual
  #10 (permalink)  
Antiguo 09/07/2013, 07:43
Avatar de barna_rasta
barna_rasta
 
Fecha de Ingreso: agosto-2003
Mensajes: 214
Antigüedad: 20 años, 8 meses
Puntos: 11
Respuesta: Crear GRAFICOS desde consola con API google

Finalmente despues de varios dias dandole vueltas a la cebolla y gogleando,
encontre la solucion.

Nos olvidamos de todo lo anterior expuesto y planteamos:

Teniendo el fichero en /tmp/grafico0.csv con las columnas:
SEMANA - 2010 - 2011 - 2012 -2013
Cita:
"1 ",367745 ,269985 ,268073 ,287646
"2 ",343729 ,288395 ,393720 ,385249
"3 ",330653 ,297594 ,350004 ,404357
"4 ",317748 ,324180 ,318972 ,367725
"5 ",310912 ,307350 ,363278 ,361848
"6 ",275798 ,325789 ,329747 ,356989
"7 ",274978 ,291914 ,336503 ,327177
"8 ",283894 ,294966 ,333348 ,287788
"9 ",291978 ,297027 ,320611 ,343504
"10 ",318932 ,308997 ,322562 ,327881
"11 ",338740 ,283180 ,322524 ,285226
"12 ",313487 ,296403 ,330604 ,296442
"13 ",283156 ,316749 ,305207 ,292316
"14 ",314782 ,380498 ,314883 ,268771
"15 ",331694 ,359216 ,323501 ,314356
"16 ",306397 ,317591 ,367551 ,285752
"17 ",285296 ,367338 ,382748 ,304425
"18 ",323747 ,400269 ,366613 ,323763
"19 ",311326 ,399952 ,380544 ,329625
"20 ",272800 ,383026 ,337980 ,331878
"21 ",264653 ,405760 ,356246 ,309363
"22 ",281740 ,389612 ,308829 ,335734
"23 ",300993 ,421008 ,305370 ,0
"24 ",318235 ,372159 ,320713 ,0
"25 ",294067 ,352550 ,324048 ,0
"26 ",303087 ,423274 ,296180 ,0
"27 ",300837 ,408148 ,328379 ,0
"28 ",294846 ,382694 ,317006 ,0
"29 ",275660 ,384468 ,304507 ,0
"30 ",293253 ,355270 ,314554 ,0
"31 ",264974 ,351602 ,311686 ,0
"32 ",232175 ,345229 ,293352 ,0
"33 ",236461 ,280920 ,255687 ,0
"34 ",271710 ,322965 ,293535 ,0
"35 ",338225 ,410150 ,401225 ,0
"36 ",329303 ,375133 ,408475 ,0
"37 ",396505 ,383980 ,364862 ,0
"38 ",380231 ,374350 ,359844 ,0
"39 ",396692 ,406653 ,373006 ,0
"40 ",357469 ,422755 ,378883 ,0
"41 ",332266 ,391751 ,313895 ,0
"42 ",326764 ,386580 ,394546 ,0
"43 ",372687 ,414610 ,357510 ,0
"44 ",287799 ,404508 ,360034 ,0
"45 ",307413 ,400408 ,363263 ,0
"46 ",299928 ,386693 ,370634 ,0
"47 ",314336 ,370042 ,347080 ,0
"48 ",343070 ,370778 ,397639 ,0
"49 ",293429 ,291873 ,320715 ,0
"50 ",475053 ,446788 ,421183 ,0
"51 ",553601 ,656852 ,717018 ,0
"52 ",254415 ,313197 ,417132 ,0
La columna SEMANA sera la que actuara de coordenada X, por lo que la entrecomillamos para ser 'string'

Y el script final " creagrafico.php " seria el siguiente, en negrita los cambios introducidos:
Código PHP:
<html>
  <head>
<?php
// lectura del archivo CSV y guarda datos en array $js
$file file("/tmp/grafico0.csv");

// $arr is an array of javascript arrays
$arr = array();
// $i is the index for the x-axis
$i 0;
foreach (
$file as $row) {
    
$arr[] = "[ $row]";
    
$i++;
}
// $js is the final javascript array
$js '['implode(','$arr). ']';
?>

    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    
      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});
      
      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table, 
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

      // Create the data table.
      var data = new google.visualization.DataTable();
   [B]
data.addColumn('string', 'Semana');
      data.addColumn('number', '2010');
      data.addColumn('number', '2011');
      data.addColumn('number', '2012');
      data.addColumn('number', '2013');
      data.addRows(<?php echo $js?>);
[/B]

      // Set chart options
      var options = {'title':'Ventas por Semanas',
                     'width':1100,
                     'height':500};

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
<!--Div that will hold the pie chart-->
    <div id="chart_div" style="width:1100; height:500"></div>
  </body>
</html>
finalmente solo nos queda ejecutar desde CONSOLA
php creagrafico.php > /tmp/a.html
y manipular el resultado a.html como necesitemos.


Felices vacaciones, yo las inicio maññññññññaaaaana!!!!