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 __());
};
/************************************************
Bar Chart Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var BarChartGadget = (function (_super) {
            __extends(BarChartGadget, _super);
            function BarChartGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.BarChartRender();
                _super.call(this, gadgetOptions);
                // Special case where Bar/Pie settings have the wrong aggregation settings.
                Models.ViewPeriodGadget.EnsureViewingPeriodCorrectForType(this, Settings.ViewingPeriod.SettingGroup.SingleDateWithoutAggregation);
            }
            BarChartGadget.CreateInstance = function (gadgetOptions) {
                var instance = new BarChartGadget(gadgetOptions);
                return instance;
            };
            BarChartGadget.TypeId = "36ce56db-375c-4284-a306-e88be9e4b582";
            return BarChartGadget;
        }(Models.ChartingDataGadget));
        Models.BarChartGadget = BarChartGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var BarChartRender = (function (_super) {
            __extends(BarChartRender, _super);
            function BarChartRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            BarChartRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
            };
            BarChartRender.prototype.updateGadget = function (handler) {
                var chartOptions = Views.ChartHelper.GetDefaultChartOptions(this);
                var leftAxis = Views.ChartHelper.GetStandardLeftYAxis(this);
                leftAxis.location = 'bottom';
                chartOptions.axes = [leftAxis];
                var chartSeries = [];
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                var hasNegativeValue = false;
                var hasPositiveValue = false;
                _.each(this.gadget.seriesMap, function (series) {
                    // Update the dataPoints "time value" from "All Data" -> ''
                    if (dataPoints && dataPoints[series.name] && dataPoints[series.name].length > 0) {
                        var seriesDataPoint = dataPoints[series.name][0];
                        seriesDataPoint[Dashboards.Models.ChartingDataGadget.AxisLabelIdx] = '';
                        // Test for a negative value.  Needed later to set Axis range
                        if (seriesDataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx] < 0) {
                            hasNegativeValue = true;
                        }
                        else if (seriesDataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx] > 0) {
                            hasPositiveValue = true;
                        }
                    }
                    // Create a new jqCart series
                    var jqSeries = {
                        title: series.title,
                        type: 'bar',
                        strokeStyle: series.color,
                        fillStyle: series.color,
                        lineWidth: 3,
                        markers: null,
                        showInScene: true,
                        data: dataPoints[series.name]
                    };
                    chartSeries.splice(0, 0, jqSeries);
                });
                chartOptions.series = chartSeries;
                var leftAxis = chartOptions.axes[0];
                leftAxis.visibleMinimum = null; // Remove default value, we set here.
                if (hasNegativeValue && hasPositiveValue) {
                    // We have a full spread, don't set an axis range.
                    leftAxis.minimum = null;
                    leftAxis.maximum = null;
                }
                else if (hasNegativeValue && !hasPositiveValue) {
                    // All negatives, set Max to 0
                    leftAxis.minimum = null;
                    leftAxis.maximum = 0;
                }
                else {
                    // We only have all positive (or no real data) so anchor at 0.
                    leftAxis.minimum = 0;
                    leftAxis.maximum = null;
                }
                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, ['bottom']);
                this.$getChart().jqChart(chartOptions);
                Views.ChartHelper.BindSimpleToolTipFormat(this);
            };
            return BarChartRender;
        }(Views.ViewPeriodChartRender));
        Views.BarChartRender = BarChartRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.BarChartGadget.TypeId,
    create: Dashboards.Models.BarChartGadget.CreateInstance,
    resourceSet: 'Gadgets'
});
//# sourceMappingURL=BarChart.js.map