UNPKG

react-meteo-chart

Version:

pre alpha of meteo chart

502 lines (440 loc) 20 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _BasicChart = _interopRequireWildcard(require("../BasicChart")); var _moment = _interopRequireDefault(require("moment")); var _reactVis = require("react-vis"); var _PluvioLegend = require("./PluvioLegend"); var _reactSizeme = _interopRequireDefault(require("react-sizeme")); var _GridDates = _interopRequireDefault(require("../GridDates")); var _PluvioCrossFormat = require("./PluvioCrossFormat"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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; } var gaps = [[1, "h"], [3, "h"], [6, "h"], [24, "h"]]; var legaps = [24, 8, 4, 1]; var MiddleVerticalRectSeries = /*#__PURE__*/ function (_VerticalRectSeries) { _inherits(MiddleVerticalRectSeries, _VerticalRectSeries); function MiddleVerticalRectSeries() { var _getPrototypeOf2; var _this; _classCallCheck(this, MiddleVerticalRectSeries); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(MiddleVerticalRectSeries)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "_handleNearestX", function (event) { var _props3 = this.props, onNearestX = _props3.onNearestX, data = _props3.data; var minDistance = -Number.POSITIVE_INFINITY; var value = null; var valueIndex = null; var coordinate = this._getXYCoordinateInContainer(event); var xScaleFn = this._getAttributeFunctor("x"); data.forEach(function (item, i) { var currentCoordinate = xScaleFn(item); var newDistance = coordinate.x - currentCoordinate; if (newDistance <= 0 && newDistance > minDistance) { minDistance = newDistance; value = item; valueIndex = i; } }); if (!value) { return; } onNearestX(value, { innerX: xScaleFn(value), index: valueIndex, event: event.nativeEvent }); }); return _this; } return MiddleVerticalRectSeries; }(_reactVis.VerticalRectSeries); var PluvioChart = /*#__PURE__*/ function (_Component) { _inherits(PluvioChart, _Component); function PluvioChart() { var _getPrototypeOf3; var _this2; _classCallCheck(this, PluvioChart); for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } _this2 = _possibleConstructorReturn(this, (_getPrototypeOf3 = _getPrototypeOf(PluvioChart)).call.apply(_getPrototypeOf3, [this].concat(args))); _defineProperty(_assertThisInitialized(_this2), "state", { cross: null, dailySum: true, showhCross: false, show3hCross: false, show6hCross: false, show24hCross: false, hsum: true, hsum3: true, hsum6: true, hsum24: true, data: null }); _defineProperty(_assertThisInitialized(_this2), "getCrossTime", function (value, span, index) { if (!value) return null; if (value.x - value.x0 < span) { if (index == 0) return _moment["default"].unix(value.x / 1000);else return _moment["default"].unix(value.x0 / 1000).add(span, "ms"); } return _moment["default"].unix(value.x / 1000); }); _defineProperty(_assertThisInitialized(_this2), "setTooltip", function (value, crossName, index) { var day = 86400000; var superArr = _this2.props.superArr; var xx = superArr.map(function (d) { return Math.max.apply(Math, d.map(function (x) { return x.x0; }).filter(function (x) { return x < value.x; })); }); var cress = superArr.map(function (d, indx) { return d.filter(function (asd, id) { return asd.x0 == xx[indx]; }); }); _this2.setState(_defineProperty({ cross: superArr.map(function (d, indx) { return { x: (value.x + value.x0) / 2, y: cress[indx][0] ? cress[indx][0].y : null }; }), data: superArr.map(function (d, indx) { return { x: _this2.getCrossTime(cress[indx][0], day / legaps[indx], index), info: cress[indx][0] ? cress[indx][0] : null }; }) }, crossName, true)); }); return _this2; } _createClass(PluvioChart, [{ key: "render", value: function render() { var _this3 = this; var width = this.props.size.width; var _this$props = this.props, superArr = _this$props.superArr, dateFrom = _this$props.dateFrom, dateTo = _this$props.dateTo, _this$props$plotHeigh = _this$props.plotHeight, plotHeight = _this$props$plotHeigh === void 0 ? 270 : _this$props$plotHeigh, _this$props$plotWidth = _this$props.plotWidth, plotWidth = _this$props$plotWidth === void 0 ? width : _this$props$plotWidth; var _this$state = this.state, cross = _this$state.cross, dailySum = _this$state.dailySum, showhCross = _this$state.showhCross, show3hCross = _this$state.show3hCross, show6hCross = _this$state.show6hCross, show24hCross = _this$state.show24hCross, hsum = _this$state.hsum, hsum3 = _this$state.hsum3, hsum6 = _this$state.hsum6, hsum24 = _this$state.hsum24, data = _this$state.data; var domainFrom = (0, _moment["default"])(_moment["default"].utc(dateFrom).local().format("YYYY-MM-DDTHH:mm")); var domainTo = (0, _moment["default"])(_moment["default"].utc(dateTo).local().format("YYYY-MM-DD:HH:mm")); var day = 86400000; var dateThreshold = day * 5; var gridDates = (0, _GridDates["default"])(dateFrom, dateTo); var isAdmin = this.props.isAdmin; var daySpan = (0, _moment["default"])(dateTo) - (0, _moment["default"])(dateFrom); var showDirectionThreshold = day * 30; var setVal = function setVal(data, name) { _this3.setState(_defineProperty({}, name, !data)); }; // const plotHeight = 270; // const plotWidth = 1400; return _react["default"].createElement(_react["default"].Fragment, null, _react["default"].createElement(_BasicChart["default"], { opacity: this.props.opacity, superArr: superArr, dateFrom: dateFrom, dateTo: dateTo, plotHeight: plotHeight > 120 ? plotHeight : 120, plotWidth: plotWidth }, superArr[0] && hsum && _react["default"].createElement(_reactVis.FlexibleXYPlot, { height: plotHeight > 120 ? plotHeight : 120, margin: { left: 65 } // width={plotWidth} , className: "dailySumPlot", onMouseLeave: function onMouseLeave() { _this3.setState({ showhCross: false }); }, xType: "time", xDomain: [domainFrom, domainTo], yDomain: Math.max.apply(Math, superArr[0].map(function (d) { return d.y; })) == 0 ? [0, 5] : [0, Math.max.apply(Math, superArr[0].map(function (d) { return d.y; }))] }, _react["default"].createElement(_reactVis.YAxis, { left: 0, tickFormat: function tickFormat(v) { return v >= 0 ? "".concat(v, " \u043C\u043C") : ""; } }), _react["default"].createElement(_reactVis.HorizontalGridLines, null), _react["default"].createElement(MiddleVerticalRectSeries // getNull={d => d.y !== null} , { data: superArr[0], fill: "".concat(_PluvioLegend.COLORS.Sum1hColor), stroke: "".concat(_PluvioLegend.COLORS.Sum1hStrokeColor), onNearestX: function onNearestX(value, _ref) { var index = _ref.index; _this3.setTooltip(value, "showhCross", index); } }), _react["default"].createElement(_reactVis.Crosshair, { values: cross }, showhCross ? _react["default"].createElement(_PluvioCrossFormat.PluvioCrossFormat, { superArr: superArr, cross: cross, data: data, indx: 0 }) : _react["default"].createElement(_PluvioCrossFormat.EmptyCrossFormat, null))), isAdmin && _react["default"].createElement(_PluvioLegend.PluvioLegend1h, { setVal: setVal, isAdmin: true })), _react["default"].createElement(_BasicChart["default"], { opacity: this.props.opacity, superArr: superArr, dateFrom: dateFrom, dateTo: dateTo, plotHeight: plotHeight > 120 ? plotHeight : 120, plotWidth: plotWidth }, superArr[1] && hsum3 && _react["default"].createElement(_reactVis.FlexibleXYPlot, { height: plotHeight > 120 ? plotHeight : 120 // width={plotWidth} , className: "dailySumPlot", margin: { left: 65 }, onMouseLeave: function onMouseLeave() { _this3.setState({ show3hCross: false }); }, xType: "time", xDomain: [domainFrom, domainTo], yDomain: Math.max.apply(Math, superArr[1].map(function (d) { return d.y; })) == 0 ? [0, 5] : [0, Math.max.apply(Math, superArr[1].map(function (d) { return d.y; }))] }, _react["default"].createElement(_reactVis.YAxis, { left: 0, tickFormat: function tickFormat(v) { return v >= 0 ? "".concat(v, " \u043C\u043C") : ""; } }), _react["default"].createElement(_reactVis.HorizontalGridLines, null), _react["default"].createElement(MiddleVerticalRectSeries, { getNull: function getNull(d) { return d.y !== null; }, data: superArr[1], fill: "".concat(_PluvioLegend.COLORS.Sum3hColor), stroke: "".concat(_PluvioLegend.COLORS.Sum3hStrokeColor), onNearestX: function onNearestX(value, _ref2) { var index = _ref2.index; _this3.setTooltip(value, "show3hCross", index); } }), _react["default"].createElement(_reactVis.Crosshair, { values: cross }, show3hCross ? _react["default"].createElement(_PluvioCrossFormat.PluvioCrossFormat, { superArr: superArr, cross: cross, data: data, indx: 1 }) : _react["default"].createElement(_PluvioCrossFormat.EmptyCrossFormat, null))), isAdmin && _react["default"].createElement(_PluvioLegend.PluvioLegend3h, { setVal: setVal, isAdmin: true })), _react["default"].createElement(_BasicChart["default"], { opacity: this.props.opacity, superArr: superArr, dateFrom: dateFrom, dateTo: dateTo, plotHeight: plotHeight > 120 ? plotHeight : 120, plotWidth: plotWidth }, superArr[2] && hsum6 && _react["default"].createElement(_reactVis.FlexibleXYPlot, { height: plotHeight > 120 ? plotHeight : 120 // width={plotWidth} , margin: { left: 65 }, className: "dailySumPlot", onMouseLeave: function onMouseLeave() { _this3.setState({ show6hCross: false }); }, xType: "time", xDomain: [domainFrom, domainTo], yDomain: Math.max.apply(Math, superArr[2].map(function (d) { return d.y; })) == 0 ? [0, 5] : [0, Math.max.apply(Math, superArr[2].map(function (d) { return d.y; }))] }, _react["default"].createElement(_reactVis.YAxis, { left: 0, tickFormat: function tickFormat(v) { return v >= 0 ? "".concat(v, " \u043C\u043C") : ""; } }), _react["default"].createElement(_reactVis.HorizontalGridLines, null), _react["default"].createElement(MiddleVerticalRectSeries, { getNull: function getNull(d) { return d.y !== null; }, data: superArr[2], fill: "".concat(_PluvioLegend.COLORS.Sum6hColor), stroke: "".concat(_PluvioLegend.COLORS.Sum6hStrokeColor), onNearestX: function onNearestX(value, _ref3) { var index = _ref3.index; _this3.setTooltip(value, "show6hCross", index); } }), _react["default"].createElement(_reactVis.Crosshair, { values: cross }, show6hCross ? _react["default"].createElement(_PluvioCrossFormat.PluvioCrossFormat, { superArr: superArr, cross: cross, data: data, indx: 2 }) : _react["default"].createElement(_PluvioCrossFormat.EmptyCrossFormat, null))), isAdmin && _react["default"].createElement(_PluvioLegend.PluvioLegend6h, { setVal: setVal, isAdmin: true }), isAdmin && _react["default"].createElement(_PluvioLegend.PluvioLegend24h, { setVal: setVal, isAdmin: true })), hsum24 && superArr[3] ? _react["default"].createElement("div", { style: { // border: "1px solid #7d9cb8", backgroundColor: (0, _BasicChart.rgbToHex)(255, 255, 255, this.props.opacity) } }, _react["default"].createElement(_reactVis.FlexibleXYPlot, { height: plotHeight > 120 ? plotHeight : 120 // width={plotWidth} , margin: { left: 65 }, className: "dailySumPlot", onMouseLeave: function onMouseLeave() { _this3.setState({ show24hCross: false }); }, xDomain: [domainFrom, domainTo], xType: "time", yDomain: Math.max.apply(Math, superArr[3].map(function (d) { return d.y; })) == 0 ? [0, 5] : [0, Math.max.apply(Math, superArr[3].map(function (d) { return d.y; }))] }, _react["default"].createElement(_reactVis.HorizontalGridLines, null), _react["default"].createElement(_reactVis.VerticalGridLines, null), _react["default"].createElement(_reactVis.YAxis, { left: 0, tickFormat: function tickFormat(v) { return v >= 0 ? "".concat(v, " \u043C\u043C") : ""; } }), _react["default"].createElement(_reactVis.XAxis, { tickFormat: function tickFormat(d) { return (0, _moment["default"])(d).format("HH:mm") == "00:00" ? "" : "".concat((0, _moment["default"])(d).format("HH:mm")); }, tickTotal: width * 0.01 }), _react["default"].createElement(_reactVis.XAxis, { tickFormat: function tickFormat(d) { return (0, _moment["default"])(d).format("HH:mm") == "00:00" ? "".concat((0, _moment["default"])(d).format("D.MM")) : ""; }, style: { line: { stroke: "#ADDDE1" }, ticks: { stroke: "#ADDDE1" }, text: { stroke: "none", fill: "#000000", fontWeight: 750, fontSize: 12 } }, tickTotal: width * 0.01 }), gridDates.map(function (date) { return _react["default"].createElement(_reactVis.DecorativeAxis, { key: "".concat(date, "-axis"), className: "decaxis", axisDomain: [], axisStart: { x: (0, _moment["default"])(date.format("YYYY-MM-DD")), y: 0 }, axisEnd: { x: (0, _moment["default"])(date.format("YYYY-MM-DD")), y: 1 }, tickTotal: 0, style: { ticks: { display: "none" }, text: { display: "none" } } }); }), _react["default"].createElement(MiddleVerticalRectSeries, { getNull: function getNull(d) { return d.y !== null; }, data: superArr[3], fill: "".concat(_PluvioLegend.COLORS.Sum24hColor), stroke: "".concat(_PluvioLegend.COLORS.Sum24hStrokeColor) // onNearestX={value => console.log(value)} , onNearestX: function onNearestX(value, _ref4) { var index = _ref4.index; _this3.setTooltip(value, "show24hCross", index); } }), _react["default"].createElement(_reactVis.Crosshair, { values: cross }, show24hCross ? _react["default"].createElement(_PluvioCrossFormat.PluvioCrossFormat, { superArr: superArr, cross: cross, data: data, indx: 3 }) : _react["default"].createElement(_PluvioCrossFormat.EmptyCrossFormat, null)))) : _react["default"].createElement(_BasicChart["default"], { opacity: this.props.opacity, superArr: superArr, dateFrom: dateFrom, dateTo: dateTo, plotHeight: plotHeight > 120 ? plotHeight : 120, plotWidth: plotWidth }), !isAdmin && _react["default"].createElement(_PluvioLegend.FullPluvioLegend, { setVal: setVal, isAdmin: true })); } }]); return PluvioChart; }(_react.Component); var _default = (0, _reactSizeme["default"])()(PluvioChart); exports["default"] = _default;