all files / chart/series/ histogram-series.js

98.78% Statements 81/82
92.31% Branches 24/26
100% Functions 17/17
98.72% Lines 77/78
9 statements, 5 functions, 9 branches Ignored     
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        24×   27× 27×     26× 26× 26× 1300× 1300×   26× 26× 26×     26× 26× 26× 26× 1300×   26×     26× 26× 26× 26× 26× 6200× 124×         124× 124×   26×     2000× 2000×   2000× 2000× 2000×           20665×          
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../../common/utils/helper", "./column-series"], function (require, exports, helper_1, column_series_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var HistogramSeries = (function (_super) {
        __extends(HistogramSeries, _super);
        function HistogramSeries() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        HistogramSeries.prototype.render = function (series) {
            _super.prototype.render.call(this, series);
            if (series.showNormalDistribution) {
                this.renderNormalDistribution(series);
            }
        };
        HistogramSeries.prototype.calculateBinInterval = function (yValues, series) {
            var mean = helper_1.sum(yValues) / yValues.length;
            var sumValue = 0;
            for (var _i = 0, yValues_1 = yValues; _i < yValues_1.length; _i++) {
                var value = yValues_1[_i];
                sumValue += (value - mean) * (value - mean);
            }
            series.histogramValues.mean = mean;
            series.histogramValues.sDValue = Math.round(Math.sqrt(sumValue / yValues.length - 1));
            series.histogramValues.binWidth = series.binInterval ||
                Math.round((3.5 * series.histogramValues.sDValue) / Math.pow(yValues.length, 1 / 3));
        };
        HistogramSeries.prototype.processInternalData = function (data, series) {
            var updatedData = [];
            var yValues = [];
            var binWidth;
            Object.keys(data).forEach(function (key) {
                yValues.push(data[key][series.yName]);
            });
            series.histogramValues = {
                yValues: yValues
            };
            var min = Math.min.apply(Math, series.histogramValues.yValues);
            this.calculateBinInterval(series.histogramValues.yValues, series);
            binWidth = series.histogramValues.binWidth;
            var yCount;
            for (var j = 0; j < data.length;) {
                yCount = yValues.filter(function (y) { return y >= min && y < (min + (binWidth)); }).length;
                updatedData.push((_a = {
                        'x': min + binWidth / 2
                    },
                    _a[series.yName] = yCount,
                    _a));
                min = min + binWidth;
                j += yCount;
            }
            return updatedData;
            var _a;
        };
        HistogramSeries.prototype.renderNormalDistribution = function (series) {
            var min = series.xAxis.actualRange.min;
            var max = series.xAxis.actualRange.max;
            var xValue;
            var pointLocation;
            var yValue;
            var direction = '';
            var startPoint = 'M';
            var yValuesCount = series.histogramValues.yValues.length;
            var binWidth = series.histogramValues.binWidth;
            var mean = series.histogramValues.mean;
            var sDValue = series.histogramValues.sDValue;
            var pointsCount = 500;
            var del = (max - min) / (pointsCount - 1);
            var distributionLine;
            for (var i = 0; i < pointsCount; i++) {
                xValue = min + i * del;
                yValue = Math.exp(-(xValue - mean) * (xValue - mean) / (2 * sDValue * sDValue)) /
                    (sDValue * Math.sqrt(2 * Math.PI));
                pointLocation = helper_1.getPoint(xValue, yValue * binWidth * yValuesCount, series.xAxis, series.yAxis, series.chart.requireInvertedAxis, series);
                direction += startPoint + ' ' + (pointLocation.x) + ' ' + (pointLocation.y) + ' ';
                startPoint = 'L';
            }
            distributionLine = series.chart.renderer.drawPath(new helper_1.PathOption(series.chart.element.id + '_Series_' + series.index + '_NDLine', 'transparent', 2, series.chart.themeStyle.errorBar, series.opacity, series.dashArray, direction));
            distributionLine.style.visibility = (series.animation.enable && series.chart.animateSeries) ?
                'hidden' : 'visible';
            series.seriesElement.appendChild(distributionLine);
        };
        HistogramSeries.prototype.doAnimation = function (series) {
            _super.prototype.doAnimation.call(this, series);
            Eif (series.showNormalDistribution) {
                helper_1.templateAnimate(series.seriesElement.lastElementChild, series.animation.duration, 500, 'FadeIn');
            }
        };
        HistogramSeries.prototype.getModuleName = function () {
            return 'HistogramSeries';
        };
        HistogramSeries.prototype.destroy = function (chart) {
        };
        return HistogramSeries;
    }(column_series_1.ColumnSeries));
    exports.HistogramSeries = HistogramSeries;
});