Point.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * @requires SuperMap.Feature.Theme.js
  3. * @requires SuperMap.Feature.Theme.Graph.js
  4. */
  5. /**
  6. * Class: SuperMap.Feature.Theme.Point
  7. * 点状图。
  8. *
  9. * 图表 Point 配置对象 chartsSetting(<SuperMap.Layer.Graph::chartsSetting>) 可设属性如下:
  10. *
  11. * Symbolizer properties:
  12. * width - {Number} 专题要素(图表)宽度,必设参数。
  13. * height - {Number} 专题要素(图表)高度,必设参数。
  14. * codomain - {Array{Number}} 图表允许展示的数据值域,长度为 2 的一维数组,第一个元素表示值域下限,第二个元素表示值域上限,必设参数。
  15. * XOffset - {Number} 专题要素(图表)在 X 方向上的偏移值,单位像素。
  16. * YOffset - {Number} 专题要素(图表)在 Y 方向上的偏移值,单位像素。
  17. * dataViewBoxParameter - {Array{Number}} 数据视图框 dataViewBox 参数,
  18. * 它是指图表框 chartBox (由图表位置、图表宽度、图表高度构成的图表范围框)在左、下,右,上四个方向上的内偏距值。
  19. * 当使用坐标轴时 dataViewBoxParameter 的默认值为:[45, 15, 15, 15];不使用坐标轴时 dataViewBoxParameter 的默认值为:[5, 5, 5, 5]。
  20. * decimalNumber - {Number} 数据值数组 dataValues 元素值小数位数,数据的小数位处理参数,取值范围:[0, 16]。如果不设置此参数,在取数据值时不对数据做小数位处理。
  21. *
  22. * useBackground - {Boolean} 是否使用图表背景框。
  23. * backgroundStyle - {Object} 背景样式,此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Rectangle::style>。
  24. * backgroundRadius - {Array} 背景框矩形圆角半径,可以用数组分别指定四个角的圆角半径,设:左上、右上、右下、左下角的半径依次为 r1、r2、r3、r4 ,
  25. * 则 backgroundRadius 为 [r1、r2、r3、r4 ],默认值[0, 0, 0, 0]。
  26. *
  27. * xShapeBlank - {Array{Number}} 水平方向上的图形空白间隔参数。
  28. * 长度为 2 的数组,第一个元素表示第一个(沿 x 轴方向)图形点与数据视图框左端的空白间距,第二个元素表示最后一个(沿 x 轴方向)图形点与数据视图框右端端的空白间距 。
  29. *
  30. * axisStyle - {Object} 坐标轴样式,此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Line::style> 。
  31. * axisUseArrow - {Boolean} 坐标轴是否使用箭头,默认值:false,不使用箭头。
  32. * axisYTick - {Number} y 轴刻度数量,默认值:0 ,不使用刻度。
  33. * axisYLabels - {Array{String}} y 轴上的标签组内容,标签顺序沿着数据视图框左面条边自上而下,等距排布。例如:["1000", "750", "500", "250", "0"]。
  34. * axisYLabelsStyle - {Object} y 轴上的标签组样式,此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Label::style> 。
  35. * axisYLabelsOffset - {Array{Number}} y 轴上的标签组偏移量。长度为 2 的数组,数组第一项表示 y 轴标签组横向上的偏移量,向左为正,默认值:0;
  36. * 数组第二项表示 y 轴标签组纵向上的偏移量,向下为正,默认值:0。
  37. * axisXLabels - {Array{String}} x 轴上的标签组内容,标签顺序沿着数据视图框下面条边自左向右排布,例如:["92年", "95年", "99年"]。
  38. * 标签排布规则:当标签数量与 xShapeInfo 中的属性 xShapeCenter 数量相同(即标签个数与数据个数相等时), 按照 xShapeCenter 提供的位置排布标签,
  39. * 否则沿数据视图框下面条边等距排布标签。
  40. * axisXLabelsStyle - {Object} x 轴上的标签组样式,此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Label::style> 。
  41. * axisXLabelsOffset - {Array{Number}} x 轴上的标签组偏移量。长度为 2 的数组,数组第一项表示 x 轴标签组横向上的偏移量,向左为正,默认值:0;
  42. * 数组第二项表示 x 轴标签组纵向上的偏移量,向下为正,默认值:0。
  43. * useXReferenceLine - {Boolean) 是否使用水平参考线,如果为 true,在 axisYTick 大于 0 时有效,水平参考线是 y 轴刻度在数据视图框里的延伸。
  44. * xReferenceLineStyle - {Object) 水平参考线样式,此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Line::style> 。
  45. *
  46. * pointStyle - {Object} 点状图中图形点基础 style,此参数控制图形点基础样式,优先级低于 pointStyleByFields 和 pointStyleByCodomain。
  47. * 此样式对象对象可设属性: <SuperMap.Feature.ShapeParameters.Point::style> 。
  48. * pointStyleByFields - {Array{Object}} 按专题字段 themeFields(<SuperMap.Layer.Graph::themeFields>)为图形点赋 style,此参数按字段控制图形点样式,
  49. * 优先级低于 pointStyleByCodomain,高于 pointStyle。此数组中的元素是样式对象,其可设属性: <SuperMap.Feature.ShapeParameters.Point::style> 。
  50. * 此参数中的 style 与 themeFields 中的字段一一对应 。例如: themeFields(<SuperMap.Layer.Graph::themeFields>) 为 ["POP_1992", "POP_1995", "POP_1999"],
  51. * pointStyleByFields 为[style1, style2, style3],则在图表中,字段 POP_1992 对应的图形点使用 style1,字段 POP_1995 对应的图形点使用 style2 ,字段 POP_1999 对应的图形点使用 style3。
  52. * pointStyleByCodomain - {Array{Object}} 按图形点代表的数据值所在值域范围控制图形点样式,优先级高于 pointStyle 和 pointStyleByFields。
  53. * (start code)
  54. * // pointStyleByCodomain 的每个元素是个包含值域信息和与值域对应样式信息的对象,该对象(必须)有三个属性:
  55. * // start: 值域值下限(包含);
  56. * // end: 值域值上限(不包含);
  57. * // style: 数据可视化图形的 style,这个样式对象的可设属性: <SuperMap.Feature.ShapeParameters.Point::style> 。
  58. * // pointStyleByCodomain 数组形如:
  59. * [
  60. * {
  61. * start:0,
  62. * end:250,
  63. * style:{
  64. * fillColor:"#00CD00"
  65. * }
  66. * },
  67. * {
  68. * start:250,
  69. * end:500,
  70. * style:{
  71. * fillColor:"#00EE00"
  72. * }
  73. * },
  74. * {
  75. * start:500,
  76. * end:750,
  77. * style:{
  78. * fillColor:"#00FF7F"
  79. * }
  80. * },
  81. * {
  82. * start:750,
  83. * end:1500,
  84. * style:{
  85. * fillColor:"#00FF00"
  86. * }
  87. * }
  88. * ]
  89. * (end)
  90. * pointHoverStyle - {Object} 图形点 hover 状态时的样式,pointHoverAble 为 true 时有效。
  91. * pointHoverAble - {Object} 是否允许图形点使用 hover 状态,默认允许。同时设置 pointHoverAble 和 pointClickAble 为 false,可以直接屏蔽图形点对专题图层事件的响应。
  92. * pointClickAble - {Object} 是否允许图形点被点击,默认允许。同时设置 pointHoverAble 和 pointClickAble 为 false,可以直接屏蔽图形点对专题图层事件的响应。
  93. *
  94. * Inherits:
  95. * - <SuperMap.Feature.Theme.Graph>
  96. */
  97. SuperMap.Feature.Theme.Point = SuperMap.Class(SuperMap.Feature.Theme.Graph, {
  98. /**
  99. * Constructor: SuperMap.Feature.Theme.Point
  100. * 创建一个点状图。
  101. *
  102. * Parameters:
  103. * data - {<SuperMap.Feature.Vector>} 用户数据,必设参数。
  104. * layer - {<SuperMap.Layer.Graph>} 此专题要素所在图层,必设参数。
  105. * fields - {Array{String}} data 中的参与此图表生成的字段名称,必设参数。
  106. * setting - {Object} 图表配置对象,必设参数。
  107. * lonlat - {<SuperMap.LonLat>} 专题要素地理位置。默认为 data 指代的地理要素 Bounds 中心。
  108. *
  109. * Returns:
  110. * {<SuperMap.Feature.Theme.Point>} 返回一个点状图。
  111. */
  112. initialize: function(data, layer, fields, setting, lonlat, options) {
  113. SuperMap.Feature.Theme.Graph.prototype.initialize.apply(this, arguments);
  114. },
  115. /**
  116. * Method: destroy
  117. * 销毁此专题要素。调用 destroy 后此对象所以属性置为 null。
  118. */
  119. destroy: function() {
  120. SuperMap.Feature.Theme.Graph.prototype.destroy.apply(this, arguments);
  121. },
  122. //装配图形(扩展接口)
  123. assembleShapes: function(){
  124. // 图表配置对象
  125. var sets = this.setting;
  126. // 默认数据视图框
  127. if(!sets.dataViewBoxParameter){
  128. if(typeof(sets.useAxis) === "undefined" || sets.useAxis){
  129. sets.dataViewBoxParameter = [45, 15, 15, 15];
  130. }
  131. else{
  132. sets.dataViewBoxParameter = [5, 5, 5, 5];
  133. }
  134. }
  135. // 重要步骤:初始化参数
  136. if(!this.initBaseParameter()) return;
  137. var dvb = this.dataViewBox;
  138. // 值域
  139. var codomain = this.DVBCodomain;
  140. // 重要步骤:定义图表 Bar 数据视图框中单位值的含义
  141. this.DVBUnitValue = (codomain[1]-codomain[0])/this.DVBHeight;
  142. var uv = this.DVBUnitValue;
  143. var fv = this.dataValues;
  144. // 获取 x 轴上的图形信息
  145. var xShapeInfo = this.calculateXShapeInfo();
  146. if(!xShapeInfo) return;
  147. // 折线每个节点的 x 位置
  148. var xsLoc = xShapeInfo.xPositions;
  149. // 背景框,默认启用
  150. if(typeof(sets.useBackground) === "undefined" || sets.useBackground){
  151. // 将背景框图形添加到模型的 shapes 数组,注意添加顺序,后添加的图形在先添加的图形之上。
  152. this.shapes.push(SuperMap.Feature.ShapeFactory.Background(this.shapeFactory, this.chartBox, sets));
  153. }
  154. // 点状图必须使用坐标轴
  155. this.shapes = this.shapes.concat(SuperMap.Feature.ShapeFactory.GraphAxis(this.shapeFactory, dvb, sets, xShapeInfo));
  156. var isDataEffective = true;
  157. var xPx; // 图形点 x 坐标
  158. var yPx; // 图形点 y 坐标
  159. for(var i = 0, len = fv.length; i < len; i++){
  160. // 数据溢出值域检查
  161. if(fv[i] < codomain[0] || fv[i] > codomain[1]) {isDataEffective = false; return null;}
  162. xPx = xsLoc[i];
  163. yPx = dvb[1] - (fv[i] - codomain[0])/uv;
  164. // 图形点参数对象
  165. var poiSP = new SuperMap.Feature.ShapeParameters.Point(xPx, yPx);
  166. // 图形点 style
  167. poiSP.style = SuperMap.Feature.ShapeFactory.ShapeStyleTool({fillColor: "#ee9900"}, sets.pointStyle, sets.pointStyleByFields, sets.pointStyleByCodomain, i, fv[i]);
  168. // 图形点 hover 样式
  169. poiSP.highlightStyle = SuperMap.Feature.ShapeFactory.ShapeStyleTool(null, sets.pointHoverStyle);
  170. // 图形点 hover click
  171. if(typeof(sets.pointHoverAble) !== "undefined"){
  172. poiSP.hoverable = sets.pointHoverAble;
  173. }
  174. if(typeof(sets.pointClickAble) !== "undefined"){
  175. poiSP.clickable = sets.pointClickAble;
  176. }
  177. // 图形携带的数据信息
  178. poiSP.refDataID = this.data.id;
  179. poiSP.dataInfo = {
  180. field: this.fields[i],
  181. value: fv[i]
  182. };
  183. // 创建图形点并把此图形添加到图表图形数组
  184. this.shapes.push(this.shapeFactory.createShape(poiSP));
  185. }
  186. // 数据范围检测未通过,清空图形
  187. if(isDataEffective === false){
  188. this.shapes = [];
  189. }
  190. // 重要步骤:将图形转为由相对坐标表示的图形,以便在地图平移缩放过程中快速重绘图形
  191. // (统计专题图模块从结构上要求使用相对坐标,assembleShapes() 函数必须在图形装配完成后调用 shapesConvertToRelativeCoordinate() 函数)
  192. this.shapesConvertToRelativeCoordinate();
  193. },
  194. /**
  195. * Method: calculateXShapeInfo
  196. * 计算 X 轴方向上的图形信息,此信息是一个对象,包含两个属性,
  197. * 属性 xPositions 是一个一维数组,该数组元素表示图形在 x 轴方向上的像素坐标值,
  198. * 如果图形在 x 方向上有一定宽度,通常取图形在 x 方向上的中心点为图形在 x 方向上的坐标值。
  199. * width 表示图形的宽度(特别注意:点的宽度始终为 0,而不是其直径)。
  200. *
  201. * 本函数中图形配置对象 setting 可设属性:
  202. * Symbolizer properties:
  203. * xShapeBlank - {Array{Number}} 水平方向上的图形空白间隔参数。
  204. * 长度为 2 的数组,第一元素表示第折线左端点与数据视图框左端的空白间距,第二个元素表示折线右端点右端与数据视图框右端端的空白间距 。
  205. *
  206. * Returns:
  207. * {Object} 如果计算失败,返回 null;如果计算成功,返回 X 轴方向上的图形信息,此信息是一个对象,包含以下两个属性:
  208. * Symbolizer properties:
  209. * xPositions - {Array{Number}} 表示图形在 x 轴方向上的像素坐标值,如果图形在 x 方向上有一定宽度,通常取图形在 x 方向上的中心点为图形在 x 方向上的坐标值。
  210. * width - {Number} 表示图形的宽度(特别注意:点的宽度始终为 0,而不是其直径)。
  211. *
  212. */
  213. calculateXShapeInfo: function(){
  214. var dvb = this.dataViewBox; // 数据视图框
  215. var sets = this.setting; // 图表配置对象
  216. var fvc = this.dataValues.length; // 数组值个数
  217. if(fvc < 1) return null;
  218. var xBlank; // x 轴空白间隔参数
  219. var xShapePositions = []; // x 轴上图形的位置
  220. var xShapeWidth = 0; // x 轴上图形宽度(自适应)
  221. var dvbWidth = this.DVBWidth; // 数据视图框宽度
  222. var unitOffset = 0; // 单位偏移量
  223. // x 轴空白间隔参数处理
  224. if(sets.xShapeBlank && sets.xShapeBlank.length && sets.xShapeBlank.length == 2){
  225. xBlank = sets.xShapeBlank;
  226. var xsLen = dvbWidth - (xBlank[0] + xBlank[1]);
  227. if(xsLen <= fvc){ return null; }
  228. unitOffset = xsLen/(fvc - 1);
  229. }
  230. else{
  231. // 默认使用等距离空白间隔,空白间隔为图形宽度
  232. unitOffset = dvbWidth/(fvc + 1);
  233. xBlank = [unitOffset, unitOffset, unitOffset];
  234. }
  235. // 图形 x 轴上的位置计算
  236. var xOffset = 0
  237. for(var i = 0; i < fvc; i++){
  238. if(i == 0){
  239. xOffset = xBlank[0];
  240. }
  241. else{
  242. xOffset += unitOffset;
  243. }
  244. xShapePositions.push(dvb[0] + xOffset);
  245. }
  246. return {
  247. "xPositions": xShapePositions,
  248. "width": xShapeWidth
  249. };
  250. },
  251. CLASS_NAME: "SuperMap.Feature.Theme.Point"
  252. });