TimelineModel.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var zrUtil = require("zrender/lib/core/util");
  2. var ComponentModel = require("../../model/Component");
  3. var List = require("../../data/List");
  4. var modelUtil = require("../../util/model");
  5. var TimelineModel = ComponentModel.extend({
  6. type: 'timeline',
  7. layoutMode: 'box',
  8. /**
  9. * @protected
  10. */
  11. defaultOption: {
  12. zlevel: 0,
  13. // 一级层叠
  14. z: 4,
  15. // 二级层叠
  16. show: true,
  17. axisType: 'time',
  18. // 模式是时间类型,支持 value, category
  19. realtime: true,
  20. left: '20%',
  21. top: null,
  22. right: '20%',
  23. bottom: 0,
  24. width: null,
  25. height: 40,
  26. padding: 5,
  27. controlPosition: 'left',
  28. // 'left' 'right' 'top' 'bottom' 'none'
  29. autoPlay: false,
  30. rewind: false,
  31. // 反向播放
  32. loop: true,
  33. playInterval: 2000,
  34. // 播放时间间隔,单位ms
  35. currentIndex: 0,
  36. itemStyle: {
  37. normal: {},
  38. emphasis: {}
  39. },
  40. label: {
  41. normal: {
  42. color: '#000'
  43. },
  44. emphasis: {}
  45. },
  46. data: []
  47. },
  48. /**
  49. * @override
  50. */
  51. init: function (option, parentModel, ecModel) {
  52. /**
  53. * @private
  54. * @type {module:echarts/data/List}
  55. */
  56. this._data;
  57. /**
  58. * @private
  59. * @type {Array.<string>}
  60. */
  61. this._names;
  62. this.mergeDefaultAndTheme(option, ecModel);
  63. this._initData();
  64. },
  65. /**
  66. * @override
  67. */
  68. mergeOption: function (option) {
  69. TimelineModel.superApply(this, 'mergeOption', arguments);
  70. this._initData();
  71. },
  72. /**
  73. * @param {number} [currentIndex]
  74. */
  75. setCurrentIndex: function (currentIndex) {
  76. if (currentIndex == null) {
  77. currentIndex = this.option.currentIndex;
  78. }
  79. var count = this._data.count();
  80. if (this.option.loop) {
  81. currentIndex = (currentIndex % count + count) % count;
  82. } else {
  83. currentIndex >= count && (currentIndex = count - 1);
  84. currentIndex < 0 && (currentIndex = 0);
  85. }
  86. this.option.currentIndex = currentIndex;
  87. },
  88. /**
  89. * @return {number} currentIndex
  90. */
  91. getCurrentIndex: function () {
  92. return this.option.currentIndex;
  93. },
  94. /**
  95. * @return {boolean}
  96. */
  97. isIndexMax: function () {
  98. return this.getCurrentIndex() >= this._data.count() - 1;
  99. },
  100. /**
  101. * @param {boolean} state true: play, false: stop
  102. */
  103. setPlayState: function (state) {
  104. this.option.autoPlay = !!state;
  105. },
  106. /**
  107. * @return {boolean} true: play, false: stop
  108. */
  109. getPlayState: function () {
  110. return !!this.option.autoPlay;
  111. },
  112. /**
  113. * @private
  114. */
  115. _initData: function () {
  116. var thisOption = this.option;
  117. var dataArr = thisOption.data || [];
  118. var axisType = thisOption.axisType;
  119. var names = this._names = [];
  120. if (axisType === 'category') {
  121. var idxArr = [];
  122. zrUtil.each(dataArr, function (item, index) {
  123. var value = modelUtil.getDataItemValue(item);
  124. var newItem;
  125. if (zrUtil.isObject(item)) {
  126. newItem = zrUtil.clone(item);
  127. newItem.value = index;
  128. } else {
  129. newItem = index;
  130. }
  131. idxArr.push(newItem);
  132. if (!zrUtil.isString(value) && (value == null || isNaN(value))) {
  133. value = '';
  134. }
  135. names.push(value + '');
  136. });
  137. dataArr = idxArr;
  138. }
  139. var dimType = {
  140. category: 'ordinal',
  141. time: 'time'
  142. }[axisType] || 'number';
  143. var data = this._data = new List([{
  144. name: 'value',
  145. type: dimType
  146. }], this);
  147. data.initData(dataArr, names);
  148. },
  149. getData: function () {
  150. return this._data;
  151. },
  152. /**
  153. * @public
  154. * @return {Array.<string>} categoreis
  155. */
  156. getCategories: function () {
  157. if (this.get('axisType') === 'category') {
  158. return this._names.slice();
  159. }
  160. }
  161. });
  162. var _default = TimelineModel;
  163. module.exports = _default;