[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/amd/src/ -> chart_output_htmltable.js (source)

   1  // This file is part of Moodle - http://moodle.org/
   2  //
   3  // Moodle is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU General Public License as published by
   5  // the Free Software Foundation, either version 3 of the License, or
   6  // (at your option) any later version.
   7  //
   8  // Moodle is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU General Public License
  14  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  15  
  16  /**
  17   * Chart output for HTML table.
  18   *
  19   * @package    core
  20   * @copyright  2016 Frédéric Massart - FMCorz.net
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @module     core/chart_output_htmltable
  23   */
  24  define([
  25      'jquery',
  26      'core/chart_output_base',
  27  ], function($, Base) {
  28  
  29      /**
  30       * Render a chart as an HTML table.
  31       *
  32       * @class
  33       * @extends {module:core/chart_output_base}
  34       * @alias module:core/chart_output_htmltable
  35       */
  36      function Output() {
  37          Base.prototype.constructor.apply(this, arguments);
  38          this._build();
  39      }
  40      Output.prototype = Object.create(Base.prototype);
  41  
  42      /**
  43       * Attach the table to the document.
  44       *
  45       * @protected
  46       */
  47      Output.prototype._build = function() {
  48          this._node.empty();
  49          this._node.append(this._makeTable());
  50      };
  51  
  52      /**
  53       * Builds the table node.
  54       *
  55       * @protected
  56       * @return {Jquery}
  57       */
  58      Output.prototype._makeTable = function() {
  59          var tbl = $('<table>'),
  60              c = this._chart,
  61              node,
  62              value,
  63              labels = c.getLabels(),
  64              hasLabel = labels.length > 0,
  65              series = c.getSeries(),
  66              seriesLabels,
  67              rowCount = series[0].getCount();
  68  
  69          // Identify the table.
  70          tbl.addClass('chart-output-htmltable');
  71  
  72          // Set the caption.
  73          if (c.getTitle() !== null) {
  74              tbl.append($('<caption>').text(c.getTitle()));
  75          }
  76  
  77          // Write the column headers.
  78          node = $('<tr>');
  79          if (hasLabel) {
  80              node.append($('<td>'));
  81          }
  82          series.forEach(function(serie) {
  83              node.append(
  84                  $('<th>')
  85                  .text(serie.getLabel())
  86                  .attr('scope', 'col')
  87              );
  88          });
  89          tbl.append(node);
  90  
  91          // Write rows.
  92          for (var rowId = 0; rowId < rowCount; rowId++) {
  93              node = $('<tr>');
  94              if (labels.length > 0) {
  95                  node.append(
  96                      $('<th>')
  97                      .text(labels[rowId])
  98                      .attr('scope', 'row')
  99                  );
 100              }
 101              for (var serieId = 0; serieId < series.length; serieId++) {
 102                  value = series[serieId].getValues()[rowId];
 103                  seriesLabels = series[serieId].getLabels();
 104                  if (seriesLabels !== null) {
 105                      value = series[serieId].getLabels()[rowId];
 106                  }
 107                  node.append($('<td>').text(value));
 108              }
 109              tbl.append(node);
 110          }
 111  
 112          return tbl;
 113      };
 114  
 115      /** @override */
 116      Output.prototype.update = function() {
 117          this._build();
 118      };
 119  
 120      return Output;
 121  
 122  });


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1