recharts
Version:
React charts
382 lines (319 loc) • 12.6 kB
JavaScript
'use strict';
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 _class, _class2, _temp; /**
* @fileOverview Cartesian Axis
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _pureRenderDecorator = require('pure-render-decorator');
var _pureRenderDecorator2 = _interopRequireDefault(_pureRenderDecorator);
var _DOMUtils = require('../util/DOMUtils');
var _DOMUtils2 = _interopRequireDefault(_DOMUtils);
var _Layer = require('../container/Layer');
var _Layer2 = _interopRequireDefault(_Layer);
var _ReactUtils = require('../util/ReactUtils');
var _ReactUtils2 = _interopRequireDefault(_ReactUtils);
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 _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 CartesianAxis = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) {
_inherits(CartesianAxis, _Component);
function CartesianAxis() {
_classCallCheck(this, CartesianAxis);
return _possibleConstructorReturn(this, Object.getPrototypeOf(CartesianAxis).apply(this, arguments));
}
_createClass(CartesianAxis, [{
key: 'getTickLineCoord',
/**
* Calculate the coordinates of endpoints in ticks
* @param {Object} data The data of a simple tick
* @return {Object} (x1, y1): The coordinate of endpoint close to label text (x2, y2): The coordinate of endpoint close to axis
*/
value: function getTickLineCoord(data) {
var _props = this.props;
var x = _props.x;
var y = _props.y;
var width = _props.width;
var height = _props.height;
var orientation = _props.orientation;
var tickSize = _props.tickSize;
var x1 = undefined;
var x2 = undefined;
var y1 = undefined;
var y2 = undefined;
var finalTickSize = data.tickSize || tickSize;
switch (orientation) {
case 'top':
x1 = x2 = data.coord;
y1 = y + height - finalTickSize;
y2 = y + height;
break;
case 'left':
y1 = y2 = data.coord;
x1 = x + width - finalTickSize;
x2 = x + width;
break;
case 'right':
y1 = y2 = data.coord;
x1 = x + finalTickSize;
x2 = x;
break;
default:
x1 = x2 = data.coord;
y1 = y + finalTickSize;
y2 = y;
break;
}
return { x1: x1, y1: y1, x2: x2, y2: y2 };
}
}, {
key: 'getBaseline',
value: function getBaseline() {
var orientation = this.props.orientation;
var baseline = undefined;
switch (orientation) {
case 'top':
baseline = 'auto';
break;
case 'bottom':
baseline = 'text-before-edge';
break;
default:
baseline = 'central';
break;
}
return baseline;
}
}, {
key: 'getTickTextAnchor',
value: function getTickTextAnchor() {
var orientation = this.props.orientation;
var textAnchor = undefined;
switch (orientation) {
case 'left':
textAnchor = 'end';
break;
case 'right':
textAnchor = 'start';
break;
default:
textAnchor = 'middle';
break;
}
return textAnchor;
}
}, {
key: 'getDy',
value: function getDy() {
var orientation = this.props.orientation;
var dy = 0;
switch (orientation) {
case 'left':
case 'right':
dy = 8;
break;
case 'top':
dy = -2;
break;
default:
dy = 15;
break;
}
return dy;
}
}, {
key: 'renderAxisLine',
value: function renderAxisLine() {
var _props2 = this.props;
var x = _props2.x;
var y = _props2.y;
var width = _props2.width;
var height = _props2.height;
var orientation = _props2.orientation;
var axisLine = _props2.axisLine;
var props = _extends({}, _ReactUtils2.default.getPresentationAttributes(this.props), {
fill: 'none'
}, _ReactUtils2.default.getPresentationAttributes(axisLine));
switch (orientation) {
case 'top':
props = _extends({}, props, { x1: x, y1: y + height, x2: x + width, y2: y + height });
break;
case 'left':
props = _extends({}, props, { x1: x + width, y1: y, x2: x + width, y2: y + height });
break;
case 'right':
props = _extends({}, props, { x1: x, y1: y, x2: x, y2: y + height });
break;
default:
props = _extends({}, props, { x1: x, y1: y, x2: x + width, y2: y });
break;
}
return _react2.default.createElement('line', _extends({ className: 'recharts-cartesian-axis-line' }, props));
}
}, {
key: 'renderTicks',
value: function renderTicks() {
var _this2 = this;
var _props3 = this.props;
var ticks = _props3.ticks;
var tickFormatter = _props3.tickFormatter;
var tickLine = _props3.tickLine;
var stroke = _props3.stroke;
var label = _props3.label;
if (!ticks || !ticks.length) {
return null;
}
var finalTicks = CartesianAxis.getTicks(this.props);
var textAnchor = this.getTickTextAnchor();
var axisProps = _ReactUtils2.default.getPresentationAttributes(this.props);
var customLabelProps = _ReactUtils2.default.getPresentationAttributes(label);
var isLabelElement = _react2.default.isValidElement(label);
var tickLineProps = _ReactUtils2.default.getPresentationAttributes(tickLine);
var items = finalTicks.map(function (entry, i) {
var lineCoord = _this2.getTickLineCoord(entry);
var tickProps = _extends({}, axisProps, { fill: 'none' }, tickLineProps, lineCoord);
var labelProps = _extends({
dy: _this2.getDy(entry),
textAnchor: textAnchor
}, axisProps, {
stroke: 'none',
fill: stroke
}, customLabelProps, {
index: i,
x: lineCoord.x1,
y: lineCoord.y1,
payload: entry
});
var labelItem = undefined;
if (label) {
labelItem = isLabelElement ? _react2.default.cloneElement(label, labelProps) : _react2.default.createElement(
'text',
_extends({}, labelProps, { className: 'recharts-cartesian-axis-tick-value' }),
tickFormatter ? tickFormatter(entry.value) : entry.value
);
}
return _react2.default.createElement(
'g',
{ className: 'recharts-cartesian-axis-tick', key: 'tick-' + i },
tickLine && _react2.default.createElement('line', _extends({ className: 'recharts-cartesian-axis-tick-line' }, tickProps)),
label && labelItem
);
});
return _react2.default.createElement(
'g',
{ className: 'recharts-cartesian-axis-ticks' },
items
);
}
}, {
key: 'render',
value: function render() {
var _props4 = this.props;
var axisLine = _props4.axisLine;
var width = _props4.width;
var height = _props4.height;
var ticks = _props4.ticks;
if (width <= 0 || height <= 0 || !ticks || !ticks.length) {
return null;
}
return _react2.default.createElement(
_Layer2.default,
{ className: 'recharts-cartesian-axis' },
axisLine && this.renderAxisLine(),
this.renderTicks()
);
}
}], [{
key: 'getTicks',
value: function getTicks(props) {
var ticks = props.ticks;
var viewBox = props.viewBox;
var minLabelGap = props.minLabelGap;
var orientation = props.orientation;
var interval = props.interval;
if (!ticks || !ticks.length) {
return [];
}
return interval === +interval ? CartesianAxis.getNumberIntervalTicks(ticks, interval) : CartesianAxis.getAutoIntervalTicks(ticks, viewBox, orientation, minLabelGap);
}
}, {
key: 'getNumberIntervalTicks',
value: function getNumberIntervalTicks(ticks, interval) {
return ticks.filter(function (entry, i) {
return i % (interval + 1) === 0;
});
}
}, {
key: 'getAutoIntervalTicks',
value: function getAutoIntervalTicks(ticks, viewBox, orientation, minLabelGap) {
var x = viewBox.x;
var y = viewBox.y;
var width = viewBox.width;
var height = viewBox.height;
var sizeKey = orientation === 'top' || orientation === 'bottom' ? 'width' : 'height';
var sign = ticks.length >= 2 ? Math.sign(ticks[1].coord - ticks[0].coord) : 1;
var pointer = undefined;
if (sign === 1) {
pointer = sizeKey === 'width' ? x : y;
} else {
pointer = sizeKey === 'width' ? x + width : y + height;
}
return ticks.filter(function (entry) {
var labelSize = _DOMUtils2.default.getStringSize(entry.value)[sizeKey];
var isShow = sign === 1 ? entry.coord - labelSize / 2 >= pointer : entry.coord + labelSize / 2 <= pointer;
if (isShow) {
pointer = entry.coord + sign * labelSize / 2 + minLabelGap;
}
return isShow;
});
}
}]);
return CartesianAxis;
}(_react.Component), _class2.displayName = 'CartesianAxis', _class2.propTypes = _extends({}, _ReactUtils.PRESENTATION_ATTRIBUTES, {
x: _react.PropTypes.number,
y: _react.PropTypes.number,
width: _react.PropTypes.number,
height: _react.PropTypes.number,
orientation: _react.PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
viewBox: _react.PropTypes.shape({
x: _react.PropTypes.number,
y: _react.PropTypes.number,
width: _react.PropTypes.number,
height: _react.PropTypes.number
}),
label: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object, _react.PropTypes.element]),
axisLine: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object]),
tickLine: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object]),
minLabelGap: _react.PropTypes.number,
ticks: _react.PropTypes.arrayOf(_react.PropTypes.shape({
value: _react.PropTypes.any,
coord: _react.PropTypes.value
})),
tickSize: _react.PropTypes.number,
tickFormatter: _react.PropTypes.func,
interval: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string])
}), _class2.defaultProps = {
x: 0,
y: 0,
width: 0,
height: 0,
viewBox: { x: 0, y: 0, width: 0, height: 0 },
// The orientation of axis
orientation: 'bottom',
// The ticks
ticks: [],
stroke: '#666',
tickLine: true,
axisLine: true,
label: true,
minLabelGap: 5,
// The width or height of tick
tickSize: 6,
interval: 'auto'
}, _temp)) || _class;
exports.default = CartesianAxis;