You are here

function ChartsAmcharts::get_chart in Charts and Graphs 6

Function that renders data.

Overrides ChartCanvas::get_chart

File

apis/amcharts/amcharts.class.inc, line 100
Implementation of abstract class ChartCanvas for amCharts library.

Class

ChartsAmcharts
Implementation of abstract class ChartCanvas for amCharts library.

Code

function get_chart() {
  $unique = chart_graphs_random_hash();
  $am_column_type = NULL;
  $am_y_axe_type = 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 = 'donut';
      break;
  }
  $color = empty($this->colour) ? '#ffffff' : $this->colour;
  $width = $this->width;
  $height = $this->height;
  $data = $this
    ->_preprocess_values();
  if (isset($this->y_legend)) {
    $y_legend = sprintf('<labels>
      <label>
        <x>2%%</x>
        <y>100%%</y>
        <align>center</align>
        <rotate>true</rotate>
        <text>
          %s
        </text>
      </label>
    </labels>', strip_tags($this->y_legend));
  }
  else {
    $y_legend = '';
  }
  if (isset($am_column_type)) {
    $column = sprintf('<column>
          <type>%s</type>
        </column>', $am_column_type);
  }
  else {
    $column = '';
  }
  if (isset($am_y_axe_type)) {
    $y_axe_type = sprintf("<axes>\n          <y_left>\n            <type>%s</type>\n          </y_left>\n        </axes>", $am_y_axe_type);
  }
  else {
    $y_axe_type = '';
  }
  $settings = "<settings>\n    <type>{$am_type}</type>\n    <depth>5</depth>\n    <background><color>{$color}</color>\n    <alpha>80</alpha></background>\n    <js_enabled>false</js_enabled>  \n    <redraw>true</redraw> \n    <data_labels>\n      <show><![CDATA[{title}: {value}%]]></show>\n      <line_color>#FFFFFF</line_color>    \n      <line_alpha>40</line_alpha>\n    </data_labels>\n    {$y_legend}\n    {$column}\n    {$y_axe_type}\n    <pie>\n      <inner_radius>20%</inner_radius>\n      <height>20</height>\n      <angle>30</angle>\n      <hover_brightness>-20</hover_brightness>\n      <gradient>linear</gradient>\n      <gradient_ratio>-10,60</gradient_ratio>\n    </pie>\n  </settings>";
  $settings = str_replace("\n", '', $settings);
  $settings = preg_replace('/>\\s+?</im', '><', $settings);
  $wmode = $this
    ->get_wmode();
  $mod_path = drupal_get_path('module', $this
    ->getModuleName());
  $file = url("{$mod_path}/downloaded/{$am_category}.swf", array(
    'absolute' => TRUE,
  ));

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

  //Keep for at least 30 seconds;
  $settings_url_query = sprintf('cid=%s', $unique);
  $settings_url = url('amcharts/getdata/settings', array(
    'absolute' => TRUE,
    'query' => $settings_url_query,
  ));
  $data_url_query = sprintf('cid=%s', $unique);
  $data_url = url('amcharts/getdata/data', array(
    'absolute' => TRUE,
    'query' => $data_url_query,
  ));
  $flashvars = array(
    //'chart_settings' => $settings,

    //'chart_data' => $data,
    'settings_file' => 'SWFSETTINGSURL',
    'data_file' => 'SWFDATAURL',
    'preloader_color' => '#999999',
    'wmode' => $wmode,
  );
  $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);
  return $out;
}