'use strict';

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

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _shape = require('@vx/shape');

var _group = require('@vx/group');

var _gradient = require('@vx/gradient');

var _mockData = require('@vx/mock-data');

var _scale = require('@vx/scale');

var _d3Array = require('d3-array');

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

var _jsxFileName = '/Users/hshoff/code/vx/packages/vx-demo/components/tiles/bars.js';


var data = _mockData.letterFrequency.slice(5);

function round(value, precision) {
  var multiplier = Math.pow(10, precision || 0);
  return Math.round(value * multiplier) / multiplier;
}

// accessors
var x = function x(d) {
  return d.letter;
};
var y = function y(d) {
  return +d.frequency * 100;
};

exports.default = function (_ref) {
  var width = _ref.width,
      height = _ref.height,
      _ref$events = _ref.events,
      events = _ref$events === undefined ? false : _ref$events;

  if (width < 10) return null;

  // bounds
  var xMax = width;
  var yMax = height - 120;

  // scales
  var xScale = (0, _scale.scaleBand)({
    rangeRound: [0, xMax],
    domain: data.map(x),
    padding: 0.4
  });
  var yScale = (0, _scale.scaleLinear)({
    rangeRound: [yMax, 0],
    domain: [0, (0, _d3Array.max)(data, y)]
  });

  return _react2.default.createElement('svg', { width: width, height: height, __source: {
      fileName: _jsxFileName,
      lineNumber: 39
    }
  }, _react2.default.createElement(_gradient.GradientTealBlue, { id: 'teal', __source: {
      fileName: _jsxFileName,
      lineNumber: 40
    }
  }), _react2.default.createElement('rect', {
    x: 0,
    y: 0,
    width: width,
    height: height,
    fill: 'url(#teal)',
    rx: 14,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 41
    }
  }), _react2.default.createElement(_group.Group, { top: 40, __source: {
      fileName: _jsxFileName,
      lineNumber: 49
    }
  }, data.map(function (d, i) {
    var barHeight = yMax - yScale(y(d));
    return _react2.default.createElement(_group.Group, { key: 'bar-' + x(d), __source: {
        fileName: _jsxFileName,
        lineNumber: 53
      }
    }, _react2.default.createElement(_shape.Bar, {
      width: xScale.bandwidth(),
      height: barHeight,
      x: xScale(x(d)),
      y: yMax - barHeight,
      fill: 'rgba(23, 233, 217, .5)',
      data: { x: x(d), y: y(d) },
      onClick: function onClick(data) {
        return function (event) {
          if (!events) return;
          alert('clicked: ' + (0, _stringify2.default)(data));
        };
      },
      __source: {
        fileName: _jsxFileName,
        lineNumber: 54
      }
    }));
  })));
};