lib/amd/src/chart_output_htmltable.js

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