(string) it's the type of the chart * Allowed values: line, linexy, pie, pie3d, bar_hs (horizontal stacked), * bar_vs (vertical), bar_hg (grouped), bar_vg, venn, scatter * * 'width' => (integer) width of the chart * * 'height' => (integer) height of the chart * * 'title' => (string) title, placed on the top. Multiple lines are * allowed. * * 'color' => (array) all global colors are placed here. You can * specify color by standard Web notation RRGGBB or by RRGGBBAA (AA means * alpha channel - needed for transparency). 'color' is also * an associative array: * 'title' => (string) color of the title * 'bgcolor' => (string) background color of the whole chart, goes beneath * chbhcolor. * 'chbgcolor' => (string) background color of the graph part of the chart. * * 'axis' => (array) another array, this time it contains various options * regarding axes: * 'type' => (string) positioning of an axis. * Allowed values: 'y' (left), 'x' (bottom), 't' (top), 'r' (right) * 'style' => (array) axis font settings: * 'color' => (string) text color * 'fontize' => (integer) font size, in pixels * 'alignment' => (string) alignment of text * Allowed values: center, left, right * * * $datasettings => (array) (optional) other settings particular * to one of the datasets. Elements of an array should be in the same * order as in $data parameter. * 'color' => (string) dataset color (as it's drew on the chart) * 'legend' => (string) name of the dataset. It will be displayed in * the legend box. * 'linestyle' => (array) array of three integers: * 0 => thickness of the line (in pixels) * 1 => length of line segment * 2 => length of blank segment * */ /* * Internal function. Encodes data set into a * simple encoding (referring to Google Chart API). */ function _googlechart_encode_simple($data) { $decriptor='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $ret=''; foreach($data as $key) { $ret .= $decriptor[$key]; } return $ret; } /* * Internal function. Encodes data set into a * text encoding (referring to Google Chart API). */ function _googlechart_encode_text($data) { $ret=''; $prefix=''; foreach($data as $key) { $ret.=$prefix . (($key-($key%10))/10) .'.'. ($key%10); $prefix=','; } return $ret; } /* * Internal function. Encodes data set into a * extended encoding (referring to Google Chart API). */ function _googlechart_encode_extended($data) { $decriptor='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._,'; $ret=''; foreach($data as $key) { $rest=$key%64; $first=($key-$rest)/64; $ret.=$decriptor[$first] . $decriptor[$rest]; } return $ret; } /* * Main Google Chart API Wrapper function. It produces an url * to Google Chart image based on $data parameter, using * presentation specified in $settings. If provided, * additional formatting can be applied by $datasettings. * * For more information refer to Google Chart API Wrapper Documentation. * @param $data * One dimensional array of values when one set of data is given * or two dimensional when multiple sets of data are given * @param $settings * Array of global chart presentation options, like size of the * image, main title etc. * @param $datasettings * Array of additional formatting settings, specific to each set * of data provided in $data parameter. */ function googlechart_get_graph($data, $settings, $datasettings=array()) { $request=array( 'chd' => '', 'chxt' => array(), 'chxl' => array(), 'chxp' => array(), 'chxr' => array(), 'chxs' => array(), 'chco' => array(), 'chdl' => array(), 'chls' => array(), ); $request_on=array(); // Default settings if(!array_key_exists('type',$settings)) $settings['type']='pie3d'; if(!array_key_exists('width',$settings)) $settings['width']=200; if(!array_key_exists('height',$settings)) $settings['height']=100; /// CHART TYPE // * 'type' => (string) it's the type of the chart // * Allowed values: line, linexy, pie, pie3d, bar_hs (horizontal stacked), // * bar_vs (vertical), bar_hg (grouped), bar_vg, venn, scatter $decriptor=array( 'pie3d' => 'p3','pie' => 'p', 'linexy' => 'lxy', 'line'=>'lc', 'bar_hs' => 'bhs', 'bar_vs' => 'bvs', 'bar_hg' => 'bhg','bar_vg' => 'bvg', 'venn' => 'v', 'scatter' => 's' ); //So-called translation array. if(!isset($decriptor[$settings['type']])) error_log("Incorrect Google Graph type."); else $request['cht']=$decriptor[$settings['type']]; //CHART SIZE if(!is_int($settings['width']) || !is_int($settings['height'])) error_log('Incorrect Google Graph size.'); $request['chs']= $settings['width'] .'x'. $settings['height']; //CHART DATA reset($data); if(!is_array(current($data))) $data=array($data); $prefix='t:'; foreach($data as $dataset) { $request['chd'].=$prefix._googlechart_encode_text($dataset); $prefix='|'; } //CHART TITLE (& FONTSIZE & COLRO) if(array_key_exists('title',$settings)) { $title=$settings['title']; $title=str_replace(' ','+',$title); $title=str_replace('\n','|',$title); $request['chtt']=$title; } //CHART AXES $request['chxt']=array(); $request['chxl']=array(); $request['chxp']=array(); $request['chxr']=array(); $request['chxs']=array(); if(array_key_exists('axis',$settings)) { $i=0; foreach($settings['axis'] as $axis) { $request['chxt'][]=$axis['type']; if(array_key_exists('value',$axis)) $request['chxl'][]=$i .':|'. implode('|',$axis['value']); if(array_key_exists('position',$axis)) $request['chxp'][]=$i .','. implode(',',$axis['position']); if(array_key_exists('range',$axis)) $request['chxr'][]=$i .','. $axis['range'][0] .','. $axis['range'][1]; if(array_key_exists('style',$axis)) { $tmp=$i .','. $axis['style']['color']; if(array_key_exists('fontsize',$axis['style'])) { $tmp.=','. $axis['style']['fontsize']; if(array_key_exists('alignment',$axis['style'])) { switch($axis['style']['alignment']) { case 'center': $tmp.= ',0'; break; case 'left': $tmp.= ',-1'; break; case 'right': $tmp.= ',1'; break; } } } $request['chxs'][]=$tmp; } ++$i; } $request['chxt']=implode(",",$request['chxt']); $request['chxl']=implode("|",$request['chxl']); $request['chxp']=implode('|',$request['chxp']); $request['chxr']=implode("|",$request['chxr']); $request['chxs']=implode("|",$request['chxs']); } ////DATASETTINGS if(count($datasettings)>0) { foreach($data as $key=>$dataset) { if(array_key_exists($key,$datasettings)) $datasett=$datasettings[$key]; else $datasett=array(); //COLOR if(array_key_exists('color',$datasett)) { $request['chco'][]=$datasett['color']; $request_on['chco']=1; } else $request['chco'][]='000000'; //LEGEND if(array_key_exists('legend',$datasett)) { $request['chdl'][]=$datasett['legend']; $request_on['chdl']=1; } else $request['chdl'][]=''; //LINE STYLE if(array_key_exists('style',$datasett)) { if(count($datasett['style'])==3) $request['chls'][]=$datasett['style'][0] .','.$datasett['style'][1] .','.$datasett['style'][2]; $request_on['chls']=1; } else $request['chls'][]='2,2,0'; } if(array_key_exists('chco',$request_on)) $request['chco']=implode(",",$request['chco']); else $request['chco']=''; if(array_key_exists('chdl',$request_on)) $request['chdl']=implode("|",$request['chdl']); else $request['chdl']=''; if($settings['type']=='pie3d' || $settings['type']=='pie') { $request['chl']=$request['chdl']; unset($request['chdl']); } if(array_key_exists('chls',$request_on)) $request['chls']=implode("|",$request['chls']); else $request['chls']=''; } //FINAL STUFF $request_string='http://chart.apis.google.com/chart?'; //Resulting request url $prefix=''; foreach($request as $key=>$value) { if($value=='') continue; if(is_array($value)) continue; $request_string.=$prefix.$key.'='.$value; $prefix='&'; } return $request_string; } /* $set=array( 'type'=>'line', 'width'=> 300, 'height'=>200, 'title' => 'My little chart\nSo much to do!', 'color' => array( 'title' => 'FF0000' ), 'axis' => array( array( 'type' => 'y', 'style'=>array('color'=>'FF0000'), 'value'=>array('Pon','Wt','Sr'), 'position'=>array(0,10,100) ), array( 'type' => 'y', 'style'=>array('color'=>'000000','fontsize'=>'22','alignment'=>'center'), 'value'=>array('Pon','Wt','Sr'), 'position'=>array(0,20,100) ) ), ); $dat[0][]=1; $dat[0][]=20; $dat[0][]=50; $dat[0][]=53; $dat[0][]=56; $dat[0][]=60; $dat[0][]=610; $dat[1][]=900; $dat[1][]=800; $dat[1][]=750; $dat[1][]=725; $dat[1][]=713; $dat[1][]=707; $dat[1][]=704; $ds=array( array('legend'=> 'Dataset1'), array('legend'=> 'Dataset2', 'color' => '0000FF','style' => array(2,2,2)) ); */