diff --git a/chart.module b/chart.module index 09e2ceb..31360dc 100644 --- a/chart.module +++ b/chart.module @@ -268,12 +268,12 @@ function chart_build($chart) { $data = array(); // Must have a chart id - if (!strlen(trim($chart['#chart_id']))){ + if (!strlen(trim($chart['#chart_id']))) { return FALSE; } // We need data :) - if (!count($chart['#data']) || !is_array($chart['#data'])){ + if (!count($chart['#data']) || !is_array($chart['#data'])) { return FALSE; } @@ -284,15 +284,15 @@ function chart_build($chart) { } // Adjust resolution - if (isset($chart['#adjust_resolution'])){ - if ($chart['#adjust_resolution'] === TRUE){ + if (isset($chart['#adjust_resolution'])) { + if ($chart['#adjust_resolution'] === TRUE) { _chart_adjust_resolution($chart['#chart_id'], $chart['#data']); } - elseif ($chart['#adjust_resolution']['#adjust'] == TRUE){ + elseif ($chart['#adjust_resolution']['#adjust'] == TRUE) { _chart_adjust_resolution($chart['#chart_id'], $chart['#data'], $chart['#adjust_resolution']['#max']); } } - + $data['chd'] = 't:' . _chart_encode_data($chart['#data']); _chart_append('cht', $chart['#type'], $data); @@ -311,6 +311,7 @@ function chart_build($chart) { _chart_append('chbh', $chart['#bar_size'], $data); _chart_append('chld', $chart['#countries'], $data); _chart_append('chtm', $chart['#georange'], $data); + _chart_append('chma', $chart['#margins'], $data); $charts[$chart['#chart_id']] = drupal_query_string_encode($data); @@ -412,42 +413,53 @@ function _chart_encode_data($data) { * for the selected encoding type. */ function _chart_adjust_resolution($chart_id, &$data, $max_value = NULL) { - static $max; + static $max, $min; - if (count($data)){ + if (count($data)) { + list ($tmax, $tmin) = _chart_get_max_min($data); // Set max data value - if (!isset($max[$chart_id])){ - $max[$chart_id] = isset($max_value) ? $max_value : _chart_get_max($data); + if (!isset($max[$chart_id])) { + $max[$chart_id] = isset($max_value) ? $max_value : $tmax; + } + // Set min data value + if (!isset($min[$chart_id])) { + $min[$chart_id] = $tmin; } + + $range = $max[$chart_id] - $min[$chart_id]; // Encoding resolution $resoluton = 100; - - // When the max is larger than the resolution + + // Bump: add this to values to make zero the new baseline + $bump = 0 - $min[$chart_id]; + + // When the range is larger than the resolution // we need to scale down the values - if ($max[$chart_id] > $resoluton){ - $divider = round($max[$chart_id] / $resoluton, 1); + if ($range > $resoluton) { + $divider = round($range / $resoluton, 1); } else { - $multiplier = round($resoluton / $max[$chart_id], 1); + $multiplier = round($resoluton / $range, 1); } - foreach($data AS $k => $v){ - if (is_array($v)){ + foreach ($data as $k => $v) { + if (is_array($v)) { _chart_adjust_resolution($chart_id, $data[$k]); } else { - if ($v != 'NULL' && $v > 0){ + if ($v != 'NULL') { + $v += $bump; // Adjust values - if ($v >= $max){ + if ($v >= $max[$chart_id] + $bump){ $data[$k] = $resoluton; } else { // Multiply or divide data values to adjust them to the resolution - if (isset($divider)){ + if (isset($divider)) { $data[$k] = floor($v / $divider); } - elseif (isset($multiplier)){ + elseif (isset($multiplier)) { $data[$k] = floor($v * $multiplier); } else { @@ -471,6 +483,13 @@ function _chart_append($attr, $value, &$data) { } switch($attr) { + // Margins + case 'chma': + // Remove any whitespace from this value + // Otherwise these spaces are encoded and the margins will not work + $data[$attr] = str_replace(" ", "", $value); + break; + // Type case 'cht': $data[$attr] = $value; @@ -587,7 +606,7 @@ function _chart_append($attr, $value, &$data) { if (!isset($data[$attr])) { $data[$attr] = ''; } - $data[$attr] .= implode(',', array($value['#size'], $value['#spacing'])); + $data[$attr] .= implode(',', array($value['#size'], $value['#spacing'], $value['#group_spacing'])); break; // Mixed axis positions, labels and styles @@ -697,22 +716,55 @@ function _chart_append($attr, $value, &$data) { /** * Return the max value of a single level array. */ -function _chart_get_max($array) { +function _chart_get_max_min($array) { + + // if first element is array, assume we have array of arrays + if (is_array($array[0])) { + + // loop through each sub-array + foreach ($array as $k => $v) { + if (is_array($v)) { + // initialize max/min values 1st time through + if ($k == 0) { + $max = max($v); + $min = min($v); + } + else { + if (max($v) > $max) { + $max = max($v); + } + if (min($v) < $min) { + $min = min($v); + } + } + } + } + + } + else { + // Just a single-level array + $max = max($array); + $min = min($array); + } + +/* rsort($array, SORT_NUMERIC); $max = is_array($array[0]) ? 1 : $array[0]; - if (count($array)){ - foreach($array AS $k => $v){ - if (is_array($v)){ + if (count($array)) { + foreach($array AS $k => $v) { + if (is_array($v)) { rsort($v, SORT_NUMERIC); - if ($v[0] > $max){ + if ($v[0] > $max) { $max = $v[0]; } } } } +*/ + + return array($max, $min); - return $max; } /** @@ -1066,10 +1118,11 @@ function chart_range_marker($start, $end, $virtical = TRUE, $color = '000000') { * * @return array */ -function chart_bar_size($size = 40, $spacing = 20) { +function chart_bar_size($size = 40, $spacing = 20, $group_spacing = 30) { return array( - '#size' => $size, - '#spacing' => $spacing, + '#size' => $size, + '#spacing' => $spacing, + '#group_spacing' => $group_spacing, ); }