@symblight/react-stockcharts
Version:
Highly customizable stock charts with ReactJS and d3
253 lines (212 loc) • 11.2 kB
JavaScript
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
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; }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
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; }
import React from "react";
import PropTypes from "prop-types";
import { isDefined, noop } from "../utils";
import { getValueFromOverride, saveNodeType, isHoverForInteractiveType } from "./utils";
import GenericChartComponent from "../GenericChartComponent";
import { getMouseCanvas } from "../GenericComponent";
import HoverTextNearMouse from "./components/HoverTextNearMouse";
import EachShape from "./wrapper/EachShape";
var Shape = function (_React$Component) {
_inherits(Shape, _React$Component);
function Shape() {
_classCallCheck(this, Shape);
var _this = _possibleConstructorReturn(this, (Shape.__proto__ || Object.getPrototypeOf(Shape)).call(this));
_this.handleDraw = _this.handleDraw.bind(_this);
_this.handleDrag = _this.handleDrag.bind(_this);
_this.handleDragComplete = _this.handleDragComplete.bind(_this);
_this.saveNodeType = saveNodeType.bind(_this);
_this.getSelectionState = isHoverForInteractiveType("shapes").bind(_this);
_this.nodes = [];
_this.state = {};
return _this;
}
_createClass(Shape, [{
key: "handleDrag",
value: function handleDrag(index, position, otherProps) {
this.setState({
override: _extends({}, otherProps, {
index: index,
position: position
})
});
}
}, {
key: "handleDragComplete",
value: function handleDragComplete(moreProps) {
var _this2 = this;
var override = this.state.override;
if (isDefined(override)) {
var shapes = this.props.shapes;
var newTextList = shapes.map(function (each, idx) {
var selected = idx === override.index;
return selected ? _extends({}, each, override, {
position: override.position,
selected: true
}) : _extends({}, each, {
selected: false
});
});
this.setState({
override: null
}, function () {
_this2.props.onDragComplete(newTextList, moreProps);
});
}
}
}, {
key: "handleDraw",
value: function handleDraw(moreProps, e) {
var _props = this.props,
enabled = _props.enabled,
shapes = _props.shapes;
var xScale = moreProps.xScale,
_moreProps$mouseXY = _slicedToArray(moreProps.mouseXY, 2),
mouseX = _moreProps$mouseXY[0],
mouseY = _moreProps$mouseXY[1],
yScale = moreProps.chartConfig.yScale,
xAccessor = moreProps.xAccessor,
currentItem = moreProps.currentItem;
var xyValue = [xAccessor(currentItem), yScale.invert(mouseY)];
var xylValue = [];
if (enabled) {
var _props2 = this.props,
defaultShape = _props2.defaultShape,
onChoosePosition = _props2.onChoosePosition,
figure = _props2.figure;
var position = [xyValue[0], xyValue[1]];
var newShape = _extends({}, defaultShape, {
selected: true,
figure: figure,
position: position,
degrees: 0,
width: 50,
height: 50,
edgeAngleTop: [xyValue[0], xyValue[1]],
edgeAngleBottom: [xyValue[0], xyValue[1]],
edgeRight: [xyValue[0], xyValue[1]],
edgeLeft: [xyValue[0], xyValue[1]]
});
if (onChoosePosition) {
onChoosePosition(newShape, moreProps, e);
}
} else {
var newLabels = [].concat(_toConsumableArray(shapes.map(function (d) {
return _extends({}, d);
})));
this.props.onDragComplete(newLabels, moreProps);
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _props3 = this.props,
hoverText = _props3.hoverText,
shapes = _props3.shapes;
var override = this.state.override;
var hoverTextWithDefault = _extends({}, Shape.defaultProps.hoverText, hoverText);
return React.createElement(
"g",
null,
shapes.map(function (shape, idx) {
return React.createElement(EachShape, _extends({
key: idx,
ref: _this3.saveNodeType(idx),
index: idx,
fill: shape.fill,
stroke: shape.stroke,
figure: shape.figure,
position: getValueFromOverride(override, idx, "position", shape.position),
selected: shape.selected,
opacity: 1,
degrees: getValueFromOverride(override, idx, "degrees", shape.degrees),
width: getValueFromOverride(override, idx, "width", shape.width),
height: getValueFromOverride(override, idx, "height", shape.height),
edgeAngleTop: getValueFromOverride(override, idx, "edgeAngleTop", shape.edgeAngleTop),
edgeAngleBottom: getValueFromOverride(override, idx, "edgeAngleBottom", shape.edgeAngleBottom),
edgeLeft: getValueFromOverride(override, idx, "edgeLeft", shape.edgeLeft),
edgeRight: getValueFromOverride(override, idx, "edgeRight", shape.edgeRight)
// edgeAngleTop={shape.edgeAngleTop}
// edgeAngleBottom={shape.edgeAngleBottom}
// edgeLeft={shape.edgeLeft}
// edgeRight={shape.edgeRight}
, edgeInteractiveCursor: "react-stockcharts-move-cursor",
lineInteractiveCursor: "react-stockcharts-move-cursor",
onDrag: _this3.handleDrag,
onDragComplete: _this3.handleDragComplete,
hoverText: hoverTextWithDefault
}, shape.appearance));
}),
React.createElement(GenericChartComponent, {
onClick: this.handleDraw,
svgDraw: noop,
canvasDraw: noop,
canvasToDraw: getMouseCanvas,
drawOn: ["mousemove", "pan"]
})
);
}
}]);
return Shape;
}(React.Component);
Shape.propTypes = {
enabled: PropTypes.bool.isRequired,
snapTo: PropTypes.func,
shouldDisableSnap: PropTypes.func.isRequired,
onStart: PropTypes.func.isRequired,
onComplete: PropTypes.func.isRequired,
onSelect: PropTypes.func,
onDoubleClick: PropTypes.func,
currentPositionStroke: PropTypes.string,
currentPositionStrokeWidth: PropTypes.number,
currentPositionstrokeOpacity: PropTypes.number,
currentPositionRadius: PropTypes.number,
hoverText: PropTypes.object.isRequired,
shapes: PropTypes.array.isRequired,
appearance: PropTypes.shape({
stroke: PropTypes.string.isRequired,
fill: PropTypes.string.isRequired,
edgeStrokeWidth: PropTypes.number.isRequired,
edgeFill: PropTypes.string.isRequired,
edgeStroke: PropTypes.string.isRequired
})
};
Shape.defaultProps = {
onStart: noop,
onComplete: noop,
onSelect: noop,
currentPositionStroke: "#000000",
currentPositionstrokeOpacity: 1,
currentPositionStrokeWidth: 3,
currentPositionRadius: 0,
shouldDisableSnap: function shouldDisableSnap(e) {
return e.button === 2 || e.shiftKey;
},
hoverText: _extends({}, HoverTextNearMouse.defaultProps, {
enable: true,
bgHeight: "auto",
bgWidth: "auto",
text: "Click to select object",
selectedText: ""
}),
shapes: [],
defaultShape: {
appearance: {
stroke: "#000000",
fill: "#000000",
edgeStrokeWidth: 1,
edgeFill: "#FFFFFF",
edgeStroke: "#000000",
r: 6
}
}
};
export default Shape;
//# sourceMappingURL=Shape.js.map