You are here

public function FrxSVGGraph::generateGroupGraphData in Forena Reports 8

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

Re-architect the data into something that the graphing engine can work with

1 call to FrxSVGGraph::generateGroupGraphData()
FrxSVGGraph::prepareGraph in src/FrxPlugin/Renderer/FrxSVGGraph.php

File

src/FrxPlugin/Renderer/FrxSVGGraph.php, line 97
FrxSVGGraph php SVG Graph generator

Class

FrxSVGGraph
SVG Graphing Renderer

Namespace

Drupal\forena\FrxPlugin\Renderer

Code

public function generateGroupGraphData(&$block_data, $group, $series, $key, $dim, $sum) {
  $dim_headers = array();
  $dim_rows = array();
  $dim_values = array();
  $rows = array();
  $legend = array();
  $counts = array();
  $data = $this->report
    ->group($block_data, $group, $sum);
  $this->graphOptions['structure'] = array(
    'key' => $group,
  );
  foreach ($data as $gk => $group_rows) {
    $row_copy = array_values($group_rows);
    $dims = $this->report
      ->group($group_rows, $dim);
    $rows[$gk] = $group_rows[0];
    foreach ($dims as $dk => $r) {
      $dims = array_values($r);
      $dim_values[$dk] = $dk;
      $dim_rows[$gk][$dk] = $r[0];
    }
  }

  // Default controling attributes
  $dim_headers = array(
    $key,
  );
  $dim_columns = $series;
  foreach ($dim_values as $dk) {
    foreach ($dim_columns as $col) {
      $structure_idx = trim($dk, '{}') . trim($col, '{}');
      $this->graphOptions['structure']['value'][] = $structure_idx;
      foreach ($this->field_sources as $k => $fld) {
        $structure_idx = $dk . $k;
        $this->graphOptions['structure'][$k][] = $structure_idx;
      }
    }
  }
  $this->graphData = array();
  $gkey = '';
  foreach ($rows as $k => $row) {
    $this
      ->pushData($row, '_group');
    $trow = array();

    // Base group
    $gkey = $this->report
      ->replace($group, TRUE);
    if ($this->wrap_label) {
      $gkey = wordwrap($gkey, $this->wrap_label);
    }
    $trow['key'] = $gkey;
    $this
      ->popData();

    // Dimensions
    $dim_data = $dim_rows[$k];
    foreach ($dim_values as $dk) {
      $dim_row = isset($dim_data[$dk]) ? $dim_data[$dk] : array();
      $this
        ->pushData($dim_row, '_dim');
      foreach ($dim_columns as $col) {
        $val = $this->report
          ->replace($col, TRUE);
        if ($val !== '' && $val !== NULL) {
          $trow[trim($dk, '{}') . trim($col, '{}')] = $val;
          @$counts[trim($dk, '{}') . trim($col, '{}')]++;
        }
        foreach ($this->field_sources as $fk => $src) {
          $trow[$dk . $fk] = $this->report
            ->replace($src, TRUE);
          if (isset($this->field_sources['legend_entries'])) {
            $legend_str = $this->report
              ->replace($this->field_sources['legend_entries']);
            $legend[$legend_str] = $legend_str;
          }
        }
      }
      $this
        ->popData();
    }
    $this->graphData[] = $trow;
    $this->counts = $counts;
  }

  // Deal with rare case where legend are supposed to come from data
  if (isset($this->field_sources['legend_entries'])) {
    $this->graphOptions['legend_entries'] = array_values($legend);
  }
  $this->graphOptions['structure']['key'] = 'key';
  return $this->graphData;
}