TimelineModel.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 zrUtil = require("zrender/lib/core/util");
  20. var ComponentModel = require("../../model/Component");
  21. var List = require("../../data/List");
  22. var modelUtil = require("../../util/model");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var TimelineModel = ComponentModel.extend({
  42. type: 'timeline',
  43. layoutMode: 'box',
  44. /**
  45. * @protected
  46. */
  47. defaultOption: {
  48. zlevel: 0,
  49. // 一级层叠
  50. z: 4,
  51. // 二级层叠
  52. show: true,
  53. axisType: 'time',
  54. // 模式是时间类型,支持 value, category
  55. realtime: true,
  56. left: '20%',
  57. top: null,
  58. right: '20%',
  59. bottom: 0,
  60. width: null,
  61. height: 40,
  62. padding: 5,
  63. controlPosition: 'left',
  64. // 'left' 'right' 'top' 'bottom' 'none'
  65. autoPlay: false,
  66. rewind: false,
  67. // 反向播放
  68. loop: true,
  69. playInterval: 2000,
  70. // 播放时间间隔,单位ms
  71. currentIndex: 0,
  72. itemStyle: {},
  73. label: {
  74. color: '#000'
  75. },
  76. data: []
  77. },
  78. /**
  79. * @override
  80. */
  81. init: function (option, parentModel, ecModel) {
  82. /**
  83. * @private
  84. * @type {module:echarts/data/List}
  85. */
  86. this._data;
  87. /**
  88. * @private
  89. * @type {Array.<string>}
  90. */
  91. this._names;
  92. this.mergeDefaultAndTheme(option, ecModel);
  93. this._initData();
  94. },
  95. /**
  96. * @override
  97. */
  98. mergeOption: function (option) {
  99. TimelineModel.superApply(this, 'mergeOption', arguments);
  100. this._initData();
  101. },
  102. /**
  103. * @param {number} [currentIndex]
  104. */
  105. setCurrentIndex: function (currentIndex) {
  106. if (currentIndex == null) {
  107. currentIndex = this.option.currentIndex;
  108. }
  109. var count = this._data.count();
  110. if (this.option.loop) {
  111. currentIndex = (currentIndex % count + count) % count;
  112. } else {
  113. currentIndex >= count && (currentIndex = count - 1);
  114. currentIndex < 0 && (currentIndex = 0);
  115. }
  116. this.option.currentIndex = currentIndex;
  117. },
  118. /**
  119. * @return {number} currentIndex
  120. */
  121. getCurrentIndex: function () {
  122. return this.option.currentIndex;
  123. },
  124. /**
  125. * @return {boolean}
  126. */
  127. isIndexMax: function () {
  128. return this.getCurrentIndex() >= this._data.count() - 1;
  129. },
  130. /**
  131. * @param {boolean} state true: play, false: stop
  132. */
  133. setPlayState: function (state) {
  134. this.option.autoPlay = !!state;
  135. },
  136. /**
  137. * @return {boolean} true: play, false: stop
  138. */
  139. getPlayState: function () {
  140. return !!this.option.autoPlay;
  141. },
  142. /**
  143. * @private
  144. */
  145. _initData: function () {
  146. var thisOption = this.option;
  147. var dataArr = thisOption.data || [];
  148. var axisType = thisOption.axisType;
  149. var names = this._names = [];
  150. if (axisType === 'category') {
  151. var idxArr = [];
  152. zrUtil.each(dataArr, function (item, index) {
  153. var value = modelUtil.getDataItemValue(item);
  154. var newItem;
  155. if (zrUtil.isObject(item)) {
  156. newItem = zrUtil.clone(item);
  157. newItem.value = index;
  158. } else {
  159. newItem = index;
  160. }
  161. idxArr.push(newItem);
  162. if (!zrUtil.isString(value) && (value == null || isNaN(value))) {
  163. value = '';
  164. }
  165. names.push(value + '');
  166. });
  167. dataArr = idxArr;
  168. }
  169. var dimType = {
  170. category: 'ordinal',
  171. time: 'time'
  172. }[axisType] || 'number';
  173. var data = this._data = new List([{
  174. name: 'value',
  175. type: dimType
  176. }], this);
  177. data.initData(dataArr, names);
  178. },
  179. getData: function () {
  180. return this._data;
  181. },
  182. /**
  183. * @public
  184. * @return {Array.<string>} categoreis
  185. */
  186. getCategories: function () {
  187. if (this.get('axisType') === 'category') {
  188. return this._names.slice();
  189. }
  190. }
  191. });
  192. var _default = TimelineModel;
  193. module.exports = _default;