= 300000) { //chart too large return ERROR_CHART_TOO_LARGE; } $url = 'http://chart.apis.google.com/chart?' .'chs=' .$settings['width']. 'x' .$settings['height'] .'&cht=' .$settings['type'] .'&chd=e:'; if ($settings['type'] == 'lc') { // for a normal line chart // quickly go through to find the maximum value to calculate the divider $i = 0; foreach ($data as $val1) { if (is_array($val1)) { $maxVal[$i] = max($val1); ++$i; } else { $maxVal[0] = $val1; } } if ($float) { $divider = 4095 / (max($maxVal) * 10); } else { $divider = 4095 / max($maxVal); } } foreach ($data as $val1Index => $val1) { if (is_array($val1)) { // multiple data sets $single_data_set = false; ///< used when filling area under the graph if ($settings['type'] != 'lc') { //for any other chart, calculate the $divider = 4095 / max($val1); //divider here. } foreach ($val1 as $val2Index => $val2) { //no need to check if it's an if ($float) { //array again $url .= _encode( (int)($val2 * 10 * $divider) ); } else { $url .= _encode( (int)($val2 * $divider) ); } // shape markers $url_chm .= _shape_markers($val1Index, $val2Index, $settings); } $url .= ','; } else { // single data set $single_data_set = true; $divider = 4095 / max($data); if ($float) { $url .= _encode( (int)($val1 * 10 * $divider) ); } else { $url .= _encode( (int)($val1 * $divider) ); } // shape markers $url_chm .= _shape_markers(0, $val1Index, $settings); } } $markers_defined = false; if ($url_chm != '') { $url .= '&chm=' .rtrim($url_chm, '|'); $markers_defined = true; } // range markers if ((is_array($settings['range']['direction'])) && (is_array($settings['range']['start'])) && (is_array($settings['range']['end']))) { //only if all these settings are $url_chm = ''; //defined foreach ($settings['range']['direction'] as $key => $dir) { switch ($dir) { case 'v': //vertical $url_chm_dir = 'R'; break; case 'h': //horizontal $url_chm_dir = 'r'; break; } if (_is_valid_color_code($settings['range']['color'][$key])) { $url_chm_color = $settings['range']['color'][$key]; } else { //if this is not a valid colour, make it light blue $url_chm_color = 'eeeeff'; } $url_chm .= $url_chm_dir. ',' .$url_chm_color. ',0,' .$settings['range']['start'][$key]. ',' .$settings['range']['end'][$key]. '|'; } if ($url_chm != '') { if (!$markers_defined) { $url .= '&chm='; } else { $url .= '|'; } $url .= rtrim($url_chm, '|'); $markers_defined = true; } } // fill area $url_chm = ''; if ($single_data_set) { if (_is_valid_color_code($settings['fill'])) { $url_chm = 'B,' .$settings['fill']. ',0,0,0|'; } } else { if (is_array($settings['fill'])) { foreach($settings['fill'] as $top => $topVal) { foreach($topVal as $bottom => $color) { if (_is_valid_color_code($color)) { $url_chm .= 'b,' .$color. ',' .$top. ',' .$bottom. ',0|'; } } } } } if ($url_chm != '') { if (!$markers_defined) { $url .= '&chm='; } else { $url .= '|'; } $url .= rtrim($url_chm, '|'); $markers_defined = true; } if (isset($settings['barwidth'])) { $url .= '&chbh=' .$settings['barwidth']; if (isset($settings['barspace'])) { $url .= ',' .$settings['barspace']; } } // data set colours if (isset($settings['datasetcolor'])) { // are codes defined at all? if (is_array($settings['datasetcolor'])) { // codes for several sets $url .= '&chco='; $i = 0; foreach($settings['datasetcolor'] as $key => $color) { if (_is_valid_color_code($color)) { if ($key != $i) { //if a colour declaration was omitted, a for (; $i < $key; ++$i) { //nice orange will be defaulted for each $url .= 'ff9900,'; //of the omitted sets } $i = $key; } $url .= $color. ','; //append the current colour ++$i; } } } } $url = rtrim($url, ','); // data set line styles if (isset($settings['datasetstyle'])) { if (is_array($settings['datasetstyle'])) { $i = 0; foreach ($settings['datasetstyle'] as $key => $style) { if ($key != $i) { //if a declaration was omited, go for some defaults for (; $i < $key; ++$i) { //for each of the omitted sets $url_chls .= '1.5,1,0|'; } $i = $key; } if (!isset($style['thick'])) { $url_chls .= '1.5'; } else { $url_chls .= $style['thick']; } if (!isset($style['line'])) { $url_chls .= ',1'; } else { $url_chls .= ','.$style['line']; } if (!isset($style['blank'])) { $url_chls .= ',0'; } else { $url_chls .= ','.$style['blank']; } $url_chls .= '|'; ++$i; } $url .= '&chls=' .rtrim($url_chls, '|'); } } // chart and background fill $declared = false; for ($counter = 0; $counter <= 1; ++$counter) { switch ($counter) { case 0: $bgOrCFull = 'background'; $bgOrCShort = 'bg'; break; case 1: $bgOrCFull = 'chart'; $bgOrCShort = 'c'; break; } $success = false; ///< stores whether at least one colour was added if ($settings[$bgOrCFull. 'filltype'] == 's') { // solid fill if (_is_valid_color_code($settings[$bgOrCFull. 'fillcolor'])) { $url_add .= 's,'.$settings[$bgOrCFull. 'fillcolor']; $success = true; } } elseif ($settings[$bgOrCFull. 'filltype'] == 'lg') { // linear gradient fill $url_add = 'lg,'; if ( (0 <= (int)$settings[$bgOrCFull. 'fillangle']) && ((int)$settings[$bgOrCFull. 'fillangle'] <= 90)) {//is angle 0 - 90? $url_add .= $settings[$bgOrCFull. 'fillangle']; if (is_array($settings[$bgOrCFull. 'fillcolor'])) { //array of colours $i = 0; foreach ($settings[$bgOrCFull. 'fillcolor'] as $color) { if (_is_valid_color_code($color)) { if ( (isset($settings[$bgOrCFull. 'filloffset'][$i])) && (0 <= (float)$settings[$bgOrCFull. 'filloffset'][$i]) && ((float)$settings[$bgOrCFull. 'filloffset'][$i] <= 1)) { $url_add .= ',' .$color. ',' .$settings[$bgOrCFull. 'filloffset'][$i]; $success = true; // a colour was added } } ++$i; } } } } elseif ($settings[$bgOrCFull. 'filltype'] == 'ls') { // linear stripes fill $url_add = 'ls,'; if ( (0 <= (int)$settings[$bgOrCFull. 'fillangle']) && ((int)$settings[$bgOrCFull. 'fillangle'] <= 90)) {//is angle 0 - 90? $url_add .= $settings[$bgOrCFull. 'fillangle']; if (is_array($settings[$bgOrCFull. 'fillcolor'])) { //array of colours $i = 0; foreach ($settings[$bgOrCFull. 'fillcolor'] as $color) { if (_is_valid_color_code($color)) { if ( (isset($settings[$bgOrCFull. 'fillwidth'][$i])) && (0 <= (float)$settings[$bgOrCFull. 'fillwidth'][$i]) && ((float)$settings[$bgOrCFull. 'fillwidth'][$i] <= 1)) { $url_add .= ',' .$color. ',' .$settings[$bgOrCFull. 'fillwidth'][$i]; $success = true; // a colour was added } } ++$i; } } } } if ($success) { //if at least one colour was added if ($declared) { //if a previous (eg. background) definition was there $url .= '|' .$bgOrCShort. ','.$url_add; //we can add it to the final url } else { $url .= '&chf=' .$bgOrCShort. ',' .$url_add; $declared = true; } } } // labels // chart title if (isset($settings['title'])) { $url .= '&chtt=' .preg_replace("/\\%0D\\%0A|\\%0A|\\%0D/", '|', urlencode($settings['title'])); $colorDefined = false; //useful if only size is set if (_is_valid_color_code($settings['titlecolor'])) { $colorDefined = true; $url .= '&chts=' .$settings['titlecolor']; } if (ctype_digit($settings['titlesize'])) { if ($colorDefined) { $url .= ',' .$settings['titlesize']; } else { $url .= '&chts=000000,' .$settings['titlesize']; } } } // legend/pie chart labels if (is_array($settings['label'])) { $url .= '&'; if (($settings['type'] == 'p') || ($settings['type'] == 'p3')) { $url .= 'chl='; } else { $url .= 'chdl='; } foreach ($settings['label'] as $key => $label) { $url .= $label; if (count($settings['label']) > ($key + 1)) { $url .= '|'; } } } // axis labels if (is_array($settings['axis'])) { $url_chxt = ''; $url_chxl = ''; $url_chxp = ''; $url_chxr = ''; $url_chxs = ''; $i = 0; foreach ($settings['axis'] as $axis => $values) { if ( ($axis == 'x') || ($axis == 't') || ($axis == 'y') || ($axis == 'r') ) { if (is_array($values)) { foreach ($values as $key => $value) { $url_chxt .= $axis; $url_chxt .= ','; if (isset($value)) { $url_chxl .= $i. ':|'; if (is_array($value)) { $url_chxl .= implode('|', $value). '|'; if (isset($settings['axisposition'][$axis][$key])) { $url_chxp .= $i. ','; if (is_array($settings['axisposition'][$axis][$key])) { $url_chxp .= implode(',', $settings['axisposition'][$axis][$key]) . '|'; } else { $url_chxp .= $settings['axisposition'][$axis][$key]. '|'; } } } else { $url_chxl .= $value. '|'; if (isset($settings['axisposition'][$axis][$key])) { $url_chxp .= $i. ','; $url_chxp .= $settings['axisposition'][$axis][$key]. '|'; } } if ((isset($settings['axisrange'][$axis][$key]['start'])) && (isset($settings['axisrange'][$axis][$key]['end']))) { $url_chxr .= $i. ',' .$settings['axisrange'][$axis][$key]['start']. ',' .$settings['axisrange'][$axis][$key]['end']. '|'; } $url_chxs_color = $settings['axiscolor'][$axis][$key]; $url_chxs_size = $settings['axissize'][$axis][$key]; switch ($settings['axisalign'][$axis][$key]) { case 'l': $url_chxs_align = '-1'; break; case 'c': $url_chxs_align = '0'; break; case 'r': $url_chxs_align = '1'; break; } if (!isset($url_chxs_color)) { //if the colour is not set $url_chxs_color = '666666'; //default it to grey } if (!isset($url_chxs_size)) { //if the size is not set $url_chxs_size = '11.5'; //default it to 11.5 } $url_chxs_ = $i. ',' .$url_chxs_color. ',' .$url_chxs_size; if (!isset($url_chxs_align)) { //if the alignment is not present $url_chxs_ .= '|'; //just omit the unneeded declaration } else { $url_chxs_ .= ',' .$url_chxs_align .'|'; } if ($url_chxs_ != ($i .',666666,11.5|')) { //if it's not just all $url_chxs .= $url_chxs_; //defaults then append it to the url } ++$i; } } } } } if ($url_chxt != '') { $url .= '&chxt=' .rtrim($url_chxt, ','); } if ($url_chxl != '') { $url .= '&chxl=' .rtrim($url_chxl, '|'); } if ($url_chxp != '') { $url .= '&chxp=' .rtrim($url_chxp, '|'); } if ($url_chxr != '') { $url .= '&chxr=' .rtrim($url_chxr, '|'); } if ($url_chxs != '') { $url .= '&chxs=' .rtrim($url_chxs, '|'); } } // grid lines if (is_array($settings['grid'])) { if (isset($settings['grid']['xstep'])) { $url .= '&chg=' .$settings['grid']['xstep']; } else { //if xstep is not defined, make every 1st level label of x axis where if (isset($settings['axis']['x'][0])) { //the gridlines appear $count = count($settings['axis']['x'][0]) - 1; if ($count == 0) { //if there is only one label on x axis $count = 10000; //make 100/count very small so it is rounded up to 0. } $url .= '&chg=' .number_format(100 / $count, 1, '.', ''); } else { //if that also fails, make no x-axis gridlines whatsoever $url .= '&chg=100'; } } if (isset($settings['grid']['ystep'])) { $url .= ',' .$settings['grid']['ystep']; } else { //if ystep is not defined, make every 1st level label of y axis where if (isset($settings['axis']['y'][0])) { //the gridlines appear $count = count($settings['axis']['y'][0]) - 1; if ($count == 0) { //if there is only one label on x axis $count = 10000; //make 100/count very small so it is rounded up to 0. } $url .= ',' .number_format(100 / $count, 1, '.', ''); } else { //if that also fails, make no y-axis gridlines whatsoever $url .= ',100'; } } $grid_defined = true; } // grid line style if (is_array($settings['gridstyle'])) { if ($grid_defined) { //don't bother if there are no grids defined if ( (isset($settings['gridstyle']['line'])) && //only add if all of the (isset($settings['gridstyle']['blank'])) ) { // settings are present $url .= ',' .$settings['gridstyle']['line']. ',' .$settings['gridstyle']['blank']; } } } //return the whole resultant url. return $url; } ?>