/**
 * * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Generated by Apache Flex Cross-Compiler from org/apache/flex/charts/beads/layouts/BarChartLayout.as
 * org.apache.flex.charts.beads.layouts.BarChartLayout
 *
 * @fileoverview
 *
 * @suppress {checkTypes|accessControls}
 */

goog.provide('org.apache.flex.charts.beads.layouts.BarChartLayout');

goog.require('org.apache.flex.charts.beads.layouts.ChartBaseLayout');
goog.require('org.apache.flex.charts.supportClasses.BarSeries');
goog.require('org.apache.flex.core.UIBase');
goog.require('org.apache.flex.events.Event');
goog.require('org.apache.flex.charts.core.ICartesianChartLayout');
goog.require('org.apache.flex.core.IBeadLayout');
goog.require('org.apache.flex.core.ISelectionModel');
goog.require('org.apache.flex.events.IEventDispatcher');
goog.require('org.apache.flex.utils.Language');



/**
 *  constructor.
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion FlexJS 0.0
 * @constructor
 * @extends {org.apache.flex.charts.beads.layouts.ChartBaseLayout}
 * @implements {org.apache.flex.core.IBeadLayout}
 * @implements {org.apache.flex.charts.core.ICartesianChartLayout}
 */
org.apache.flex.charts.beads.layouts.BarChartLayout = function() {
  org.apache.flex.charts.beads.layouts.BarChartLayout.base(this, 'constructor');
};
goog.inherits(org.apache.flex.charts.beads.layouts.BarChartLayout, org.apache.flex.charts.beads.layouts.ChartBaseLayout);


/**
 * @private
 * @type {number}
 */
org.apache.flex.charts.beads.layouts.BarChartLayout.prototype._gap = 20;


/**
 * @asprivate
 * @protected
 * @override
 */
org.apache.flex.charts.beads.layouts.BarChartLayout.prototype.performLayout = function() {
  var /** @type {org.apache.flex.core.ISelectionModel} */ selectionModel = org.apache.flex.utils.Language.as(this.chart.getBeadByType(org.apache.flex.core.ISelectionModel), org.apache.flex.core.ISelectionModel);
  var /** @type {Array} */ dp = org.apache.flex.utils.Language.as(selectionModel.dataProvider, Array);
  if (!dp)
    return;
  var /** @type {number} */ n = dp.length;
  var /** @type {number} */ useWidth = org.apache.flex.utils.Language.as(this.chartDataGroup, org.apache.flex.core.UIBase, true).width;
  var /** @type {number} */ useHeight = org.apache.flex.utils.Language.as(this.chartDataGroup, org.apache.flex.core.UIBase, true).height;
  var /** @type {number} */ itemHeight = (useHeight - this.gap * (dp.length - 1)) / dp.length;
  var /** @type {number} */ seriesHeight = itemHeight / this.chart.series.length;
  var /** @type {number} */ ypos = useHeight;
  var /** @type {number} */ maxXValue = 0;
  var /** @type {number} */ minXValue = 0;
  var /** @type {number} */ scaleFactor = 1.0;
  var /** @type {boolean} */ determineScale = true;
  if (this.horizontalAxisBead != null && !isNaN(this.horizontalAxisBead.maximum)) {
    maxXValue = this.horizontalAxisBead.maximum;
    determineScale = false;
  }
  if (this.horizontalAxisBead != null && !isNaN(this.horizontalAxisBead.minimum)) {
    minXValue = this.horizontalAxisBead.minimum;
  }
  for (var /** @type {number} */ s = 0; s < this.chart.series.length; s++) {
    var /** @type {org.apache.flex.charts.supportClasses.BarSeries} */ bcs = org.apache.flex.utils.Language.as(this.chart.series[s], org.apache.flex.charts.supportClasses.BarSeries);
    for (var /** @type {number} */ i = 0; i < n; i++) {
      var /** @type {Object} */ data = dp[i];
      var /** @type {string} */ field = bcs.xField;
      var /** @type {number} */ xValue = Number(data[field]);
      if (determineScale)
        maxXValue = Math.max(xValue, maxXValue);
    }
  }
  var /** @type {number} */ range = maxXValue - minXValue;
  scaleFactor = useWidth / range;
  for (i = 0; i < n; i++) {
    data = dp[i];
    for (s = 0; s < this.chart.series.length; s++) {
      bcs = org.apache.flex.utils.Language.as(this.chart.series[s], org.apache.flex.charts.supportClasses.BarSeries);
      var /** @type {org.apache.flex.charts.core.IChartItemRenderer} */ child = this.chartDataGroup.getItemRendererForSeriesAtIndex(bcs, i);
      xValue = Number(data[bcs.xField]) - minXValue;
      if (xValue > maxXValue)
        xValue = maxXValue;
      xValue = xValue * scaleFactor;
      child.x = 0;
      child.y = ypos - seriesHeight;
      child.width = xValue;
      child.height = seriesHeight;
      ypos -= seriesHeight;
    }
    ypos -= this.gap;
  }
  org.apache.flex.utils.Language.as(this.chart, org.apache.flex.events.IEventDispatcher, true).dispatchEvent(new org.apache.flex.events.Event("layoutComplete"));
};


Object.defineProperties(org.apache.flex.charts.beads.layouts.BarChartLayout.prototype, /** @lends {org.apache.flex.charts.beads.layouts.BarChartLayout.prototype} */ {
/** @export */
gap: {
get: /** @this {org.apache.flex.charts.beads.layouts.BarChartLayout} */ function() {
  return this._gap;
},
set: /** @this {org.apache.flex.charts.beads.layouts.BarChartLayout} */ function(value) {
  this._gap = value;
}}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.flex.charts.beads.layouts.BarChartLayout.prototype.FLEXJS_CLASS_INFO = { names: [{ name: 'BarChartLayout', qName: 'org.apache.flex.charts.beads.layouts.BarChartLayout'}], interfaces: [org.apache.flex.core.IBeadLayout, org.apache.flex.charts.core.ICartesianChartLayout] };


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('org.apache.flex.charts.beads.layouts.BarChartLayout', org.apache.flex.charts.beads.layouts.BarChartLayout);



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.flex.charts.beads.layouts.BarChartLayout.prototype.FLEXJS_REFLECTION_INFO = function () {
  return {
    variables: function () {
      return {
      };
    },
    accessors: function () {
      return {
        'gap': { type: 'Number', declaredBy: 'org.apache.flex.charts.beads.layouts.BarChartLayout'}
      };
    },
    methods: function () {
      return {
        'BarChartLayout': { type: '', declaredBy: 'org.apache.flex.charts.beads.layouts.BarChartLayout'}
      };
    }
  };
};