-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathLine.js
More file actions
258 lines (226 loc) · 9.73 KB
/
Line.js
File metadata and controls
258 lines (226 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {ShapeFactory} from './feature/ShapeFactory';
import {Point} from './feature/Point';
import {Line as RenderLine} from './feature/Line';
import {Graph} from './Graph';
/**
* @class FeatureThemeLine
* @aliasclass Feature.Theme.Line
* @deprecatedclass SuperMap.Feature.Theme.Line
* @classdesc 折线图。
* @category Visualization Theme
* @example
* // pointStyleByCodomain 参数用法示例
* // pointStyleByCodomain 的每个元素是个包含值域信息和与值域对应样式信息的对象,该对象(必须)有三个属性:
* // start: 值域值下限(包含);
* // end: 值域值上限(不包含);
* // style: 数据可视化图形的 style,这个样式对象的可设属性: <Point.style> 。
* // pointStyleByCodomain 数组形如:
* [
* {
* start:0,
* end:250,
* style:{
* fillColor:"#00CD00"
* }
* },
* {
* start:250,
* end:500,
* style:{
* fillColor:"#00EE00"
* }
* },
* {
* start:500,
* end:750,
* style:{
* fillColor:"#00FF7F"
* }
* },
* {
* start:750,
* end:1500,
* style:{
* fillColor:"#00FF00"
* }
* }
* ]
*
* @extends FeatureThemeGraph
* @param {FeatureVector} data - 用户数据。
* @param {SuperMap.Layer.Graph} layer - 此专题要素所在图层。
* @param {Array.<string>} fields - data 中的参与此图表生成的字段名称。
* @param {FeatureThemeLine.setting} setting - 图表配置对象。
* @param {LonLat} [lonlat] - 专题要素地理位置。默认为 data 指代的地理要素 Bounds 中心。
* @usage
* @private
*/
export class Line extends Graph {
constructor(data, layer, fields, setting, lonlat, options) {
super(data, layer, fields, setting, lonlat, options);
this.CLASS_NAME = "SuperMap.Feature.Theme.Line";
}
/**
* @function FeatureThemeLine.prototype.destroy
* @override
*/
destroy() {
super.destroy();
}
/**
* @function FeatureThemeLine.prototype.assembleShapes
* @description 装配图形(扩展接口)。
*/
assembleShapes() {
// 图表配置对象
var sets = this.setting;
// 默认数据视图框
if (!sets.dataViewBoxParameter) {
if (typeof(sets.useAxis) === "undefined" || sets.useAxis) {
sets.dataViewBoxParameter = [45, 15, 15, 15];
} else {
sets.dataViewBoxParameter = [5, 5, 5, 5];
}
}
// 重要步骤:初始化参数
if (!this.initBaseParameter()) {
return;
}
var dvb = this.dataViewBox;
// 值域
var codomain = this.DVBCodomain;
// 重要步骤:定义图表 FeatureThemeBar 数据视图框中单位值的含义
this.DVBUnitValue = (codomain[1] - codomain[0]) / this.DVBHeight;
var uv = this.DVBUnitValue;
// 数据值数组
var fv = this.dataValues;
if (fv.length < 1) {
return;
} // 没有数据
// 获取 x 轴上的图形信息
var xShapeInfo = this.calculateXShapeInfo();
if (!xShapeInfo) {
return;
}
// 折线每个节点的 x 位置
var xsLoc = xShapeInfo.xPositions;
// 背景框,默认启用
if (typeof(sets.useBackground) === "undefined" || sets.useBackground) {
// 将背景框图形添加到模型的 shapes 数组,注意添加顺序,后添加的图形在先添加的图形之上。
this.shapes.push(ShapeFactory.Background(this.shapeFactory, this.chartBox, sets));
}
// 折线图必须使用坐标轴
this.shapes = this.shapes.concat(ShapeFactory.GraphAxis(this.shapeFactory, dvb, sets, xShapeInfo));
// var isDataEffective = true;
var xPx; // 折线节点 x 坐标
var yPx; // 折线节点 y 坐标
var poiLists = []; // 折线节点数组
var shapePois = []; // 折线节点图形数组
for (var i = 0, len = fv.length; i < len; i++) {
// 数据溢出值域检查
if (fv[i] < codomain[0] || fv[i] > codomain[1]) {
// isDataEffective = false;
return null;
}
xPx = xsLoc[i];
yPx = dvb[1] - (fv[i] - codomain[0]) / uv;
// 折线节点参数对象
var poiSP = new Point(xPx, yPx);
// 折线节点 style
poiSP.style = ShapeFactory.ShapeStyleTool({fillColor: "#ee9900"}, sets.pointStyle, sets.pointStyleByFields, sets.pointStyleByCodomain, i, fv[i]);
// 折线节点 hover 样式
poiSP.highlightStyle = ShapeFactory.ShapeStyleTool(null, sets.pointHoverStyle);
// 折线节点 hover click
if (typeof(sets.pointHoverAble) !== "undefined") {
poiSP.hoverable = sets.pointHoverAble;
}
if (typeof(sets.pointClickAble) !== "undefined") {
poiSP.clickable = sets.pointClickAble;
}
// 图形携带的数据信息
poiSP.refDataID = this.data.id;
poiSP.dataInfo = {
field: this.fields[i],
value: fv[i]
};
// 创建图形并把此图形添加到折线节点图形数组
shapePois.push(this.shapeFactory.createShape(poiSP));
// 添加折线节点到折线节点数组
var poi = [xPx, yPx];
poiLists.push(poi);
}
// 折线参数对象
var lineSP = new RenderLine(poiLists);
lineSP.style = ShapeFactory.ShapeStyleTool({strokeColor: "#ee9900"}, sets.lineStyle);
// 禁止事件响应
lineSP.clickable = false;
lineSP.hoverable = false;
var shapeLine = this.shapeFactory.createShape(lineSP);
this.shapes.push(shapeLine);
// 添加节点到图表图形数组
this.shapes = this.shapes.concat(shapePois);
// // 数据范围检测未通过,清空图形
// if (isDataEffective === false) {
// this.shapes = [];
// }
// 重要步骤:将图形转为由相对坐标表示的图形,以便在地图平移缩放过程中快速重绘图形
// (统计专题图模块从结构上要求使用相对坐标,assembleShapes() 函数必须在图形装配完成后调用 shapesConvertToRelativeCoordinate() 函数)
this.shapesConvertToRelativeCoordinate();
}
/**
* @function FeatureThemeLine.prototype.calculateXShapeInfo
* @description 计算 X 轴方向上的图形信息,此信息是一个对象,包含两个属性,
* 属性 xPositions 是一个一维数组,该数组元素表示图形在 x 轴方向上的像素坐标值,
* 如果图形在 x 方向上有一定宽度,通常取图形在 x 方向上的中心点为图形在 x 方向上的坐标值。
* width 表示图形的宽度(特别注意:点的宽度始终为 0,而不是其直径)。
* 本函数中图形配置对象 setting 可设属性:<br>
* xShapeBlank - {Array.<number>} 水平方向上的图形空白间隔参数。
* 长度为 2 的数组,第一元素表示第折线左端点与数据视图框左端的空白间距,第二个元素表示折线右端点右端与数据视图框右端端的空白间距 。
* @returns {Object} 如果计算失败,返回 null;如果计算成功,返回 X 轴方向上的图形信息,此信息是一个对象,包含以下两个属性:<br>
* xPositions - {Array.<number>} 表示图形在 x 轴方向上的像素坐标值,如果图形在 x 方向上有一定宽度,通常取图形在 x 方向上的中心点为图形在 x 方向上的坐标值。<br>
* width - {number} 表示图形的宽度(特别注意:点的宽度始终为 0,而不是其直径)。
*/
calculateXShapeInfo() {
var dvb = this.dataViewBox; // 数据视图框
var sets = this.setting; // 图表配置对象
var fvc = this.dataValues.length; // 数组值个数
if (fvc < 1) {
return null;
}
var xBlank; // x 轴空白间隔参数
var xShapePositions = []; // x 轴上图形的位置
var xShapeWidth = 0; // x 轴上图形宽度(自适应)
var dvbWidth = this.DVBWidth; // 数据视图框宽度
var unitOffset = 0; // 单位偏移量
// x 轴空白间隔参数处理
if (sets.xShapeBlank && sets.xShapeBlank.length && sets.xShapeBlank.length == 2) {
xBlank = sets.xShapeBlank;
var xsLen = dvbWidth - (xBlank[0] + xBlank[1]);
if (xsLen <= fvc) {
return null;
}
unitOffset = xsLen / (fvc - 1);
} else {
// 默认使用等距离空白间隔,空白间隔为图形宽度
unitOffset = dvbWidth / (fvc + 1);
xBlank = [unitOffset, unitOffset, unitOffset];
}
// 图形 x 轴上的位置计算
var xOffset = 0
for (var i = 0; i < fvc; i++) {
if (i == 0) {
xOffset = xBlank[0];
} else {
xOffset += unitOffset;
}
xShapePositions.push(dvb[0] + xOffset);
}
return {
"xPositions": xShapePositions,
"width": xShapeWidth
};
}
}