ThemeRiverSeries.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import SeriesModel from '../../model/Series.js';
  42. import prepareSeriesDataSchema from '../../data/helper/createDimensions.js';
  43. import { getDimensionTypeByAxis } from '../../data/helper/dimensionHelper.js';
  44. import SeriesData from '../../data/SeriesData.js';
  45. import * as zrUtil from 'zrender/lib/core/util.js';
  46. import { groupData, SINGLE_REFERRING } from '../../util/model.js';
  47. import LegendVisualProvider from '../../visual/LegendVisualProvider.js';
  48. import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup.js';
  49. var DATA_NAME_INDEX = 2;
  50. var ThemeRiverSeriesModel = /** @class */function (_super) {
  51. __extends(ThemeRiverSeriesModel, _super);
  52. function ThemeRiverSeriesModel() {
  53. var _this = _super !== null && _super.apply(this, arguments) || this;
  54. _this.type = ThemeRiverSeriesModel.type;
  55. return _this;
  56. }
  57. /**
  58. * @override
  59. */
  60. ThemeRiverSeriesModel.prototype.init = function (option) {
  61. // eslint-disable-next-line
  62. _super.prototype.init.apply(this, arguments);
  63. // Put this function here is for the sake of consistency of code style.
  64. // Enable legend selection for each data item
  65. // Use a function instead of direct access because data reference may changed
  66. this.legendVisualProvider = new LegendVisualProvider(zrUtil.bind(this.getData, this), zrUtil.bind(this.getRawData, this));
  67. };
  68. /**
  69. * If there is no value of a certain point in the time for some event,set it value to 0.
  70. *
  71. * @param {Array} data initial data in the option
  72. * @return {Array}
  73. */
  74. ThemeRiverSeriesModel.prototype.fixData = function (data) {
  75. var rawDataLength = data.length;
  76. /**
  77. * Make sure every layer data get the same keys.
  78. * The value index tells which layer has visited.
  79. * {
  80. * 2014/01/01: -1
  81. * }
  82. */
  83. var timeValueKeys = {};
  84. // grouped data by name
  85. var groupResult = groupData(data, function (item) {
  86. if (!timeValueKeys.hasOwnProperty(item[0] + '')) {
  87. timeValueKeys[item[0] + ''] = -1;
  88. }
  89. return item[2];
  90. });
  91. var layerData = [];
  92. groupResult.buckets.each(function (items, key) {
  93. layerData.push({
  94. name: key,
  95. dataList: items
  96. });
  97. });
  98. var layerNum = layerData.length;
  99. for (var k = 0; k < layerNum; ++k) {
  100. var name_1 = layerData[k].name;
  101. for (var j = 0; j < layerData[k].dataList.length; ++j) {
  102. var timeValue = layerData[k].dataList[j][0] + '';
  103. timeValueKeys[timeValue] = k;
  104. }
  105. for (var timeValue in timeValueKeys) {
  106. if (timeValueKeys.hasOwnProperty(timeValue) && timeValueKeys[timeValue] !== k) {
  107. timeValueKeys[timeValue] = k;
  108. data[rawDataLength] = [timeValue, 0, name_1];
  109. rawDataLength++;
  110. }
  111. }
  112. }
  113. return data;
  114. };
  115. /**
  116. * @override
  117. * @param option the initial option that user gave
  118. * @param ecModel the model object for themeRiver option
  119. */
  120. ThemeRiverSeriesModel.prototype.getInitialData = function (option, ecModel) {
  121. var singleAxisModel = this.getReferringComponents('singleAxis', SINGLE_REFERRING).models[0];
  122. var axisType = singleAxisModel.get('type');
  123. // filter the data item with the value of label is undefined
  124. var filterData = zrUtil.filter(option.data, function (dataItem) {
  125. return dataItem[2] !== undefined;
  126. });
  127. // ??? TODO design a stage to transfer data for themeRiver and lines?
  128. var data = this.fixData(filterData || []);
  129. var nameList = [];
  130. var nameMap = this.nameMap = zrUtil.createHashMap();
  131. var count = 0;
  132. for (var i = 0; i < data.length; ++i) {
  133. nameList.push(data[i][DATA_NAME_INDEX]);
  134. if (!nameMap.get(data[i][DATA_NAME_INDEX])) {
  135. nameMap.set(data[i][DATA_NAME_INDEX], count);
  136. count++;
  137. }
  138. }
  139. var dimensions = prepareSeriesDataSchema(data, {
  140. coordDimensions: ['single'],
  141. dimensionsDefine: [{
  142. name: 'time',
  143. type: getDimensionTypeByAxis(axisType)
  144. }, {
  145. name: 'value',
  146. type: 'float'
  147. }, {
  148. name: 'name',
  149. type: 'ordinal'
  150. }],
  151. encodeDefine: {
  152. single: 0,
  153. value: 1,
  154. itemName: 2
  155. }
  156. }).dimensions;
  157. var list = new SeriesData(dimensions, this);
  158. list.initData(data);
  159. return list;
  160. };
  161. /**
  162. * The raw data is divided into multiple layers and each layer
  163. * has same name.
  164. */
  165. ThemeRiverSeriesModel.prototype.getLayerSeries = function () {
  166. var data = this.getData();
  167. var lenCount = data.count();
  168. var indexArr = [];
  169. for (var i = 0; i < lenCount; ++i) {
  170. indexArr[i] = i;
  171. }
  172. var timeDim = data.mapDimension('single');
  173. // data group by name
  174. var groupResult = groupData(indexArr, function (index) {
  175. return data.get('name', index);
  176. });
  177. var layerSeries = [];
  178. groupResult.buckets.each(function (items, key) {
  179. items.sort(function (index1, index2) {
  180. return data.get(timeDim, index1) - data.get(timeDim, index2);
  181. });
  182. layerSeries.push({
  183. name: key,
  184. indices: items
  185. });
  186. });
  187. return layerSeries;
  188. };
  189. /**
  190. * Get data indices for show tooltip content
  191. */
  192. ThemeRiverSeriesModel.prototype.getAxisTooltipData = function (dim, value, baseAxis) {
  193. if (!zrUtil.isArray(dim)) {
  194. dim = dim ? [dim] : [];
  195. }
  196. var data = this.getData();
  197. var layerSeries = this.getLayerSeries();
  198. var indices = [];
  199. var layerNum = layerSeries.length;
  200. var nestestValue;
  201. for (var i = 0; i < layerNum; ++i) {
  202. var minDist = Number.MAX_VALUE;
  203. var nearestIdx = -1;
  204. var pointNum = layerSeries[i].indices.length;
  205. for (var j = 0; j < pointNum; ++j) {
  206. var theValue = data.get(dim[0], layerSeries[i].indices[j]);
  207. var dist = Math.abs(theValue - value);
  208. if (dist <= minDist) {
  209. nestestValue = theValue;
  210. minDist = dist;
  211. nearestIdx = layerSeries[i].indices[j];
  212. }
  213. }
  214. indices.push(nearestIdx);
  215. }
  216. return {
  217. dataIndices: indices,
  218. nestestValue: nestestValue
  219. };
  220. };
  221. ThemeRiverSeriesModel.prototype.formatTooltip = function (dataIndex, multipleSeries, dataType) {
  222. var data = this.getData();
  223. var name = data.getName(dataIndex);
  224. var value = data.get(data.mapDimension('value'), dataIndex);
  225. return createTooltipMarkup('nameValue', {
  226. name: name,
  227. value: value
  228. });
  229. };
  230. ThemeRiverSeriesModel.type = 'series.themeRiver';
  231. ThemeRiverSeriesModel.dependencies = ['singleAxis'];
  232. ThemeRiverSeriesModel.defaultOption = {
  233. // zlevel: 0,
  234. z: 2,
  235. colorBy: 'data',
  236. coordinateSystem: 'singleAxis',
  237. // gap in axis's orthogonal orientation
  238. boundaryGap: ['10%', '10%'],
  239. // legendHoverLink: true,
  240. singleAxisIndex: 0,
  241. animationEasing: 'linear',
  242. label: {
  243. margin: 4,
  244. show: true,
  245. position: 'left',
  246. fontSize: 11
  247. },
  248. emphasis: {
  249. label: {
  250. show: true
  251. }
  252. }
  253. };
  254. return ThemeRiverSeriesModel;
  255. }(SeriesModel);
  256. export default ThemeRiverSeriesModel;