'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Rect = require('./Rect');

var _Rect2 = _interopRequireDefault(_Rect);

var _BarChartTooltip = require('./BarChartTooltip');

var _BarChartTooltip2 = _interopRequireDefault(_BarChartTooltip);

var _utils = require('../Chart/utils');

var ChartUtils = _interopRequireWildcard(_utils);

var _d3Axis = require('d3-axis');

var axis = _interopRequireWildcard(_d3Axis);

var _d3TimeFormat = require('d3-time-format');

var timeFormat = _interopRequireWildcard(_d3TimeFormat);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var d3 = Object.assign({}, axis, timeFormat);
var array = _propTypes2.default.array,
    number = _propTypes2.default.number,
    object = _propTypes2.default.object,
    string = _propTypes2.default.string,
    bool = _propTypes2.default.bool;

var BarChart = function (_React$Component) {
  _inherits(BarChart, _React$Component);

  function BarChart(props) {
    _classCallCheck(this, BarChart);

    var _this = _possibleConstructorReturn(this, (BarChart.__proto__ || Object.getPrototypeOf(BarChart)).call(this, props));

    _this.state = {
      tooltip: {
        display: false,
        color: null,
        title: '',
        height: '',
        data: { key: '', value: '' },
        pos: { x: '', y: '' }
      }
    };
    _this.showToolTip = _this.showToolTip.bind(_this);
    _this.hideToolTip = _this.hideToolTip.bind(_this);
    return _this;
  }

  _createClass(BarChart, [{
    key: 'showToolTip',
    value: function showToolTip(e) {
      this.setState({
        tooltip: {
          display: true,
          color: e.target.getAttribute('fill'),
          title: this.props.tooltipTitle,
          height: e.target.getAttribute('data-tooltip-pos') - 35,
          data: {
            key: e.target.getAttribute('data-key'),
            value: e.target.getAttribute('data-value')
          },
          pos: {
            x: e.target.getAttribute('data-tooltip-x'),
            y: e.target.getAttribute('data-tooltip-y')
          }
        }
      });
    }
  }, {
    key: 'hideToolTip',
    value: function hideToolTip() {
      this.setState({
        tooltip: {
          display: false,
          data: { key: '', value: '' }
        }
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          barPadding = _props.barPadding,
          borderRadius = _props.borderRadius,
          className = _props.className,
          rawData = _props.data,
          height = _props.height,
          margin = _props.margin,
          width = _props.width,
          xDataType = _props.xDataType,
          showXLabel = _props.showXLabel,
          showIcon = _props.showIcon,
          XLabelFontSize = _props.XLabelFontSize,
          tooltipTitle = _props.tooltipTitle,
          rest = _objectWithoutProperties(_props, ['barPadding', 'borderRadius', 'className', 'data', 'height', 'margin', 'width', 'xDataType', 'showXLabel', 'showIcon', 'XLabelFontSize', 'tooltipTitle']);

      var innerWidth = width - (margin.left + margin.right);
      var innerHeight = height - (margin.top + margin.bottom);
      var transform = 'translate(-' + margin.left + ', ' + margin.top + ')';
      var xScale = null;
      var data = JSON.parse(JSON.stringify(rawData));
      var xScaleTimeLineData = JSON.parse(JSON.stringify(rawData));

      if (this.props.xDataType === 'date') {
        var parseDate = d3.timeParse('%m-%d-%Y %H:%M:%S');
        data.forEach(function (d) {
          d.x = parseDate(d.x);
        });
        xScaleTimeLineData.forEach(function (d) {
          d.x = parseDate(d.x);
        });

        xScale = ChartUtils.xScaleBand(data, innerWidth, barPadding);
      } else {
        xScale = ChartUtils.xScaleLinear(data, innerWidth, barPadding);
      }

      var yScale = ChartUtils.yScaleLinear(data, innerHeight, 0);

      return _react2.default.createElement(
        'div',
        _extends({
          className: (0, _classnames2.default)('bar-chart__container', 'svg-overflow-visible', className)
        }, rest),
        _react2.default.createElement(
          'svg',
          { width: width, height: height },
          _react2.default.createElement(
            'g',
            { transform: transform },
            _react2.default.createElement(_Rect2.default, {
              borderRadius: borderRadius,
              data: data,
              fill: '#4898da',
              hideToolTip: this.hideToolTip,
              innerHeight: innerHeight,
              showToolTip: this.showToolTip,
              transform: transform,
              xScale: xScale,
              yScale: yScale,
              xDataType: xDataType,
              showXLabel: showXLabel,
              showIcon: showIcon,
              XLabelFontSize: XLabelFontSize
            }),
            _react2.default.createElement(_BarChartTooltip2.default, { tooltip: this.state.tooltip, yScale: yScale })
          )
        )
      );
    }
  }]);

  return BarChart;
}(_react2.default.Component);

BarChart.propTypes = {
  barPadding: number,
  borderRadius: number,
  data: array.isRequired,
  height: number,
  margin: object,
  width: number,
  xDataType: string,
  showXLabel: bool,
  showIcon: bool,
  tooltipTitle: string,
  XLabelFontSize: string
};

BarChart.defaultProps = {
  barPadding: 0.1,
  borderRadius: 3,
  height: 70,
  margin: { top: 0, right: 5, bottom: -5, left: 10 },
  width: 200,
  xDataType: 'month',
  showXLabel: false,
  showIcon: false,
  tooltipTitle: '',
  XLabelFontSize: '16'
};

exports.default = BarChart;
//# sourceMappingURL=BarChart.js.map