MapSeries.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var createListSimply = require("../helper/createListSimply");
  21. var SeriesModel = require("../../model/Series");
  22. var _format = require("../../util/format");
  23. var encodeHTML = _format.encodeHTML;
  24. var addCommas = _format.addCommas;
  25. var dataSelectableMixin = require("../../component/helper/selectableMixin");
  26. var _dataProvider = require("../../data/helper/dataProvider");
  27. var retrieveRawAttr = _dataProvider.retrieveRawAttr;
  28. var geoSourceManager = require("../../coord/geo/geoSourceManager");
  29. var _sourceHelper = require("../../data/helper/sourceHelper");
  30. var makeSeriesEncodeForNameBased = _sourceHelper.makeSeriesEncodeForNameBased;
  31. /*
  32. * Licensed to the Apache Software Foundation (ASF) under one
  33. * or more contributor license agreements. See the NOTICE file
  34. * distributed with this work for additional information
  35. * regarding copyright ownership. The ASF licenses this file
  36. * to you under the Apache License, Version 2.0 (the
  37. * "License"); you may not use this file except in compliance
  38. * with the License. You may obtain a copy of the License at
  39. *
  40. * http://www.apache.org/licenses/LICENSE-2.0
  41. *
  42. * Unless required by applicable law or agreed to in writing,
  43. * software distributed under the License is distributed on an
  44. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  45. * KIND, either express or implied. See the License for the
  46. * specific language governing permissions and limitations
  47. * under the License.
  48. */
  49. var MapSeries = SeriesModel.extend({
  50. type: 'series.map',
  51. dependencies: ['geo'],
  52. layoutMode: 'box',
  53. /**
  54. * Only first map series of same mapType will drawMap
  55. * @type {boolean}
  56. */
  57. needsDrawMap: false,
  58. /**
  59. * Group of all map series with same mapType
  60. * @type {boolean}
  61. */
  62. seriesGroup: [],
  63. getInitialData: function (option) {
  64. var data = createListSimply(this, {
  65. coordDimensions: ['value'],
  66. encodeDefaulter: zrUtil.curry(makeSeriesEncodeForNameBased, this)
  67. });
  68. var valueDim = data.mapDimension('value');
  69. var dataNameMap = zrUtil.createHashMap();
  70. var selectTargetList = [];
  71. var toAppendNames = [];
  72. for (var i = 0, len = data.count(); i < len; i++) {
  73. var name = data.getName(i);
  74. dataNameMap.set(name, true);
  75. selectTargetList.push({
  76. name: name,
  77. value: data.get(valueDim, i),
  78. selected: retrieveRawAttr(data, i, 'selected')
  79. });
  80. }
  81. var geoSource = geoSourceManager.load(this.getMapType(), this.option.nameMap, this.option.nameProperty);
  82. zrUtil.each(geoSource.regions, function (region) {
  83. var name = region.name;
  84. if (!dataNameMap.get(name)) {
  85. selectTargetList.push({
  86. name: name
  87. });
  88. toAppendNames.push(name);
  89. }
  90. });
  91. this.updateSelectedMap(selectTargetList); // Complete data with missing regions. The consequent processes (like visual
  92. // map and render) can not be performed without a "full data". For example,
  93. // find `dataIndex` by name.
  94. data.appendValues([], toAppendNames);
  95. return data;
  96. },
  97. /**
  98. * If no host geo model, return null, which means using a
  99. * inner exclusive geo model.
  100. */
  101. getHostGeoModel: function () {
  102. var geoIndex = this.option.geoIndex;
  103. return geoIndex != null ? this.dependentModels.geo[geoIndex] : null;
  104. },
  105. getMapType: function () {
  106. return (this.getHostGeoModel() || this).option.map;
  107. },
  108. // _fillOption: function (option, mapName) {
  109. // Shallow clone
  110. // option = zrUtil.extend({}, option);
  111. // option.data = geoCreator.getFilledRegions(option.data, mapName, option.nameMap);
  112. // return option;
  113. // },
  114. getRawValue: function (dataIndex) {
  115. // Use value stored in data instead because it is calculated from multiple series
  116. // FIXME Provide all value of multiple series ?
  117. var data = this.getData();
  118. return data.get(data.mapDimension('value'), dataIndex);
  119. },
  120. /**
  121. * Get model of region
  122. * @param {string} name
  123. * @return {module:echarts/model/Model}
  124. */
  125. getRegionModel: function (regionName) {
  126. var data = this.getData();
  127. return data.getItemModel(data.indexOfName(regionName));
  128. },
  129. /**
  130. * Map tooltip formatter
  131. *
  132. * @param {number} dataIndex
  133. */
  134. formatTooltip: function (dataIndex, multipleSeries, dataType, renderMode) {
  135. // FIXME orignalData and data is a bit confusing
  136. var data = this.getData();
  137. var formattedValue = addCommas(this.getRawValue(dataIndex));
  138. var name = data.getName(dataIndex);
  139. var seriesGroup = this.seriesGroup;
  140. var seriesNames = [];
  141. for (var i = 0; i < seriesGroup.length; i++) {
  142. var otherIndex = seriesGroup[i].originalData.indexOfName(name);
  143. var valueDim = data.mapDimension('value');
  144. if (!isNaN(seriesGroup[i].originalData.get(valueDim, otherIndex))) {
  145. seriesNames.push(encodeHTML(seriesGroup[i].name));
  146. }
  147. }
  148. var newLine = renderMode === 'html' ? '<br/>' : '\n';
  149. return seriesNames.join(', ') + newLine + encodeHTML(name + ' : ' + formattedValue);
  150. },
  151. /**
  152. * @implement
  153. */
  154. getTooltipPosition: function (dataIndex) {
  155. if (dataIndex != null) {
  156. var name = this.getData().getName(dataIndex);
  157. var geo = this.coordinateSystem;
  158. var region = geo.getRegion(name);
  159. return region && geo.dataToPoint(region.center);
  160. }
  161. },
  162. setZoom: function (zoom) {
  163. this.option.zoom = zoom;
  164. },
  165. setCenter: function (center) {
  166. this.option.center = center;
  167. },
  168. defaultOption: {
  169. // 一级层叠
  170. zlevel: 0,
  171. // 二级层叠
  172. z: 2,
  173. coordinateSystem: 'geo',
  174. // map should be explicitly specified since ec3.
  175. map: '',
  176. // If `geoIndex` is not specified, a exclusive geo will be
  177. // created. Otherwise use the specified geo component, and
  178. // `map` and `mapType` are ignored.
  179. // geoIndex: 0,
  180. // 'center' | 'left' | 'right' | 'x%' | {number}
  181. left: 'center',
  182. // 'center' | 'top' | 'bottom' | 'x%' | {number}
  183. top: 'center',
  184. // right
  185. // bottom
  186. // width:
  187. // height
  188. // Aspect is width / height. Inited to be geoJson bbox aspect
  189. // This parameter is used for scale this aspect
  190. aspectScale: 0.75,
  191. ///// Layout with center and size
  192. // If you wan't to put map in a fixed size box with right aspect ratio
  193. // This two properties may more conveninet
  194. // layoutCenter: [50%, 50%]
  195. // layoutSize: 100
  196. // 数值合并方式,默认加和,可选为:
  197. // 'sum' | 'average' | 'max' | 'min'
  198. // mapValueCalculation: 'sum',
  199. // 地图数值计算结果小数精度
  200. // mapValuePrecision: 0,
  201. // 显示图例颜色标识(系列标识的小圆点),图例开启时有效
  202. showLegendSymbol: true,
  203. // 选择模式,默认关闭,可选single,multiple
  204. // selectedMode: false,
  205. dataRangeHoverLink: true,
  206. // 是否开启缩放及漫游模式
  207. // roam: false,
  208. // Define left-top, right-bottom coords to control view
  209. // For example, [ [180, 90], [-180, -90] ],
  210. // higher priority than center and zoom
  211. boundingCoords: null,
  212. // Default on center of map
  213. center: null,
  214. zoom: 1,
  215. scaleLimit: null,
  216. label: {
  217. show: false,
  218. color: '#000'
  219. },
  220. // scaleLimit: null,
  221. itemStyle: {
  222. borderWidth: 0.5,
  223. borderColor: '#444',
  224. areaColor: '#eee'
  225. },
  226. emphasis: {
  227. label: {
  228. show: true,
  229. color: 'rgb(100,0,0)'
  230. },
  231. itemStyle: {
  232. areaColor: 'rgba(255,215,0,0.8)'
  233. }
  234. },
  235. nameProperty: 'name'
  236. }
  237. });
  238. zrUtil.mixin(MapSeries, dataSelectableMixin);
  239. var _default = MapSeries;
  240. module.exports = _default;