FrxSVGGraph.inc 21.3 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * @file FrxSVGGraph
 * php SVG Graph generator
 *
 * @author davidmetzler
 *
 */
9
10
include_once('FrxCrosstab.inc');
include_once('FrxTable.inc');
11
class FrxSVGGraph extends FrxRenderer {
12
13
  private $graph;
  private $links;
14
  public $templateName = 'Graph (svg)';
15
  public $xy_data = FALSE;
16
  private $point_attributes;
17
18
  private $headers = array();
  private $weight;
19
20
21
  private $graphData;
  private $graphOptions;
  private $colors;
22
23
24
25
26
27
28
29
  private $unset_attrs = array(
    'base_type',
    'crosstab_columns',
    'content',
    'sections',
    'columns',
    'style',
  );
30
31
32

  // Place to indicate which fields are sourced from the data.
  private $field_sources = array();
33

34
  public function __construct() {
35
    parent::__construct();
36
    $library = forena_library_file('SVGGraph');
metzlerd's avatar
metzlerd committed
37
    require_once $library;
38
  }
39

40
41
42
  /**
   * Re-architect the data into something that the graphing engine can work with
   */
43
  public function generateGraphData(&$data, $series, $key) {
44
45

    // Default controling attributes
46
    $dim_headers = array($key);
47
48

    $this->graphOptions['structure']['value'] = array();
David Metzler's avatar
David Metzler committed
49
50
    foreach($series as $col) {
      $this->graphOptions['structure']['value'][] = trim("$col", '{}');
51
52
    }

David Metzler's avatar
David Metzler committed
53

54
55
56
57
58
    $this->graphData = array();
    foreach ($data as $row) {
      Frx::Data()->push($row, '_row');
      $trow = array();
      // Base group
59
      $trow['key'] =  $this->teng->replace($key, TRUE);;
60
61
62
      // Dimensions
      foreach($series as $col) {
          $val = $this->teng->replace($col, TRUE);
63
          if ($val != '' && $val !==NULL) $trow[trim("$col", '{}')] = $val;
64
      }
65
      foreach($this->field_sources as $k => $src) {
66
        $trow[$k] = $this->teng->replace($src, TRUE);
67
68
69
70
71
72
      }
      Frx::Data()->pop();
      $this->graphData[] = $trow;
    }
  }

73
74
75
76

   /**
   * Re-architect the data into something that the graphing engine can work with
   */
77
  public function generateGroupGraphData(&$block_data, $group, $series, $key, $dim) {
78
79
80
81
82
83
84
    $dim_headers = array();
    $dim_rows = array();
    $dim_columns = array();
    $group_headers = array();
    $dim_values = array();
    $rows = array();
    $data = $this->frxReport->group($block_data, $group);
85
    $this->graphOptions['structure'] = array('key' => $group);
86
87
88
89
90
91
92
93
94
95
96
97
98

    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
99
    $dim_headers = array($key);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    $dim_columns = $series;

    // Add the dimension headers.
    foreach ($dim_values as $dk) {
      foreach ($dim_headers as $i => $col) {
        $cell = '';
        if (count($dim_columns) > 1) {
          $cell = $i ? $col : $dk . ' ' . $col;
        }
        $hrow []  = $cell;
      }
    }

    foreach($dim_values as $dk) {
      foreach($dim_columns as $col) {
115
        $structure_idx = $dk . $col; 
116
117
        $this->graphOptions['structure']['value'][] = $structure_idx;
        foreach($this->field_sources as $k=>$fld) {
118
          $structure_idx = $dk . $k; 
119
120
          $this->graphOptions['structure'][$k][] = $structure_idx;
        }
121
122
123
124
      }
    }

    $this->graphData = array();
125
    $gkey = '';
126
127
128
129
130
    foreach ($rows as $k=>$row) {
      Frx::Data()->push($row, '_group');
      $trow = array();
      // Base group

131
      $gkey = $this->teng->replace($group, TRUE);
132
      $trow[$group] = $gkey;
133
      Frx::Data()->pop();
134
135
136
137
138
139
      // 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) {
140

141
          $cell = $col;
142
          $val = $this->teng->replace($col, TRUE);
143
144
          //if(!$val) $val = 0;
          if ($val !== '' && $val !== NULL) $trow[$dk . $col] = $val;
145
          foreach($this->field_sources as $fk => $src) {
146
            $trow[$dk . $fk] = $this->teng->replace($src, TRUE);
147
          }
148
149
150
151
152
153
        }
        frx::Data()->pop();
      }
      $this->graphData[] = $trow;

    }
154

155
156
157
158
159
160
    return $this->graphData;
  }

  /**
   * Add column for cross tabs.
   */
161
162
163
164
165
166
167
168
169
170
  private function addColumn($type, $token, $label,  &$config) {
    $key = trim($token, '{}');
    $this->weight++;
    $config['crosstab_columns'][$key] = array(
        'contents' => $token,
        'label' => $label,
        'type' => $type,
        'weight' => $this->weight,
    );
  }
171
172
173
174

  /**
   * Derive config variables from graph.
   */
175
  public function scrapeConfig() {
176
177
178
179
180
181
    $this->weight = 0;
    $attrs = array();
    $svg = '';
    $nodes =  $this->reportDocNode->xpath('//svg');
    if ($nodes) {
      $svg = $nodes[0];
182
183
      $config = $this->mergedAttributes($svg);
    }
184
185

    // Determine graph type
186
    $graph_type =  isset($config['type']) ? $config['type'] : 'BarGraph';
187

188
189
    $types = $this->graphTypes();
    $types = array_change_key_case($types);
190
    $config['base_type'] = $types[strtolower($graph_type)]['type'];
191

192
    $this->extractTemplateHTML($this->reportDocDomNode, $config, array('svg', 'table'));
193
194
195
196
197
198
199
200
201
202
203
    if (!isset($config['key'])) $config['key'] =  @$config['label'];
    if (!isset($config['key'])) $config['key'] = @$config['seriesx'];
    $key = $config['key'];
    $dim = @$config['dim'];
    $series = @(array)$config['series'];

    if ($key) {
      $keys = explode(' ', $key);
      foreach ($keys as $k) {
        $this->addColumn('heading', $k, trim($k, '{}'), $config);
      }
204
    }
205
206
207
208
    if ($series) foreach ($series as $col) {
      $this->addColumn('value',$col , trim($col, '{}'), $config);
    }

209
210
211
212
213
214
215
    // Get the data cells
    if ($dim) {
      $dims = (array)$dim;
      foreach($dims as $dim) {
        $this->addColumn('crosstab', $dim, trim($dim, '{}'), $config);
      }
    }
216
    $config['style'] = $config['type'];
217
218
219
    return $config;
  }

220
221
222
223
  /**
   * Render the graph.
   * @return Ambigous <string, mixed>
   */
224
225
226
227
228
  public function render() {
    // Get data from source
    $output = '';
    $options = array();
    $series = array();
229
230
231
    $label = '';
    $links = '';
    $dim='';
232
233
    $seriesx = array();
    $attributes = $this->mergedAttributes();
234
    // Default in xpath for backward compatibility
235
236
237
238
    // Legacy options.  New charts should be generated using Frx:attribute syntax
    if (isset($attributes['options'])) {
      parse_str($attributes['options'], $options);
      unset($attributes['options']);
239
240
241
      foreach ($options as $key => $value) {
        $options[$key] = $this->teng->replace($value, TRUE);
      }
242
243
244
245
246
247
      $options  = array_merge($attributes, $options);
    }
    else {
      $options = $attributes;
    }

248
249
250
251
252
    // Main Graphing options
    $path = $this->extract('path', $options);
    if (!$path) $path = $this->extract('xpath', $options); //Deprecated
    if (!$path) $path = '*';
    $group = $this->extract('group', $options);
253
    $series = @(array)$attributes['series'];
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    $label = @$attributes['label'];
    $key = @$attributes['key'];
    if (!$key) $key = @$attributes['label']; //Backward compatibility for old graphs.
    $options['key'] = $key;
    $dim = $this->extract('dim', $options);
    $type = $this->extract('type', $options);
    if (!$key) $key = @$options['seriesx']; // Deprecated

    // Determine basic data to iterate.
    $data = Frx::Data()->currentContext();
    if (is_object($data)) {
      if (method_exists($data, 'xpath')) {
        $nodes = $data->xpath($path);
      }
      else {
        $nodes = $data;
      }
271
    }
272
273
    else {
      $nodes = (array)$data;
274
275
    }

276
277
    // Force structured data
    $options['structured_data'] = TRUE;
278
279
280
    $options['structure'] = array('key' => 'key');

    // Default in american colour;
281
282
    $this->field_sources = array();
    if (isset($options['color'])) $options['colour'] = $options['color'];
283
284

    // Find out data that is designed to be sepecif to series.
285
    $this->field_sources = array();
286
    $i= 0;
287
288
289
    foreach ($options as $fk => $opt) {
      if ($fk != 'value' && $fk != 'key' && $opt && !is_array($opt) && strpos($options[$fk], '{')!==FALSE) {
        $this->field_sources[$fk] = $opt;
290
        $options['structure'][$fk] = $fk;
291
292
      }
    }
293
294
    if (isset($attributes['height'])) $options['height'] = $this->teng->replace($attributes['height'], TRUE);
    if (isset($attributes['width'])) $options['width'] = $this->teng->replace($attributes['width'], TRUE);
295
296
297
298
    if (isset($options['legend_entries']) && !isset($options['label']) && !isset($options['show_labels'])) {
      $options['show_labels'] = FALSE;
    }

299

300
301
    $this->graphOptions = $options;

302
    if ($group && $dim) {
303
      $this->generateGroupGraphData($nodes, $group, $series, $key, $dim);
304
305
    }
    else {
306
      $this->generateGraphData($nodes, $series, $key);
307
    }
308

309

310
    // Deal with rare case where legend are supposed to come from data
311
312
313
314
315
316
317
318
    if (isset($this->field_sources['legend_entries'])) {
      $legend = array();
      foreach($this->graphData as $row) {
        $legend[] = $row['legend_entries'];
      }
      $this->graphOptions['legend_entries'] = $legend;
    }
    if (isset($this->graphOptions['legend_entries']) && !is_array($this->graphOptions['legend_entries'])) {
319
      $this->graphOptions['legend_entries'] = explode('|', $this->graphOptions['legend_entries']);
320
    }
321
    if ($data) {
322
      $output = $this->renderChart($type, $data);
323
324
325
326
    }
    return $output;
  }

metzlerd's avatar
metzlerd committed
327
328
  static function graphTypes() {
    return array(
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
      'BarGraph' => array('type' => 'Bar Graph', 'style' => 'Simple' ),
      'Bar3DGraph' => array('type' => 'Bar Graph', 'style' => '3D' ),
      'StackedBarGraph' => array('type' => 'Bar Graph', 'style' => 'Stacked'),
      'GroupedBarGraph' => array('type' => 'Bar Graph', 'style' => 'Grouped'),
      'CylinderGraph' => array('type' => 'Bar Graph', 'style' => 'Cylinder'),
      'StackedCylinderGraph' => array('type' => 'Bar Graph', 'style' => 'Stacked Cylinder'),
      'GroupedCylinderGraph' =>  array('type' => 'Bar Graph', 'style' => 'Grouped Cylinder'),
      'PieGraph' => array('type' => 'Pie Chart', 'style' => 'Simple'),
      'Pie3DGraph' => array('type' => 'Pie Chart', 'style' => '3D'),
      'HorizontalBarGraph' => array('type' => 'Bar Graph', 'style' => 'Horizontal'),
      'LineGraph' =>array('type' => 'Line Graph', 'style' => 'Simple'),
      'MultiLineGraph' => array('type' => 'Line Graph', 'style' => 'Multi'),
      'ScatterGraph' => array('type' => 'Scatter Plot', 'style' => 'Simple', 'xaxis' => TRUE),
      'MultiScatterGraph' => array('type' => 'Scatter Plot', 'style' => '3D', 'xaxis' => TRUE),
      'RadarGraph' => array('type' => 'Radar Graph', 'style' => 'Simple'),
      'MultiRadarGraph' => array('type' => 'Radar Graph', 'style' => 'Multi'),
metzlerd's avatar
metzlerd committed
345
346
347
348
349
350
351
352
353
354
355
356
    );
  }

  static function graphOptions() {
    $data = FrxSVGGraph::graphTypes();
    foreach($data as $key => $value) {
      $type[$value['type']] = $value['type'];
      $style[$value['type']][$key] = $value['style'];
    }
    return array('types' => $type, 'styles' => $style);
  }

357
  function renderChart($type, $data ) {
358
359
    $options['width'] = @$options['width'] ? $options['width'] : 600;
    $options['height'] = @$options['height'] ? $options['height'] : 400;
metzlerd's avatar
metzlerd committed
360
361
362
363
364
    $type = strtolower($type);
    // Legacy sustitions for backcward compatibility.
    if ($type == 'piechart') $type = 'piegraph';
    if ($type == 'scatterplot') $type = 'scattergraph';
    if ($type == 'multiscatterplot') $type = 'multiscattergraph';
metzlerd's avatar
metzlerd committed
365

metzlerd's avatar
metzlerd committed
366
367
368
369
370
371
    // Newly defined types
    $graph_types = FrxSVGGraph::graphTypes();

    // Build map for array types.
    $lower_graphs_types = array_change_key_case($graph_types);
    $graph_classes = array_combine(array_keys($lower_graphs_types), array_keys($graph_types));
372

metzlerd's avatar
metzlerd committed
373

metzlerd's avatar
metzlerd committed
374
375
    if ($data && isset($graph_classes[$type])) {
      $class = $graph_classes[$type];
376
      $output = $this->renderGraph($class);
377
    }
metzlerd's avatar
metzlerd committed
378
    return $output;
379
380
381
  }


382
  function renderGraph($type) {
383
    static $jsinc = '';
384

385
386
387
    $options = $this->graphOptions;
    $data = $this->graphData;

388
    if (!isset($options['scatter_2d']) && ($type == 'ScatterGraph' || $type=='MultiScatterGraph') && $this->xy_data && !isset($options['scatter_2d'])) {
389
390
      $options['scatter_2d'] = TRUE;
    }
metzlerd's avatar
metzlerd committed
391
    else {
392
      $options['scatter_2d'] = (bool) @$options['scatter_2d'];
metzlerd's avatar
metzlerd committed
393
    }
394
395
396
397
398
    $width = (@$options['width']) ? @$options['width'] : 600;
    $height = (@$options['height']) ? @$options['height'] : 400;
    $graph = new SVGGraph($width, $height, $options);
    $this->graph = $graph;
    $graph->Values($data);
399
    if (isset($options['colour']) && is_array($options['colour'])) {
400
      $graph->Colours($options['colour']);
401
    }
402
    // Generate the graph
403
404
405
406
407
    $output = $graph->Fetch($type, FALSE);
    // Add a viewbox to be compatible with Prince PDF generation.
    $output = str_replace('<svg width', "<svg viewBox='0 0 $width $height' width", $output);
    $javascript = '/<script[^>]*?>.*?<\/script>/si';
    $noscript = '';
408
    if (!$jsinc && $this->format == 'web') {
409
      if (@!$options['no_js']) {
410
411
412
        $js =  $graph->FetchJavascript();
        $output .= $js;
      }
413
      $jsinc = TRUE;
414
415
416
417
    }
    return $output;
  }

418
419
420
421
422
423
424
425
426
427
428
429
430
431
  /**
   * Generate series from crosstab.
   * @param unknown $config
   */
  private function seriesFromColumns(&$config) {
    $series = array();
    foreach ($config['crosstab_columns'] as $key => $col) {
      if ($col['type'] == 'value') {
        $series[] = '{' . $key . '}';
      }
    }
    $config['series'] = $series;
  }

432
433
434
  private function keyFromColumns(&$config) {
    $key = '';
    foreach ($config['crosstab_columns'] as $k => $col) {
435
      if ($col['type'] == 'heading') {
436
        $key = trim($key . ' {' . $k . '}');
437
438
      }
    }
439
    $config['key'] = $key;
440
441
  }

442
  public function configForm($config) {
443
    $form = array();
David Metzler's avatar
David Metzler committed
444
445
    $graph_options = FrxSVGGraph::graphOptions();
    $graph_types = FrxSVGGraph::graphTypes();
446
    $gt = array_change_key_case($graph_types);
447

448
    $type = @$config['style'];
449
450
451
452
453
454
    if (!isset($config['base_type'])) {
      $base_type = $type ? $gt[strtolower($type)]['type'] : 'Bar Graph';
    }
    else {
      $base_type = $config['base_type'];
    }
455
    $styles = $graph_options['styles'][$base_type];
David Metzler's avatar
David Metzler committed
456
    $types = $graph_options['types'];
457
458
    $form = parent::configForm($config);

459
    $form['base_type'] = array(
David Metzler's avatar
David Metzler committed
460
461
462
        '#type' => 'select',
        '#title' => t('Graph Type'),
        '#options' => $graph_options['types'],
463
        '#default_value' => $base_type,
464
        '#ajax' => $this->configAjax(),
David Metzler's avatar
David Metzler committed
465
466
    );

467
    $form['style'] = array(
David Metzler's avatar
David Metzler committed
468
469
470
        '#type' => 'select',
        '#title' => t('Style'),
        '#options' => $styles,
471
        '#default_value' => $type,
472
        '#ajax' => $this->configAjax(),
David Metzler's avatar
David Metzler committed
473
474
    );

475
    $form['gen_table'] = array(
David Metzler's avatar
David Metzler committed
476
477
478
        '#type' => 'checkbox',
        '#title' => t('Include data table with graph'),
        '#default_value' => @$config['gen_table'],
479
        '#ajax' => $this->configAjax(),
David Metzler's avatar
David Metzler committed
480
481
482
    );


483
484
485
486
487
488
489
490
491
492
493
    $this->weight_sort($config['crosstab_columns']);
    $types = array('heading' => t('Label'), 'crosstab' => t('Crosstab'), 'value' => 'Value', 'ignore' => t('Ignore'));
    $form['crosstab_columns'] = array('#theme' => 'forena_element_draggable',   '#draggable_id' => 'FrxCrosstab-columns');
    foreach ($config['crosstab_columns'] as $key => $col) {
      $ctl = array();
      $ctl['label'] = array(
          '#type' => 'textfield',
          '#size' => 30,
          '#title' => t('Label'),
          '#default_value' => $col['label'],
      );
David Metzler's avatar
David Metzler committed
494

495
496
497
498
499
      $ctl['contents'] = array(
          '#type' => 'textfield',
          '#size' => '30',
          '#title' => t('Data'),
          '#default_value' => $col['contents'],
David Metzler's avatar
David Metzler committed
500
501
      );

502
503
504
505
506
507
508
      $ctl['type'] = array(
          '#type' => 'radios',
          '#title' => t('Type'),
          '#default_value' => $col['type'],
          '#options' => $types,
          '#ajax' => $this->configAjax()
      );
David Metzler's avatar
David Metzler committed
509

510
511
512
513
514
      $ctl['weight'] = array(
          "#type" => 'weight',
          '#title' => t('Weight'),
          '#delta' => 50,
          '#default_value' => $col['weight'],
David Metzler's avatar
David Metzler committed
515
516
      );

517
      $form['crosstab_columns'][$key] = $ctl;
David Metzler's avatar
David Metzler committed
518
519
    }

520
521
522
523
524
    $form['label'] = array(
    	'#type' => 'textfield',
      '#title' => t('Graph Label'),
      '#default_value' => @$config['label'],
    );
525
526

    $form['link'] = array(
David Metzler's avatar
David Metzler committed
527
        '#type' => 'textfield',
528
        '#title' => t('Link Url'),
David Metzler's avatar
David Metzler committed
529
530
531
        '#default_value' => @$config['link'],
    );

532
533
534
    $form['legend_entries'] = array(
        '#type' => 'textfield',
        '#title' => t('Legend'),
535
        '#default_value' => @$config['legend_entries'],
536
537
538
539
540
541
542
543
544
    );

    $form['tooltip'] = array(
        '#type' => 'textfield',
        '#title' => t('Tool Tip'),
        '#default_value' => @$config['tooltip'],
    );


545
    return $form;
David Metzler's avatar
David Metzler committed
546
547
  }

548
549
550
551
552
553
554
555
556
557
558
  public function configValidate(&$config) {
    $type = $config['style'];
    $base_type = $config['base_type'];
    $graph_options = $this->graphOptions();
    if(!array_key_exists($type, $graph_options['styles'][$base_type])) {
      $styles = array_keys($graph_options['styles'][$base_type]);
      $config['type'] = $config['style'] = array_shift($styles);
    }
    $config['type'] = $config['style'];
  }

559
  public function generate($xml, &$config) {
560
561
    $config['class'] = get_class($this);
    $media = 'FrxSVGGraph';
David Metzler's avatar
David Metzler committed
562
    $div = $this->blockDiv($config);
563
    $this->removeChildren($div);
564
    $attrs  = array();
565
    // PUt on the header
566
567
568
569
    if (isset($config['header']['value'])) {
      $header = $this->extract('header', $config);
      $this->addFragment($div, $header['value']);
    }
570

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    // Determine columns and make sure we represent them all
    $found_columns = $this->columns($xml);
    if (!$found_columns) {
      $found_columns = $this->columns($xml, '/*');
    }

    $numeric_columns = $this->numeric_columns;
    $new_columns = @$config['crosstab_columns'] ? FALSE : TRUE;
    foreach ($found_columns as $column => $label) {
      $token = '{' . $column . '}';
      if ($new_columns) {
        $type = isset($numeric_columns[$column]) ? 'value' : 'heading';
      } else {
        $type = 'ignore';
      }
      if (!isset($config['crosstab_columns'][$column])) {
        $this->addColumn($type, '{' . $column . '}', $column, $config);
      }
    }

    // Generate the grouping row
    $group = '';
    $dim = array();
594

595
    foreach($config['crosstab_columns'] as $col) {
596
      if ($col['type'] == 'heading') $group[] = $col['contents'];
597
598
      if ($col['type'] == 'crosstab') $dim = $col['contents'];
    }
599
    if ($group) $config['group'] = is_array($group) ?  implode(' ', $group) : $group;
600
    if ($dim) $config['dim'] = $dim;
601
602
603



604
605
606
607
608
    $this->seriesFromColumns($config);
    $this->keyFromColumns($config);

    // Clean colors
    if (isset($config['colors'])) foreach ($config['colors'] as $i => $color) if (!$color) unset($color[$i]);
609

610
611
612
613
614
615
    $type = $this->extract('type', $config);
    if (!$type) $type = 'Bar Graph';

    $gen_table = $this->extract('gen_table', $config);
    $graph_options = FrxSVGGraph::graphOptions();
    // LImit the config
616
617
618
619
    $frxattrs = $config;
    // Unset common option configurations?
    foreach($this->unset_attrs as $k) unset($frxattrs[$k]);

620
    $frxattrs = $this->arrayAttributes($frxattrs);
621
622


623
624
    $frxattrs['renderer'] = 'FrxSVGGraph';
    $frxattrs['type'] = $type;
David Metzler's avatar
David Metzler committed
625

626
627
628
629
630
631
632
633
634
635
636
    $svg = $this->setFirstNode($div, 2, 'svg',  NULL, NULL,  $frxattrs);

    if (isset($config['footer']['value'])) $this->addFragment($div, $config['footer']['value']);
    if ($gen_table) {
      if($group && $dim) {
        $this->generateCrossTab($xml, $config, $div, $group, $dim);
      }
      else {
        $this->generateTable($xml, $config, $div);
      }
    }
metzlerd's avatar
metzlerd committed
637

638
  }
David Metzler's avatar
David Metzler committed
639

640
641
642
643
644
645
646
647
648
649
650
651
  /**
   * Generate a basic table.
   * @param unknown $xml
   * @param unknown $config
   * @param unknown $div
   */
  function generateTable($xml, &$config, &$div) {
    $attrs = array('foreach' => '*');
    $table = $this->setFirstNode($div, 4, 'table');
    if (@$config['caption']) {
      $this->setFirstNode($table, 6, 'caption', $config['caption']);
    }
652

653
654
655
656
    $thead = $this->setFirstNode($table, 6, 'thead');
    $throw = $this->setFirstNode($thead, 8, 'tr');
    $tbody = $this->setFirstNode($table, 6, 'tbody');
    $tdrow = $this->setFirstNode($tbody, 8, 'tr', NULL, array(),$attrs);
David Metzler's avatar
David Metzler committed
657

658
659
660
    if (isset($config['crosstab_columns'])) foreach ($config['crosstab_columns'] as $key => $col) if ($col['type']!='ignore') {
      $this->addNode($throw, 10, 'th', $col['label']);
      $this->addNode($tdrow, 10, 'td', $col['contents']);
David Metzler's avatar
David Metzler committed
661
    }
662
  }
663

664
665
666
667
668
669
670
671
672
  /**
   * Generate a crosstab table.
   * @param unknown $xml
   * @param unknown $config
   * @param unknown $div
   * @param unknown $group
   * @param unknown $dim
   */
  function generateCrossTab($xml, &$config, &$div, $group, $dim) {
673
    $attrs = array(); 
674
    $table_frx['renderer'] = 'FrxCrosstab';
675
    $table_frx['group'] = is_array($group) ? implode(' ', $group) : $group;
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    $table_frx['dim'] = $dim;
    $table = $this->setFirstNode($div, 4, 'table', NULL, $attrs, $table_frx);
    $thead = $this->setFirstNode($table, 6, 'thead');
    $throw = $this->setFirstNode($thead, 8, 'tr');
    $tbody = $this->setFirstNode($table, 6, 'tbody');
    $tdrow = $this->setFirstNode($tbody, 8, 'tr', NULL, NULL, $attrs);
    if ($config['crosstab_columns']) foreach ($config['crosstab_columns'] as $key => $col) if ($col['type']!=='ignore') {
      if ($col['type']=='heading') {
        $tag = 'th';
      }
      else {
        $tag = 'td';
      }
      if ($col['type'] != 'crosstab') {
        $this->addNode($throw, 10, $tag, $col['label']);
        $this->addNode($tdrow, 10, $tag, $col['contents']);
      }
    }
David Metzler's avatar
David Metzler committed
694

695
  }
696
697

}