| 1 | efrain | 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 pie.
 | 
        
           |  |  | 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_pie
 | 
        
           |  |  | 22 |  */
 | 
        
           |  |  | 23 | define(['core/chart_base'], function(Base) {
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 |     /**
 | 
        
           |  |  | 26 |      * Pie chart.
 | 
        
           |  |  | 27 |      *
 | 
        
           |  |  | 28 |      * @class
 | 
        
           |  |  | 29 |      * @extends {module:core/chart_base}
 | 
        
           |  |  | 30 |      */
 | 
        
           |  |  | 31 |     function Pie() {
 | 
        
           |  |  | 32 |         Base.prototype.constructor.apply(this, arguments);
 | 
        
           |  |  | 33 |     }
 | 
        
           |  |  | 34 |     Pie.prototype = Object.create(Base.prototype);
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     /** @override */
 | 
        
           |  |  | 37 |     Pie.prototype.TYPE = 'pie';
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Whether the chart should be displayed as doughnut or not.
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @type {Bool}
 | 
        
           |  |  | 43 |      * @protected
 | 
        
           |  |  | 44 |      */
 | 
        
           |  |  | 45 |     Pie.prototype._doughnut = null;
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /** @override */
 | 
        
           |  |  | 48 |     Pie.prototype.create = function(Klass, data) {
 | 
        
           |  |  | 49 |         var chart = Base.prototype.create.apply(this, arguments);
 | 
        
           |  |  | 50 |         chart.setDoughnut(data.doughnut);
 | 
        
           |  |  | 51 |         return chart;
 | 
        
           |  |  | 52 |     };
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      * Overridden to add appropriate colors to the series.
 | 
        
           |  |  | 56 |      *
 | 
        
           |  |  | 57 |      * @override
 | 
        
           |  |  | 58 |      */
 | 
        
           |  |  | 59 |     Pie.prototype.addSeries = function(series) {
 | 
        
           |  |  | 60 |         if (series.getColor() === null) {
 | 
        
           |  |  | 61 |             var colors = [];
 | 
        
           |  |  | 62 |             var configColorSet = this.getConfigColorSet() || Base.prototype.COLORSET;
 | 
        
           |  |  | 63 |             for (var i = 0; i < series.getCount(); i++) {
 | 
        
           |  |  | 64 |                 colors.push(configColorSet[i % configColorSet.length]);
 | 
        
           |  |  | 65 |             }
 | 
        
           |  |  | 66 |             series.setColors(colors);
 | 
        
           |  |  | 67 |         }
 | 
        
           |  |  | 68 |         return Base.prototype.addSeries.apply(this, arguments);
 | 
        
           |  |  | 69 |     };
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |     /**
 | 
        
           |  |  | 72 |      * Get whether the chart should be displayed as doughnut or not.
 | 
        
           |  |  | 73 |      *
 | 
        
           |  |  | 74 |      * @method getDoughnut
 | 
        
           |  |  | 75 |      * @returns {Bool}
 | 
        
           |  |  | 76 |      */
 | 
        
           |  |  | 77 |     Pie.prototype.getDoughnut = function() {
 | 
        
           |  |  | 78 |         return this._doughnut;
 | 
        
           |  |  | 79 |     };
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * Set whether the chart should be displayed as doughnut or not.
 | 
        
           |  |  | 83 |      *
 | 
        
           |  |  | 84 |      * @method setDoughnut
 | 
        
           |  |  | 85 |      * @param {Bool} doughnut True for doughnut type, false for pie.
 | 
        
           |  |  | 86 |      */
 | 
        
           |  |  | 87 |     Pie.prototype.setDoughnut = function(doughnut) {
 | 
        
           |  |  | 88 |         this._doughnut = Boolean(doughnut);
 | 
        
           |  |  | 89 |     };
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Validate a series.
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * Overrides parent implementation to validate that there is only
 | 
        
           |  |  | 95 |      * one series per chart instance.
 | 
        
           |  |  | 96 |      *
 | 
        
           |  |  | 97 |      * @override
 | 
        
           |  |  | 98 |      */
 | 
        
           |  |  | 99 |     Pie.prototype._validateSeries = function() {
 | 
        
           |  |  | 100 |         if (this._series.length >= 1) {
 | 
        
           |  |  | 101 |             throw new Error('Pie charts only support one serie.');
 | 
        
           |  |  | 102 |         }
 | 
        
           |  |  | 103 |         return Base.prototype._validateSeries.apply(this, arguments);
 | 
        
           |  |  | 104 |     };
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     return Pie;
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | });
 |