UNPKG

react-meteo-chart

Version:

pre alpha of meteo chart

686 lines (606 loc) 29.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _reactMeteoChart = require("react-meteo-chart"); var _reactApollo = require("react-apollo"); var _moment = _interopRequireDefault(require("moment")); var _apolloBoost = _interopRequireDefault(require("apollo-boost")); var _reactLoading = _interopRequireDefault(require("react-loading")); var _reactSelect = _interopRequireDefault(require("react-select")); var _graphqlTag = _interopRequireDefault(require("graphql-tag")); require("./style.css"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } 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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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; } function _templateObject2() { var data = _taggedTemplateLiteral(["\n query($token: String!) {\n me(token: $token) {\n lastLogin\n }\n }\n"]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n query amsPlotData(\n $token: String!\n $stationId: String!\n $since: DateTime\n $to: DateTime\n $typeName: String!\n $maxPoints: Int\n ) {\n amsPlotData(\n token: $token\n stationId: $stationId\n typeName: $typeName\n since: $since\n to: $to\n maxPoints: $maxPoints\n )\n }\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var day = 86400000; var windDirectionHandler = function windDirectionHandler(msgs, minmax, height) { var arr = []; var dy = minmax[1] - minmax[0]; var y_dir = minmax[0] - dy / 5; arr = msgs.filter(function (e, i) { return e[1] !== null; }).map(function (point) { switch (point[1]) { case 0: return { x: point[0] * 1000, y: y_dir, customComponent: "circle", size: 10, style: { stroke: "black", fill: "white" }, z: 0 }; case 99: return { x: point[0], y: y_dir, customComponent: "circle", size: 20, style: { stroke: "black", fill: "white" }, z: 0 }; default: var val = point[1]; var velocity = point[2]; var triNum = Math.floor(velocity / 25); var longfeathNum = Math.floor(velocity % 25 / 5); var leftVelocity = velocity > 1 ? velocity - triNum * 25 - longfeathNum * 5 : 0; var translate = 0; var things = []; if (velocity <= 1) if (val <= 180) things.push(_react["default"].createElement("path", { d: "M489.33 178.76C489.33 191.29 479.56 201.46 467.52 201.46C455.48 201.46 445.7 191.29 445.7 178.76C445.7 166.24 455.48 156.07 467.52 156.07C479.56 156.07 489.33 166.24 489.33 178.76Z", key: "".concat(val).concat(point[0]).concat(translate) }));else things.push(_react["default"].createElement("path", { d: "M443.63 344C443.63 356.53 433.86 366.7 421.82 366.7C409.78 366.7 400 356.53 400 344C400 331.47 409.78 321.3 421.82 321.3C433.86 321.3 443.63 331.47 443.63 344Z", key: "".concat(val).concat(point[0]).concat(translate) })); for (var i = 0; i < triNum; i++) { if (val <= 180) things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M389.38 126.04L257.46 257.96L389.38 257.96L389.38 126.04Z", key: "".concat(val).concat(point[0]).concat(translate) }));else things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M389.59 409.96L257.68 278.04L389.59 278.04L389.59 409.96Z", key: "".concat(val).concat(point[0]).concat(translate) })); translate += 120; } for (var _i = 0; _i < longfeathNum; _i++) { if (val <= 180) things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M514.03 149.92L387.53 281.28L373.08 267.36L499.58 136L514.03 149.92Z", key: "".concat(val).concat(point[0]).concat(translate) }));else things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M520 391.41L389.6 256L375.15 269.92L505.55 405.33L520 391.41Z", key: "".concat(val).concat(point[0]).concat(translate) })); translate += 60; } if (leftVelocity) { if (leftVelocity < 4) { if (val <= 180) things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M460.19 207.58L390.45 280L376 266.08L445.74 193.66L460.19 207.58Z", key: "".concat(val).concat(point[0]).concat(translate) }));else things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M460.19 328.42L390.45 256L376 269.92L445.74 342.34L460.19 328.42Z", key: "".concat(val).concat(point[0]).concat(translate) })); } else if (leftVelocity < 5) { if (val <= 180) things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M514.03 149.92L387.53 281.28L373.08 267.36L499.58 136L514.03 149.92Z", key: "".concat(val).concat(point[0]).concat(translate) }));else things.push(_react["default"].createElement("path", { transform: "translate(".concat(-translate, ")"), d: "M520 391.41L389.6 256L375.15 269.92L505.55 405.33L520 391.41Z", key: "".concat(val).concat(point[0]).concat(translate) })); } } return { x: point[0] * 1000, y: y_dir, customComponent: function customComponent(row, positionInPixels) { return _react["default"].createElement("g", { // transform={` scale(0.10) `} transform: " scale(0.080) rotate(".concat(val - 90, " 270 270)") // transformOrigin={"2000% 50%"} , version: "1.1", id: "Layer_1", xmlns: "http://www.w3.org/2000/svg", x: "0", y: "0", viewBox: "0 0 512 512", style: { fill: "#50C8EF" } // style="enable-background:new 0 0 512 512;" }, _react["default"].createElement("path", { d: "M24 255.18L389.57 255.18L389.57 280L24 280L24 255.18Z", key: "".concat(point[0], "-1") }), _react["default"].createElement("path", { d: "M50.34 267.54L114.23 306.46L89.08 321.79L46.62 295.92L46.58 295.94L0 267.56L0.03 267.54L0 267.53L46.58 239.15L46.61 239.17L89.08 213.3L114.22 228.62L50.34 267.54Z", key: "".concat(point[0], "-2") }), things.map(function (d) { return d; })); }, z: val }; } }); arr.push({ x: arr[0] ? arr[0].x : null, y: minmax[0] - dy / 1.5, customComponent: "circle", size: 0, style: { stroke: "black", fill: "white" }, z: 0 }); return arr; }; var currentStation = (0, _graphqlTag["default"])(_templateObject()); var ME = (0, _graphqlTag["default"])(_templateObject2()); var selecterStyle = { control: function control(base, state) { return _objectSpread({}, base, { backgroundColor: "ccccff", opacity: 1, height: "12px", width: "100px", border: state.isFocused ? 0 : 0, boxShadow: state.isFocused ? 0 : 0, "&:hover": { border: state.isFocused ? 0 : 0 } }); }, dropdownIndicator: function dropdownIndicator(e) { return _objectSpread({}, e, { color: "#646464", transform: "rotate(180deg)" }); }, menu: function menu(e) { return _objectSpread({}, e, { color: "#00000", width: "100px", backgroundColor: "#ffFffF" }); } }; var zooms = [[1, "d", "День"], [3, "d", "3 Дня"], [7, "d", "Неделя"], [14, "d", "2 Недели"]]; var selectValues = [{ label: "День", value: 0 }, { label: "3 Дня", value: 1 }, { label: "Неделя", value: 2 }, { label: "2 Недели", value: 3 }]; var zoomsvar = [3, 6, 12, 24]; // const selectedId = "22829"; // let dateFrom = "2019-03-23T06:30:00"; // let dateTo = "2019-03-25T06:30:00"; var types = ["ff", "TTT1", "tend", "RRR6", "dd_ff", "P0P0P0P03", "PPPP4"]; var minmax = [0, 0]; var AmsChartGen = /*#__PURE__*/ function (_React$Component) { _inherits(AmsChartGen, _React$Component); function AmsChartGen(props) { var _this; _classCallCheck(this, AmsChartGen); _this = _possibleConstructorReturn(this, _getPrototypeOf(AmsChartGen).call(this, props)); _defineProperty(_assertThisInitialized(_this), "dataTransformer", function (data, indx, minmax) { var chunk = []; switch (indx) { case 0: chunk = data.data.amsPlotData.map(function (s) { return { x: s[0] * 1000, y: s[1] !== null ? s[1] : "" }; }); break; case 3: data.data.amsPlotData.forEach(function (d) { chunk = data.data.amsPlotData.map(function (s) { return { x0: s[0] * 1000, x: s[1] * 1000, y: s[2] !== null ? s[2] : "" }; }); }); break; case 4: chunk = windDirectionHandler(data.data.amsPlotData, minmax, _this.props.height); break; case 2: data.data.amsPlotData.forEach(function (d) { chunk = data.data.amsPlotData.filter(function (d) { return d[2] !== null; }).map(function (s) { return { x: (s[0] * 1000 + s[1] * 1000) / 2, y: (s[4] + s[5]) / 2, yLow: s[2], yHigh: s[3], yOpen: s[4] > s[5] ? s[4] : s[5], yClose: s[4] > s[5] ? s[5] : s[4], color: s[4] > s[5] ? "#dd0000dd" : "#00dd00dd" }; }); }); break; default: chunk = data.data.amsPlotData.map(function (s) { return { x: s[0] * 1000, y: s[1] !== null ? s[1] : "" }; }); } return chunk; }); _defineProperty(_assertThisInitialized(_this), "getTend", function (variables) { if (!_this.props.dateFrom) return "".concat(zoomsvar[_this.state.zoom], "tend");else { var gap = +(0, _moment["default"])(_this.state.to).format("X") - +(0, _moment["default"])(_this.state.since).format("X"); if (gap <= 86400) return "3tend";else if (gap <= 86400 * 3) return "6tend";else if (gap <= 86400 * 7) return "12tend";else if (gap <= 86400 * 14) return "24tend"; return "48tend"; } }); _this.state = { chunks: [], isLoadingChunks: [], chunksLength: [], zoom: 0, since: _this.props.dateFrom ? (0, _moment["default"])(_this.props.dateFrom).format("YYYY-MM-DDTHH:mm:ss") : null, to: (0, _moment["default"])(_this.props.dateTo).format("YYYY-MM-DDTHH:mm:ss"), deep: true, wind: [], dataGap: _this.props.dateTo, stationId: _this.props.stationId, loadMessage: _this.props.loadMessage !== undefined ? _this.props.loadMessage : true }; _this.deep = true; _this.deepDarkFantasy = true; _this.apollo = new _apolloBoost["default"]({ uri: _this.props.client }); return _this; } _createClass(AmsChartGen, [{ key: "componentDidUpdate", value: function componentDidUpdate() { if (this.state.to !== (0, _moment["default"])(this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss")) { this.setState({ to: (0, _moment["default"])(this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss") }); if (!this.props.animated) this.deep = true; } if (this.props.stationId !== this.state.stationId) { this.setState({ stationId: this.props.stationId }); this.deep = true; } } }, { key: "render", value: function render() { var _this2 = this; var variables = { token: this.props.token, stationId: this.state.stationId, since: this.state.since ? this.state.since : (0, _moment["default"])(this.props.dateTo).add(-zooms[this.state.zoom][0], zooms[this.state.zoom][1]).format("YYYY-MM-DDTHH:mm:ss"), to: (0, _moment["default"])(this.props.dateTo).format("YYYY-MM-DDTHH:mm:ss"), dataSince: (0, _moment["default"])(this.state.dataGap).add(-zooms[this.state.zoom][0], zooms[this.state.zoom][1]), dataTo: (0, _moment["default"])(this.state.dataGap), maxPoints: this.props.maxPoints }; // if (this.state.chunks[0] && this.state.chunks[4]) { // this.windDirectionHandler() // } if (this.props.doneLoading) { this.props.doneLoading(this.state.isLoadingChunks.filter(function (d) { return d == false; }).length == 0 && this.state.isLoadingChunks.length > 0); } var superArr = this.state.chunks.map(function (d) { return d.sort(function (a, b) { return a.x > b.x; }).filter(function (s) { return s.x >= +_moment["default"].utc(variables.since).format("X") * 1000 && s.x <= +_moment["default"].utc(variables.to).format("X") * 1000; }); }); // console.log("since :", moment.utc(variables.since).format("X")); // console.log("to :", moment.utc(variables.to).format("X")); // console.log("superArr :", superArr); // TODO: Сделать нормальную проверку границ if (+(0, _moment["default"])(variables.since).format("X") < +variables.dataSince.format("X") && this.props.dir == -1) { // this.deepDarkFantasy = false; this.preFetch(this.props.dir); } if (+(0, _moment["default"])(variables.to).format("X") > +variables.dataTo.format("X") && this.props.dir == 1) { // this.deepDarkFantasy = false; this.preFetch(this.props.dir); } return _react["default"].createElement(_reactApollo.ApolloProvider, { client: this.apollo }, _react["default"].createElement("div", { className: "App" }, this.state.isLoadingChunks.filter(function (d) { return d == false; }).length > 0 && this.state.loadMessage && _react["default"].createElement("div", { style: { display: "flex", justifyContent: "center", alignContent: "center", alignItems: "center", flexDirection: "column", fontSize: "17px", height: "100%" } }, _react["default"].createElement(_reactLoading["default"], { type: "balls", color: "#333333", height: 25, width: 50 }), " ", _react["default"].createElement("p", null, " \u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0433\u0440\u0430\u0444\u0438\u043A\u043E\u0432 ")), _react["default"].createElement(_reactApollo.Query, { query: ME, variables: { token: variables.token } }, function (_ref) { var loading = _ref.loading, error = _ref.error, data = _ref.data, client = _ref.client; if (loading) return _react["default"].createElement("div", null); if (error) return _react["default"].createElement("p", null, "\u041E\u0448\u0438\u0431\u043A\u0430..."); if (!_this2.client) _this2.client = client; var req = []; { _this2.deep && types.forEach(function (t, indx) { req[indx] = client.query({ query: currentStation, variables: { token: variables.token, stationId: variables.stationId, typeName: indx == 2 ? _this2.getTend(variables) : t, since: variables.since, to: variables.to, maxPoints: indx == 4 ? _this2.props.maxPoints / 10 : _this2.props.maxPoints } }); setTimeout(function () { return _this2.setState(function (state) { var chunk = false; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); if (indx == 4) Promise.all([req[0], req[4]]).then(function (data) { var filtredData = data[0].data.amsPlotData.map(function (d) { return d[1]; }); var minmax = [Math.min.apply(Math, _toConsumableArray(filtredData)), Math.max.apply(Math, _toConsumableArray(filtredData))]; setTimeout(function () { return _this2.setState(function (state) { var chunk = _this2.dataTransformer(data[1], indx, minmax); state.chunksLength[indx] = chunk.length; state.chunks[indx] = chunk; }); }); setTimeout(function () { return _this2.setState(function (state) { var chunk = true; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); });else { req[indx].then(function (data) { setTimeout(function () { return _this2.setState(function (state) { var chunk = _this2.dataTransformer(data, indx); state.chunksLength[indx] = chunk.length; state.chunks[indx] = chunk; }); }); setTimeout(function () { return _this2.setState(function (state) { var chunk = true; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); }); } }); _this2.deep = false; } _this2.preFetch = /*#__PURE__*/ function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(dir) { var req; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: req = []; types.forEach(function (t, indx) { req[indx] = client.query({ query: currentStation, variables: { token: variables.token, stationId: variables.stationId, typeName: indx == 2 ? "".concat(zoomsvar[_this2.state.zoom]).concat(t) : t, since: (0, _moment["default"])(variables.dataSince).add(dir, "d").format("YYYY-MM-DDTHH:mm"), to: (0, _moment["default"])(variables.dataTo).add(dir, "d").format("YYYY-MM-DDTHH:mm"), maxPoints: indx == 4 ? _this2.props.maxPoints / 10 : _this2.props.maxPoints } }); setTimeout(function () { return _this2.setState(function (state) { var chunk = false; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); if (indx == 4) Promise.all([req[0], req[4]]).then(function (data) { var filtredData = data[0].data.amsPlotData.map(function (d) { return d[1]; }); var minmax = [Math.min.apply(Math, _toConsumableArray(filtredData)), Math.max.apply(Math, _toConsumableArray(filtredData))]; setTimeout(function () { return _this2.setState(function (state) { var chunk = _this2.dataTransformer(data[1], indx, minmax); if (dir == -1) state.chunks[indx] = [].concat(_toConsumableArray(chunk), _toConsumableArray(state.chunks[indx].slice(0, _this2.state.chunksLength[indx]))); if (dir == 1) state.chunks[indx] = [].concat(_toConsumableArray(state.chunks[indx].slice(state.chunks[indx].length - _this2.state.chunksLength[indx], state.chunks[indx].length)), _toConsumableArray(chunk)); state.chunksLength[indx] = chunk.length; }); }); setTimeout(function () { return _this2.setState(function (state) { var chunk = true; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); });else { req[indx].then(function (data) { setTimeout(function () { return _this2.setState(function (state) { var chunk = _this2.dataTransformer(data, indx); if (dir == -1) state.chunks[indx] = [].concat(_toConsumableArray(chunk), _toConsumableArray(state.chunks[indx].slice(0, _this2.state.chunksLength[indx]))); if (dir == 1) state.chunks[indx] = [].concat(_toConsumableArray(state.chunks[indx].slice(state.chunks[indx].length - _this2.state.chunksLength[indx], state.chunks[indx].length)), _toConsumableArray(chunk)); state.chunksLength[indx] = chunk.length; }); }); setTimeout(function () { return _this2.setState(function (state) { var chunk = true; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); }); } }); _this2.setState({ dataGap: (0, _moment["default"])(_this2.state.dataGap).add(dir, "d").format("YYYY-MM-DDTHH:mm") }); _this2.deepDarkFantasy = true; case 4: case "end": return _context.stop(); } } }, _callee); })); return function (_x) { return _ref2.apply(this, arguments); }; }(); return null; }), _react["default"].createElement(_reactMeteoChart.AmsChart, { opacity: this.props.opacity, superArr: superArr, plotHeight: this.props.height, dateFrom: variables.since, dateTo: variables.to, isAdmin: this.props.isAdmin, animated: this.props.animated // arrows={true} }), _react["default"].createElement("div", { style: { marginTop: "-40px", width: "100px" } }, !this.props.isAdmin && _react["default"].createElement(_reactSelect["default"], { theme: function theme(_theme) { return _objectSpread({}, _theme, { borderRadius: 1, spacing: { baseUnit: 2, controlHeight: 30, menuGutter: 2 }, colors: _objectSpread({}, _theme.colors, { primary: "#cee0eb", neutral0: "#000000" }) }); }, isSearchable: false // opacity={10} , onChange: function onChange(option) { return _this2.state.zoom !== option.value ? (_this2.setState({ zoom: option.value }), _this2.deep = true) : ""; }, placeholder: "", value: selectValues[this.state.zoom], menuPlacement: "top", menuPosition: "fixed", styles: selecterStyle, options: selectValues })))); } }]); return AmsChartGen; }(_react["default"].Component); var _default = AmsChartGen; exports["default"] = _default;