You are here

function ChartsGraphsAmcharts::get_chart in Charts and Graphs 7

Same name and namespace in other branches
  1. 6.2 apis/charts_graphs_amcharts/charts_graphs_amcharts.class.inc \ChartsGraphsAmcharts::get_chart()

Function that renders data.

Overrides ChartsGraphsCanvas::get_chart

File

apis/charts_graphs_amcharts/charts_graphs_amcharts.class.inc, line 197

Class

ChartsGraphsAmcharts
Implementation of abstract class ChartsGraphsFlashCanvas for amCharts library.

Code

function get_chart() {
  $unique = charts_graphs_random_hash();
  $am_column_type = NULL;
  $am_y_axe_type = NULL;
  $inner_radius = NULL;
  $angle_pie_3d = NULL;
  $height_pie_3d = NULL;
  switch ($this->type) {
    case 'bar':
      $am_category = 'amcolumn';
      $am_type = 'column';
      break;
    case 'stacked_bar':
      $am_category = 'amcolumn';
      $am_type = 'column';
      $am_column_type = 'stacked';
      break;
    case '100_stacked_bar':
      $am_category = 'amcolumn';
      $am_type = 'column';
      $am_column_type = '100% stacked';
      break;
    case 'bar_3d':
      $am_category = 'amcolumn';
      $am_type = 'column';
      $am_column_type = '3d column';
      break;
    case 'side_bar':
      $am_category = 'amcolumn';
      $am_type = 'bar';
      break;
    case 'stacked_side_bar':
      $am_category = 'amcolumn';
      $am_type = 'bar';
      $am_column_type = 'stacked';
      break;
    case '100_stacked_side_bar':
      $am_category = 'amcolumn';
      $am_type = 'bar';
      $am_column_type = '100% stacked';
      break;
    case 'side_bar_3d':
      $am_category = 'amcolumn';
      $am_type = 'bar';
      $am_column_type = '3d column';
      break;
    case 'line':
      $am_category = 'amline';
      $am_type = 'line';
      break;
    case 'area':
      $am_category = 'amline';
      $am_type = 'line';
      break;
    case 'stacked_area':
      $am_category = 'amline';
      $am_type = 'line';
      $am_y_axe_type = 'stacked';
      break;
    case '100_stacked_area':
      $am_category = 'amline';
      $am_type = 'line';
      $am_y_axe_type = '100% stacked';
      break;
    case 'pie':
      $am_category = 'ampie';
      $am_type = 'pie';
      break;
    case 'donut':
      $am_category = 'ampie';
      $am_type = 'pie';
      $inner_radius = '30%';
      break;
    case 'pie_3d':
      $am_category = 'ampie';
      $am_type = 'pie';
      $angle_pie_3d = 30;
      $height_pie_3d = 20;
      break;
    case 'donut_3d':
      $am_category = 'ampie';
      $am_type = 'pie';
      $inner_radius = '30%';
      $angle_pie_3d = 30;
      $height_pie_3d = 20;
      break;
  }
  $width = $this->width;
  $height = $this->height;
  $data = $this
    ->_preprocess_values();
  $settings = array(
    array(
      '#id' => 'type',
      '#value' => $am_type,
    ),
    array(
      '#id' => 'depth',
      '#value' => 5,
    ),
    array(
      '#id' => 'js_enabled',
      '#value' => 'false',
    ),
    array(
      '#id' => 'redraw',
      '#value' => 'true',
    ),
    array(
      '#id' => 'data_labels',
      '#children' => array(
        array(
          '#id' => 'show',
          '#cdata' => '{title}: {value}',
        ),
        array(
          '#id' => 'line_color',
          '#value' => '#FFFFFF',
        ),
        array(
          '#id' => 'line_alpha',
          '#value' => 40,
        ),
      ),
    ),
  );

  /**
   * Applying background colour setting if available.
   */
  if (isset($this->colour) && !empty($this->colour)) {
    $settings[] = array(
      '#id' => 'background',
      '#children' => array(
        array(
          '#id' => 'color',
          '#value' => $this
            ->encode_for_xml($this->colour),
        ),
        array(
          '#id' => 'alpha',
          '#value' => 100,
        ),
      ),
    );
  }

  /**
   * Applying user defined min and max y axis values.
   */
  if (($this->type == 'bar' || $this->type == 'line') && (isset($this->y_min) || isset($this->y_max))) {
    $children = array(
      array(
        '#id' => 'strict_min_max',
        '#value' => 'true',
      ),
    );
    if (isset($this->y_min)) {
      $children[] = array(
        '#id' => 'min',
        '#value' => $this->y_min,
      );
    }
    if (isset($this->y_max)) {
      $children[] = array(
        '#id' => 'max',
        '#value' => $this->y_max,
      );
    }
    $y_axis_limits_container = $this->type == 'bar' ? 'value' : 'y_left';
    $settings[] = array(
      '#id' => 'values',
      '#children' => array(
        array(
          '#id' => $y_axis_limits_container,
          '#children' => $children,
        ),
      ),
    );
  }
  if (isset($this->y_legend)) {
    $label = array(
      array(
        '#id' => 'x',
        '#value' => '2%',
      ),
      array(
        '#id' => 'y',
        '#value' => '100%',
      ),
      array(
        '#id' => 'align',
        '#value' => 'center',
      ),
      array(
        '#id' => 'rotate',
        '#value' => 'true',
      ),
      array(
        '#id' => 'text',
        '#value' => strip_tags($this->y_legend),
      ),
    );
    $settings[] = array(
      '#id' => 'labels',
      '#children' => $label,
    );
  }
  if (isset($am_column_type)) {
    $column = array(
      '#id' => 'column',
      '#children' => array(
        '#id' => 'type',
        '#value' => $am_column_type,
      ),
    );
    $settings[] = $column;
  }
  if (isset($am_y_axe_type)) {
    $y_axe_type = array(
      '#id' => 'axes',
      '#value' => array(
        '#id' => 'y_left',
        '#value' => array(
          '#id' => 'type',
          '#value' => $am_y_axe_type,
        ),
      ),
    );
    $settings[] = $y_axe_type;
  }
  if ($am_type == 'pie') {
    $pie = array(
      array(
        '#id' => 'hover_brightness',
        '#value' => -20,
      ),
      array(
        '#id' => 'gradient',
        '#value' => 'linear',
      ),
      array(
        '#id' => 'gradient_ratio',
        '#value' => '-10,60',
      ),
    );
    if ($inner_radius !== NULL) {
      $pie[] = array(
        '#id' => 'inner_radius',
        '#value' => $inner_radius,
      );
    }
    if ($angle_pie_3d !== NULL) {
      $pie[] = array(
        '#id' => 'angle',
        '#value' => $angle_pie_3d,
      );
    }
    if ($height_pie_3d !== NULL) {
      $pie[] = array(
        '#id' => 'height',
        '#value' => $height_pie_3d,
      );
    }
    $colors = array_slice($this
      ->series_colours(), 0, count(reset($this->series)));
    $pie[] = array(
      '#id' => 'colors',
      '#value' => implode(',', $colors),
    );
    $pie = array(
      '#id' => 'pie',
      '#children' => $pie,
    );
    $settings[] = $pie;
  }
  else {
    $colors = array_slice($this
      ->series_colours(), 0, count($this->series));
    $settings[] = array(
      '#id' => 'colors',
      '#value' => implode(',', $colors),
    );
  }
  $settings = $this
    ->merge_xml_values($settings, $this->settings);
  $settings = array(
    '#id' => 'settings',
    '#children' => $settings,
  );
  $this->settings_to_use = array(
    $settings,
  );
  $wmode = $this
    ->get_wmode();
  $mod_path = drupal_get_path('module', $this
    ->getModuleName());

  // TODO The second parameter to this function call should be an array.
  $file = url("{$mod_path}/downloaded/{$am_category}.swf", array(
    'absolute' => TRUE,
  ));

  //-- Prepare URLs that javascript will retrieve data from, using cache:
  $unique = charts_graphs_random_hash();
  $arr = (array) $this;
  $tocache = new stdClass();
  $tocache->settings = $this
    ->get_xml_file_from_array($this->settings_to_use);
  $tocache->data = $this
    ->get_xml_file_from_array($this->data_to_use);
  cache_set($unique, $tocache, 'cache', REQUEST_TIME + 30);

  //Keep for at least 30 seconds;
  $settings_url_query = sprintf('cid=%s', $unique);

  // TODO The second parameter to this function call should be an array.
  $settings_url = url('charts_graphs_amcharts/getdata/settings', array(
    'absolute' => TRUE,
    'query' => $settings_url_query,
  ));
  $data_url_query = sprintf('cid=%s', $unique);

  // TODO The second parameter to this function call should be an array.
  $data_url = url('charts_graphs_amcharts/getdata/data', array(
    'absolute' => TRUE,
    'query' => $data_url_query,
  ));
  $flashvars = array(
    'settings_file' => 'SWFSETTINGSURL',
    'data_file' => 'SWFDATAURL',
    'preloader_color' => '#999999',
    'wmode' => $wmode,
  );
  if (isset($this->key) && !empty($this->key)) {
    $flashvars['key'] = $this->key;
  }
  $args = array(
    'params' => array(
      'width' => $width,
      'height' => $height,
      'wmode' => $wmode,
    ),
    'flashvars' => $flashvars,
  );
  $out = swf($file, $args);
  $out = str_replace('SWFSETTINGSURL', $settings_url, $out);
  $out = str_replace('SWFDATAURL', $data_url, $out);
  $element = array(
    '#markup' => $out,
  );
  return $element;
}