UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

241 lines (240 loc) 8.02 kB
import { SERIES_LAYOUT_BY_COLUMN, SOURCE_FORMAT_ARRAY_ROWS, SOURCE_FORMAT_OBJECT_ROWS } from "../../util/types.mjs"; import { normalizeToArray } from "../../util/model.mjs"; import { createHashMap, clone, isObject, map, each, hasOwn, bind, extend, isNumber } from "../../../../zrender/lib/core/util.mjs"; import { getRawSourceItemGetter, getRawSourceDataCounter, getRawSourceValueGetter } from "./dataProvider.mjs"; import { parseDataValue } from "./dataValueHelper.mjs"; import { throwError } from "../../util/log.mjs"; import { detectSourceFormat, createSource } from "../Source.mjs"; var ExternalSource = function() { function ExternalSource2() { } ExternalSource2.prototype.getRawData = function() { throw new Error("not supported"); }; ExternalSource2.prototype.getRawDataItem = function(dataIndex) { throw new Error("not supported"); }; ExternalSource2.prototype.cloneRawData = function() { return; }; ExternalSource2.prototype.getDimensionInfo = function(dim) { return; }; ExternalSource2.prototype.cloneAllDimensionInfo = function() { return; }; ExternalSource2.prototype.count = function() { return; }; ExternalSource2.prototype.retrieveValue = function(dataIndex, dimIndex) { return; }; ExternalSource2.prototype.retrieveValueFromItem = function(dataItem, dimIndex) { return; }; ExternalSource2.prototype.convertValue = function(rawVal, dimInfo) { return parseDataValue(rawVal, dimInfo); }; return ExternalSource2; }(); function createExternalSource(internalSource, externalTransform) { var extSource = new ExternalSource(); var data = internalSource.data; var sourceFormat = extSource.sourceFormat = internalSource.sourceFormat; var sourceHeaderCount = internalSource.startIndex; var errMsg = ""; if (internalSource.seriesLayoutBy !== SERIES_LAYOUT_BY_COLUMN) { throwError(errMsg); } var dimensions = []; var dimsByName = {}; var dimsDef = internalSource.dimensionsDefine; if (dimsDef) { each(dimsDef, function(dimDef, idx) { var name = dimDef.name; var dimDefExt = { index: idx, name, displayName: dimDef.displayName }; dimensions.push(dimDefExt); if (name != null) { var errMsg_1 = ""; if (hasOwn(dimsByName, name)) { throwError(errMsg_1); } dimsByName[name] = dimDefExt; } }); } else { for (var i = 0; i < internalSource.dimensionsDetectedCount || 0; i++) { dimensions.push({ index: i }); } } var rawItemGetter = getRawSourceItemGetter(sourceFormat, SERIES_LAYOUT_BY_COLUMN); if (externalTransform.__isBuiltIn) { extSource.getRawDataItem = function(dataIndex) { return rawItemGetter(data, sourceHeaderCount, dimensions, dataIndex); }; extSource.getRawData = bind(getRawData, null, internalSource); } extSource.cloneRawData = bind(cloneRawData, null, internalSource); var rawCounter = getRawSourceDataCounter(sourceFormat, SERIES_LAYOUT_BY_COLUMN); extSource.count = bind(rawCounter, null, data, sourceHeaderCount, dimensions); var rawValueGetter = getRawSourceValueGetter(sourceFormat); extSource.retrieveValue = function(dataIndex, dimIndex) { var rawItem = rawItemGetter(data, sourceHeaderCount, dimensions, dataIndex); return retrieveValueFromItem(rawItem, dimIndex); }; var retrieveValueFromItem = extSource.retrieveValueFromItem = function(dataItem, dimIndex) { if (dataItem == null) { return; } var dimDef = dimensions[dimIndex]; if (dimDef) { return rawValueGetter(dataItem, dimIndex, dimDef.name); } }; extSource.getDimensionInfo = bind(getDimensionInfo, null, dimensions, dimsByName); extSource.cloneAllDimensionInfo = bind(cloneAllDimensionInfo, null, dimensions); return extSource; } function getRawData(upstream) { var sourceFormat = upstream.sourceFormat; if (!isSupportedSourceFormat(sourceFormat)) { var errMsg = ""; throwError(errMsg); } return upstream.data; } function cloneRawData(upstream) { var sourceFormat = upstream.sourceFormat; var data = upstream.data; if (!isSupportedSourceFormat(sourceFormat)) { var errMsg = ""; throwError(errMsg); } if (sourceFormat === SOURCE_FORMAT_ARRAY_ROWS) { var result = []; for (var i = 0, len = data.length; i < len; i++) { result.push(data[i].slice()); } return result; } else if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS) { var result = []; for (var i = 0, len = data.length; i < len; i++) { result.push(extend({}, data[i])); } return result; } } function getDimensionInfo(dimensions, dimsByName, dim) { if (dim == null) { return; } if (isNumber(dim) || !isNaN(dim) && !hasOwn(dimsByName, dim)) { return dimensions[dim]; } else if (hasOwn(dimsByName, dim)) { return dimsByName[dim]; } } function cloneAllDimensionInfo(dimensions) { return clone(dimensions); } var externalTransformMap = createHashMap(); function registerExternalTransform(externalTransform) { externalTransform = clone(externalTransform); var type = externalTransform.type; var errMsg = ""; if (!type) { throwError(errMsg); } var typeParsed = type.split(":"); if (typeParsed.length !== 2) { throwError(errMsg); } var isBuiltIn = false; if (typeParsed[0] === "echarts") { type = typeParsed[1]; isBuiltIn = true; } externalTransform.__isBuiltIn = isBuiltIn; externalTransformMap.set(type, externalTransform); } function applyDataTransform(rawTransOption, sourceList, infoForPrint) { var pipedTransOption = normalizeToArray(rawTransOption); var pipeLen = pipedTransOption.length; var errMsg = ""; if (!pipeLen) { throwError(errMsg); } for (var i = 0, len = pipeLen; i < len; i++) { var transOption = pipedTransOption[i]; sourceList = applySingleDataTransform(transOption, sourceList); if (i !== len - 1) { sourceList.length = Math.max(sourceList.length, 1); } } return sourceList; } function applySingleDataTransform(transOption, upSourceList, infoForPrint, pipeIndex) { var errMsg = ""; if (!upSourceList.length) { throwError(errMsg); } if (!isObject(transOption)) { throwError(errMsg); } var transType = transOption.type; var externalTransform = externalTransformMap.get(transType); if (!externalTransform) { throwError(errMsg); } var extUpSourceList = map(upSourceList, function(upSource) { return createExternalSource(upSource, externalTransform); }); var resultList = normalizeToArray(externalTransform.transform({ upstream: extUpSourceList[0], upstreamList: extUpSourceList, config: clone(transOption.config) })); return map(resultList, function(result, resultIndex) { var errMsg2 = ""; if (!isObject(result)) { throwError(errMsg2); } if (!result.data) { throwError(errMsg2); } var sourceFormat = detectSourceFormat(result.data); if (!isSupportedSourceFormat(sourceFormat)) { throwError(errMsg2); } var resultMetaRawOption; var firstUpSource = upSourceList[0]; if (firstUpSource && resultIndex === 0 && !result.dimensions) { var startIndex = firstUpSource.startIndex; if (startIndex) { result.data = firstUpSource.data.slice(0, startIndex).concat(result.data); } resultMetaRawOption = { seriesLayoutBy: SERIES_LAYOUT_BY_COLUMN, sourceHeader: startIndex, dimensions: firstUpSource.metaRawOption.dimensions }; } else { resultMetaRawOption = { seriesLayoutBy: SERIES_LAYOUT_BY_COLUMN, sourceHeader: 0, dimensions: result.dimensions }; } return createSource(result.data, resultMetaRawOption, null); }); } function isSupportedSourceFormat(sourceFormat) { return sourceFormat === SOURCE_FORMAT_ARRAY_ROWS || sourceFormat === SOURCE_FORMAT_OBJECT_ROWS; } export { ExternalSource, applyDataTransform, registerExternalTransform };