website stats » Google API Charts con PHP | Solo Código |

Google API Charts con PHP

Escrito por J.F. el Domingo, 23 de Diciembre del 2007 a las 13:20

Un código de ejemplo para usar Google API Charts con PHP. Simplemente introducimos los datos en un array, creamos una url codificada y lo metemos en la etiqueta img. Aquí está el código de ejemplo:

PHP:
  1. <?
  2.  
  3. // Here's an array containing some data to plot
  4. $test_data=array(0.5,6,12,17,2,0.3,9);
  5.  
  6. // Here's where we call the chart, and return the encoded chart data
  7. echo "<img src=http://chart.apis.google.com/chart?chtt=".urlencode("It's an example!")."&cht=lc&chs=450x125&chd=".chart_data($test_data).">";
  8.  
  9. // And here's the function
  10.  
  11. function chart_data($values) {
  12.  
  13. // Port of JavaScript from http://code.google.com/apis/chart/
  14. // http://james.cridland.net/code
  15.  
  16. // First, find the maximum value from the values given
  17.  
  18. $maxValue = max($values);
  19.  
  20. // A list of encoding characters to help later, as per Google's example
  21. $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  22.  
  23. $chartData = "s:";
  24.   for ($i = 0; $i <count($values); $i++) {
  25.     $currentValue = $values[$i];
  26.  
  27.     if ($currentValue> -1) {
  28.     $chartData.=substr($simpleEncoding,61*($currentValue/$maxValue),1);
  29.     }
  30.       else {
  31.       $chartData.='_';
  32.       }
  33.   }
  34.  
  35. // Return the chart data - and let the Y axis to show the maximum value
  36. return $chartData."&chxt=y&chxl=0:|0|".$maxValue;
  37. }
  38.  
  39. ?>

Vía: | Ribosomatic | Autor del tutorial

Categoria: PHP, General

No hay comentarios

Entradas relacionadas


Deja un comentario

Escribir un comentario

Puedes usar las siguientes etiquetas HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Solo Código

Solo Código es una colección de códigos de todo tipo que pueden resultar útiles para el diseño de páginas web. Está enfocado tanto para aquellos que son expertos, como para aquellos principiantes que quieren encontrar recursos útiles, o no saben implementar determinadas funciones en sus webs. No olvides que Solo Código es un blog de Informática Práctica, donde tenemos otras secciones interesantes como:
| Tutoriales | Trucos | Software | Links | Buscar |