You are here

function _chart_adjust_resolution in Google Chart Tools: Image Charts 7

Same name and namespace in other branches
  1. 5 chart.module \_chart_adjust_resolution()
  2. 6 chart.module \_chart_adjust_resolution()

Adjusts chart data transforming values so that they may be represented properly within the given resolution for the selected encoding type.

1 call to _chart_adjust_resolution()
chart_build in ./chart.module
Build chart query data.

File

./chart.module, line 474
Provides primary Drupal hook implementations.

Code

function _chart_adjust_resolution($chart_id, &$data, $max_value = NULL) {
  $max =& drupal_static(__FUNCTION__, array());
  if (count($data)) {

    // Set max data value
    if (!isset($max[$chart_id])) {
      $max[$chart_id] = isset($max_value) ? $max_value : _chart_get_max($data);
    }

    // Encoding resolution
    $resoluton = 100;

    // When the max is larger than the resolution
    // we need to scale down the values
    if ($max[$chart_id] > $resoluton) {
      $divider = $max[$chart_id] / $resoluton;
    }
    elseif ($max[$chart_id] > 0) {
      $multiplier = $resoluton / $max[$chart_id];
    }
    foreach ($data as $k => $v) {
      if (is_array($v)) {
        _chart_adjust_resolution($chart_id, $data[$k]);
      }
      else {
        if (is_numeric($v)) {

          // Adjust values
          if ($v >= $max) {
            $data[$k] = $resoluton;
          }
          else {

            // Multiply or divide data values to adjust them to the resolution
            if (isset($divider)) {
              $data[$k] = floor($v / $divider);
            }
            elseif (isset($multiplier)) {
              $data[$k] = floor($v * $multiplier);
            }
            else {
              $data[$k] = $v;
            }
          }
        }
      }
    }
  }
}