From f56210dc9ca64c7ee028d8fba783b1eb4333d14d Mon Sep 17 00:00:00 2001 From: chenxianhui Date: Fri, 10 Nov 2023 10:12:37 +0800 Subject: [PATCH] =?UTF-8?q?ISVJ-6584=20=E5=A4=A7=E5=B1=8F=E8=83=BD?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA=E6=95=B0=E6=8D=AE=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=9A=84=E5=AD=97=E6=AE=B5=E5=88=AB=E5=90=8D=20review?= =?UTF-8?q?=20by=20luox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/_utils/EchartsDataService.js | 14 ++++++------- .../__tests__/iServerRestService.spec.js | 10 +++++++++ src/common/_utils/iServerRestService.js | 13 +++++++++++- src/common/_utils/statistics.js | 21 ++++++++++++------- test/unit/mocks/supermap.js | 3 +++ 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/common/_utils/EchartsDataService.js b/src/common/_utils/EchartsDataService.js index f657ac1d..d66a5547 100644 --- a/src/common/_utils/EchartsDataService.js +++ b/src/common/_utils/EchartsDataService.js @@ -159,13 +159,13 @@ export default class EchartsDataService { const sortMatchItem = datasetOptions.find(item => item.sort && item.sort !== 'unsort'); const xField = datasetOptions[0].xField; const yFields = datasetOptions.map(item => item.yField); - const fieldCaptions = data.fieldCaptions; // 所有字段 - const xFieldIndex = fieldCaptions.indexOf(xField); // x字段的下标 + const fields = data.fields; // 所有字段 + const xFieldIndex = fields.indexOf(xField); // x字段的下标 const fieldValueIndex = this._getUniqFieldDatas(data, xFieldIndex); const xData = this._stasticXData(fieldValueIndex); const yDatas = []; yFields.forEach(yField => { - const yFieldIndex = fieldCaptions.indexOf(yField); // y字段的下标 + const yFieldIndex = fields.indexOf(yField); // y字段的下标 const fieldValues = yFieldIndex < 0 ? [] : data.fieldValues[yFieldIndex]; // y字段的所有feature值 const yData = this._stasticYData(fieldValues, fieldValueIndex); yDatas.push(yData); @@ -391,11 +391,11 @@ export default class EchartsDataService { * @returns {Object} 解析好的Ydata,xdata */ _fieldsData(data, datasetOption, features) { - let fieldCaptions, fieldValues, xFieldIndex, yFieldIndex, xData, yData, result; + let fields, fieldValues, xFieldIndex, yFieldIndex, xData, yData, result; let { yField, xField, isStastic } = datasetOption; - fieldCaptions = data.fieldCaptions; // 所有x字段 - xFieldIndex = fieldCaptions.indexOf(xField); // x字段的下标 - yFieldIndex = fieldCaptions.indexOf(yField); // y字段的下标 + fields = data.fields; // 所有x字段 + xFieldIndex = fields.indexOf(xField); // x字段的下标 + yFieldIndex = fields.indexOf(yField); // y字段的下标 fieldValues = yFieldIndex < 0 ? [] : data.fieldValues[yFieldIndex]; // y字段的所有feature值 // 该数据是否需要统计,统计的是数组下标 if (isStastic) { diff --git a/src/common/_utils/__tests__/iServerRestService.spec.js b/src/common/_utils/__tests__/iServerRestService.spec.js index dbdaba6b..2d65221f 100644 --- a/src/common/_utils/__tests__/iServerRestService.spec.js +++ b/src/common/_utils/__tests__/iServerRestService.spec.js @@ -26,4 +26,14 @@ describe('iServerRestService', () => { expect(params[0][0].queryOption).toBe('ATTRIBUTEANDGEOMETRY'); done(); }); + it('_getDataFeaturesBySql', done => { + const service = new iServerRestService('url', { hasGeometry: true }); + service._getDataFeaturesBySql('mock', {}); + service.on({ + getdatasucceeded: data => { + expect(data.fields).toEqual(['SMID', '名称']) + done(); + }, + }) + }); }); diff --git a/src/common/_utils/iServerRestService.js b/src/common/_utils/iServerRestService.js index 2ce775e9..91eb4437 100644 --- a/src/common/_utils/iServerRestService.js +++ b/src/common/_utils/iServerRestService.js @@ -293,8 +293,19 @@ export default class iServerRestService extends Events { // 数据来自restdata---results.result.features this.features = results.result.features; features = this.features.features; + let fields = []; let fieldCaptions = []; let fieldTypes = []; + if(results.result.datasetInfos) { + const fieldInfos = results.result.datasetInfos[0].fieldInfos; + fieldInfos.forEach(fieldInfo => { + if(fieldInfo.name) { + fields.push(fieldInfo.name.toUpperCase()); + fieldCaptions.push(fieldInfo.caption.toUpperCase()); + fieldTypes.push(fieldInfo.type); + } + }); + } if (features && features.length > 0) { - data = statisticsFeatures(features); + data = statisticsFeatures(features, fields, fieldCaptions, fieldTypes); data.totalCount = results.result.totalCount; } else { this.triggerEvent('featureisempty', { diff --git a/src/common/_utils/statistics.js b/src/common/_utils/statistics.js index 9c4dee6a..f1321f61 100644 --- a/src/common/_utils/statistics.js +++ b/src/common/_utils/statistics.js @@ -2,18 +2,25 @@ export function statisticsFeatures(features, fields, fieldCaptions, fieldTypes) const data = { features, fields: fields || [], - fieldCaptions: fieldCaptions || [], fieldValues: [], fieldTypes }; - if (features && !!features.length && !fieldCaptions && !fields) { - const properties = Object.assign({}, features[0].properties, features[features.length - 1].properties); - // 获取每个字段的名字和类型 - for (let attr in properties) { - data.fieldCaptions.push(attr); - data.fields.push(attr); + if(features && !!features.length) { + if(fieldCaptions && fieldCaptions.length) { + features.forEach(feature => { + const newProperties = {}; + for (const field in feature.properties) { + const index = fields.indexOf(field); + const fieldCaption = fieldCaptions[index]; + newProperties[fieldCaption || field] = feature.properties[field]; + } + feature.properties = newProperties; + }); } + const properties = Object.assign({}, features[0].properties, features[features.length - 1].properties); + data.fields = Object.keys(properties); } + for (let m in data.fields) { const fieldValue = []; for (let j in features) { diff --git a/test/unit/mocks/supermap.js b/test/unit/mocks/supermap.js index 12126a10..1f4da6ac 100644 --- a/test/unit/mocks/supermap.js +++ b/test/unit/mocks/supermap.js @@ -559,6 +559,9 @@ var results = { var GetFeaturesBySQLService = (SuperMap.GetFeaturesBySQLService = (url, options) => { const result = { result: { + datasetInfos: [{ + fieldInfos:[{name: "SmID", caption: "SmID", type: "INT32"}, {name: "NAME", caption: "名称", type: "WTEXT"}] + }], features: { type: 'FeatureCollection', features: [