recharts
Version:
React charts
219 lines (192 loc) • 8.4 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _LodashUtils = require('./LodashUtils');
var _LodashUtils2 = _interopRequireDefault(_LodashUtils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var PRESENTATION_ATTRIBUTES = {
alignmentBaseline: _react.PropTypes.string,
baselineShift: _react.PropTypes.string,
clip: _react.PropTypes.string,
clipPath: _react.PropTypes.string,
clipRule: _react.PropTypes.string,
color: _react.PropTypes.string,
colorInterpolation: _react.PropTypes.string,
colorInterpolationFilters: _react.PropTypes.string,
colorProfile: _react.PropTypes.string,
colorRendering: _react.PropTypes.string,
cursor: _react.PropTypes.string,
direction: _react.PropTypes.oneOf(['ltr', 'rtl', 'inherit']),
display: _react.PropTypes.string,
dominantBaseline: _react.PropTypes.string,
enableBackground: _react.PropTypes.string,
fill: _react.PropTypes.string,
fillOpacity: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.number]),
fillRule: _react.PropTypes.oneOf(['nonzero', 'evenodd', 'inherit']),
filter: _react.PropTypes.string,
floodColor: _react.PropTypes.string,
floodOpacity: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.number]),
font: _react.PropTypes.string,
fontFamily: _react.PropTypes.string,
fontSize: _react.PropTypes.number,
fontSizeAdjust: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
fontStretch: _react.PropTypes.oneOf(['normal', 'wider', 'narrower', 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded', 'inherit']),
fontStyle: _react.PropTypes.oneOf(['normal', 'italic', 'oblique', 'inherit']),
fontVariant: _react.PropTypes.oneOf(['normal', 'small-caps', 'inherit']),
fontWeight: _react.PropTypes.oneOf(['normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900, 'inherit']),
glyphOrientationHorizontal: _react.PropTypes.string,
glyphOrientationVertical: _react.PropTypes.string,
imageRendering: _react.PropTypes.oneOf(['auto', 'optimizeSpeed', 'optimizeQuality', 'inherit']),
kerning: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
letterSpacing: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
lightingColor: _react.PropTypes.string,
markerEnd: _react.PropTypes.string,
markerMid: _react.PropTypes.string,
markerStart: _react.PropTypes.string,
mask: _react.PropTypes.string,
opacity: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
overflow: _react.PropTypes.oneOf(['visible', 'hidden', 'scroll', 'auto', 'inherit']),
pointerEvents: _react.PropTypes.oneOf(['visiblePainted', 'visibleFill', 'visibleStroke', 'visible', 'painted', 'fill', 'stroke', 'all', 'none', 'inherit']),
shapeRendering: _react.PropTypes.oneOf(['auto', 'optimizeSpeed', 'crispEdges', 'geometricPrecision', 'inherit']),
stopColor: _react.PropTypes.string,
stopOpacity: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
stroke: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
strokeDasharray: _react.PropTypes.string,
strokeDashoffset: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
strokeLinecap: _react.PropTypes.oneOf(['butt', 'round', 'square', 'inherit']),
strokeLinejoin: _react.PropTypes.oneOf(['miter', 'round', 'bevel', 'inherit']),
strokeMiterlimit: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
strokeOpacity: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
strokeWidth: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
textAnchor: _react.PropTypes.oneOf(['start', 'middle', 'end', 'inherit']),
textDecoration: _react.PropTypes.oneOf(['none', 'underline', 'overline', 'line-through', 'blink', 'inherit']),
textRendering: _react.PropTypes.oneOf(['auto', 'optimizeSpeed', 'optimizeLegibility', 'geometricPrecision', 'inherit']),
unicodeBidi: _react.PropTypes.oneOf(['normal', 'embed', 'bidi-override', 'inherit']),
visibility: _react.PropTypes.oneOf(['visible', 'hidden', 'collapse', 'inherit']),
wordSpacing: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
writingMode: _react.PropTypes.oneOf(['lr-tb', 'rl-tb', 'tb-rl', 'lr', 'rl', 'tb', 'inherit']),
transform: _react.PropTypes.string,
style: _react.PropTypes.object
};
var EVENT_ATTRIBUTES = {
onActivate: _react.PropTypes.func,
onClick: _react.PropTypes.func,
onFocusin: _react.PropTypes.func,
onFocusout: _react.PropTypes.func,
onload: _react.PropTypes.func,
onMouseDown: _react.PropTypes.func,
onMouseMove: _react.PropTypes.func,
onMouseOut: _react.PropTypes.func,
onMouseOver: _react.PropTypes.func,
onMouseUp: _react.PropTypes.func
};
exports.default = {
PRESENTATION_ATTRIBUTES: PRESENTATION_ATTRIBUTES,
/*
* Find and return all matched children by type. `type` can be a React element class or
* string
*/
findAllByType: function findAllByType(children, type) {
var result = [];
var childType = type;
if (type && type.displayName) {
childType = type.displayName;
}
_react2.default.Children.forEach(children, function (child) {
if (child && child.type && child.type.displayName === childType) {
result.push(child);
}
});
return result;
},
/*
* Return the first matched child by type, return null otherwise.
* `type` can be a React element class or string.
*/
findChildByType: function findChildByType(children, type) {
var result = this.findAllByType(children, type);
return result && result[0];
},
/*
* Create a new array of children excluding the ones matched the type
*/
withoutType: function withoutType(children) {
var newChildren = [];
var types = [].slice.call(arguments, 1);
types = types.map(function (type) {
if (type && type.displayName) {
return type.displayName;
}
return type;
});
_react2.default.Children.forEach(children, function (child) {
if (child && child.type && child.type.displayName && types.indexOf(child.type.displayName) !== -1) {
return;
}
newChildren.push(child);
});
return newChildren;
},
/**
* get all the presentation attribute of svg element
* @param {Object} el A react element or the props of a react element
* @return {Object} attributes or null
*/
getPresentationAttributes: function getPresentationAttributes(el) {
if (!el) {
return null;
}
var props = _react2.default.isValidElement(el) ? el.props : el;
var result = null;
for (var key in props) {
if (props.hasOwnProperty(key) && PRESENTATION_ATTRIBUTES[key]) {
if (!result) {
result = {};
}
result[key] = props[key];
}
}
return result;
},
/**
* get all the event attribute of svg element
* @param {Object} el A react element or the props of a react element
* @return {Object} attributes or null
*/
getEventAttributes: function getEventAttributes(el) {
if (!el) {
return null;
}
var props = _react2.default.isValidElement(el) ? el.props : el;
var result = null;
for (var key in props) {
if (props.hasOwnProperty(key) && EVENT_ATTRIBUTES[key]) {
if (!result) {
result = {};
}
result[key] = props[key];
}
}
return result;
},
/**
* validate the width and height props of a chart element
* @param {Object} el A chart element
* @return {Boolean} true If the props width and height are number, and greater than 0
*/
validateWidthHeight: function validateWidthHeight(el) {
if (!el || !el.props) {
return false;
}
var _el$props = el.props;
var width = _el$props.width;
var height = _el$props.height;
if (!_LodashUtils2.default.isNumber(width) || width <= 0 || !_LodashUtils2.default.isNumber(height) || height <= 0) {
return false;
}
return true;
}
};