lib/amd/src/chart_bar.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 bar.
  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_bar
  21. */
  22. define(['core/chart_base'], function(Base) {
  23. /**
  24. * Bar chart.
  25. *
  26. * @extends {module:core/chart_base}
  27. * @class
  28. */
  29. function Bar() {
  30. Base.prototype.constructor.apply(this, arguments);
  31. }
  32. Bar.prototype = Object.create(Base.prototype);
  33. /**
  34. * Whether the bars should be displayed horizontally or not.
  35. *
  36. * @type {Bool}
  37. * @protected
  38. */
  39. Bar.prototype._horizontal = false;
  40. /**
  41. * Whether the bars should be stacked or not.
  42. *
  43. * @type {Bool}
  44. * @protected
  45. */
  46. Bar.prototype._stacked = false;
  47. /** @override */
  48. Bar.prototype.TYPE = 'bar';
  49. /** @override */
  50. Bar.prototype.create = function(Klass, data) {
  51. var chart = Base.prototype.create.apply(this, arguments);
  52. chart.setHorizontal(data.horizontal);
  53. chart.setStacked(data.stacked);
  54. return chart;
  55. };
  56. /** @override */
  57. Bar.prototype._setDefaults = function() {
  58. Base.prototype._setDefaults.apply(this, arguments);
  59. var axis = this.getYAxis(0, true);
  60. axis.setMin(0);
  61. };
  62. /**
  63. * Get whether the bars should be displayed horizontally or not.
  64. *
  65. * @returns {Bool}
  66. */
  67. Bar.prototype.getHorizontal = function() {
  68. return this._horizontal;
  69. };
  70. /**
  71. * Get whether the bars should be stacked or not.
  72. *
  73. * @returns {Bool}
  74. */
  75. Bar.prototype.getStacked = function() {
  76. return this._stacked;
  77. };
  78. /**
  79. * Set whether the bars should be displayed horizontally or not.
  80. *
  81. * It sets the X Axis to zero if the min value is null.
  82. *
  83. * @param {Bool} horizontal True if the bars should be displayed horizontally, false otherwise.
  84. */
  85. Bar.prototype.setHorizontal = function(horizontal) {
  86. var axis = this.getXAxis(0, true);
  87. if (axis.getMin() === null) {
  88. axis.setMin(0);
  89. }
  90. this._horizontal = Boolean(horizontal);
  91. };
  92. /**
  93. * Set whether the bars should be stacked or not.
  94. *
  95. * @method setStacked
  96. * @param {Bool} stacked True if the chart should be stacked or false otherwise.
  97. */
  98. Bar.prototype.setStacked = function(stacked) {
  99. this._stacked = Boolean(stacked);
  100. };
  101. return Bar;
  102. });