You are here

function _chart_adjust_resolution in Google Chart Tools: Image Charts 6

Same name and namespace in other branches
  1. 5 chart.module \_chart_adjust_resolution()
  2. 7 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 433
Provides Google chart API integration.

Code

function _chart_adjust_resolution($chart_id, &$data, $max_value = NULL) {
  static $max;
  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
    $resolution = 100;

    // When the max is larger than the resolution
    // we need to scale down the values
    if ($max[$chart_id] > $resolution) {
      $divider = $max[$chart_id] / $resolution;
    }
    elseif ($max[$chart_id] > 0) {
      $multiplier = $resolution / $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] = $resolution;
          }
          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;
            }
          }
        }
      }
    }
  }
}