LinesSeries.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var SeriesModel = require("../../model/Series");
  22. var List = require("../../data/List");
  23. var _util = require("zrender/lib/core/util");
  24. var concatArray = _util.concatArray;
  25. var mergeAll = _util.mergeAll;
  26. var map = _util.map;
  27. var _format = require("../../util/format");
  28. var encodeHTML = _format.encodeHTML;
  29. var CoordinateSystem = require("../../CoordinateSystem");
  30. /*
  31. * Licensed to the Apache Software Foundation (ASF) under one
  32. * or more contributor license agreements. See the NOTICE file
  33. * distributed with this work for additional information
  34. * regarding copyright ownership. The ASF licenses this file
  35. * to you under the Apache License, Version 2.0 (the
  36. * "License"); you may not use this file except in compliance
  37. * with the License. You may obtain a copy of the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing,
  42. * software distributed under the License is distributed on an
  43. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  44. * KIND, either express or implied. See the License for the
  45. * specific language governing permissions and limitations
  46. * under the License.
  47. */
  48. /* global Uint32Array, Float64Array, Float32Array */
  49. var Uint32Arr = typeof Uint32Array === 'undefined' ? Array : Uint32Array;
  50. var Float64Arr = typeof Float64Array === 'undefined' ? Array : Float64Array;
  51. function compatEc2(seriesOpt) {
  52. var data = seriesOpt.data;
  53. if (data && data[0] && data[0][0] && data[0][0].coord) {
  54. seriesOpt.data = map(data, function (itemOpt) {
  55. var coords = [itemOpt[0].coord, itemOpt[1].coord];
  56. var target = {
  57. coords: coords
  58. };
  59. if (itemOpt[0].name) {
  60. target.fromName = itemOpt[0].name;
  61. }
  62. if (itemOpt[1].name) {
  63. target.toName = itemOpt[1].name;
  64. }
  65. return mergeAll([target, itemOpt[0], itemOpt[1]]);
  66. });
  67. }
  68. }
  69. var LinesSeries = SeriesModel.extend({
  70. type: 'series.lines',
  71. dependencies: ['grid', 'polar'],
  72. visualColorAccessPath: 'lineStyle.color',
  73. init: function (option) {
  74. // The input data may be null/undefined.
  75. option.data = option.data || []; // Not using preprocessor because mergeOption may not have series.type
  76. compatEc2(option);
  77. var result = this._processFlatCoordsArray(option.data);
  78. this._flatCoords = result.flatCoords;
  79. this._flatCoordsOffset = result.flatCoordsOffset;
  80. if (result.flatCoords) {
  81. option.data = new Float32Array(result.count);
  82. }
  83. LinesSeries.superApply(this, 'init', arguments);
  84. },
  85. mergeOption: function (option) {
  86. // The input data may be null/undefined.
  87. option.data = option.data || [];
  88. compatEc2(option);
  89. if (option.data) {
  90. // Only update when have option data to merge.
  91. var result = this._processFlatCoordsArray(option.data);
  92. this._flatCoords = result.flatCoords;
  93. this._flatCoordsOffset = result.flatCoordsOffset;
  94. if (result.flatCoords) {
  95. option.data = new Float32Array(result.count);
  96. }
  97. }
  98. LinesSeries.superApply(this, 'mergeOption', arguments);
  99. },
  100. appendData: function (params) {
  101. var result = this._processFlatCoordsArray(params.data);
  102. if (result.flatCoords) {
  103. if (!this._flatCoords) {
  104. this._flatCoords = result.flatCoords;
  105. this._flatCoordsOffset = result.flatCoordsOffset;
  106. } else {
  107. this._flatCoords = concatArray(this._flatCoords, result.flatCoords);
  108. this._flatCoordsOffset = concatArray(this._flatCoordsOffset, result.flatCoordsOffset);
  109. }
  110. params.data = new Float32Array(result.count);
  111. }
  112. this.getRawData().appendData(params.data);
  113. },
  114. _getCoordsFromItemModel: function (idx) {
  115. var itemModel = this.getData().getItemModel(idx);
  116. var coords = itemModel.option instanceof Array ? itemModel.option : itemModel.getShallow('coords');
  117. return coords;
  118. },
  119. getLineCoordsCount: function (idx) {
  120. if (this._flatCoordsOffset) {
  121. return this._flatCoordsOffset[idx * 2 + 1];
  122. } else {
  123. return this._getCoordsFromItemModel(idx).length;
  124. }
  125. },
  126. getLineCoords: function (idx, out) {
  127. if (this._flatCoordsOffset) {
  128. var offset = this._flatCoordsOffset[idx * 2];
  129. var len = this._flatCoordsOffset[idx * 2 + 1];
  130. for (var i = 0; i < len; i++) {
  131. out[i] = out[i] || [];
  132. out[i][0] = this._flatCoords[offset + i * 2];
  133. out[i][1] = this._flatCoords[offset + i * 2 + 1];
  134. }
  135. return len;
  136. } else {
  137. var coords = this._getCoordsFromItemModel(idx);
  138. for (var i = 0; i < coords.length; i++) {
  139. out[i] = out[i] || [];
  140. out[i][0] = coords[i][0];
  141. out[i][1] = coords[i][1];
  142. }
  143. return coords.length;
  144. }
  145. },
  146. _processFlatCoordsArray: function (data) {
  147. var startOffset = 0;
  148. if (this._flatCoords) {
  149. startOffset = this._flatCoords.length;
  150. } // Stored as a typed array. In format
  151. // Points Count(2) | x | y | x | y | Points Count(3) | x | y | x | y | x | y |
  152. if (typeof data[0] === 'number') {
  153. var len = data.length; // Store offset and len of each segment
  154. var coordsOffsetAndLenStorage = new Uint32Arr(len);
  155. var coordsStorage = new Float64Arr(len);
  156. var coordsCursor = 0;
  157. var offsetCursor = 0;
  158. var dataCount = 0;
  159. for (var i = 0; i < len;) {
  160. dataCount++;
  161. var count = data[i++]; // Offset
  162. coordsOffsetAndLenStorage[offsetCursor++] = coordsCursor + startOffset; // Len
  163. coordsOffsetAndLenStorage[offsetCursor++] = count;
  164. for (var k = 0; k < count; k++) {
  165. var x = data[i++];
  166. var y = data[i++];
  167. coordsStorage[coordsCursor++] = x;
  168. coordsStorage[coordsCursor++] = y;
  169. if (i > len) {}
  170. }
  171. }
  172. return {
  173. flatCoordsOffset: new Uint32Array(coordsOffsetAndLenStorage.buffer, 0, offsetCursor),
  174. flatCoords: coordsStorage,
  175. count: dataCount
  176. };
  177. }
  178. return {
  179. flatCoordsOffset: null,
  180. flatCoords: null,
  181. count: data.length
  182. };
  183. },
  184. getInitialData: function (option, ecModel) {
  185. var lineData = new List(['value'], this);
  186. lineData.hasItemOption = false;
  187. lineData.initData(option.data, [], function (dataItem, dimName, dataIndex, dimIndex) {
  188. // dataItem is simply coords
  189. if (dataItem instanceof Array) {
  190. return NaN;
  191. } else {
  192. lineData.hasItemOption = true;
  193. var value = dataItem.value;
  194. if (value != null) {
  195. return value instanceof Array ? value[dimIndex] : value;
  196. }
  197. }
  198. });
  199. return lineData;
  200. },
  201. formatTooltip: function (dataIndex) {
  202. var data = this.getData();
  203. var itemModel = data.getItemModel(dataIndex);
  204. var name = itemModel.get('name');
  205. if (name) {
  206. return name;
  207. }
  208. var fromName = itemModel.get('fromName');
  209. var toName = itemModel.get('toName');
  210. var html = [];
  211. fromName != null && html.push(fromName);
  212. toName != null && html.push(toName);
  213. return encodeHTML(html.join(' > '));
  214. },
  215. preventIncremental: function () {
  216. return !!this.get('effect.show');
  217. },
  218. getProgressive: function () {
  219. var progressive = this.option.progressive;
  220. if (progressive == null) {
  221. return this.option.large ? 1e4 : this.get('progressive');
  222. }
  223. return progressive;
  224. },
  225. getProgressiveThreshold: function () {
  226. var progressiveThreshold = this.option.progressiveThreshold;
  227. if (progressiveThreshold == null) {
  228. return this.option.large ? 2e4 : this.get('progressiveThreshold');
  229. }
  230. return progressiveThreshold;
  231. },
  232. defaultOption: {
  233. coordinateSystem: 'geo',
  234. zlevel: 0,
  235. z: 2,
  236. legendHoverLink: true,
  237. hoverAnimation: true,
  238. // Cartesian coordinate system
  239. xAxisIndex: 0,
  240. yAxisIndex: 0,
  241. symbol: ['none', 'none'],
  242. symbolSize: [10, 10],
  243. // Geo coordinate system
  244. geoIndex: 0,
  245. effect: {
  246. show: false,
  247. period: 4,
  248. // Animation delay. support callback
  249. // delay: 0,
  250. // If move with constant speed px/sec
  251. // period will be ignored if this property is > 0,
  252. constantSpeed: 0,
  253. symbol: 'circle',
  254. symbolSize: 3,
  255. loop: true,
  256. // Length of trail, 0 - 1
  257. trailLength: 0.2 // Same with lineStyle.color
  258. // color
  259. },
  260. large: false,
  261. // Available when large is true
  262. largeThreshold: 2000,
  263. // If lines are polyline
  264. // polyline not support curveness, label, animation
  265. polyline: false,
  266. // If clip the overflow.
  267. // Available when coordinateSystem is cartesian or polar.
  268. clip: true,
  269. label: {
  270. show: false,
  271. position: 'end' // distance: 5,
  272. // formatter: 标签文本格式器,同Tooltip.formatter,不支持异步回调
  273. },
  274. lineStyle: {
  275. opacity: 0.5
  276. }
  277. }
  278. });
  279. var _default = LinesSeries;
  280. module.exports = _default;