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 __());
};
/************************************************
Pie Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PieGadget = (function (_super) {
            __extends(PieGadget, _super);
            function PieGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PieRender();
                _super.call(this, gadgetOptions);
                // Special case where Comparison/Pie settings have the wrong aggregation settings.
                Models.ViewPeriodGadget.EnsureViewingPeriodCorrectForType(this, Settings.ViewingPeriod.SettingGroup.SingleDateWithoutAggregation);
            }
            PieGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PieGadget(gadgetOptions);
                return instance;
            };
            PieGadget.TypeId = "50054ee3-4193-41bf-b472-19277f962790";
            return PieGadget;
        }(Models.ChartingDataGadget));
        Models.PieGadget = PieGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PieRender = (function (_super) {
            __extends(PieRender, _super);
            function PieRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PieRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
            };
            PieRender.prototype.updateGadget = function (handler) {
                var chartOptions = Dashboards.Views.ChartHelper.GetDefaultChartOptions(this);
                var seriesData = [];
                var fillStyleColors = [];
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                _.each(this.gadget.seriesMap, function (series) {
                    fillStyleColors.push(series.color);
                    var dataSeriesName = series.name;
                    var seriesDataPoint = [];
                    seriesDataPoint.push(series.title);
                    if (dataPoints && dataPoints[dataSeriesName] && dataPoints[dataSeriesName][0]) {
                        var absValue = Math.abs(dataPoints[dataSeriesName][0][Dashboards.Models.ChartingDataGadget.ValueIdx]);
                        seriesDataPoint.push(absValue);
                    }
                    else {
                        seriesDataPoint.push(0);
                    }
                    seriesData.push(seriesDataPoint);
                });
                var options = {
                    data: seriesData,
                    fillStyles: fillStyleColors,
                    strokeStyles: fillStyleColors
                };
                if (this.renderSize > 1) {
                    options = {
                        data: seriesData,
                        fillStyles: fillStyleColors,
                        strokeStyles: fillStyleColors,
                        labels: {
                            valueType: 'percentage',
                            font: Views.ChartHelper.GetAxisLabelFontSize(this.renderSize, false) + ' Arial',
                            fillStyle: '#9fa0a4'
                        },
                        labelsPosition: 'outside',
                        labelsAlign: 'circle',
                        labelsExtend: 20,
                        leaderLineWidth: 1,
                        leaderLineStrokeStyle: '#9fa0a4'
                    };
                }
                else {
                    options = {
                        data: seriesData,
                        fillStyles: fillStyleColors,
                        strokeStyles: fillStyleColors
                    };
                }
                var chartSeries = Dashboards.Views.ChartHelper.GetPieChartSeriesOptions(options);
                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.BindPieChartLabelFormat(this);
                this.$getChart().jqChart(chartOptions);
                Views.ChartHelper.BindPieToolTipFormat(this);
            };
            return PieRender;
        }(Views.ViewPeriodChartRender));
        Views.PieRender = PieRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PieGadget.TypeId,
    create: Dashboards.Models.PieGadget.CreateInstance,
    resourceSet: 'Gadgets'
});
//# sourceMappingURL=PieChart.js.map