'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; }; }();

require('../../../styles/components/area-chart.css');

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _PlotArea = require('./PlotArea');

var _PlotArea2 = _interopRequireDefault(_PlotArea);

var _Axis = require('./Axis');

var _Axis2 = _interopRequireDefault(_Axis);

var _ToolTip = require('./ToolTip');

var _ToolTip2 = _interopRequireDefault(_ToolTip);

var _Dots = require('../Chart/Dots');

var _Dots2 = _interopRequireDefault(_Dots);

var _ChartOverlay = require('../Chart/ChartOverlay');

var _ChartOverlay2 = _interopRequireDefault(_ChartOverlay);

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

var ChartUtils = _interopRequireWildcard(_utils);

var _d3Shape = require('d3-shape');

var d3Shape = _interopRequireWildcard(_d3Shape);

var _d3Format = require('d3-format');

var d3Format = _interopRequireWildcard(_d3Format);

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

var d3Axis = _interopRequireWildcard(_d3Axis);

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({}, d3Shape, d3Format, d3Axis);

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

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

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

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

  _createClass(AreaChart, [{
    key: 'mouseMove',
    value: function mouseMove(toolTipData, toolTipPos) {
      this.setState({
        tooltip: {
          display: true,
          data: toolTipData,
          pos: toolTipPos
        }
      });
    }
  }, {
    key: 'showToolTip',
    value: function showToolTip(e) {
      this.setState({
        tooltip: {
          display: true,
          data: {
            key: e.target.getAttribute('data-key'),
            value: e.target.getAttribute('data-value')
          },
          pos: {
            x: e.target.getAttribute('cx'),
            y: e.target.getAttribute('cy')
          }
        }
      });
    }
  }, {
    key: 'hideToolTip',
    value: function hideToolTip() {
      this.setState({
        tooltip: {
          display: false,
          data: {}
        }
      });
    }
  }, {
    key: 'renderToolTip',
    value: function renderToolTip(tooltipContentType) {
      var _this2 = this;

      return _react2.default.createElement(
        _ToolTip2.default,
        { tooltip: this.state.tooltip },
        tooltipContentType.map(function (type, index) {
          if (type === 'up') {
            return _react2.default.createElement(
              'g',
              { key: index },
              _react2.default.createElement('polygon', { fill: '#A676B2', points: '38,-3 34,1 42,1' }),
              _react2.default.createElement(
                'text',
                null,
                _react2.default.createElement(
                  'tspan',
                  {
                    className: type,
                    textAnchor: 'middle',
                    x: '58px',
                    y: '2px',
                    fontSize: '11px',
                    fill: '#A676B2'
                  },
                  _this2.state.tooltip.data[type]
                )
              )
            );
          } else if (type === 'down') {
            return _react2.default.createElement(
              'g',
              { key: index },
              _react2.default.createElement('polygon', { fill: '#A676B2', points: '38,17 34,13 42,13' }),
              _react2.default.createElement(
                'text',
                null,
                _react2.default.createElement(
                  'tspan',
                  {
                    className: type,
                    textAnchor: 'middle',
                    x: '60px',
                    y: '18px',
                    fontSize: '11px',
                    fill: '#A676B2'
                  },
                  _this2.state.tooltip.data[type]
                )
              )
            );
          }
          return _react2.default.createElement(
            'g',
            { key: index },
            _react2.default.createElement(
              'text',
              null,
              _react2.default.createElement(
                'tspan',
                {
                  className: type,
                  textAnchor: 'middle',
                  x: '-20px',
                  y: '15px',
                  fontSize: '28px',
                  fill: '#A676B2'
                },
                _this2.state.tooltip.data[type]
              )
            )
          );
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          className = _props.className,
          data = _props.data,
          dotVisible = _props.dotVisible,
          height = _props.height,
          hideAxisX = _props.hideAxisX,
          hideAxisY = _props.hideAxisY,
          tooltipVisible = _props.tooltipVisible,
          tooltipContentType = _props.tooltipContentType,
          width = _props.width,
          xDataType = _props.xDataType,
          yBuffer = _props.yBuffer,
          rest = _objectWithoutProperties(_props, ['className', 'data', 'dotVisible', 'height', 'hideAxisX', 'hideAxisY', 'tooltipVisible', 'tooltipContentType', 'width', 'xDataType', 'yBuffer']);

      var margin = { top: 5, right: 50, bottom: 20, left: 50 };
      var innerWidth = width - (margin.left + margin.right);
      var innerHeight = height - (margin.top + margin.bottom);
      var xAxis = null;
      var xScale = null;
      var xScaleAxis = null;

      if (xDataType === 'date') {
        var parseDate = d3.timeParse('%m-%d-%Y %H:%M:%S');
        data.forEach(function (d) {
          d.x = parseDate(d.x);
        });
        xScale = ChartUtils.xScaleTime(data, width);
        xScaleAxis = ChartUtils.xScaleTime(data, width - 40);
        xAxis = d3.axisBottom().scale(xScaleAxis).tickValues(data.map(function (d) {
          return d.x;
        })).ticks(5).tickFormat(d3.timeFormat('%a'));
      } else {
        xScale = ChartUtils.xScaleLinear(data, width);
        xScaleAxis = ChartUtils.xScaleLinear(data, width - 40);
        xAxis = d3.axisBottom().scale(xScaleAxis).tickValues(data.map(function (d) {
          return d.x;
        })).ticks(5);
      }

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

      var yAxis = d3.axisLeft().scale(yScale).ticks(5);

      return _react2.default.createElement(
        'div',
        _extends({
          className: (0, _classnames2.default)('octagon', 'area-chart__container', className)
        }, rest),
        _react2.default.createElement(
          'svg',
          { width: width, height: innerHeight },
          _react2.default.createElement(
            'g',
            { transform: 'translate(2, 4)' },
            _react2.default.createElement(_PlotArea2.default, _extends({}, this.props, {
              data: data,
              xScale: xScale,
              yScale: yScale
            })),
            hideAxisX ? '' : _react2.default.createElement(_Axis2.default, {
              height: innerHeight,
              width: innerWidth - 10,
              axis: xAxis,
              axisType: 'x',
              orientation: 'bottom'
            }),
            hideAxisY ? '' : _react2.default.createElement(_Axis2.default, {
              height: innerHeight,
              axis: yAxis,
              axisType: 'y',
              orientation: 'left'
            }),
            tooltipVisible ? this.renderToolTip(tooltipContentType) : null,
            dotVisible ? _react2.default.createElement(_Dots2.default, {
              data: data,
              showToolTip: this.showToolTip,
              hideToolTip: this.hideToolTip,
              xDataType: xDataType,
              xScale: xScale,
              yScale: yScale
            }) : '',
            tooltipVisible ? _react2.default.createElement(_ChartOverlay2.default, {
              data: data,
              height: innerHeight,
              hideToolTip: this.hideToolTip,
              mouseMove: this.mouseMove,
              width: width,
              xDataType: xDataType,
              xScale: xScale,
              yScale: yScale
            }) : ''
          )
        )
      );
    }
  }]);

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

AreaChart.propTypes = {
  data: _propTypes2.default.array.isRequired,
  dotVisible: _propTypes2.default.bool,
  height: _propTypes2.default.number,
  hideAxisX: _propTypes2.default.bool,
  hideAxisY: _propTypes2.default.bool,
  tooltipContentType: _propTypes2.default.array.isRequired,
  tooltipVisible: _propTypes2.default.bool,
  width: _propTypes2.default.number,
  xDataType: _propTypes2.default.string.isRequired,
  yBuffer: _propTypes2.default.number.isRequired
};

AreaChart.defaultProps = {
  color: '#00A4D8',
  dotVisible: true,
  height: 150,
  padding: 20,
  tooltipContentType: ['x', 'y'],
  tooltipVisible: true,
  width: 500,
  xDataType: 'number',
  yBuffer: 0
};

AreaChart.Axis = _Axis2.default;
AreaChart.PlotArea = _PlotArea2.default;
exports.default = AreaChart;
//# sourceMappingURL=AreaChart.js.map