var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var TrendAnalysisChartGadget = (function (_super) {
            __extends(TrendAnalysisChartGadget, _super);
            function TrendAnalysisChartGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.TrendAnalysisChartRender();
                _super.call(this, gadgetOptions);
            }
            TrendAnalysisChartGadget.CreateInstance = function (gadgetOptions) {
                var instance = new TrendAnalysisChartGadget(gadgetOptions);
                return instance;
            };
            TrendAnalysisChartGadget.TypeId = "c4491cd2-d10d-426d-b5b4-9fa7d14dc865";
            return TrendAnalysisChartGadget;
        }(Models.ChartingDataGadget));
        Models.TrendAnalysisChartGadget = TrendAnalysisChartGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var TrendAnalysisChartRender = (function (_super) {
            __extends(TrendAnalysisChartRender, _super);
            function TrendAnalysisChartRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            TrendAnalysisChartRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
            };
            TrendAnalysisChartRender.prototype.updateGadget = function (handler) {
                var _this = this;
                var chartOptions = Views.ChartHelper.GetDefaultChartOptions(this);
                this.leftAxisType = handler.byName(Settings.Gadget.LeftAxisType.name);
                this.rightAxisType = handler.byName(Settings.Gadget.RightAxisType.name);
                // Add left/right Axes as needed.
                // Also look for Column charts.  In this case we must use category axis, not date time
                var hasLeftAxisSeries = false;
                var hasRightAxisSeries = false;
                _.each(this.gadget.seriesMap, function (series) {
                    if (series.isPrimary) {
                        hasLeftAxisSeries = true;
                    }
                    else {
                        hasRightAxisSeries = true;
                    }
                });
                // Add bottom axis
                // Column types require a category axis, else column get drawn over each other.
                chartOptions.axes = [Views.ChartHelper.GetStandardBottomXAxis(this)];
                // Add other Axes as required
                if (hasLeftAxisSeries) {
                    var leftAxis = Views.ChartHelper.GetStandardLeftYAxis(this);
                    leftAxis.title = handler.byName(Settings.Gadget.LeftAxisTitle.name);
                    Views.ChartHelper.BindChartLeftAxisLimits(leftAxis, handler);
                    chartOptions.axes.push(leftAxis);
                }
                if (hasRightAxisSeries) {
                    var onlyHasRightAxis = !hasLeftAxisSeries && hasRightAxisSeries;
                    var rightAxis = Views.ChartHelper.GetStandardRightYAxis(this, onlyHasRightAxis);
                    rightAxis.title = handler.byName(Settings.Gadget.RightAxisTitle.name);
                    Views.ChartHelper.BindChartRightAxisLimits(rightAxis, handler);
                    chartOptions.axes.push(rightAxis);
                }
                Dashboards.Views.ChartHelper.BindTargetLines(this, chartOptions.axes, handler);
                // Bind Series
                var chartSeries = [];
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                _.each(this.gadget.seriesMap, function (series) {
                    // Create a copy of the data series
                    var seriesData = dataPoints[series.name];
                    var defaultAxisType = series.isPrimary ? _this.leftAxisType : _this.rightAxisType;
                    var jqSeries = Views.ChartHelper.SetupChartSeries(_this, series, series.seriesType ? series.seriesType : defaultAxisType, series.color);
                    jqSeries.axisY = series.isPrimary ? 'y1' : 'y2';
                    jqSeries.data = seriesData;
                    // If either of the axis types are stacked, set the group so
                    // the data can appear on different axis. The axisY value is
                    // unique to the specific axis so is safe to use.
                    if (_this.leftAxisType === 'stackedColumn' || _this.rightAxisType === 'stackedColumn') {
                        jqSeries.stackedGroupName = jqSeries.axisY;
                    }
                    // If this series is a Stacked Column, we must "tweak" the data points.
                    if (jqSeries.type === 'stackedColumn') {
                        _this.modifyDataPointsForStackedColumns(jqSeries.data);
                    }
                    chartSeries.push(jqSeries);
                });
                chartOptions.series = chartSeries;
                var existingSeries = this.$getChart().jqChart('option', 'series');
                Views.ChartHelper.UpdateSeriesVisibility(chartSeries, existingSeries);
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                Views.ChartHelper.BindChartAxisLabelFormat(this, ['left', 'right']);
                this.$getChart().jqChart(chartOptions);
                Views.ChartHelper.BindTimeValueTooltipFormat(this, this.gadget, handler);
                Views.ChartHelper.ShowMarkerForNoDataPlotted(this);
            };
            TrendAnalysisChartRender.prototype.modifyDataPointsForStackedColumns = function (dataPoints) {
                // Since we have stacked column data, we need to ensure each "X axis label" (i.e. Time label)
                // is unique.  If not, the jqChart will group all similar items.
                // i.e. 1:00am from Jan 1, will be merged with 1:00am from Jan 2, etc.  
                // Some data is repeated, while other data is hidden.
                // 
                // We can resolve this by padding the axis labels with leading white-space.  This provides unique
                // group names, while the label rendering trims the white space.
                var foundLabels = [];
                _.each(dataPoints, function (dataPoint) {
                    var foundLabel = false;
                    var prefixPadCount = 0;
                    while (!foundLabel) {
                        var activeLabel = dataPoint[Dashboards.Models.ChartingDataGadget.AxisLabelIdx];
                        if (prefixPadCount > 0) {
                            // Padd out by 1 char
                            activeLabel = StringUtil.padLeft(activeLabel, ' ', activeLabel.length + prefixPadCount);
                        }
                        if (foundLabels[activeLabel]) {
                            // try again.
                            prefixPadCount++;
                        }
                        else {
                            // Never seen this label, mark as found.
                            foundLabels[activeLabel] = 1;
                            foundLabel = true;
                            dataPoint[Dashboards.Models.ChartingDataGadget.AxisLabelIdx] = activeLabel;
                        }
                    }
                });
            };
            return TrendAnalysisChartRender;
        }(Views.ViewPeriodChartRender));
        Views.TrendAnalysisChartRender = TrendAnalysisChartRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.TrendAnalysisChartGadget.TypeId,
    create: Dashboards.Models.TrendAnalysisChartGadget.CreateInstance,
    resourceSet: 'Gadgets'
});
//# sourceMappingURL=TrendAnalysisChart.js.map