UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

412 lines (411 loc) 17.1 kB
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); }; import { computed } from 'mobx'; import { get, min, size, toNumber, max, isNumber, includes, zipObject, orderBy } from 'lodash'; import { EChartFieldType, utils } from '@qn-pandora/app-sdk'; import { getFieldIndex } from '../../../base-pandora-visualization/services/chart-style/utils'; import bind from '../../../utils/bind'; import { SingleChartDrillDownService } from '../../../services/chart-drill-down'; import { SingleChartActionService } from '../../../services/chart-common-action'; import OneDChartStore from '../stores/one-d'; import { normalizeTimeFormat } from '../../../utils/normalizeTimeFormat'; import { ESingleFieldTrendMode } from '../../../constants/chart-style'; import { ETrend, times } from './constant'; var _a = 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 min(data); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "maxValue", { get: function () { var data = this.getData(); return max(data); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentMetricKey", { get: function () { return get(this.currentMetrics, [0, 'key']); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "currentMetricName", { get: function () { return get(this.currentMetrics, [0, 'name']); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "colorMetricKey", { get: function () { return 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 (!includes(times, name)) { if (item.trendMode === ESingleFieldTrendMode.ZeroAndNotZero) { if (isNumber(value) && value === 0) { valueType = ETrend.Down; } else if (isNumber(value) && value !== 0) { valueType = ETrend.Up; } } else { if (isNumber(value) && value < 0) { valueType = ETrend.Down; } else if (isNumber(value) && value === 0) { valueType = ETrend.Equal; } else if (isNumber(value) && value > 0) { valueType = ETrend.Up; } } } return __assign(__assign({}, item), { value: isNumber(value) && unit.length > 0 ? getUnitTransformer(unit[1])(value, precision) : 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 === EChartFieldType.Time; }) .map(function (bucket) { return bucket.name; }); }, enumerable: false, configurable: true }); Object.defineProperty(SingleChartStore.prototype, "defaultColor", { get: function () { return (get(this.themeOption, [ 'gauge', this.chartStyleService.chartType, 'color' ]) || 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 = getFieldIndex(field, fields); var data = rows.map(function (row) { return get(row, [fieldIndex, 0]); }); var fieldType = this.getFieldType(field); if (fieldType === 'Time') { return normalizeTimeFormat(data[0]); } return data[0]; }; SingleChartStore.prototype.getFieldType = function (field) { var fields = this.originDataset.fields; var fieldIndex = getFieldIndex(field, fields); return fieldIndex === -1 ? '' : fields[fieldIndex].fieldType; }; SingleChartStore.prototype.displayDataFormatter = function (params) { var unitTransformer = this.chartStyleService.unitTransformer; var currentData = toNumber(params); if (!isNaN(currentData)) { return "" + unitTransformer(currentData); } return '--'; }; SingleChartStore.prototype.isValueNumber = function (value) { return !isNaN(toNumber(value)); }; SingleChartStore.prototype.getDataWithMetric = function (key) { var result = []; var _a = this.originDataset, rows = _a.rows, fields = _a.fields; if (key && size(fields) > 0 && size(rows) > 0) { if (this.isDateSeries) { var fieldNames_1 = fields.map(function (field) { return field.name; }); var sortField = '_time'; if (!includes(this.currentTimeBuckets, '_time')) { sortField = '_indexTime'; } var fieldsMap = rows.map(function (row) { var obj = zipObject(fieldNames_1, row.map(function (item) { return item[0]; })); return obj; }); var filterResult = orderBy(fieldsMap, sortField, 'desc').map(function (result) { return get(result, key); }); return filterResult; } var metricIndex_1 = getFieldIndex(key, fields); return rows.map(function (row) { return 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 }, SingleChartDrillDownService, SingleChartActionService); }; // 此方法用于将数据中的最大最小配置到styleService中,当前代码中暂时没有地方用到 // 如果后期增加自动调整最大最小值时可以直接调用此方法 SingleChartStore.prototype.updateMinMaxByData = function () { if (isNumber(this.minValue)) { this.chartStyleService.setGaugeMin(this.minValue); } if (isNumber(this.maxValue)) { this.chartStyleService.setGaugeMax(this.maxValue); } }; __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetrics", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetricType", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricType", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "chartStyleOption", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "minValue", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "maxValue", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricKey", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentMetricName", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "colorMetricKey", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "displayField", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "currentTimeBuckets", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SingleChartStore.prototype, "defaultColor", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "getFieldValue", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "getFieldType", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "displayDataFormatter", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "isValueNumber", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getDataWithMetric", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getData", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], SingleChartStore.prototype, "getColorData", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "handleDrillDown", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SingleChartStore.prototype, "updateMinMaxByData", null); return SingleChartStore; }(OneDChartStore)); export default SingleChartStore;