UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

171 lines (170 loc) 6.47 kB
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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; import { first, sum, max, get } from 'lodash'; // 请求返回数据转换为树状结构 function formatData(currentData, index, buckets, bucketValue) { var result = []; if (index > buckets.length - 1) { return result; } var currentBucket = buckets[index]; var firstColKey = get(first(buckets), 'key'); currentData.map(function (data) { var _a; var currentBucketKey = get(currentBucket, 'key'); var itemIndex = result.findIndex(function (r) { return r[firstColKey].current === data[currentBucketKey]; }); var restMetricInfo = __assign({}, data); delete restMetricInfo[currentBucketKey]; if (itemIndex !== -1) { result[itemIndex].children.push(__assign({}, restMetricInfo)); } else { var restDataInfo_1 = __assign({}, data); buckets.map(function (bucket) { delete restDataInfo_1[get(bucket, 'key')]; }); result[result.length] = __assign(__assign({}, restDataInfo_1), (_a = { bucketValue: bucketValue ? bucketValue.concat(data[currentBucketKey]) : [data[currentBucketKey]], fieldType: currentBucket.fieldType }, _a[firstColKey] = { current: data[currentBucketKey] }, _a.children = __spread(restMetricInfo), _a)); } }); return result.map(function (data) { return (__assign(__assign({}, data), { children: formatData(data.children, index + 1, buckets, get(data, 'bucketValue')) })); }); } // 添加metric信息 function addMetric(data, metrics, tableChartName, children) { var _a; var resultMetric = {}; metrics.forEach(function (metric) { var current = children && children.length ? children.every(function (child) { return Array.isArray(child[metric].current); }) ? sumForArray(children.map(function (child) { return child[metric].current; })) : sum(children.map(function (child) { return child[metric].current; })) : data[metric] || 0; resultMetric[metric] = { changed: 0, compare: 0, curMaxProcess: 0, curSumProcess: 0, current: current }; }); return __assign(__assign((_a = {}, _a[tableChartName] = data[tableChartName], _a.fieldType = data.fieldType, _a.bucketValue = data.bucketValue, _a), resultMetric), { children: children // IKeyValues<IMetricValue> => ITableDataSource }); } function sumForArray(data) { var result = []; data.forEach(function (item, index) { if (index === 0) { result = item; } else { result = result.map(function (data, i) { return (data || 0) + (get(item, i) || 0); }); } }); return result; } // 添加metric信息 function addMetricInfoForFormatData(data, metrics, firstColKey) { if (!data.children || data.children.length === 0) { return addMetric(data, metrics, firstColKey); } var children = data.children.map(function (child) { return addMetricInfoForFormatData(child, metrics, firstColKey); }); return addMetric(data, metrics, firstColKey, children); } // 计算最大值 function getMaxValue(result, metricMaxMap) { result.forEach(function (data) { if (data.children) { getMaxValue(data.children, metricMaxMap); } else { Object.keys(metricMaxMap).map(function (metric) { metricMaxMap[metric] = max([ metricMaxMap[metric], data[metric].current ]); }); } }); } // 计算和 function getSumValue(result, metricSumMap) { result.forEach(function (data) { if (data.children) { getSumValue(data.children, metricSumMap); } else { Object.keys(metricSumMap).map(function (metric) { metricSumMap[metric] = sum([ metricSumMap[metric], data[metric].current ]); }); } }); } function calcCurProgress(result, metricMaxMap, metricSumMap) { result.forEach(function (data) { if (data.children) { return calcCurProgress(data.children, metricMaxMap, metricSumMap); } else { Object.keys(metricMaxMap).map(function (metric) { var current = data; current[metric].curMaxProgress = metricMaxMap[metric] ? current[metric].current / metricMaxMap[metric] : 0; current[metric].curSumProgress = metricSumMap[metric] ? current[metric].current / metricSumMap[metric] : 0; }); } }); } export function toTreeDataSource(data, buckets, metrics) { var fData = formatData(data, 0, buckets); var result = fData.map(function (data) { return addMetricInfoForFormatData(data, metrics, get(first(buckets), 'key')); }); var metricMaxMap = {}; // 存储最大值信息 var metricSumMap = {}; // 存储和信息 metrics.forEach(function (metric) { metricMaxMap[metric] = -Infinity; metricSumMap[metric] = -Infinity; }); getMaxValue(result, metricMaxMap); getSumValue(result, metricSumMap); calcCurProgress(result, metricMaxMap, metricSumMap); return result; }