UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

417 lines (416 loc) 17.9 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 __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __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 app_sdk_1 = require("@qn-pandora/app-sdk"); var utils_1 = require("../../../base-pandora-visualization/services/chart-style/utils"); var bind_1 = __importDefault(require("../../../utils/bind")); var chart_drill_down_1 = require("../../../services/chart-drill-down"); var chart_common_action_1 = require("../../../services/chart-common-action"); var one_d_1 = __importDefault(require("../stores/one-d")); var normalizeTimeFormat_1 = require("../../../utils/normalizeTimeFormat"); var chart_style_1 = require("../../../constants/chart-style"); var constant_1 = require("./constant"); var _a = app_sdk_1.utils.unit, getUnitTransformer = _a.getUnitTransformer, toFixed = _a.toFixed; var SingleChartStore = /** @class */ (function (_super) { __extends(SingleChartStore, _super); function SingleChartStore() { return _super !== null && _super.apply(this, arguments) || this; } Object.defineProperty(SingleChartStore.prototype, "colorMetrics", { get: function () { var _this = this; var colorMetrics = this.chartStyleService.colorMetrics; return colorMetrics .map(function (metric) { return _this.originDataset.fields.find(function (field) { return field.key === metric; }); }) .filter(function (metric) { return !!metric; }); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "colorMetricType", { get: function () { var _this = this; var _a = this.dataset.fields, fields = _a === void 0 ? [] : _a; var field = fields.find(function (item) { return item && item.key === _this.colorMetricKey; }); return field && field.type; }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentMetricType", { get: function () { var _this = this; var _a = this.dataset.fields, fields = _a === void 0 ? [] : _a; var field = fields.find(function (item) { return item && item.key === _this.currentMetricKey; }); return field && field.type; }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "chartStyleOption", { get: function () { return this.chartStyleService.option; }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentData", { get: function () { var data = this.getData(); return data[0]; }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "colorData", { get: function () { var data = this.getColorData(); return data[0]; }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "minValue", { get: function () { var data = this.getData(); return lodash_1.min(data); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "maxValue", { get: function () { var data = this.getData(); return lodash_1.max(data); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentMetricKey", { get: function () { return lodash_1.get(this.currentMetrics, [0, 'key']); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentMetricName", { get: function () { return lodash_1.get(this.currentMetrics, [0, 'name']); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "colorMetricKey", { get: function () { return lodash_1.get(this.colorMetrics, [0, 'key']); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "displayField", { get: function () { var _this = this; var displayField = this.chartStyleService.displayField; var fields = displayField.fields; var resultFields = fields ? fields.map(function (item) { var name = item.name, unit = item.unit, precision = item.precision; var value = _this.getFieldValue(name); var valueType = ''; if (!lodash_1.includes(constant_1.times, name)) { if (item.trendMode === chart_style_1.ESingleFieldTrendMode.ZeroAndNotZero) { if (lodash_1.isNumber(value) && value === 0) { valueType = constant_1.ETrend.Down; } else if (lodash_1.isNumber(value) && value !== 0) { valueType = constant_1.ETrend.Up; } } else { if (lodash_1.isNumber(value) && value < 0) { valueType = constant_1.ETrend.Down; } else if (lodash_1.isNumber(value) && value === 0) { valueType = constant_1.ETrend.Equal; } else if (lodash_1.isNumber(value) && value > 0) { valueType = constant_1.ETrend.Up; } } } return __assign(__assign({}, item), { value: lodash_1.isNumber(value) && unit.length > 0 ? getUnitTransformer(unit[1])(value, precision) : lodash_1.isNumber(value) ? toFixed(value, precision) : value, trend: valueType }); }) : []; return __assign(__assign({}, displayField), { fields: resultFields }); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentTimeBuckets", { get: function () { return this.currentBuckets .filter(function (bucket) { return bucket.fieldType === app_sdk_1.EChartFieldType.Time; }) .map(function (bucket) { return bucket.name; }); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "defaultColor", { get: function () { return (lodash_1.get(this.themeOption, [ 'gauge', this.chartStyleService.chartType, 'color' ]) || lodash_1.get(this.themeOption, 'color')); }, enumerable: false, configurable: true }); SingleChartStore.prototype.getFieldValue = function (field) { var _a = this.originDataset, fields = _a.fields, rows = _a.rows; var fieldIndex = utils_1.getFieldIndex(field, fields); var data = rows.map(function (row) { return lodash_1.get(row, [fieldIndex, 0]); }); var fieldType = this.getFieldType(field); if (fieldType === 'Time') { return normalizeTimeFormat_1.normalizeTimeFormat(data[0]); } return data[0]; }; SingleChartStore.prototype.getFieldType = function (field) { var fields = this.originDataset.fields; var fieldIndex = utils_1.getFieldIndex(field, fields); return fieldIndex === -1 ? '' : fields[fieldIndex].fieldType; }; SingleChartStore.prototype.displayDataFormatter = function (params) { var unitTransformer = this.chartStyleService.unitTransformer; var currentData = lodash_1.toNumber(params); if (!isNaN(currentData)) { return "" + unitTransformer(currentData); } return '--'; }; SingleChartStore.prototype.isValueNumber = function (value) { return !isNaN(lodash_1.toNumber(value)); }; SingleChartStore.prototype.getDataWithMetric = function (key) { var result = []; var _a = this.originDataset, rows = _a.rows, fields = _a.fields; if (key && lodash_1.size(fields) > 0 && lodash_1.size(rows) > 0) { if (this.isDateSeries) { var fieldNames_1 = fields.map(function (field) { return field.name; }); var sortField = '_time'; if (!lodash_1.includes(this.currentTimeBuckets, '_time')) { sortField = '_indexTime'; } var fieldsMap = rows.map(function (row) { var obj = lodash_1.zipObject(fieldNames_1, row.map(function (item) { return item[0]; })); return obj; }); var filterResult = lodash_1.orderBy(fieldsMap, sortField, 'desc').map(function (result) { return lodash_1.get(result, key); }); return filterResult; } var metricIndex_1 = utils_1.getFieldIndex(key, fields); return rows.map(function (row) { return lodash_1.get(row, [metricIndex_1, 0]); }); } return result; }; /** * 将dataset数据转换成单值图需要的数据, 返回值为字符串或数值数组, 便于根据stat函数求值 */ SingleChartStore.prototype.getData = function () { return this.getDataWithMetric(this.currentMetricKey); }; SingleChartStore.prototype.getColorData = function () { return this.getDataWithMetric(this.colorMetricKey); }; SingleChartStore.prototype.handleDrillDown = function () { this.onAction({ currentBuckets: this.currentBuckets, currentMetrics: this.currentMetrics, rows: this.originDataset.rows, currentData: this.currentData, fields: this.originDataset.fields }, chart_drill_down_1.SingleChartDrillDownService, chart_common_action_1.SingleChartActionService); }; // 此方法用于将数据中的最大最小配置到styleService中,当前代码中暂时没有地方用到 // 如果后期增加自动调整最大最小值时可以直接调用此方法 SingleChartStore.prototype.updateMinMaxByData = function () { if (lodash_1.isNumber(this.minValue)) { this.chartStyleService.setGaugeMin(this.minValue); } if (lodash_1.isNumber(this.maxValue)) { this.chartStyleService.setGaugeMax(this.maxValue); } }; __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetrics", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetricType", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricType", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "chartStyleOption", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentData", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorData", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "minValue", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "maxValue", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricKey", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricName", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetricKey", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "displayField", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentTimeBuckets", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "defaultColor", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "getFieldValue", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "getFieldType", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "displayDataFormatter", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "isValueNumber", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getDataWithMetric", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getData", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getColorData", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "handleDrillDown", null); __decorate([ bind_1.default, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "updateMinMaxByData", null); return SingleChartStore; }(one_d_1.default)); exports.default = SingleChartStore;