"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var d3 = require("d3");
var D3Chart_1 = require("../D3Chart");
var _ = require("lodash");
var common_1 = require("../common");
var StackWidth = 8;
var CircleRadius = 8;
/**
 * 横向，气泡Bar图
*/
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.__ClassName = '_vc_bar_papaw';
        _this.__Grid = {
            top: 30,
            left: 36,
            right: 24,
            bottom: 30,
        };
        _this.widthScale = null;
        _this.heightScale = null;
        return _this;
    }
    default_1.prototype.sort = function () {
        var _this = this;
        this.DATA = _.orderBy(this.props.data, function (d) { return parseFloat(d[_this.props.valueField]); }, 'desc');
        if (this.DATA.length > this.MAXCOUNT) {
            this.DATA.length = this.MAXCOUNT;
        }
        var _a = __read(this.extent(this.DATA, function (d) { return d[_this.props.valueField]; }), 2), min = _a[0], max = _a[1];
        //        max = DefUtil.fixMax(max);
        this.widthScale = d3.scaleBand().rangeRound([0, this.LAYOUT.w]).domain(this.DATA.map(function (item) { return item[_this.props.labelField]; }));
        this.heightScale = d3.scaleLinear().rangeRound([this.LAYOUT.h, 0]).domain([0, max]).nice();
        this.DATA = d3.stack().keys([this.props.valueField])(this.DATA)[0];
    };
    default_1.prototype.build = function () {
        this.SVG.append("g")
            .attr("class", "_x_axis")
            .attr("transform", "translate(" + this.LAYOUT.x + "," + (this.LAYOUT.y + this.LAYOUT.h) + ")");
        this.SVG.append("g")
            .attr("class", "_y_axis")
            .attr("transform", "translate(" + this.LAYOUT.x + "," + this.LAYOUT.y + ")");
        this.SVG.append("g")
            .attr("class", "_axis_line")
            .attr("transform", "translate(" + this.LAYOUT.x + "," + this.LAYOUT.y + ")");
        this.SVG.append('g').attr('class', '_container')
            .attr("transform", "translate(" + this.LAYOUT.x + "," + this.LAYOUT.y + ")");
        this.update();
    };
    default_1.prototype.update = function () {
        var _this = this;
        var band = this.widthScale.bandwidth();
        var diff = (band - StackWidth) / 2;
        var group = this.SVG.select('g._container').selectAll('g._item').data(this.DATA, function (d) { return d.data[_this.props.labelField]; });
        var enter = group.enter().append('g')
            .attr('transform', "translate(-220,0)");
        enter.merge(group.select('g._item'))
            .attr('class', function (d, i) { return _this.itemClass(d, i); })
            .transition()
            .duration(this.DURATION)
            .attr('transform', function (d, i) { return "translate(" + band * i + ",0)"; });
        enter.append("rect")
            .attr('x', diff)
            .attr('y', this.LAYOUT.h)
            .attr('width', StackWidth)
            .attr('height', 0)
            .merge(group.select('rect'))
            .transition()
            .duration(this.DURATION)
            .attr('x', diff)
            .attr('y', function (d) { return _this.heightScale(d[1]); })
            .attr('width', StackWidth)
            .attr('height', function (d) { return _this.heightScale(d[0]) - _this.heightScale(d[1]); });
        enter.append('circle')
            .attr('cx', diff + StackWidth / 2)
            .attr('cy', this.LAYOUT.h)
            .attr('r', CircleRadius)
            .merge(group.select('circle'))
            .transition()
            .duration(this.DURATION)
            .attr('cx', diff + StackWidth / 2)
            .attr('cy', function (d) { return _this.heightScale(d[1]); });
        enter.on('mouseover', function (d) {
            if (_.has(_this.props, 'tip')) {
                common_1.ToolTip.show(_this.props.tip(d.data));
            }
            else {
                common_1.ToolTip.show("<h2>" + _this.FormatLabel(d.data[_this.props.labelField]) + " : <small class=\"vapfont\">" + _this.FormatValue(d.data[_this.props.valueField]) + "</small></h2>");
            }
        }).on('mouseout', function (d) {
            common_1.ToolTip.hide();
        });
        if (this.props.onClick) {
            enter.on('click', function (d) { _this.props.onClick(d.data); });
        }
        var xAxis = this.SVG.select('g._x_axis').call(d3.axisBottom(this.widthScale).tickSize(1).tickFormat(function (e) { return _this.FormatLabel(e); }));
        if (this.props.rotate) {
            xAxis.selectAll("text")
                .style("text-anchor", "middle")
                // .attr("dx", "-12") 
                .attr("dy", "-2")
                .attr("transform", "rotate(300)");
        }
        this.SVG.select('g._y_axis').call(d3.axisLeft(this.heightScale).ticks(3).tickSize(1).tickFormat(function (e) { return _this.FormatValue(e); }));
        this.SVG.select('g._axis_line').call(d3.axisLeft(this.heightScale).ticks(3).tickSize(-this.LAYOUT.w).tickFormat(function () { return ''; }));
        group.exit().remove();
    };
    return default_1;
}(D3Chart_1.default));
exports.default = default_1;
