You are here

public function FrxSVGGraph::prepareGraph in Forena Reports 8

Same name in this branch
  1. 8 src/FrxPlugin/Template/FrxSVGGraph.php \Drupal\forena\Template\FrxSVGGraph::prepareGraph()
  2. 8 src/FrxPlugin/Renderer/FrxSVGGraph.php \Drupal\forena\FrxPlugin\Renderer\FrxSVGGraph::prepareGraph()

File

src/FrxPlugin/Template/FrxSVGGraph.php, line 235
FrxSVGGraph php SVG Graph generator

Class

FrxSVGGraph

Namespace

Drupal\forena\Template

Code

public function prepareGraph() {
  $skin_options = $this
    ->getDataContext('skin');
  $options = isset($skin_options['FrxSVGGraph']) ? $skin_options['FrxSVGGraph'] : array();
  $attributes = $this
    ->mergedAttributes();

  // Default in xpath for backward compatibility
  // Legacy options.  New charts should be generated using FrxAPI:attribute syntax
  if (isset($attributes['options'])) {
    $attr_options = array();
    parse_str($attributes['options'], $attr_options);
    unset($attributes['options']);
    foreach ($attr_options as $key => $value) {
      $options[$key] = $this->report
        ->replace($value, TRUE);
    }
  }
  else {
    $options = array_merge($options, $attributes);
  }

  // Main Graphing options
  $path = $this
    ->extract('path', $options);
  if (!$path) {
    $path = $this
      ->extract('xpath', $options);
  }

  //Deprecated
  if (!$path) {
    $path = '*';
  }
  $group = $this
    ->extract('group', $options);
  $series = @(array) $attributes['series'];
  $sums = @(array) $attributes['sum'];
  $key = @$attributes['key'];
  if (!$key) {
    $key = @$attributes['label'];
  }

  //Backward compatibility for old graphs.
  $options['key'] = $key;
  $dim = $this
    ->extract('dim', $options);
  $this->wrap_label = (int) $this
    ->extract('wrap_label', $options);
  if (!$key) {
    $key = @$options['seriesx'];
  }

  // Deprecated
  // Determine basic data to iterate.
  $data = $this
    ->currentDataContext();
  if (is_object($data)) {
    if (method_exists($data, 'xpath')) {
      $nodes = $data
        ->xpath($path);
    }
    else {
      $nodes = $data;
    }
  }
  else {
    $nodes = (array) $data;
  }

  // Force structured data
  $options['structured_data'] = TRUE;
  $options['structure'] = array(
    'key' => 'key',
  );

  // Default in american colour;
  $this->field_sources = array();
  if (isset($options['color'])) {
    $options['colour'] = $options['color'];
  }

  // Find out data that is designed to be sepecif to series.
  $this->field_sources = array();
  foreach ($options as $fk => $opt) {
    if ($fk != 'value' && $fk != 'key' && $opt && !is_array($opt) && strpos($options[$fk], '{') !== FALSE) {
      $this->field_sources[$fk] = $opt;
      $options['structure'][$fk] = $fk;
    }
  }
  if (isset($attributes['height'])) {
    $options['height'] = $this->report
      ->replace($attributes['height'], TRUE);
  }
  if (isset($attributes['width'])) {
    $options['width'] = $this->report
      ->replace($attributes['width'], TRUE);
  }
  if (isset($options['legend_entries']) && !isset($options['label']) && !isset($options['show_labels'])) {
    $options['show_labels'] = FALSE;
  }
  $this->graphOptions = $options;
  if ($group) {
    $this
      ->generateGroupGraphData($nodes, $group, $series, $key, $dim, $sums);
  }
  else {
    $this
      ->generateGraphData($nodes, $series, $key);
  }
  if (isset($this->graphOptions['legend_entries']) && !is_array($this->graphOptions['legend_entries'])) {
    $this->graphOptions['legend_entries'] = explode('|', $this->graphOptions['legend_entries']);
  }
}