UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

494 lines (493 loc) 21.2 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var mobx_1 = require("mobx"); var lodash_1 = require("lodash"); var bind_1 = __importDefault(require("../../../utils/bind")); var sleep_1 = __importDefault(require("../../../utils/sleep")); var store_1 = __importDefault(require("../../../stores/store")); var constants_1 = require("../../../constants"); var drill_down_1 = require("../../../constants/drill-down"); /** * 所有图表的基础Store */ var BaseChartStore = /** @class */ (function (_super) { __extends(BaseChartStore, _super); function BaseChartStore(getProps) { var _this = _super.call(this) || this; _this.onEvents = {}; _this.echartInst = null; // 图表实际的宽度 _this.chartWidth = 0; // 图表实际的高度 _this.chartHeight = 0; // 图表点击的位置 _this.position = {}; _this.debounceHandleClient = lodash_1.debounce(_this.handleChartClient, 300); _this.getProps = getProps; _this.chartContainerService = getProps().chartContainerService; _this.chartConditionService = _this.chartContainerService.chartConditionService; var _a = _this.chartConditionService, analysisService = _a.analysisService, chartStyleService = _a.chartStyleService, chartActionService = _a.chartActionService; _this.analysisServices = analysisService; _this.chartStyleService = chartStyleService; _this.searchConditionService = _this.chartContainerService.searchConditionService; _this.chartActionService = chartActionService; _this.onBrushAction = getProps().onBrushAction; _this.onLegendSelectChangedAction = getProps().onLegendSelectChangedAction; /** * fix: 解决鼠标快速离开图表, tooltip不隐藏的问题, * 监听globalout方法, 手动触发隐藏tooltip的action */ _this.addDisposer(mobx_1.reaction(function () { return _this.echartInst; }, function () { if (_this.echartInst) { _this.echartInst.on('globalout', 'tooltip', function () { setTimeout(function () { _this.echartInst.dispatchAction({ type: 'hideTip' }); }, 200); }); // 获取onclick 的位置 _this.echartInst.getZr().on('click', function (e) { _this.setPosition({ clientX: lodash_1.get(e, ['event', 'clientX']), clientY: lodash_1.get(e, ['event', 'clientY']) }); }); } })); return _this; } Object.defineProperty(BaseChartStore.prototype, "isLowPerfDevice", { get: function () { return /android/gi.test(window.navigator.userAgent); }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "actionEnable", { get: function () { var _a; return (_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.enable; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "drillDownType", { get: function () { var _a; return (_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.drillDownActionService.drillDownType; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "searchTime", { get: function () { return this.searchConditionService.time; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "dataset", { get: function () { return this.chartConditionService.dataset; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "originDataset", { get: function () { return this.chartConditionService.originDataset; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "fields", { get: function () { return this.dataset.fields; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "rows", { get: function () { return this.dataset.rows; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "hasData", { get: function () { return this.rows.length; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "theme", { get: function () { return this.chartContainerService.themeService.theme; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "themeOption", { get: function () { return this.chartContainerService.themeService.themeOption; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChartStore.prototype, "chartCanClick", { get: function () { var _a; // action存在 if (this.actionEnable && ((_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.commonAction)) { return true; } // 下钻存在动作 if (this.actionEnable && this.drillDownType && this.drillDownType !== drill_down_1.DrillDownType.None) { return true; } return false; }, enumerable: false, configurable: true }); BaseChartStore.prototype.setPosition = function (position) { this.position = position; }; BaseChartStore.prototype.getCanvasCtx = function () { if (this._canvasCtx) { return this._canvasCtx; } else if (this.echartInst) { var dom = this.echartInst.getDom(); if (dom) { var canvas = dom.querySelector('canvas'); if (canvas) { this._canvasCtx = canvas.getContext('2d'); return this._canvasCtx; } } } }; BaseChartStore.prototype.measureText = function (txt) { var canvasCtx = this.getCanvasCtx(); if (canvasCtx) { canvasCtx.font = '12px sans-serif'; return canvasCtx.measureText(txt); } }; BaseChartStore.prototype.handleChartClient = function (client) { if (!client) { return; } var width = client.width, height = client.height; this.setChartWidth(width); this.setChartHeight(height); }; BaseChartStore.prototype.setChartWidth = function (width) { this.chartWidth = width; }; BaseChartStore.prototype.setChartHeight = function (height) { this.chartHeight = height; }; BaseChartStore.prototype.setEchartInst = function (echart) { this.echartInst = echart; }; BaseChartStore.prototype.onChartReadyCallback = function (echart) { var _this = this; // TBD: 为什么需要延迟,待确认 setTimeout(function () { _this.setEchartInst(echart); }, 300); }; // 通用的图表事件 BaseChartStore.prototype.onAction = function (eventParams, ChartDrillService, ChartCommonActionService) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var handleDrillDown, handleCommonAction; return __generator(this, function (_c) { switch (_c.label) { case 0: handleDrillDown = (_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.drillDownActionService.onDrillDown; handleCommonAction = (_b = this.chartActionService) === null || _b === void 0 ? void 0 : _b.commonAction; if (!this.actionEnable || (!handleDrillDown && !handleCommonAction)) { return [2 /*return*/]; } /* 修复 echarts 报错 Can't read property 'getWidth' of null 原因在于hover会触发 showTip,并且加了防抖,setTimeout 执行回调后实例已经清除了 */ if (this.echartInst) { this.echartInst.dispatchAction({ type: 'hideTip' }); } return [4 /*yield*/, sleep_1.default(100)]; case 1: _c.sent(); if (handleCommonAction) { this.onCommonAction(eventParams, ChartCommonActionService); return [2 /*return*/]; } if (handleDrillDown) { this.onDrillDown(eventParams, ChartDrillService); } return [2 /*return*/]; } }); }); }; // 图表跳转事件 BaseChartStore.prototype.onCommonAction = function (eventParams, ChartCommonActionService) { var _a; return __awaiter(this, void 0, void 0, function () { var handleAction, eventService, event, token; return __generator(this, function (_b) { handleAction = (_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.commonAction; eventService = new ChartCommonActionService(); event = eventService.toEvent(eventParams); token = eventService.toActionToken(eventParams); handleAction === null || handleAction === void 0 ? void 0 : handleAction({ chartContainerService: this.chartContainerService, token: token, event: event, time: this.searchTime }, lodash_1.get(eventParams, 'position') || this.position); return [2 /*return*/]; }); }); }; // 通用的图表下钻逻辑 BaseChartStore.prototype.onDrillDown = function (eventParams, ChartDrillService) { var _a; return __awaiter(this, void 0, void 0, function () { var handleDrillDown, eventService, event, token; return __generator(this, function (_b) { handleDrillDown = (_a = this.chartActionService) === null || _a === void 0 ? void 0 : _a.drillDownActionService.onDrillDown; if (!this.drillDownType || !handleDrillDown) { return [2 /*return*/]; } eventService = new ChartDrillService(this.drillDownType); event = eventService.toEvent(eventParams); token = eventService.toDrillDownToken(eventParams); handleDrillDown({ chartContainerService: this.chartContainerService, token: token, event: event, time: this.searchTime }); return [2 /*return*/]; }); }); }; BaseChartStore.prototype.getCurrentDomRect = function () { if (this.echartInst) { var dom = this.echartInst.getDom(); return dom.getBoundingClientRect(); } return null; }; __decorate([ mobx_1.observable.ref, __metadata("design:type", Object) ], BaseChartStore.prototype, "echartInst", void 0); __decorate([ mobx_1.observable, __metadata("design:type", Number) ], BaseChartStore.prototype, "chartWidth", void 0); __decorate([ mobx_1.observable, __metadata("design:type", Number) ], BaseChartStore.prototype, "chartHeight", void 0); __decorate([ mobx_1.observable.ref, __metadata("design:type", Object) ], BaseChartStore.prototype, "chartStyleService", void 0); __decorate([ mobx_1.observable.ref, __metadata("design:type", Object) ], BaseChartStore.prototype, "chartContainerService", void 0); __decorate([ mobx_1.observable.ref, __metadata("design:type", Object) ], BaseChartStore.prototype, "position", void 0); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "actionEnable", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "drillDownType", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "searchTime", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "dataset", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "originDataset", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "fields", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "rows", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "hasData", null); __decorate([ mobx_1.computed, __metadata("design:type", String), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "theme", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "themeOption", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], BaseChartStore.prototype, "chartCanClick", null); __decorate([ bind_1.default, mobx_1.action, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "setPosition", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "handleChartClient", null); __decorate([ bind_1.default, mobx_1.action, __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "setChartWidth", null); __decorate([ bind_1.default, mobx_1.action, __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "setChartHeight", null); __decorate([ bind_1.default, mobx_1.action, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "setEchartInst", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], BaseChartStore.prototype, "onChartReadyCallback", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function, Function]), __metadata("design:returntype", Promise) ], BaseChartStore.prototype, "onAction", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], BaseChartStore.prototype, "onCommonAction", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], BaseChartStore.prototype, "onDrillDown", null); return BaseChartStore; }(store_1.default)); exports.default = BaseChartStore;