UNPKG

recharts

Version:
309 lines (260 loc) 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createLabeledScales = exports.ScaleHelper = exports.rectWithCoords = exports.rectWithPoints = exports.formatAxisMap = void 0; var _every2 = _interopRequireDefault(require("lodash/every")); var _mapValues2 = _interopRequireDefault(require("lodash/mapValues")); var _ChartUtils = require("./ChartUtils"); var _ReactUtils = require("./ReactUtils"); var _DataUtils = require("./DataUtils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Calculate the scale function, position, width, height of axes * @param {Object} props Latest props * @param {Object} axisMap The configuration of axes * @param {Object} offset The offset of main part in the svg element * @param {String} axisType The type of axes, x-axis or y-axis * @param {String} chartName The name of chart * @return {Object} Configuration */ var formatAxisMap = function formatAxisMap(props, axisMap, offset, axisType, chartName) { var width = props.width, height = props.height, layout = props.layout, children = props.children; var ids = Object.keys(axisMap); var steps = { left: offset.left, leftMirror: offset.left, right: width - offset.right, rightMirror: width - offset.right, top: offset.top, topMirror: offset.top, bottom: height - offset.bottom, bottomMirror: height - offset.bottom }; var hasBar = !!(0, _ReactUtils.findChildByType)(children, 'Bar'); return ids.reduce(function (result, id) { var axis = axisMap[id]; var orientation = axis.orientation, domain = axis.domain, _axis$padding = axis.padding, padding = _axis$padding === void 0 ? {} : _axis$padding, mirror = axis.mirror, reversed = axis.reversed; var offsetKey = "".concat(orientation).concat(mirror ? 'Mirror' : ''); var calculatedPadding, range, x, y, needSpace; if (axis.type === 'number' && (axis.padding === 'gap' || axis.padding === 'no-gap')) { var diff = domain[1] - domain[0]; var smallestDistanceBetweenValues = Infinity; var sortedValues = axis.categoricalDomain.sort(); sortedValues.forEach(function (value, index) { if (index > 0) { smallestDistanceBetweenValues = Math.min((value || 0) - (sortedValues[index - 1] || 0), smallestDistanceBetweenValues); } }); var smallestDistanceInPercent = smallestDistanceBetweenValues / diff; var rangeWidth = axis.layout === 'vertical' ? offset.height : offset.width; if (axis.padding === 'gap') { calculatedPadding = smallestDistanceInPercent * rangeWidth / 2; } if (axis.padding === 'no-gap') { var gap = (0, _DataUtils.getPercentValue)(props.barCategoryGap, smallestDistanceInPercent * rangeWidth); var halfBand = smallestDistanceInPercent * rangeWidth / 2; calculatedPadding = halfBand - gap - (halfBand - gap) / rangeWidth * gap; } } if (axisType === 'xAxis') { range = [offset.left + (padding.left || 0) + (calculatedPadding || 0), offset.left + offset.width - (padding.right || 0) - (calculatedPadding || 0)]; } else if (axisType === 'yAxis') { range = layout === 'horizontal' ? [offset.top + offset.height - (padding.bottom || 0), offset.top + (padding.top || 0)] : [offset.top + (padding.top || 0) + (calculatedPadding || 0), offset.top + offset.height - (padding.bottom || 0) - (calculatedPadding || 0)]; } else { range = axis.range; } if (reversed) { range = [range[1], range[0]]; } var _parseScale = (0, _ChartUtils.parseScale)(axis, chartName, hasBar), scale = _parseScale.scale, realScaleType = _parseScale.realScaleType; scale.domain(domain).range(range); (0, _ChartUtils.checkDomainOfScale)(scale); var ticks = (0, _ChartUtils.getTicksOfScale)(scale, _objectSpread(_objectSpread({}, axis), {}, { realScaleType: realScaleType })); if (axisType === 'xAxis') { needSpace = orientation === 'top' && !mirror || orientation === 'bottom' && mirror; x = offset.left; y = steps[offsetKey] - needSpace * axis.height; } else if (axisType === 'yAxis') { needSpace = orientation === 'left' && !mirror || orientation === 'right' && mirror; x = steps[offsetKey] - needSpace * axis.width; y = offset.top; } var finalAxis = _objectSpread(_objectSpread(_objectSpread({}, axis), ticks), {}, { realScaleType: realScaleType, x: x, y: y, scale: scale, width: axisType === 'xAxis' ? offset.width : axis.width, height: axisType === 'yAxis' ? offset.height : axis.height }); finalAxis.bandSize = (0, _ChartUtils.getBandSizeOfAxis)(finalAxis, ticks); if (!axis.hide && axisType === 'xAxis') { steps[offsetKey] += (needSpace ? -1 : 1) * finalAxis.height; } else if (!axis.hide) { steps[offsetKey] += (needSpace ? -1 : 1) * finalAxis.width; } return _objectSpread(_objectSpread({}, result), {}, _defineProperty({}, id, finalAxis)); }, {}); }; exports.formatAxisMap = formatAxisMap; var rectWithPoints = function rectWithPoints(_ref, _ref2) { var x1 = _ref.x, y1 = _ref.y; var x2 = _ref2.x, y2 = _ref2.y; return { x: Math.min(x1, x2), y: Math.min(y1, y2), width: Math.abs(x2 - x1), height: Math.abs(y2 - y1) }; }; /** * Compute the x, y, width, and height of a box from two reference points. * @param {Object} coords x1, x2, y1, and y2 * @return {Object} object */ exports.rectWithPoints = rectWithPoints; var rectWithCoords = function rectWithCoords(_ref3) { var x1 = _ref3.x1, y1 = _ref3.y1, x2 = _ref3.x2, y2 = _ref3.y2; return rectWithPoints({ x: x1, y: y1 }, { x: x2, y: y2 }); }; exports.rectWithCoords = rectWithCoords; var ScaleHelper = /*#__PURE__*/function () { function ScaleHelper(scale) { _classCallCheck(this, ScaleHelper); this.scale = void 0; this.scale = scale; } _createClass(ScaleHelper, [{ key: "domain", get: function get() { return this.scale.domain; } }, { key: "range", get: function get() { return this.scale.range; } }, { key: "rangeMin", get: function get() { return this.range()[0]; } }, { key: "rangeMax", get: function get() { return this.range()[1]; } }, { key: "bandwidth", get: function get() { return this.scale.bandwidth; } }, { key: "apply", value: function apply(value) { var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, bandAware = _ref4.bandAware, position = _ref4.position; if (value === undefined) { return undefined; } if (position) { switch (position) { case 'start': { return this.scale(value); } case 'middle': { var offset = this.bandwidth ? this.bandwidth() / 2 : 0; return this.scale(value) + offset; } case 'end': { var _offset = this.bandwidth ? this.bandwidth() : 0; return this.scale(value) + _offset; } default: { return this.scale(value); } } } if (bandAware) { var _offset2 = this.bandwidth ? this.bandwidth() / 2 : 0; return this.scale(value) + _offset2; } return this.scale(value); } }, { key: "isInRange", value: function isInRange(value) { var range = this.range(); var first = range[0]; var last = range[range.length - 1]; return first <= last ? value >= first && value <= last : value >= last && value <= first; } }], [{ key: "create", value: function create(obj) { return new ScaleHelper(obj); } }]); return ScaleHelper; }(); exports.ScaleHelper = ScaleHelper; ScaleHelper.EPS = 1e-4; var createLabeledScales = function createLabeledScales(options) { var scales = Object.keys(options).reduce(function (res, key) { return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, key, ScaleHelper.create(options[key]))); }, {}); return _objectSpread(_objectSpread({}, scales), {}, { apply: function apply(coord) { var _ref5 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, bandAware = _ref5.bandAware, position = _ref5.position; return (0, _mapValues2["default"])(coord, function (value, label) { return scales[label].apply(value, { bandAware: bandAware, position: position }); }); }, isInRange: function isInRange(coord) { return (0, _every2["default"])(coord, function (value, label) { return scales[label].isInRange(value); }); } }); }; exports.createLabeledScales = createLabeledScales;