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

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

  public function __construct() {
    parent::__construct();
    $library = forena_library_file('SVGGraph');
    require_once $library;
  }

  /**
   * Re-architect the data into something that the graphing engine can work with
   */
  public function generateGraphData(&$data, $series, $key) {
    // Default controlling attributes
David Metzler's avatar
David Metzler committed
42
    $counts = array();
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    $legend = array();
    $this->graphOptions['structure']['value'] = array();
    foreach($series as $col) {
      $this->graphOptions['structure']['value'][] = trim("$col", '{}');
    }


    $this->graphData = array();
    foreach ($data as $row) {
      Frx::Data()->push($row, '_row');
      $trow = array();
      // Base group
      $trow['key'] =  $this->wrap_label ? wordwrap($this->teng->replace($key, TRUE), $this->wrap_label) : $this->teng->replace($key, TRUE);
      // Dimensions
      foreach($series as $col) {
          $val = $this->teng->replace($col, TRUE);
David Metzler's avatar
David Metzler committed
59 60 61 62
          if ($val != '' && $val !==NULL) {
            $trow[trim("$col", '{}')] = $val;
            @$counts[trim("$col", '{}')]++;
          }
63 64 65 66 67 68 69 70 71 72 73
      }
      foreach($this->field_sources as $k => $src) {
        $trow[$k] = $this->teng->replace($src, TRUE);
      }
      if(isset($this->field_sources['legend_entries'])) {
        $legend_str = $trow['legend_entries'];
        $legend[$legend_str] = $legend_str;
      }
      Frx::Data()->pop();
      $this->graphData[] = $trow;
    }
David Metzler's avatar
David Metzler committed
74
    $this->counts = $counts;
75 76 77 78 79 80 81 82 83 84 85

    // 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);
    }
  }


   /**
   * Re-architect the data into something that the graphing engine can work with
   */
86
  public function generateGroupGraphData(&$block_data, $group, $series, $key, $dim, $sum) {
87 88 89 90 91
    $dim_headers = array();
    $dim_rows = array();
    $dim_values = array();
    $rows = array();
    $legend = array();
David Metzler's avatar
David Metzler committed
92
    $counts = array();
93
    $data = $this->frxReport->group($block_data, $group, $sum);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $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) {
112
        $structure_idx = trim($dk, '{}') . trim($col, '{}');
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $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->teng->replace($group, TRUE);
      if ($this->wrap_label) $gkey = wordwrap($gkey, $this->wrap_label);
130
      $trow['key'] = $gkey;
131 132 133 134 135 136 137 138 139
      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->teng->replace($col, TRUE);
David Metzler's avatar
David Metzler committed
140 141 142 143
          if ($val !== '' && $val !== NULL) {
            $trow[trim($dk, '{}') . trim($col, '{}')] = $val;
            @$counts[trim($dk, '{}') . trim($col, '{}')]++;
          }
144 145 146 147 148 149 150 151 152 153 154 155 156 157
          foreach($this->field_sources as $fk => $src) {
            $trow[$dk . $fk] = $this->teng->replace($src, TRUE);
            if (isset($this->field_sources['legend_entries'])) {
              $legend_str = $this->teng->replace($this->field_sources['legend_entries']);
              $legend[$legend_str] = $legend_str;
            }

          }
        }


        frx::Data()->pop();
      }
      $this->graphData[] = $trow;
David Metzler's avatar
David Metzler committed
158
      $this->counts = $counts;
159 160 161 162 163 164 165 166

    }

    // 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);
    }
167
    $this->graphOptions['structure']['key'] = 'key';
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    return $this->graphData;
  }

  /**
   * Add column for cross tabs.
   */
  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,
    );
  }

  /**
   * Derive config variables from graph.
   */
  public function scrapeConfig() {
    $this->weight = 0;

    $nodes =  $this->reportDocNode->xpath('svg');
    if ($nodes) {
      $svg = $nodes[0];
      $config = $this->mergedAttributes($svg);
    }
    $table_nodes = $this->reportDocNode->xpath('table');
    $config['gen_table'] = $table_nodes ? 1 : 0;
    // Determine graph type
    $graph_type =  isset($config['type']) ? $config['type'] : 'BarGraph';

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

    $this->extractTemplateHTML($this->reportDocDomNode, $config, array('svg', 'table'));
    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);
      }
    }
    if ($series) foreach ($series as $col) {
      $this->addColumn('value',$col , trim($col, '{}'), $config);
    }

    // Get the data cells
    if ($dim) {
      $dims = (array)$dim;
      foreach($dims as $dim) {
        $this->addColumn('crosstab', $dim, trim($dim, '{}'), $config);
      }
    }
    $config['style'] = $config['type'];
    return $config;
  }

233
  public function prepareGraph() {
234 235
    $skin_options = Frx::Data()->getContext('skin');
    $options = isset($skin_options['FrxSVGGraph']) ? $skin_options['FrxSVGGraph'] : array();
236 237 238 239
    $attributes = $this->mergedAttributes();
    // Default in xpath for backward compatibility
    // Legacy options.  New charts should be generated using Frx:attribute syntax
    if (isset($attributes['options'])) {
240 241
      $attr_options = array();
      parse_str($attributes['options'], $attr_options);
242
      unset($attributes['options']);
243
      foreach ($attr_options as $key => $value) {
244 245 246 247
        $options[$key] = $this->teng->replace($value, TRUE);
      }
    }
    else {
248
      $options = array_merge($options, $attributes);
249 250 251 252 253 254 255 256
    }

    // 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'];
257
    $sums = @(array)$attributes['sum'];
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    $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 = Frx::Data()->currentContext();
    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->teng->replace($attributes['height'], TRUE);
    if (isset($attributes['width'])) $options['width'] = $this->teng->replace($attributes['width'], TRUE);
    if (isset($options['legend_entries']) && !isset($options['label']) && !isset($options['show_labels'])) {
      $options['show_labels'] = FALSE;
    }


    $this->graphOptions = $options;

305 306
    if ($group) {
      $this->generateGroupGraphData($nodes, $group, $series, $key, $dim, $sums);
307 308 309 310 311 312 313 314
    }
    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']);
    }
David Metzler's avatar
David Metzler committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

  }

  public function validSeries() {
    $valid = TRUE;
    $removed = array();
    if (is_array($this->graphOptions['structure']['value'])) {
      foreach ($this->graphOptions['structure']['value'] as $k=>$series) {
        if (!isset($this->counts[$series])) {
          // remove empty series.
          unset($this->graphOptions['structure']['value'][$k]);
          $removed[] = $k;
        }
      }

      if ($removed) {
        $this->graphOptions['structure']['value'] = array_values($this->graphOptions['structure']['value']);
        foreach($this->graphOptions as $option => $data) {
          if (is_array($data)) {
            $modified = FALSE;
            foreach($removed as $k) {
              if (isset($data[$k])) {
                unset($this->graphOptions[$option][$k]);
                $modified = TRUE;
              }
            }
            if ($modified) {
              $this->graphOptions[$option] = array_values($this->graphOptions[$option]);
            }
          }
        }
        if (!$this->graphOptions['structure']['value']) $valid = FALSE;
      }
    }
    return $valid;
350 351 352 353 354 355 356 357 358 359 360
  }

  /**
   * Render the graph.
   * @return Ambigous <string, mixed>
   */
  public function render() {
    // Get data from source
    $output = '';
    $this->prepareGraph();
    $type = $this->graphOptions['type'];
David Metzler's avatar
David Metzler committed
361
    if ($this->graphData && $this->validSeries()) {
362
      $output = $this->renderChart($type);
363 364 365 366 367 368 369 370 371 372 373
    }

    return $output;
  }

  static function graphTypes() {
    return array(
      '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'),
374
      'BarAndLineGraph' => array('type' => 'Bar and Line Graph', 'style' => 'Grouped'),
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
      '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'),
    );
  }

  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);
  }

399 400
  function renderChart($type ) {

401 402 403 404 405 406 407 408 409 410 411 412
    $type = strtolower($type);
    // Legacy sustitions for backcward compatibility.
    if ($type == 'piechart') $type = 'piegraph';
    if ($type == 'scatterplot') $type = 'scattergraph';
    if ($type == 'multiscatterplot') $type = 'multiscattergraph';

    // 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));
413
    if (isset($graph_classes[$type])) {
414 415 416 417 418 419 420 421 422 423
      $class = $graph_classes[$type];
      $output = $this->renderGraph($class);
    }
    return $output;
  }


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

424
    $options = $this->replaceTokens($this->graphOptions);
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    $data = $this->graphData;

    if (!isset($options['scatter_2d']) && ($type == 'ScatterGraph' || $type=='MultiScatterGraph') && $this->xy_data && !isset($options['scatter_2d'])) {
      $options['scatter_2d'] = TRUE;
    }
    else {
      $options['scatter_2d'] = (bool) @$options['scatter_2d'];
    }
    $width = (@$options['width']) ? @$options['width'] : 600;
    $height = (@$options['height']) ? @$options['height'] : 400;
    $graph = new SVGGraph($width, $height, $options);
    $this->graph = $graph;
    $graph->Values($data);
    if (isset($options['colour']) && is_array($options['colour'])) {
      $graph->Colours($options['colour']);
    }
    // Generate the graph
    $output = $graph->Fetch($type, FALSE);
    // Add a viewbox to be compatible with Prince PDF generation.
David Metzler's avatar
David Metzler committed
444
    if (!@$options['noviewbox']) $options['auto_fit'] = true; 
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    if (!@$options['noviewbox'] && !strpos($output, 'viewBox')) $output = str_replace('<svg width', "<svg viewBox='0 0 $width $height' width", $output);


    if (!$jsinc && $this->format == 'web') {
      if (@!$options['no_js']) {
        $js =  $graph->FetchJavascript();
        $output .= $js;
      }
      $jsinc = TRUE;
    }
    return $output;
  }

  /**
   * 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;
  }

  private function keyFromColumns(&$config) {
    $key = '';
    foreach ($config['crosstab_columns'] as $k => $col) {
      if ($col['type'] == 'heading') {
        $key = trim($key . ' {' . $k . '}');
      }
    }
    $config['key'] = $key;
  }

  public function configForm($config) {
    $graph_options = FrxSVGGraph::graphOptions();
    $graph_types = FrxSVGGraph::graphTypes();
    $gt = array_change_key_case($graph_types);

    $type = @$config['style'];
    if (!isset($config['base_type'])) {
      $base_type = $type ? $gt[strtolower($type)]['type'] : 'Bar Graph';
    }
    else {
      $base_type = $config['base_type'];
    }
    $styles = $graph_options['styles'][$base_type];
    $form = parent::configForm($config);

    $form['base_type'] = array(
        '#type' => 'select',
        '#title' => t('Graph Type'),
        '#options' => $graph_options['types'],
        '#default_value' => $base_type,
        '#ajax' => $this->configAjax(),
    );

    $form['style'] = array(
        '#type' => 'select',
        '#title' => t('Style'),
        '#options' => $styles,
        '#default_value' => $type,
        '#ajax' => $this->configAjax(),
    );

    $form['gen_table'] = array(
        '#type' => 'checkbox',
        '#title' => t('Include data table with graph'),
        '#default_value' => @$config['gen_table'],
        '#ajax' => $this->configAjax(),
    );


    $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'],
      );

      $ctl['contents'] = array(
          '#type' => 'textfield',
          '#size' => '30',
          '#title' => t('Data'),
          '#default_value' => $col['contents'],
      );

      $ctl['type'] = array(
          '#type' => 'radios',
          '#title' => t('Type'),
          '#default_value' => $col['type'],
          '#options' => $types,
          '#ajax' => $this->configAjax()
      );

      $ctl['weight'] = array(
          "#type" => 'weight',
          '#title' => t('Weight'),
          '#delta' => 50,
          '#default_value' => $col['weight'],
      );

      $form['crosstab_columns'][$key] = $ctl;
    }

    $form['label'] = array(
    	'#type' => 'textfield',
      '#title' => t('Graph Label'),
      '#default_value' => @$config['label'],
    );

    $form['link'] = array(
        '#type' => 'textfield',
        '#title' => t('Link Url'),
        '#default_value' => @$config['link'],
    );

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

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


    return $form;
  }

  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'];
  }

  public function generate($xml, &$config) {
    $config['class'] = get_class($this);
    $media = 'FrxSVGGraph';
    $div = $this->blockDiv($config);
    $this->removeChildren($div);

    // PUt on the header
    if (isset($config['header']['value'])) {
      $header = $this->extract('header', $config);
      $this->addFragment($div, $header['value']);
    }

    // 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();

    foreach($config['crosstab_columns'] as $col) {
      if ($col['type'] == 'heading') $group[] = $col['contents'];
      if ($col['type'] == 'crosstab') $dim = $col['contents'];
    }
    if ($group) $config['group'] = is_array($group) ?  implode(' ', $group) : $group;
    if ($dim) $config['dim'] = $dim;



    $this->seriesFromColumns($config);
    $this->keyFromColumns($config);

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

    $type = $this->extract('type', $config);
    if (!$type) $type = 'Bar Graph';

    $gen_table = $this->extract('gen_table', $config);

    // LImit the config
    $frxattrs = $config;
    // Unset common option configurations?
    foreach($this->unset_attrs as $k) unset($frxattrs[$k]);

    $frxattrs = $this->arrayAttributes($frxattrs);


    $frxattrs['renderer'] = 'FrxSVGGraph';
    $frxattrs['type'] = $type;

    $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);
      }
    }

  }

  /**
   * 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']);
    }

    $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);

    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']);
    }
  }

  /**
   * 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) {
    $attrs = array();
    $table_frx['renderer'] = 'FrxCrosstab';
    $table_frx['group'] = is_array($group) ? implode(' ', $group) : $group;
    $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']);
      }
    }

  }

735
}