You are here

public function FrxSVGGraph::generateGroupGraphData in Forena Reports 7.5

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

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

File

src/Renderer/FrxSVGGraph.php, line 87
FrxSVGGraph php SVG Graph generator

Class

FrxSVGGraph

Namespace

Drupal\forena\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->frxReport
    ->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->frxReport
      ->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) {
    Frx::Data()
      ->push($row, '_group');
    $trow = array();

    // Base group
    $gkey = $this->frxReport
      ->replace($group, TRUE);
    if ($this->wrap_label) {
      $gkey = wordwrap($gkey, $this->wrap_label);
    }
    $trow['key'] = $gkey;
    Frx::Data()
      ->pop();

    // Dimensions
    $dim_data = $dim_rows[$k];
    foreach ($dim_values as $dk) {
      $dim_row = isset($dim_data[$dk]) ? $dim_data[$dk] : array();
      frx::Data()
        ->push($dim_row, '_dim');
      foreach ($dim_columns as $col) {
        $val = $this->frxReport
          ->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->frxReport
            ->replace($src, TRUE);
          if (isset($this->field_sources['legend_entries'])) {
            $legend_str = $this->frxReport
              ->replace($this->field_sources['legend_entries']);
            $legend[$legend_str] = $legend_str;
          }
        }
      }
      frx::Data()
        ->pop();
    }
    $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;
}