You are here

function _chart_adjust_resolution in Google Chart Tools: Image Charts 5

Same name and namespace in other branches
  1. 6 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 string.

File

./chart.module, line 348
Google Charting API. Developed by Tj Holowaychuk

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
    $resoluton = 100;

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

          // 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;
            }
          }
        }
      }
    }
  }
}