Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GphpChart.class.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. class GphpChart
  3. {
  4. var $chart;
  5. var $chart_url;
  6. var $base_url = "http://chart.apis.google.com/chart?";
  7. var $width = 300;
  8. var $height = 200;
  9. var $types = array ("lc","lxy","bhs","bvs","bhg","bvg","p","p3","v","s");
  10. var $chart_types = array('l' => 'line','b' => 'bar','p'=> 'pie','v' => 'venn','s' => 'scatter');
  11. var $mandatory_parameters = array('chs','chd','cht');
  12. var $data_prepared = false;
  13. var $allowed_parameters = array(
  14. 'l' => array('chtt','chdl','chco','chf','chxt','chg','chm','chls','chxp'),
  15. 'b' => array('chtt','chbh','chdl','chco','chf','chxt','chxp'),
  16. 'p' => array('chtt','chco','chf','chl'),
  17. 'v' => array('chtt','chdl','chco','chf'),
  18. 's' => array('chtt','chdl','chco','chf','chxt','chg','chm','chxp'),
  19. );
  20. var $range = 1;
  21. var $encodings = array(
  22. 's' => array('sep' => '','set' => ',','range' => 61,'missing' => '_'),
  23. 't' => array('sep' => ',','set' => '|','range' => 100,'missing' => -1),
  24. 'e' => array('sep' => '','set' => ',','range' => 4096,'missing' => '__'),
  25. );
  26. var $simple_encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  27. // min and max values of horizontal axis
  28. var $max_xt = 0;
  29. var $min_xt = 100000; // fake value to be sure we got the min data value
  30. // min and max values for vertical axis
  31. var $max_yr = 0;
  32. var $min_yr = 100000; // fake value to be sure we got the min data value
  33. var $ratio = false;
  34. var $cached = true;
  35. var $prepared = false;
  36. function GphpChart($type = 'lc',$encoding = 't')
  37. {
  38. $this->chart = (object) NULL;
  39. // $chart = new stdClass();
  40. if(!in_array($type,$this->types)) return false;
  41. else $this->chart->cht = $type;
  42. $this->chart_type = $this->chart_types[substr($this->chart->cht,0,1)];
  43. if(!in_array($encoding,array_keys($this->encodings))) return false;
  44. else $this->encoding = $encoding;
  45. $this->sep = $this->encodings[$this->encoding]['sep'];
  46. $this->range = $this->encodings[$this->encoding]['range'];
  47. $this->missing = $this->encodings[$this->encoding]['missing'];
  48. $this->set = $this->encodings[$this->encoding]['set']; // set separator
  49. if($this->chart_type == 'venn') $this->set = ',';
  50. $string = $this->simple_encoding;
  51. unset($this->simple_encoding);
  52. for($i = 0;$i< strlen($string);$i++) $this->simple_encoding[] = $string[$i];
  53. $this->extended_encoding = $this->simple_encoding;
  54. $this->extended_encoding[] = '-'; $this->extended_encoding[] = '.'; $this->extended_encoding[] = '_'; $this->extended_encoding[] = ',';
  55. }
  56. /* PRE GENERATION : add labels, data, axis, etc */
  57. function add_data($values,$color = '')
  58. {
  59. $this->cached = false;
  60. if($color != '' && strlen($color) == 6) $this->chart->chco[] = $color;
  61. $this->datas[] = $values;
  62. }
  63. function add_labels($axis,$values)
  64. {
  65. $this->cached = false;
  66. if($values_type = $values['values_type'])
  67. {
  68. if($values_type == 'discret')
  69. {
  70. $min = $values['min']; $max = $values['max'];
  71. unset($values);
  72. for($i = $min; $i<=$max;$i++) $values[] = $i;
  73. }
  74. }
  75. // reverse order for Bar Horizontal
  76. if($this->chart->cht == 'bhs' && is_string($values[0])) $values = array_combine(array_keys($values),array_reverse(array_values($values)));
  77. $this->labels[$axis][] = $values;
  78. if($axis == 'x' || $axis == 't')
  79. {
  80. $this->max_xt = max($this->max_xt,max($values));
  81. $this->min_xt = min($this->min_xt,min($values));
  82. }
  83. // min and max values for vertical axis are calculated in prepare_data()
  84. }
  85. function set_bar_width($width,$space = 0)
  86. {
  87. $this->cached = false;
  88. $this->chart->chbh = (int) $width;
  89. if($space != 0) $this->chart->chbh .= ','.$space;
  90. }
  91. function fill($area,$type,$params)
  92. {
  93. $this->cached = false;
  94. $this->chart->chf[] = "$area,$type,$params";
  95. }
  96. function add_legend($array)
  97. {
  98. $this->cached = false;
  99. if($this->chart_type == 'pie') $this->chart->chl = implode('|',$array);
  100. else $this->chart->chdl = implode('|',$array);
  101. }
  102. function add_style($string)
  103. {
  104. $this->cached = false;
  105. if($this->chart_type == 'line') $this->chart->chls[] = $string;
  106. }
  107. function add_grid($string)
  108. {
  109. $this->cached = false;
  110. if($this->chart_type == 'line' || $this->chart_type == 'scatter') $this->chart->chg[] = $string;
  111. }
  112. function add_marker($string)
  113. {
  114. $this->cached = false;
  115. if($this->chart_type == 'line' || $this->chart_type == 'scatter') $this->chart->chm[] = $string;
  116. }
  117. /* END PRE GENERATION FUNCTIONS */
  118. /* GENERATE FUNCTIONS : call prepare functions, prepare url, outputs url or full image string */
  119. function get_Image_URL()
  120. {
  121. if($this->cached)
  122. {
  123. if(!$this->filename) $this->generate_filename();
  124. return $this->filename;
  125. }
  126. else
  127. {
  128. if(!$this->prepared) $this->prepare();
  129. return $this->chart_url;
  130. }
  131. }
  132. function get_Image_String()
  133. {
  134. if($this->cached)
  135. {
  136. if(!$this->filename) $this->generate_filename();
  137. $string = '<img alt="'.$this->title.'" src="'.$this->filename.'" />';
  138. }
  139. else
  140. {
  141. if(!$this->prepared) $this->prepare();
  142. $string = '<img alt="'.$this->title.'" src="'.$this->chart_url.'" />';
  143. }
  144. return $string;
  145. }
  146. function prepare()
  147. {
  148. if(!$this->data_prepared) $this->prepare_data();
  149. $this->prepare_labels();
  150. $this->prepare_title();
  151. $this->prepare_styles();
  152. $this->prepare_url();
  153. $this->prepared = true;
  154. }
  155. /* END GENERATE FUNCTIONS */
  156. /* CACHE FUNCTIONS */
  157. function generate_filename()
  158. {
  159. $this->filename = urlencode($this->title).'.png';
  160. }
  161. function save_Image()
  162. {
  163. if(!$this->filename) $this->generate_filename();
  164. /* get image file */
  165. //$this->chart_url = htmlspecialchars($this->chart_url);
  166. //$this->chart_url = urlencode($this->chart_url);
  167. if( function_exists('file_get_contents') && $this->image_content = file_get_contents($this->chart_url) )
  168. $this->image_fetched = true;
  169. if(!$this->image_fetched)
  170. {
  171. if($fp = fopen($this->chart_url,'r'))
  172. {
  173. $this->image_content = fread($fp);
  174. fclose($fp);
  175. $this->image_fetched = true;
  176. }
  177. }
  178. /* write image to cache */
  179. if($this->image_fetched)
  180. {
  181. $fp = fopen($this->filename,'w+');
  182. if($fp)
  183. {
  184. fwrite($fp,$this->image_content);
  185. fclose($fp);
  186. }
  187. else { return false; }
  188. }
  189. else { return false; }
  190. return true;
  191. }
  192. /* PREPARE FUNCTIONS : called by generate functions, these ones parse labels and data */
  193. function prepare_url()
  194. {
  195. $this->chart_url = $this->base_url;
  196. /*
  197. foreach($this->mandatory_parameters as $param)
  198. {
  199. if(!isset($this->chart->$param)) return false;
  200. $params[] = $param.'='.$this->chart->$param;
  201. }
  202. */
  203. foreach($this->chart as $k => $v)
  204. {
  205. if($v != '') $params[] = "$k=$v";
  206. }
  207. $this->chart_url .= implode('&',$params);
  208. }
  209. function prepare_styles()
  210. {
  211. // SIZE
  212. if(($this->width * $this->height) > 300000)
  213. {
  214. // reduces dimensions to match API limits ( 300mpixels )
  215. $size = $this->width * $this->height;
  216. $this->width = round($this->width * (300000 / $size),0);
  217. $this->height = round($this->height * (300000 / $size),0);
  218. }
  219. $this->chart->chs = $this->width.'x'.$this->height;
  220. // colors
  221. if(isset($this->chart->chco) && is_array($this->chart->chco)) $this->chart->chco = implode(',',$this->chart->chco);
  222. if(isset($this->chart->chf) && is_array($this->chart->chf)) $this->chart->chf = implode('|',$this->chart->chf);
  223. // styles
  224. if($this->chart_type == 'scatter' || $this->chart_type == 'line')
  225. {
  226. if($this->chart_type == 'line') if(isset($this->chart->chls) && count($this->chart->chls)) $this->chart->chls = implode('|',$this->chart->chls);
  227. if(isset($this->chart->chg) && count($this->chart->chg)) $this->chart->chg = implode('|',$this->chart->chg);
  228. // markers
  229. if(isset($this->chart->chm) && count($this->chart->chm)) $this->chart->chm = implode('|',$this->chart->chm);
  230. }
  231. }
  232. function prepare_size()
  233. {
  234. }
  235. function prepare_data()
  236. {
  237. // for lines charts, calculate ratio
  238. if($this->chart_type == 'line' || $this->chart_type == 'bar' || $this->chart_type == 'scatter')
  239. {
  240. $this->max_yr = 0;
  241. foreach($this->datas as $n => $data)
  242. {
  243. if($this->chart_type == 'scatter' && $n == 2) continue; // ignore min max values for plots sizes
  244. $this->max_yr = max($this->max_yr,max($data));
  245. $this->min_yr = min($this->min_yr,min($data));
  246. }
  247. $this->ratio = 0.9 * $this->range / $this->max_yr;
  248. }
  249. foreach($this->datas as $n => $data)
  250. {
  251. if($this->chart_type == 'scatter' && $n == 2) $data = $this->encode_data($data,false); // do not normalize plots sizes
  252. else $data = $this->encode_data($data);
  253. if($this->chart->cht == 'lxy')
  254. {
  255. $this->datas[$n] = implode($this->sep,array_keys($data)).'|'.implode($this->sep,array_values($data));
  256. }
  257. else $this->datas[$n] = implode($this->sep,$data);
  258. }
  259. $this->chart->chd = "$this->encoding:";
  260. $this->chart->chd .= implode($this->set,$this->datas);
  261. $this->data_prepared = true;
  262. }
  263. function prepare_labels()
  264. {
  265. //chxt= axis titles
  266. //chxl= set:labels
  267. //chxr= range
  268. //chxp= positions
  269. $n = 0;
  270. if(count($this->labels))
  271. foreach($this->labels as $axis => $labelles)
  272. {
  273. foreach($labelles as $pos => $labels)
  274. {
  275. // axis type
  276. $this->chart->chxt[$n] = $axis;
  277. if(!count($labels)) continue; // no values = "neither positions nor labels. The Chart API therefore assumes a range of 0 to 100 and spaces the values evenly."
  278. // axis range
  279. if($this->chart_type == 'line' || $this->chart_type == 'bar')
  280. {
  281. if($axis == 'x' || $axis == 't')
  282. {
  283. if($this->max_xt) $this->chart->chxr[$n] = $n.','.$this->min_xt.','.$this->max_xt;
  284. }
  285. else
  286. {
  287. if($this->max_yr) $this->chart->chxr[$n] = $n.','.$this->min_yr.','.$this->max_yr;
  288. }
  289. }
  290. // axis labels
  291. $this->chart->chxl[$n] = "$n:|".implode('|',$labels);
  292. if($this->chart_type == 'line' || $this->chart_type == 'bar' || $this->chart_type == 'scatter')
  293. {
  294. if(array_slice(array_keys($labels),0,2) != array(0,1)) $this->chart->chxp[$n] = "$n,".implode(',',array_keys($labels));
  295. else $this->chart->chxp[$n] = "$n,".implode(',',array_values($labels));
  296. }
  297. $n++;
  298. }
  299. }
  300. if(count($this->chart->chxr)) $this->chart->chxr = implode('|',$this->chart->chxr);
  301. if(count($this->chart->chxp)) $this->chart->chxp = implode('|',$this->chart->chxp);
  302. if(count($this->chart->chxt)) $this->chart->chxt = implode(',',$this->chart->chxt);
  303. if(count($this->chart->chxl)) $this->chart->chxl = implode('|',$this->chart->chxl);
  304. }
  305. function prepare_title()
  306. {
  307. //chtt=first+line|second+line
  308. $this->chart->chtt = str_replace(array("\n","\n\r",'<br />','<br>'),'|',$this->title);
  309. $this->chart->chtt = str_replace(' ','+',$this->chart->chtt);
  310. }
  311. /* END PREPARE FUNCTIONS */
  312. /* ENCODING */
  313. function encode_data($data,$ratio = true)
  314. {
  315. if($this->encoding == 's')
  316. {
  317. foreach($data as $n => $value)
  318. {
  319. if(empty($value) || $value == '') $data[$n] = $this->missing;
  320. else $data[$n] = $this->simple_encoding[$value];
  321. }
  322. }
  323. elseif($this->encoding == 't')
  324. {
  325. foreach($data as $n => $value)
  326. {
  327. if(empty($value) || $value == '') $data[$n] = $this->missing;
  328. elseif($ratio && $this->ratio) $data[$n] = (float) round($value * $this->ratio,1);
  329. else $data[$n] = (float) $value;
  330. }
  331. }
  332. elseif($this->encoding == 'e')
  333. {
  334. $max = 0; $min = 100000;
  335. foreach($data as $n => $value)
  336. {
  337. if(empty($value) || $value == '') $data[$n] = $this->missing;
  338. else
  339. {
  340. // normalize
  341. if($ratio && $this->ratio) $value = round($value * $this->ratio,0);
  342. // encode
  343. $max = max($max,$value);
  344. $min = min($min,$value);
  345. $value = $this->extended_encode($value);
  346. $data[$n] = $value;
  347. }
  348. }
  349. }
  350. return $data;
  351. }
  352. function extended_encode($value)
  353. {
  354. $first = floor($value / 64);
  355. $second = $value - ($first * 64);
  356. $first = $this->extended_encoding[$first];
  357. $second = $this->extended_encoding[$second];
  358. return $first.$second;
  359. }
  360. }
  361. ?>