ThemeRiverSeries.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 SeriesModel = require("../../model/Series");
  20. var createDimensions = require("../../data/helper/createDimensions");
  21. var _dimensionHelper = require("../../data/helper/dimensionHelper");
  22. var getDimensionTypeByAxis = _dimensionHelper.getDimensionTypeByAxis;
  23. var List = require("../../data/List");
  24. var zrUtil = require("zrender/lib/core/util");
  25. var _model = require("../../util/model");
  26. var groupData = _model.groupData;
  27. var _format = require("../../util/format");
  28. var encodeHTML = _format.encodeHTML;
  29. var LegendVisualProvider = require("../../visual/LegendVisualProvider");
  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. var DATA_NAME_INDEX = 2;
  49. var ThemeRiverSeries = SeriesModel.extend({
  50. type: 'series.themeRiver',
  51. dependencies: ['singleAxis'],
  52. /**
  53. * @readOnly
  54. * @type {module:zrender/core/util#HashMap}
  55. */
  56. nameMap: null,
  57. /**
  58. * @override
  59. */
  60. init: function (option) {
  61. // eslint-disable-next-line
  62. ThemeRiverSeries.superApply(this, 'init', arguments); // Put this function here is for the sake of consistency of code style.
  63. // Enable legend selection for each data item
  64. // Use a function instead of direct access because data reference may changed
  65. this.legendVisualProvider = new LegendVisualProvider(zrUtil.bind(this.getData, this), zrUtil.bind(this.getRawData, this));
  66. },
  67. /**
  68. * If there is no value of a certain point in the time for some event,set it value to 0.
  69. *
  70. * @param {Array} data initial data in the option
  71. * @return {Array}
  72. */
  73. fixData: function (data) {
  74. var rawDataLength = data.length;
  75. /**
  76. * Make sure every layer data get the same keys.
  77. * The value index tells which layer has visited.
  78. * {
  79. * 2014/01/01: -1
  80. * }
  81. */
  82. var timeValueKeys = {}; // grouped data by name
  83. var groupResult = groupData(data, function (item) {
  84. if (!timeValueKeys.hasOwnProperty(item[0])) {
  85. timeValueKeys[item[0]] = -1;
  86. }
  87. return item[2];
  88. });
  89. var layData = [];
  90. groupResult.buckets.each(function (items, key) {
  91. layData.push({
  92. name: key,
  93. dataList: items
  94. });
  95. });
  96. var layerNum = layData.length;
  97. for (var k = 0; k < layerNum; ++k) {
  98. var name = layData[k].name;
  99. for (var j = 0; j < layData[k].dataList.length; ++j) {
  100. var timeValue = layData[k].dataList[j][0];
  101. timeValueKeys[timeValue] = k;
  102. }
  103. for (var timeValue in timeValueKeys) {
  104. if (timeValueKeys.hasOwnProperty(timeValue) && timeValueKeys[timeValue] !== k) {
  105. timeValueKeys[timeValue] = k;
  106. data[rawDataLength] = [];
  107. data[rawDataLength][0] = timeValue;
  108. data[rawDataLength][1] = 0;
  109. data[rawDataLength][2] = name;
  110. rawDataLength++;
  111. }
  112. }
  113. }
  114. return data;
  115. },
  116. /**
  117. * @override
  118. * @param {Object} option the initial option that user gived
  119. * @param {module:echarts/model/Model} ecModel the model object for themeRiver option
  120. * @return {module:echarts/data/List}
  121. */
  122. getInitialData: function (option, ecModel) {
  123. var singleAxisModel = ecModel.queryComponents({
  124. mainType: 'singleAxis',
  125. index: this.get('singleAxisIndex'),
  126. id: this.get('singleAxisId')
  127. })[0];
  128. var axisType = singleAxisModel.get('type'); // filter the data item with the value of label is undefined
  129. var filterData = zrUtil.filter(option.data, function (dataItem) {
  130. return dataItem[2] !== undefined;
  131. }); // ??? TODO design a stage to transfer data for themeRiver and lines?
  132. var data = this.fixData(filterData || []);
  133. var nameList = [];
  134. var nameMap = this.nameMap = zrUtil.createHashMap();
  135. var count = 0;
  136. for (var i = 0; i < data.length; ++i) {
  137. nameList.push(data[i][DATA_NAME_INDEX]);
  138. if (!nameMap.get(data[i][DATA_NAME_INDEX])) {
  139. nameMap.set(data[i][DATA_NAME_INDEX], count);
  140. count++;
  141. }
  142. }
  143. var dimensionsInfo = createDimensions(data, {
  144. coordDimensions: ['single'],
  145. dimensionsDefine: [{
  146. name: 'time',
  147. type: getDimensionTypeByAxis(axisType)
  148. }, {
  149. name: 'value',
  150. type: 'float'
  151. }, {
  152. name: 'name',
  153. type: 'ordinal'
  154. }],
  155. encodeDefine: {
  156. single: 0,
  157. value: 1,
  158. itemName: 2
  159. }
  160. });
  161. var list = new List(dimensionsInfo, this);
  162. list.initData(data);
  163. return list;
  164. },
  165. /**
  166. * The raw data is divided into multiple layers and each layer
  167. * has same name.
  168. *
  169. * @return {Array.<Array.<number>>}
  170. */
  171. getLayerSeries: function () {
  172. var data = this.getData();
  173. var lenCount = data.count();
  174. var indexArr = [];
  175. for (var i = 0; i < lenCount; ++i) {
  176. indexArr[i] = i;
  177. }
  178. var timeDim = data.mapDimension('single'); // data group by name
  179. var groupResult = groupData(indexArr, function (index) {
  180. return data.get('name', index);
  181. });
  182. var layerSeries = [];
  183. groupResult.buckets.each(function (items, key) {
  184. items.sort(function (index1, index2) {
  185. return data.get(timeDim, index1) - data.get(timeDim, index2);
  186. });
  187. layerSeries.push({
  188. name: key,
  189. indices: items
  190. });
  191. });
  192. return layerSeries;
  193. },
  194. /**
  195. * Get data indices for show tooltip content
  196. * @param {Array.<string>|string} dim single coordinate dimension
  197. * @param {number} value axis value
  198. * @param {module:echarts/coord/single/SingleAxis} baseAxis single Axis used
  199. * the themeRiver.
  200. * @return {Object} {dataIndices, nestestValue}
  201. */
  202. getAxisTooltipData: function (dim, value, baseAxis) {
  203. if (!zrUtil.isArray(dim)) {
  204. dim = dim ? [dim] : [];
  205. }
  206. var data = this.getData();
  207. var layerSeries = this.getLayerSeries();
  208. var indices = [];
  209. var layerNum = layerSeries.length;
  210. var nestestValue;
  211. for (var i = 0; i < layerNum; ++i) {
  212. var minDist = Number.MAX_VALUE;
  213. var nearestIdx = -1;
  214. var pointNum = layerSeries[i].indices.length;
  215. for (var j = 0; j < pointNum; ++j) {
  216. var theValue = data.get(dim[0], layerSeries[i].indices[j]);
  217. var dist = Math.abs(theValue - value);
  218. if (dist <= minDist) {
  219. nestestValue = theValue;
  220. minDist = dist;
  221. nearestIdx = layerSeries[i].indices[j];
  222. }
  223. }
  224. indices.push(nearestIdx);
  225. }
  226. return {
  227. dataIndices: indices,
  228. nestestValue: nestestValue
  229. };
  230. },
  231. /**
  232. * @override
  233. * @param {number} dataIndex index of data
  234. */
  235. formatTooltip: function (dataIndex) {
  236. var data = this.getData();
  237. var htmlName = data.getName(dataIndex);
  238. var htmlValue = data.get(data.mapDimension('value'), dataIndex);
  239. if (isNaN(htmlValue) || htmlValue == null) {
  240. htmlValue = '-';
  241. }
  242. return encodeHTML(htmlName + ' : ' + htmlValue);
  243. },
  244. defaultOption: {
  245. zlevel: 0,
  246. z: 2,
  247. coordinateSystem: 'singleAxis',
  248. // gap in axis's orthogonal orientation
  249. boundaryGap: ['10%', '10%'],
  250. // legendHoverLink: true,
  251. singleAxisIndex: 0,
  252. animationEasing: 'linear',
  253. label: {
  254. margin: 4,
  255. show: true,
  256. position: 'left',
  257. color: '#000',
  258. fontSize: 11
  259. },
  260. emphasis: {
  261. label: {
  262. show: true
  263. }
  264. }
  265. }
  266. });
  267. var _default = ThemeRiverSeries;
  268. module.exports = _default;