LinesSeries.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. compatEc2(option);
  87. if (option.data) {
  88. // Only update when have option data to merge.
  89. var result = this._processFlatCoordsArray(option.data);
  90. this._flatCoords = result.flatCoords;
  91. this._flatCoordsOffset = result.flatCoordsOffset;
  92. if (result.flatCoords) {
  93. option.data = new Float32Array(result.count);
  94. }
  95. }
  96. LinesSeries.superApply(this, 'mergeOption', arguments);
  97. },
  98. appendData: function (params) {
  99. var result = this._processFlatCoordsArray(params.data);
  100. if (result.flatCoords) {
  101. if (!this._flatCoords) {
  102. this._flatCoords = result.flatCoords;
  103. this._flatCoordsOffset = result.flatCoordsOffset;
  104. } else {
  105. this._flatCoords = concatArray(this._flatCoords, result.flatCoords);
  106. this._flatCoordsOffset = concatArray(this._flatCoordsOffset, result.flatCoordsOffset);
  107. }
  108. params.data = new Float32Array(result.count);
  109. }
  110. this.getRawData().appendData(params.data);
  111. },
  112. _getCoordsFromItemModel: function (idx) {
  113. var itemModel = this.getData().getItemModel(idx);
  114. var coords = itemModel.option instanceof Array ? itemModel.option : itemModel.getShallow('coords');
  115. return coords;
  116. },
  117. getLineCoordsCount: function (idx) {
  118. if (this._flatCoordsOffset) {
  119. return this._flatCoordsOffset[idx * 2 + 1];
  120. } else {
  121. return this._getCoordsFromItemModel(idx).length;
  122. }
  123. },
  124. getLineCoords: function (idx, out) {
  125. if (this._flatCoordsOffset) {
  126. var offset = this._flatCoordsOffset[idx * 2];
  127. var len = this._flatCoordsOffset[idx * 2 + 1];
  128. for (var i = 0; i < len; i++) {
  129. out[i] = out[i] || [];
  130. out[i][0] = this._flatCoords[offset + i * 2];
  131. out[i][1] = this._flatCoords[offset + i * 2 + 1];
  132. }
  133. return len;
  134. } else {
  135. var coords = this._getCoordsFromItemModel(idx);
  136. for (var i = 0; i < coords.length; i++) {
  137. out[i] = out[i] || [];
  138. out[i][0] = coords[i][0];
  139. out[i][1] = coords[i][1];
  140. }
  141. return coords.length;
  142. }
  143. },
  144. _processFlatCoordsArray: function (data) {
  145. var startOffset = 0;
  146. if (this._flatCoords) {
  147. startOffset = this._flatCoords.length;
  148. } // Stored as a typed array. In format
  149. // Points Count(2) | x | y | x | y | Points Count(3) | x | y | x | y | x | y |
  150. if (typeof data[0] === 'number') {
  151. var len = data.length; // Store offset and len of each segment
  152. var coordsOffsetAndLenStorage = new Uint32Arr(len);
  153. var coordsStorage = new Float64Arr(len);
  154. var coordsCursor = 0;
  155. var offsetCursor = 0;
  156. var dataCount = 0;
  157. for (var i = 0; i < len;) {
  158. dataCount++;
  159. var count = data[i++]; // Offset
  160. coordsOffsetAndLenStorage[offsetCursor++] = coordsCursor + startOffset; // Len
  161. coordsOffsetAndLenStorage[offsetCursor++] = count;
  162. for (var k = 0; k < count; k++) {
  163. var x = data[i++];
  164. var y = data[i++];
  165. coordsStorage[coordsCursor++] = x;
  166. coordsStorage[coordsCursor++] = y;
  167. if (i > len) {}
  168. }
  169. }
  170. return {
  171. flatCoordsOffset: new Uint32Array(coordsOffsetAndLenStorage.buffer, 0, offsetCursor),
  172. flatCoords: coordsStorage,
  173. count: dataCount
  174. };
  175. }
  176. return {
  177. flatCoordsOffset: null,
  178. flatCoords: null,
  179. count: data.length
  180. };
  181. },
  182. getInitialData: function (option, ecModel) {
  183. var lineData = new List(['value'], this);
  184. lineData.hasItemOption = false;
  185. lineData.initData(option.data, [], function (dataItem, dimName, dataIndex, dimIndex) {
  186. // dataItem is simply coords
  187. if (dataItem instanceof Array) {
  188. return NaN;
  189. } else {
  190. lineData.hasItemOption = true;
  191. var value = dataItem.value;
  192. if (value != null) {
  193. return value instanceof Array ? value[dimIndex] : value;
  194. }
  195. }
  196. });
  197. return lineData;
  198. },
  199. formatTooltip: function (dataIndex) {
  200. var data = this.getData();
  201. var itemModel = data.getItemModel(dataIndex);
  202. var name = itemModel.get('name');
  203. if (name) {
  204. return name;
  205. }
  206. var fromName = itemModel.get('fromName');
  207. var toName = itemModel.get('toName');
  208. var html = [];
  209. fromName != null && html.push(fromName);
  210. toName != null && html.push(toName);
  211. return encodeHTML(html.join(' > '));
  212. },
  213. preventIncremental: function () {
  214. return !!this.get('effect.show');
  215. },
  216. getProgressive: function () {
  217. var progressive = this.option.progressive;
  218. if (progressive == null) {
  219. return this.option.large ? 1e4 : this.get('progressive');
  220. }
  221. return progressive;
  222. },
  223. getProgressiveThreshold: function () {
  224. var progressiveThreshold = this.option.progressiveThreshold;
  225. if (progressiveThreshold == null) {
  226. return this.option.large ? 2e4 : this.get('progressiveThreshold');
  227. }
  228. return progressiveThreshold;
  229. },
  230. defaultOption: {
  231. coordinateSystem: 'geo',
  232. zlevel: 0,
  233. z: 2,
  234. legendHoverLink: true,
  235. hoverAnimation: true,
  236. // Cartesian coordinate system
  237. xAxisIndex: 0,
  238. yAxisIndex: 0,
  239. symbol: ['none', 'none'],
  240. symbolSize: [10, 10],
  241. // Geo coordinate system
  242. geoIndex: 0,
  243. effect: {
  244. show: false,
  245. period: 4,
  246. // Animation delay. support callback
  247. // delay: 0,
  248. // If move with constant speed px/sec
  249. // period will be ignored if this property is > 0,
  250. constantSpeed: 0,
  251. symbol: 'circle',
  252. symbolSize: 3,
  253. loop: true,
  254. // Length of trail, 0 - 1
  255. trailLength: 0.2 // Same with lineStyle.color
  256. // color
  257. },
  258. large: false,
  259. // Available when large is true
  260. largeThreshold: 2000,
  261. // If lines are polyline
  262. // polyline not support curveness, label, animation
  263. polyline: false,
  264. // If clip the overflow.
  265. // Available when coordinateSystem is cartesian or polar.
  266. clip: true,
  267. label: {
  268. show: false,
  269. position: 'end' // distance: 5,
  270. // formatter: 标签文本格式器,同Tooltip.formatter,不支持异步回调
  271. },
  272. lineStyle: {
  273. opacity: 0.5
  274. }
  275. }
  276. });
  277. var _default = LinesSeries;
  278. module.exports = _default;