ThemeRiverSeries.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 =
  51. /** @class */
  52. function (_super) {
  53. __extends(ThemeRiverSeriesModel, _super);
  54. function ThemeRiverSeriesModel() {
  55. var _this = _super !== null && _super.apply(this, arguments) || this;
  56. _this.type = ThemeRiverSeriesModel.type;
  57. return _this;
  58. }
  59. /**
  60. * @override
  61. */
  62. ThemeRiverSeriesModel.prototype.init = function (option) {
  63. // eslint-disable-next-line
  64. _super.prototype.init.apply(this, arguments); // Put this function here is for the sake of consistency of code style.
  65. // Enable legend selection for each data item
  66. // Use a function instead of direct access because data reference may changed
  67. this.legendVisualProvider = new LegendVisualProvider(zrUtil.bind(this.getData, this), zrUtil.bind(this.getRawData, this));
  68. };
  69. /**
  70. * If there is no value of a certain point in the time for some event,set it value to 0.
  71. *
  72. * @param {Array} data initial data in the option
  73. * @return {Array}
  74. */
  75. ThemeRiverSeriesModel.prototype.fixData = function (data) {
  76. var rawDataLength = data.length;
  77. /**
  78. * Make sure every layer data get the same keys.
  79. * The value index tells which layer has visited.
  80. * {
  81. * 2014/01/01: -1
  82. * }
  83. */
  84. var timeValueKeys = {}; // 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 gived
  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'); // filter the data item with the value of label is undefined
  123. var filterData = zrUtil.filter(option.data, function (dataItem) {
  124. return dataItem[2] !== undefined;
  125. }); // ??? TODO design a stage to transfer data for themeRiver and lines?
  126. var data = this.fixData(filterData || []);
  127. var nameList = [];
  128. var nameMap = this.nameMap = zrUtil.createHashMap();
  129. var count = 0;
  130. for (var i = 0; i < data.length; ++i) {
  131. nameList.push(data[i][DATA_NAME_INDEX]);
  132. if (!nameMap.get(data[i][DATA_NAME_INDEX])) {
  133. nameMap.set(data[i][DATA_NAME_INDEX], count);
  134. count++;
  135. }
  136. }
  137. var dimensions = prepareSeriesDataSchema(data, {
  138. coordDimensions: ['single'],
  139. dimensionsDefine: [{
  140. name: 'time',
  141. type: getDimensionTypeByAxis(axisType)
  142. }, {
  143. name: 'value',
  144. type: 'float'
  145. }, {
  146. name: 'name',
  147. type: 'ordinal'
  148. }],
  149. encodeDefine: {
  150. single: 0,
  151. value: 1,
  152. itemName: 2
  153. }
  154. }).dimensions;
  155. var list = new SeriesData(dimensions, this);
  156. list.initData(data);
  157. return list;
  158. };
  159. /**
  160. * The raw data is divided into multiple layers and each layer
  161. * has same name.
  162. */
  163. ThemeRiverSeriesModel.prototype.getLayerSeries = function () {
  164. var data = this.getData();
  165. var lenCount = data.count();
  166. var indexArr = [];
  167. for (var i = 0; i < lenCount; ++i) {
  168. indexArr[i] = i;
  169. }
  170. var timeDim = data.mapDimension('single'); // data group by name
  171. var groupResult = groupData(indexArr, function (index) {
  172. return data.get('name', index);
  173. });
  174. var layerSeries = [];
  175. groupResult.buckets.each(function (items, key) {
  176. items.sort(function (index1, index2) {
  177. return data.get(timeDim, index1) - data.get(timeDim, index2);
  178. });
  179. layerSeries.push({
  180. name: key,
  181. indices: items
  182. });
  183. });
  184. return layerSeries;
  185. };
  186. /**
  187. * Get data indices for show tooltip content
  188. */
  189. ThemeRiverSeriesModel.prototype.getAxisTooltipData = function (dim, value, baseAxis) {
  190. if (!zrUtil.isArray(dim)) {
  191. dim = dim ? [dim] : [];
  192. }
  193. var data = this.getData();
  194. var layerSeries = this.getLayerSeries();
  195. var indices = [];
  196. var layerNum = layerSeries.length;
  197. var nestestValue;
  198. for (var i = 0; i < layerNum; ++i) {
  199. var minDist = Number.MAX_VALUE;
  200. var nearestIdx = -1;
  201. var pointNum = layerSeries[i].indices.length;
  202. for (var j = 0; j < pointNum; ++j) {
  203. var theValue = data.get(dim[0], layerSeries[i].indices[j]);
  204. var dist = Math.abs(theValue - value);
  205. if (dist <= minDist) {
  206. nestestValue = theValue;
  207. minDist = dist;
  208. nearestIdx = layerSeries[i].indices[j];
  209. }
  210. }
  211. indices.push(nearestIdx);
  212. }
  213. return {
  214. dataIndices: indices,
  215. nestestValue: nestestValue
  216. };
  217. };
  218. ThemeRiverSeriesModel.prototype.formatTooltip = function (dataIndex, multipleSeries, dataType) {
  219. var data = this.getData();
  220. var name = data.getName(dataIndex);
  221. var value = data.get(data.mapDimension('value'), dataIndex);
  222. return createTooltipMarkup('nameValue', {
  223. name: name,
  224. value: value
  225. });
  226. };
  227. ThemeRiverSeriesModel.type = 'series.themeRiver';
  228. ThemeRiverSeriesModel.dependencies = ['singleAxis'];
  229. ThemeRiverSeriesModel.defaultOption = {
  230. // zlevel: 0,
  231. z: 2,
  232. colorBy: 'data',
  233. coordinateSystem: 'singleAxis',
  234. // gap in axis's orthogonal orientation
  235. boundaryGap: ['10%', '10%'],
  236. // legendHoverLink: true,
  237. singleAxisIndex: 0,
  238. animationEasing: 'linear',
  239. label: {
  240. margin: 4,
  241. show: true,
  242. position: 'left',
  243. fontSize: 11
  244. },
  245. emphasis: {
  246. label: {
  247. show: true
  248. }
  249. }
  250. };
  251. return ThemeRiverSeriesModel;
  252. }(SeriesModel);
  253. export default ThemeRiverSeriesModel;