You are here

function _chart_append in Google Chart Tools: Image Charts 5

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

When the value passed is valid append a chart API attribute and parsed values to $data.

1 call to _chart_append()
chart_build in ./chart.module
Build chart query string.

File

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

Code

function _chart_append($attr, $value, &$data) {

  // Size and fill contain defaults, all other attributes must be set
  if (!isset($value) && $attr != 'chs' && $attr != 'chf') {
    return;
  }
  switch ($attr) {

    // Type
    case 'cht':
      $data[$attr] = $value;
      break;

    // Size
    case 'chs':
      if (_chart_is_valid_size($value)) {
        $width = $value['#width'];
        $height = $value['#height'];
      }
      else {
        $width = 300;
        $height = 150;
      }
      _chart_override_aspect($width, $height);
      $data[$attr] = $width . 'x' . $height;
      break;

    // Labels
    case 'chl':
      $data[$attr] = implode('|', $value);
      break;

    // Color
    case 'chco':
      $data[$attr] = implode(',', $value);
      break;

    // Chart and background fill
    case 'chf':
      if (variable_get('chart_global_bg', FALSE) || !isset($value)) {
        $data[$attr] = implode(',', chart_fill('bg', trim(variable_get('chart_global_bg', 'FFFFFF'), '#')));
      }
      else {
        $data[$attr] = implode(',', $value);
      }
      break;

    // Chart title
    case 'chtt':
      if (is_array($value)) {
        $data[$attr] = $value['#title'];
        $data['chts'] .= isset($value['#color']) ? $value['#color'] : '000000';
        $data['chts'] .= ',';
        $data['chts'] .= isset($value['#size']) ? $value['#size'] : 14;
      }
      else {
        $data[$attr] = $value;
      }
      break;
    default:

    // Legends
    case 'chdl':
      $data[$attr] = implode('|', $value);
      break;

    // Line styles
    case 'chls':
      $styles = array();
      if (count($value)) {
        foreach ($value as $k => $v) {
          $tmp_style = array();
          if (!is_array($v)) {

            // Style parameters
            $tmp_style[] = $v;
          }
          else {

            // Array of styles
            $styles[] = implode(',', $v);
          }
          if (count($tmp_style)) {
            $styles[] = implode(',', $tmp_style);
          }
        }
      }
      if (count($styles)) {
        $data[$attr] = implode('|', $styles);
      }
      break;

    // Grid lines
    case 'chg':
      $data[$attr] = implode(',', $value);
      break;

    // Shape markers
    case 'chm':
      if (count($value)) {
        $markers = array();
        foreach ($value as $marker) {
          $markers[] = implode(',', $marker);
        }
        $data[$attr] = implode('|', $markers);
      }
      else {
        $data[$attr] = implode(',', $value);
      }
      break;

    // Bar chart bar sizing
    case 'chbh':
      $data[$attr] .= implode(',', array(
        $value['#size'],
        $value['#spacing'],
      ));
      break;

    // Mixed axis positions, labels and styles
    // @todo: refactor
    case 'chxt':
      $index = 0;
      $positions = array();
      $types = array();
      $regular = array();

      // Seperate regular labels and generate range labels
      if (count($value)) {
        foreach ($value as $axis => $indices) {
          if (count($indices)) {
            ksort($indices);
            foreach ($indices as $i => $labels) {
              if (count($labels)) {
                foreach ($labels as $j => $label) {

                  // Regular
                  if (isset($label['#label'])) {

                    // Array of labels
                    if (is_array($label['#label'])) {
                      foreach ($label['#label'] as $l) {
                        $regular[$axis][$i][] = is_array($l) ? $l : array(
                          '#label' => $l,
                          '#position' => NULL,
                        );
                      }
                    }
                    else {
                      $regular[$axis][$i][] = $label;
                    }
                  }
                  elseif (isset($label['#start']) && isset($label['#end'])) {
                    $data['chxr'] .= $index . ',' . $label['#start'] . ',' . $label['#end'] . '|';
                    $types[] = $axis;
                    $index++;
                  }
                }
              }
            }
          }
        }
      }

      // Generate regular labels
      if (count($regular)) {
        foreach ($regular as $axis => $indices) {
          if (count($indices)) {
            foreach ($indices as $i => $labels) {
              $types[] = $axis;
              $data['chxl'] .= $index . ':';
              if (count($labels)) {
                foreach ($labels as $j => $label) {
                  $data['chxl'] .= '|' . $label['#label'];
                  $positions[$index]['data'][] = isset($label['#position']) ? $label['#position'] : 0;
                  if ($label['#position']) {
                    $positions[$index]['set'] = TRUE;
                  }
                }
              }
              $data['chxl'] .= '|';
              $index++;
            }
          }
        }
      }

      // Generate positions
      if (count($positions)) {
        foreach ($positions as $i => $position) {
          if ($position['set'] == TRUE) {
            $data['chxp'] .= $i . ',' . implode(',', $position['data']) . '|';
          }
        }
      }
      $data['chxt'] = implode(',', $types);
      $data['chxl'] = rtrim($data['chxl'], '|');
      $data['chxr'] = rtrim($data['chxr'], '|');
      $data['chxp'] = rtrim($data['chxp'], '|');
      break;

    // Mixed axis label styles
    case 'chxs':
      if (count($value)) {
        $styles = array();
        foreach ($value as $i => $style) {
          $styles[] = implode(',', $style);
        }
        $data[$attr] = implode('|', $styles);
      }
      break;
  }
}