You are here

function _chart_append in Google Chart Tools: Image Charts 7

Same name and namespace in other branches
  1. 5 chart.module \_chart_append()
  2. 6 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 data.

File

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

Code

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

  // Size and fill contain defaults, all other attributes must be set
  if (!$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'];
        if (!isset($data['chts'])) {
          $data['chts'] = '';
        }
        $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;

    // Legend's position
    case 'chdlp':
      $data[$attr] = $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':
      if (!isset($data[$attr])) {
        $data[$attr] = '';
      }

      //If not array, use as is.
      if (!is_array($value)) {
        $data[$attr] .= $value;
      }
      else {
        $data[$attr] .= implode(',', array(
          $value['#size'],
          $value['#spacing'],
          $value['#group_spacing'],
        ));
      }
      break;

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

      // Separate 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'])) {
                    if (!isset($data['chxr'])) {
                      $data['chxr'] = '';
                    }
                    $data['chxr'] .= $index . ',' . $label['#start'] . ',' . $label['#end'] . '|';
                    $types[] = $axis;
                    $index++;
                  }
                }
              }
            }
          }
        }
      }

      // Generate regular labels
      if (count($regular)) {
        foreach ($regular as $axis => $indices) {
          if (count($indices)) {
            if (!isset($data['chxl'])) {
              $data['chxl'] = '';
            }
            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 (isset($position['set']) && $position['set'] == TRUE) {
            if (!isset($data['chxp'])) {
              $data['chxp'] = '';
            }
            $data['chxp'] .= $i . ',' . implode(',', $position['data']) . '|';
          }
        }
      }
      $data['chxt'] = implode(',', $types);
      $data['chxl'] = isset($data['chxl']) ? rtrim($data['chxl'], '|') : '';
      $data['chxr'] = isset($data['chxr']) ? rtrim($data['chxr'], '|') : '';
      $data['chxp'] = isset($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;

    // Geographical Scope
    case 'chtm':
      $data[$attr] = $value;
      break;

    // Country List
    case 'chld':
      $data[$attr] = implode('', $value);
      break;

    // Margins
    case 'chma':
      $chma = array(
        $value['#left_margin'],
        $value['#right_margin'],
        $value['#top_margin'],
        $value['#bottom_margin'],
      );
      $data[$attr] = implode(',', $chma);

      //Optional legend values
      if (isset($value['#legend_width']) && isset($value['#legend_height'])) {
        $data[$attr] .= '|' . $value['#legend_width'] . "," . $value['#legend_height'];
      }
      break;
  }
}