recharts
Version:
React charts
286 lines (230 loc) • 11.1 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, _temp; /**
* @fileOverview Line Chart
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _Surface = require('../container/Surface');
var _Surface2 = _interopRequireDefault(_Surface);
var _Tooltip = require('../component/Tooltip');
var _Tooltip2 = _interopRequireDefault(_Tooltip);
var _Curve = require('../shape/Curve');
var _Curve2 = _interopRequireDefault(_Curve);
var _Dot = require('../shape/Dot');
var _Dot2 = _interopRequireDefault(_Dot);
var _CartesianChart2 = require('./CartesianChart');
var _CartesianChart3 = _interopRequireDefault(_CartesianChart2);
var _Line = require('../cartesian/Line');
var _Line2 = _interopRequireDefault(_Line);
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 LineChart = (_temp = _class = function (_CartesianChart) {
_inherits(LineChart, _CartesianChart);
function LineChart() {
_classCallCheck(this, LineChart);
return _possibleConstructorReturn(this, Object.getPrototypeOf(LineChart).apply(this, arguments));
}
_createClass(LineChart, [{
key: 'getComposedData',
/**
* Compose the data of each group
* @param {Object} xAxis The configuration of x-axis
* @param {Object} yAxis The configuration of y-axis
* @param {String} dataKey The unique key of a group
* @return {Array} Composed data
*/
value: function getComposedData(xAxis, yAxis, dataKey) {
var _props = this.props;
var data = _props.data;
var layout = _props.layout;
var xTicks = this.getAxisTicks(xAxis);
var yTicks = this.getAxisTicks(yAxis);
return data.map(function (entry, index) {
return {
x: layout === 'horizontal' ? xTicks[index].coord : xAxis.scale(entry[dataKey]),
y: layout === 'horizontal' ? yAxis.scale(entry[dataKey]) : yTicks[index].coord,
value: entry[dataKey]
};
});
}
/**
* Handler of mouse entering line chart
* @param {String} key The unique key of a group of data
* @return {Object} no return
*/
}, {
key: 'handleLineMouseEnter',
value: function handleLineMouseEnter(key) {
this.setState({
activeLineKey: key
});
}
/**
* Handler of mouse leaving line chart
* @return {Object} no return
*/
}, {
key: 'handleLineMouseLeave',
value: function handleLineMouseLeave() {
this.setState({
activeLineKey: null
});
}
}, {
key: 'renderCursor',
value: function renderCursor(xAxisMap, yAxisMap, offset) {
var children = this.props.children;
var tooltipItem = _ReactUtils2.default.findChildByType(children, _Tooltip2.default);
if (!tooltipItem || !tooltipItem.props.cursor || !this.state.isTooltipActive) {
return null;
}
var layout = this.props.layout;
var activeTooltipIndex = this.state.activeTooltipIndex;
var axisMap = layout === 'horizontal' ? xAxisMap : yAxisMap;
var ids = Object.keys(axisMap);
var axis = axisMap[ids[0]];
var ticks = this.getAxisTicks(axis);
var start = ticks[activeTooltipIndex].coord;
var x1 = layout === 'horizontal' ? start : offset.left;
var y1 = layout === 'horizontal' ? offset.top : start;
var x2 = layout === 'horizontal' ? start : offset.left + offset.width;
var y2 = layout === 'horizontal' ? offset.top + offset.height : start;
var cursorProps = _extends({
stroke: '#ccc'
}, _ReactUtils2.default.getPresentationAttributes(tooltipItem.props.cursor), {
points: [{ x: x1, y: y1 }, { x: x2, y: y2 }]
});
return _react2.default.isValidElement(tooltipItem.props.cursor) ? _react2.default.cloneElement(tooltipItem.props.cursor, cursorProps) : _react2.default.createElement(_Curve2.default, _extends({}, cursorProps, { type: 'linear', className: 'recharts-tooltip-cursor' }));
}
/**
* Draw the main part of line chart
* @param {Array} items All the instance of Line
* @param {Object} xAxisMap The configuration of all x-axes
* @param {Object} yAxisMap The configuration of all y-axes
* @param {Object} offset The offset of main part in the svg element
* @return {ReactComponent} All the instances of Line
*/
}, {
key: 'renderItems',
value: function renderItems(items, xAxisMap, yAxisMap, offset) {
var _this2 = this;
var children = this.props.children;
var _state = this.state;
var activeLineKey = _state.activeLineKey;
var isTooltipActive = _state.isTooltipActive;
var activeTooltipIndex = _state.activeTooltipIndex;
var tooltipItem = _ReactUtils2.default.findChildByType(children, _Tooltip2.default);
var hasDot = tooltipItem && isTooltipActive;
var dotItems = [];
var lineItems = items.map(function (child, i) {
var _child$props = child.props;
var xAxisId = _child$props.xAxisId;
var yAxisId = _child$props.yAxisId;
var dataKey = _child$props.dataKey;
var strokeWidth = _child$props.strokeWidth;
var stroke = _child$props.stroke;
var points = _this2.getComposedData(xAxisMap[xAxisId], yAxisMap[yAxisId], dataKey);
var activePoint = points[activeTooltipIndex];
var pointStyle = { fill: stroke, strokeWidth: 2, stroke: '#fff' };
var finalStrokeWidth = strokeWidth === +strokeWidth ? strokeWidth : 1;
finalStrokeWidth = activeLineKey === dataKey ? finalStrokeWidth + 2 : finalStrokeWidth;
if (hasDot && activePoint) {
dotItems.push(_react2.default.createElement(_Dot2.default, _extends({ key: 'area-dot-' + i, cx: activePoint.x, cy: activePoint.y, r: 4 }, pointStyle)));
}
return _react2.default.cloneElement(child, _extends({
key: 'line-' + i
}, offset, {
strokeWidth: finalStrokeWidth,
onMouseLeave: _this2.handleLineMouseLeave.bind(_this2),
onMouseEnter: _this2.handleLineMouseEnter.bind(_this2, dataKey),
points: points
}));
}, this);
return _react2.default.createElement(
'g',
{ key: 'recharts-line-wrapper' },
_react2.default.createElement(
'g',
{ key: 'recharts-line' },
lineItems
),
_react2.default.createElement(
'g',
{ key: 'recharts-line-dot' },
dotItems
)
);
}
}, {
key: 'render',
value: function render() {
if (!_ReactUtils2.default.validateWidthHeight(this)) {
return null;
}
var _props2 = this.props;
var style = _props2.style;
var children = _props2.children;
var className = _props2.className;
var width = _props2.width;
var height = _props2.height;
var items = _ReactUtils2.default.findAllByType(children, _Line2.default);
var xAxisMap = this.getAxisMap('xAxis', items);
var yAxisMap = this.getAxisMap('yAxis', items);
var offset = this.getOffset(xAxisMap, yAxisMap);
xAxisMap = this.getFormatAxisMap(xAxisMap, offset, 'xAxis');
yAxisMap = this.getFormatAxisMap(yAxisMap, offset, 'yAxis');
return _react2.default.createElement(
'div',
{ className: (0, _classnames2.default)('recharts-wrapper', className),
style: _extends({ position: 'relative', cursor: 'default' }, style),
onMouseEnter: this.handleMouseEnter.bind(this, offset, xAxisMap, yAxisMap),
onMouseMove: this.handleMouseMove.bind(this, offset, xAxisMap, yAxisMap),
onMouseLeave: this.handleMouseLeave.bind(this)
},
_react2.default.createElement(
_Surface2.default,
{ width: width, height: height },
this.renderGrid(xAxisMap, yAxisMap, offset),
this.renderReferenceLines(xAxisMap, yAxisMap, offset),
this.renderXAxis(xAxisMap),
this.renderYAxis(yAxisMap),
this.renderCursor(xAxisMap, yAxisMap, offset),
this.renderItems(items, xAxisMap, yAxisMap, offset)
),
this.renderLegend(items),
this.renderTooltip(items, offset)
);
}
}]);
return LineChart;
}(_CartesianChart3.default), _class.displayName = 'LineChart', _class.propTypes = {
width: _react.PropTypes.number,
height: _react.PropTypes.number,
data: _react.PropTypes.arrayOf(_react.PropTypes.object),
layout: _react.PropTypes.oneOf(['horizontal', 'vertical']),
margin: _react.PropTypes.shape({
top: _react.PropTypes.number,
right: _react.PropTypes.number,
bottom: _react.PropTypes.number,
left: _react.PropTypes.number
}),
className: _react.PropTypes.string,
style: _react.PropTypes.object,
children: _react.PropTypes.oneOfType([_react.PropTypes.arrayOf(_react.PropTypes.node), _react.PropTypes.node])
}, _class.defaultProps = {
style: {},
layout: 'horizontal',
margin: { top: 5, right: 5, bottom: 5, left: 5 }
}, _temp);
exports.default = LineChart;