pareto.src.js 9.33 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/**
 * @license  Highcharts JS v6.0.4 (2017-12-15)
 *
 * Pareto series type for Highcharts
 *
 * (c) 2010-2017 Sebastian Bochan
 *
 * License: www.highcharts.com/license
 */
'use strict';
(function(factory) {
    if (typeof module === 'object' && module.exports) {
        module.exports = factory;
    } else {
        factory(Highcharts);
    }
}(function(Highcharts) {
    var derivedSeriesMixin = (function(H) {

        var each = H.each,
            Series = H.Series,
            addEvent = H.addEvent,
            fireEvent = H.fireEvent,
            wrap = H.wrap,
            noop = H.noop;


        /* ***************************************************************************
         *
         * DERIVED SERIES MIXIN
         *
         **************************************************************************** */

        /**
         * Provides methods for auto setting/updating series data based on the based series data,
         * 
         * @mixin
         **/
        var derivedSeriesMixin = {
            /**
             * Initialise series
             *
             * returns {undefined}
             **/
            init: function() {
                Series.prototype.init.apply(this, arguments);

                this.initialised = false;
                this.baseSeries = null;
                this.eventRemovers = [];

                this.addEvents();
            },

            /**
             * Method to be implemented - inside the method the series has already access to the base series
             * via m `this.baseSeries` and the bases data is initialised. It should
             * return data in the format accepted by Series.setData() method
             *
             * @returns {Array} - an array of data
             **/
            setDerivedData: noop,

            /**
             * Sets base series for the series
             *
             * returns {undefined} 
             **/
            setBaseSeries: function() {
                var chart = this.chart,
                    baseSeriesOptions = this.options.baseSeries,
                    baseSeries =
                    baseSeriesOptions &&
                    (chart.series[baseSeriesOptions] || chart.get(baseSeriesOptions));

                this.baseSeries = baseSeries || null;
            },

            /**
             * Adds events for the series
             *
             * @returns {undefined}
             **/
            addEvents: function() {
                var derivedSeries = this,
                    chartSeriesLinked;

                chartSeriesLinked = addEvent(this.chart, 'seriesLinked', function() {
                    derivedSeries.setBaseSeries();

                    if (derivedSeries.baseSeries && !derivedSeries.initialised) {
                        derivedSeries.setDerivedData();
                        derivedSeries.addBaseSeriesEvents();
                        derivedSeries.initialised = true;
                    }
                });

                this.eventRemovers.push(
                    chartSeriesLinked
                );
            },

            /**
             * Adds events to the base series - it required for recalculating the data in
             * the series if the base series is updated / removed / etc.
             *
             * @returns {undefined}
             **/
            addBaseSeriesEvents: function() {
                var derivedSeries = this,
                    updatedDataRemover,
                    destroyRemover;

                updatedDataRemover = addEvent(derivedSeries.baseSeries, 'updatedData', function() {
                    derivedSeries.setDerivedData();
                });

                destroyRemover = addEvent(derivedSeries.baseSeries, 'destroy', function() {
                    derivedSeries.baseSeries = null;
                    derivedSeries.initialised = false;
                });

                derivedSeries.eventRemovers.push(
                    updatedDataRemover,
                    destroyRemover
                );
            },

            /**
             * Destroys the series
             *
             * @returns {undefined}
             **/
            destroy: function() {
                each(this.eventRemovers, function(remover) {
                    remover();
                });

                Series.prototype.destroy.apply(this, arguments);
            }
        };

        /**
         * Adds a new chart event after the series are linked
         **/
        wrap(H.Chart.prototype, 'linkSeries', function(p) {
            p.call(this);

            fireEvent(this, 'seriesLinked');
        });
        return derivedSeriesMixin;
    }(Highcharts));
    (function(H, derivedSeriesMixin) {
        /**
         * (c) 2010-2017 Sebastian Bochan
         *
         * License: www.highcharts.com/license
         */


        var each = H.each,
            correctFloat = H.correctFloat,
            seriesType = H.seriesType,
            merge = H.merge;


        /**
         * The pareto series type.
         *
         * @constructor seriesTypes.pareto
         * @augments seriesTypes.line
         */

        /**
         * A pareto diagram is a type of chart that contains both bars and a line graph, 
         * where individual values are represented in descending order by bars, 
         * and the cumulative total is represented by the line.
         * 
         * @extends {plotOptions.line}
         * @product highcharts
         * @sample {highcharts} highcharts/demo/pareto/
         *         Pareto diagram
         * @since 6.0.0
         * @excluding allAreas,boostThreshold,borderColor,borderRadius,
         *         borderWidth,crisp,colorAxis,depth,data,edgeColor,edgeWidth,
         *         findNearestPointBy,gapSize,gapUnit,grouping,groupPadding,groupZPadding,maxPointWidth,
         *         keys,negativeColor,pointInterval,pointIntervalUnit,pointPadding,
         *         pointPlacement,pointRange,pointStart,pointWidth,shadow,step,softThreshold,
         *         stacking,threshold,zoneAxis,zones
         * @optionparent plotOptions.pareto
         */

        seriesType('pareto', 'line', {
            /**
             * Higher zIndex than column series to draw line above shapes.
             */
            zIndex: 3
        }, merge(derivedSeriesMixin, {
            /**
             * calculate sum and return percent points
             * 
             * @param  {Object} series
             * @return {Array} Returns array of points [x,y]
             */
            setDerivedData: function() {
                if (this.baseSeries.yData.length > 1) {
                    var xValues = this.baseSeries.xData,
                        yValues = this.baseSeries.yData,
                        sum = this.sumPointsPercents(yValues, xValues, null, true);

                    this.setData(this.sumPointsPercents(yValues, xValues, sum, false), false);
                }
            },
            /**
             * calculate y sum and each percent point
             *
             * @param  {Array} yValues y values
             * @param  {Array} xValues x values
             * @param  {Number} sum of all y values 
             * @param  {Boolean} isSum declares if calculate sum of all points
             * @return {Array} Returns sum of points or array of points [x,y]
             */
            sumPointsPercents: function(yValues, xValues, sum, isSum) {
                var sumY = 0,
                    sumPercent = 0,
                    percentPoints = [],
                    percentPoint;

                each(yValues, function(point, i) {
                    if (point !== null) {
                        if (isSum) {
                            sumY += point;
                        } else {
                            percentPoint = (point / sum) * 100;
                            percentPoints.push([xValues[i], correctFloat(sumPercent + percentPoint)]);
                            sumPercent += percentPoint;
                        }
                    }
                });

                return isSum ? sumY : percentPoints;
            }
        }));

        /**
         * A `pareto` series. If the [type](#series.pareto.type) option is not
         * specified, it is inherited from [chart.type](#chart.type).
         * 
         * For options that apply to multiple series, it is recommended to add
         * them to the [plotOptions.series](#plotOptions.series) options structure.
         * To apply to all series of this specific type, apply it to [plotOptions.
         * pareto](#plotOptions.pareto).
         * 
         * @type {Object}
         * @since 6.0.0
         * @extends series,plotOptions.pareto
         * @excluding data,dataParser,dataURL
         * @product highcharts
         * @apioption series.pareto
         */

        /**
         * An integer identifying the index to use for the base series, or a string
         * representing the id of the series.
         *
         * @type {Number|String}
         * @default undefined
         * @apioption series.pareto.baseSeries
         */

        /**
         * An array of data points for the series. For the `pareto` series type,
         * points are calculated dynamically.
         * 
         * @type {Array<Object|Array>}
         * @since 6.0.0
         * @extends series.column.data
         * @product highcharts
         * @apioption series.pareto.data
         */

    }(Highcharts, derivedSeriesMixin));
}));