react-vis
Version:
Data visualization library based on React and d3.
168 lines (159 loc) • 6.03 kB
JavaScript
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; }
// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import React from 'react';
import PropTypes from 'prop-types';
import { ORIENTATION } from "../../utils/axis-utils"; // Assuming that 16px = 1em
var ADJUSTMENT_FOR_TEXT_SIZE = 16;
var MARGIN = 6;
var LEFT = ORIENTATION.LEFT,
RIGHT = ORIENTATION.RIGHT,
TOP = ORIENTATION.TOP,
BOTTOM = ORIENTATION.BOTTOM;
var defaultProps = {
position: 'end'
};
/**
* Compute transformations, keyed by orientation
* @param {number} width - width of axis
* @param {number} height - height of axis
* @returns {Object} Object of transformations, keyed by orientation
*/
var transformation = function transformation(width, height) {
var _ref;
return _ref = {}, _defineProperty(_ref, LEFT, {
end: {
x: ADJUSTMENT_FOR_TEXT_SIZE,
y: MARGIN,
rotation: -90,
textAnchor: 'end'
},
middle: {
x: ADJUSTMENT_FOR_TEXT_SIZE,
y: height / 2 - MARGIN,
rotation: -90,
textAnchor: 'middle'
},
start: {
x: ADJUSTMENT_FOR_TEXT_SIZE,
y: height - MARGIN,
rotation: -90,
textAnchor: 'start'
}
}), _defineProperty(_ref, RIGHT, {
end: {
x: ADJUSTMENT_FOR_TEXT_SIZE * -0.5,
y: MARGIN,
rotation: -90,
textAnchor: 'end'
},
middle: {
x: ADJUSTMENT_FOR_TEXT_SIZE * -0.5,
y: height / 2 - MARGIN,
rotation: -90,
textAnchor: 'middle'
},
start: {
x: ADJUSTMENT_FOR_TEXT_SIZE * -0.5,
y: height - MARGIN,
rotation: -90,
textAnchor: 'start'
}
}), _defineProperty(_ref, TOP, {
start: {
x: MARGIN,
y: ADJUSTMENT_FOR_TEXT_SIZE,
rotation: 0,
textAnchor: 'start'
},
middle: {
x: width / 2 - MARGIN,
y: ADJUSTMENT_FOR_TEXT_SIZE,
rotation: 0,
textAnchor: 'middle'
},
end: {
x: width - MARGIN,
y: ADJUSTMENT_FOR_TEXT_SIZE,
rotation: 0,
textAnchor: 'end'
}
}), _defineProperty(_ref, BOTTOM, {
start: {
x: MARGIN,
y: -MARGIN,
rotation: 0,
textAnchor: 'start'
},
middle: {
x: width / 2 - MARGIN,
y: -MARGIN,
rotation: 0,
textAnchor: 'middle'
},
end: {
x: width - MARGIN,
y: -MARGIN,
rotation: 0,
textAnchor: 'end'
}
}), _ref;
};
var propTypes = {
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
orientation: PropTypes.oneOf([LEFT, RIGHT, TOP, BOTTOM]).isRequired,
style: PropTypes.object,
title: PropTypes.string.isRequired
};
function AxisTitle(_ref2) {
var orientation = _ref2.orientation,
position = _ref2.position,
width = _ref2.width,
height = _ref2.height,
style = _ref2.style,
title = _ref2.title;
var outerGroupTranslateX = orientation === LEFT ? width : 0;
var outerGroupTranslateY = orientation === TOP ? height : 0;
var outerGroupTransform = "translate(".concat(outerGroupTranslateX, ", ").concat(outerGroupTranslateY, ")");
var _transformation$orien = transformation(width, height)[orientation][position],
x = _transformation$orien.x,
y = _transformation$orien.y,
rotation = _transformation$orien.rotation,
textAnchor = _transformation$orien.textAnchor;
var innerGroupTransform = "translate(".concat(x, ", ").concat(y, ") rotate(").concat(rotation, ")");
return /*#__PURE__*/React.createElement("g", {
transform: outerGroupTransform,
className: "rv-xy-plot__axis__title"
}, /*#__PURE__*/React.createElement("g", {
style: _objectSpread({
textAnchor: textAnchor
}, style),
transform: innerGroupTransform
}, /*#__PURE__*/React.createElement("text", {
style: style
}, title)));
}
AxisTitle.displayName = 'AxisTitle';
AxisTitle.propTypes = propTypes;
AxisTitle.defaultProps = defaultProps;
export default AxisTitle;