[ Index ]

PHP Cross Reference of Moodle 310

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   * @copyright  2016 Frédéric Massart - FMCorz.net
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   * @module     core/chart_output_htmltable
  22   */
  23  define([
  24      'jquery',
  25      'core/chart_output_base',
  26  ], function($, Base) {
  27  
  28      /**
  29       * Render a chart as an HTML table.
  30       *
  31       * @class
  32       * @extends {module:core/chart_output_base}
  33       */
  34      function Output() {
  35          Base.prototype.constructor.apply(this, arguments);
  36          this._build();
  37      }
  38      Output.prototype = Object.create(Base.prototype);
  39  
  40      /**
  41       * Attach the table to the document.
  42       *
  43       * @protected
  44       */
  45      Output.prototype._build = function() {
  46          this._node.empty();
  47          this._node.append(this._makeTable());
  48      };
  49  
  50      /**
  51       * Builds the table node.
  52       *
  53       * @protected
  54       * @return {Jquery}
  55       */
  56      Output.prototype._makeTable = function() {
  57          var tbl = $('<table>'),
  58              c = this._chart,
  59              node,
  60              value,
  61              labels = c.getLabels(),
  62              hasLabel = labels.length > 0,
  63              series = c.getSeries(),
  64              seriesLabels,
  65              rowCount = series[0].getCount();
  66  
  67          // Identify the table.
  68          tbl.addClass('chart-output-htmltable generaltable');
  69  
  70          // Set the caption.
  71          if (c.getTitle() !== null) {
  72              tbl.append($('<caption>').text(c.getTitle()));
  73          }
  74  
  75          // Write the column headers.
  76          node = $('<tr>');
  77          if (hasLabel) {
  78              node.append($('<td>'));
  79          }
  80          series.forEach(function(serie) {
  81              node.append(
  82                  $('<th>')
  83                  .text(serie.getLabel())
  84                  .attr('scope', 'col')
  85              );
  86          });
  87          tbl.append(node);
  88  
  89          // Write rows.
  90          for (var rowId = 0; rowId < rowCount; rowId++) {
  91              node = $('<tr>');
  92              if (labels.length > 0) {
  93                  node.append(
  94                      $('<th>')
  95                      .text(labels[rowId])
  96                      .attr('scope', 'row')
  97                  );
  98              }
  99              for (var serieId = 0; serieId < series.length; serieId++) {
 100                  value = series[serieId].getValues()[rowId];
 101                  seriesLabels = series[serieId].getLabels();
 102                  if (seriesLabels !== null) {
 103                      value = series[serieId].getLabels()[rowId];
 104                  }
 105                  node.append($('<td>').text(value));
 106              }
 107              tbl.append(node);
 108          }
 109  
 110          return tbl;
 111      };
 112  
 113      /** @override */
 114      Output.prototype.update = function() {
 115          this._build();
 116      };
 117  
 118      return Output;
 119  
 120  });


Generated: Wed Jan 22 11:59:49 2025 Cross-referenced by PHPXref 0.7.1