SliderTimelineView.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 BoundingRect from 'zrender/lib/core/BoundingRect.js';
  42. import * as matrix from 'zrender/lib/core/matrix.js';
  43. import * as graphic from '../../util/graphic.js';
  44. import { createTextStyle } from '../../label/labelStyle.js';
  45. import * as layout from '../../util/layout.js';
  46. import TimelineView from './TimelineView.js';
  47. import TimelineAxis from './TimelineAxis.js';
  48. import { createSymbol, normalizeSymbolOffset, normalizeSymbolSize } from '../../util/symbol.js';
  49. import * as numberUtil from '../../util/number.js';
  50. import { merge, each, extend, isString, bind, defaults, retrieve2 } from 'zrender/lib/core/util.js';
  51. import { parsePercent } from 'zrender/lib/contain/text.js';
  52. import { makeInner } from '../../util/model.js';
  53. import { getECData } from '../../util/innerStore.js';
  54. import { enableHoverEmphasis } from '../../util/states.js';
  55. import { createTooltipMarkup } from '../tooltip/tooltipMarkup.js';
  56. import { createScaleByModel } from '../../coord/axisHelper.js';
  57. import { scaleCalcNiceDirectly } from '../../coord/axisNiceTicks.js';
  58. var PI = Math.PI;
  59. var labelDataIndexStore = makeInner();
  60. var SliderTimelineView = /** @class */function (_super) {
  61. __extends(SliderTimelineView, _super);
  62. function SliderTimelineView() {
  63. var _this = _super !== null && _super.apply(this, arguments) || this;
  64. _this.type = SliderTimelineView.type;
  65. return _this;
  66. }
  67. SliderTimelineView.prototype.init = function (ecModel, api) {
  68. this.api = api;
  69. };
  70. /**
  71. * @override
  72. */
  73. SliderTimelineView.prototype.render = function (timelineModel, ecModel, api) {
  74. this.model = timelineModel;
  75. this.api = api;
  76. this.ecModel = ecModel;
  77. this.group.removeAll();
  78. if (timelineModel.get('show', true)) {
  79. var layoutInfo_1 = this._layout(timelineModel, api);
  80. var mainGroup_1 = this._createGroup('_mainGroup');
  81. var labelGroup = this._createGroup('_labelGroup');
  82. var axis_1 = this._axis = this._createAxis(layoutInfo_1, timelineModel);
  83. timelineModel.formatTooltip = function (dataIndex) {
  84. var name = axis_1.scale.getLabel({
  85. value: dataIndex
  86. });
  87. return createTooltipMarkup('nameValue', {
  88. noName: true,
  89. value: name
  90. });
  91. };
  92. each(['AxisLine', 'AxisTick', 'Control', 'CurrentPointer'], function (name) {
  93. this['_render' + name](layoutInfo_1, mainGroup_1, axis_1, timelineModel);
  94. }, this);
  95. this._renderAxisLabel(layoutInfo_1, labelGroup, axis_1, timelineModel);
  96. this._position(layoutInfo_1, timelineModel);
  97. }
  98. this._doPlayStop();
  99. this._updateTicksStatus();
  100. };
  101. /**
  102. * @override
  103. */
  104. SliderTimelineView.prototype.remove = function () {
  105. this._clearTimer();
  106. this.group.removeAll();
  107. };
  108. /**
  109. * @override
  110. */
  111. SliderTimelineView.prototype.dispose = function () {
  112. this._clearTimer();
  113. };
  114. SliderTimelineView.prototype._layout = function (timelineModel, api) {
  115. var labelPosOpt = timelineModel.get(['label', 'position']);
  116. var orient = timelineModel.get('orient');
  117. var viewRect = getViewRect(timelineModel, api);
  118. var parsedLabelPos;
  119. // Auto label offset.
  120. if (labelPosOpt == null || labelPosOpt === 'auto') {
  121. parsedLabelPos = orient === 'horizontal' ? viewRect.y + viewRect.height / 2 < api.getHeight() / 2 ? '-' : '+' : viewRect.x + viewRect.width / 2 < api.getWidth() / 2 ? '+' : '-';
  122. } else if (isString(labelPosOpt)) {
  123. parsedLabelPos = {
  124. horizontal: {
  125. top: '-',
  126. bottom: '+'
  127. },
  128. vertical: {
  129. left: '-',
  130. right: '+'
  131. }
  132. }[orient][labelPosOpt];
  133. } else {
  134. // is number
  135. parsedLabelPos = labelPosOpt;
  136. }
  137. var labelAlignMap = {
  138. horizontal: 'center',
  139. vertical: parsedLabelPos >= 0 || parsedLabelPos === '+' ? 'left' : 'right'
  140. };
  141. var labelBaselineMap = {
  142. horizontal: parsedLabelPos >= 0 || parsedLabelPos === '+' ? 'top' : 'bottom',
  143. vertical: 'middle'
  144. };
  145. var rotationMap = {
  146. horizontal: 0,
  147. vertical: PI / 2
  148. };
  149. // Position
  150. var mainLength = orient === 'vertical' ? viewRect.height : viewRect.width;
  151. var controlModel = timelineModel.getModel('controlStyle');
  152. var showControl = controlModel.get('show', true);
  153. var controlSize = showControl ? controlModel.get('itemSize') : 0;
  154. var controlGap = showControl ? controlModel.get('itemGap') : 0;
  155. var sizePlusGap = controlSize + controlGap;
  156. // Special label rotate.
  157. var labelRotation = timelineModel.get(['label', 'rotate']) || 0;
  158. labelRotation = labelRotation * PI / 180; // To radian.
  159. var playPosition;
  160. var prevBtnPosition;
  161. var nextBtnPosition;
  162. var controlPosition = controlModel.get('position', true);
  163. var showPlayBtn = showControl && controlModel.get('showPlayBtn', true);
  164. var showPrevBtn = showControl && controlModel.get('showPrevBtn', true);
  165. var showNextBtn = showControl && controlModel.get('showNextBtn', true);
  166. var xLeft = 0;
  167. var xRight = mainLength;
  168. // position[0] means left, position[1] means middle.
  169. if (controlPosition === 'left' || controlPosition === 'bottom') {
  170. showPlayBtn && (playPosition = [0, 0], xLeft += sizePlusGap);
  171. showPrevBtn && (prevBtnPosition = [xLeft, 0], xLeft += sizePlusGap);
  172. showNextBtn && (nextBtnPosition = [xRight - controlSize, 0], xRight -= sizePlusGap);
  173. } else {
  174. // 'top' 'right'
  175. showPlayBtn && (playPosition = [xRight - controlSize, 0], xRight -= sizePlusGap);
  176. showPrevBtn && (prevBtnPosition = [0, 0], xLeft += sizePlusGap);
  177. showNextBtn && (nextBtnPosition = [xRight - controlSize, 0], xRight -= sizePlusGap);
  178. }
  179. var axisExtent = [xLeft, xRight];
  180. if (timelineModel.get('inverse')) {
  181. axisExtent.reverse();
  182. }
  183. return {
  184. viewRect: viewRect,
  185. mainLength: mainLength,
  186. orient: orient,
  187. rotation: rotationMap[orient],
  188. labelRotation: labelRotation,
  189. labelPosOpt: parsedLabelPos,
  190. labelAlign: timelineModel.get(['label', 'align']) || labelAlignMap[orient],
  191. labelBaseline: timelineModel.get(['label', 'verticalAlign']) || timelineModel.get(['label', 'baseline']) || labelBaselineMap[orient],
  192. // Based on mainGroup.
  193. playPosition: playPosition,
  194. prevBtnPosition: prevBtnPosition,
  195. nextBtnPosition: nextBtnPosition,
  196. axisExtent: axisExtent,
  197. controlSize: controlSize,
  198. controlGap: controlGap
  199. };
  200. };
  201. SliderTimelineView.prototype._position = function (layoutInfo, timelineModel) {
  202. // Position is be called finally, because bounding rect is needed for
  203. // adapt content to fill viewRect (auto adapt offset).
  204. // Timeline may be not all in the viewRect when 'offset' is specified
  205. // as a number, because it is more appropriate that label aligns at
  206. // 'offset' but not the other edge defined by viewRect.
  207. var mainGroup = this._mainGroup;
  208. var labelGroup = this._labelGroup;
  209. var viewRect = layoutInfo.viewRect;
  210. if (layoutInfo.orient === 'vertical') {
  211. // transform to horizontal, inverse rotate by left-top point.
  212. var m = matrix.create();
  213. var rotateOriginX = viewRect.x;
  214. var rotateOriginY = viewRect.y + viewRect.height;
  215. matrix.translate(m, m, [-rotateOriginX, -rotateOriginY]);
  216. matrix.rotate(m, m, -PI / 2);
  217. matrix.translate(m, m, [rotateOriginX, rotateOriginY]);
  218. viewRect = viewRect.clone();
  219. viewRect.applyTransform(m);
  220. }
  221. var viewBound = getBound(viewRect);
  222. var mainBound = getBound(mainGroup.getBoundingRect());
  223. var labelBound = getBound(labelGroup.getBoundingRect());
  224. var mainPosition = [mainGroup.x, mainGroup.y];
  225. var labelsPosition = [labelGroup.x, labelGroup.y];
  226. labelsPosition[0] = mainPosition[0] = viewBound[0][0];
  227. var labelPosOpt = layoutInfo.labelPosOpt;
  228. if (labelPosOpt == null || isString(labelPosOpt)) {
  229. // '+' or '-'
  230. var mainBoundIdx = labelPosOpt === '+' ? 0 : 1;
  231. toBound(mainPosition, mainBound, viewBound, 1, mainBoundIdx);
  232. toBound(labelsPosition, labelBound, viewBound, 1, 1 - mainBoundIdx);
  233. } else {
  234. var mainBoundIdx = labelPosOpt >= 0 ? 0 : 1;
  235. toBound(mainPosition, mainBound, viewBound, 1, mainBoundIdx);
  236. labelsPosition[1] = mainPosition[1] + labelPosOpt;
  237. }
  238. mainGroup.setPosition(mainPosition);
  239. labelGroup.setPosition(labelsPosition);
  240. mainGroup.rotation = labelGroup.rotation = layoutInfo.rotation;
  241. setOrigin(mainGroup);
  242. setOrigin(labelGroup);
  243. function setOrigin(targetGroup) {
  244. targetGroup.originX = viewBound[0][0] - targetGroup.x;
  245. targetGroup.originY = viewBound[1][0] - targetGroup.y;
  246. }
  247. function getBound(rect) {
  248. // [[xmin, xmax], [ymin, ymax]]
  249. return [[rect.x, rect.x + rect.width], [rect.y, rect.y + rect.height]];
  250. }
  251. function toBound(fromPos, from, to, dimIdx, boundIdx) {
  252. fromPos[dimIdx] += to[dimIdx][boundIdx] - from[dimIdx][boundIdx];
  253. }
  254. };
  255. SliderTimelineView.prototype._createAxis = function (layoutInfo, timelineModel) {
  256. var data = timelineModel.getData();
  257. var axisType = timelineModel.get('axisType') || timelineModel.get('type');
  258. if (axisType !== 'category' && axisType !== 'time') {
  259. axisType = 'value';
  260. }
  261. var scale = createScaleByModel(timelineModel, axisType, false);
  262. // Customize scale. The `tickValue` is `dataIndex`.
  263. scale.getTicks = function () {
  264. return data.mapArray(['value'], function (value) {
  265. return {
  266. value: value
  267. };
  268. });
  269. };
  270. var dataExtent = data.getDataExtent('value');
  271. scale.setExtent(dataExtent[0], dataExtent[1]);
  272. scaleCalcNiceDirectly(scale, {
  273. fixMinMax: [true, true]
  274. });
  275. var axis = new TimelineAxis('value', scale, layoutInfo.axisExtent, axisType);
  276. axis.model = timelineModel;
  277. return axis;
  278. };
  279. SliderTimelineView.prototype._createGroup = function (key) {
  280. var newGroup = this[key] = new graphic.Group();
  281. this.group.add(newGroup);
  282. return newGroup;
  283. };
  284. SliderTimelineView.prototype._renderAxisLine = function (layoutInfo, group, axis, timelineModel) {
  285. var axisExtent = axis.getExtent();
  286. if (!timelineModel.get(['lineStyle', 'show'])) {
  287. return;
  288. }
  289. var line = new graphic.Line({
  290. shape: {
  291. x1: axisExtent[0],
  292. y1: 0,
  293. x2: axisExtent[1],
  294. y2: 0
  295. },
  296. style: extend({
  297. lineCap: 'round'
  298. }, timelineModel.getModel('lineStyle').getLineStyle()),
  299. silent: true,
  300. z2: 1
  301. });
  302. group.add(line);
  303. var progressLine = this._progressLine = new graphic.Line({
  304. shape: {
  305. x1: axisExtent[0],
  306. x2: this._currentPointer ? this._currentPointer.x : axisExtent[0],
  307. y1: 0,
  308. y2: 0
  309. },
  310. style: defaults({
  311. lineCap: 'round',
  312. lineWidth: line.style.lineWidth
  313. }, timelineModel.getModel(['progress', 'lineStyle']).getLineStyle()),
  314. silent: true,
  315. z2: 1
  316. });
  317. group.add(progressLine);
  318. };
  319. SliderTimelineView.prototype._renderAxisTick = function (layoutInfo, group, axis, timelineModel) {
  320. var _this = this;
  321. var data = timelineModel.getData();
  322. // Show all ticks, despite ignoring strategy.
  323. var ticks = axis.scale.getTicks();
  324. this._tickSymbols = [];
  325. // The value is dataIndex, see the customized scale.
  326. each(ticks, function (tick) {
  327. var tickCoord = axis.dataToCoord(tick.value);
  328. var itemModel = data.getItemModel(tick.value);
  329. var itemStyleModel = itemModel.getModel('itemStyle');
  330. var hoverStyleModel = itemModel.getModel(['emphasis', 'itemStyle']);
  331. var progressStyleModel = itemModel.getModel(['progress', 'itemStyle']);
  332. var symbolOpt = {
  333. x: tickCoord,
  334. y: 0,
  335. onclick: bind(_this._changeTimeline, _this, tick.value)
  336. };
  337. var el = giveSymbol(itemModel, itemStyleModel, group, symbolOpt);
  338. el.ensureState('emphasis').style = hoverStyleModel.getItemStyle();
  339. el.ensureState('progress').style = progressStyleModel.getItemStyle();
  340. enableHoverEmphasis(el);
  341. var ecData = getECData(el);
  342. if (itemModel.get('tooltip')) {
  343. ecData.dataIndex = tick.value;
  344. ecData.dataModel = timelineModel;
  345. } else {
  346. ecData.dataIndex = ecData.dataModel = null;
  347. }
  348. _this._tickSymbols.push(el);
  349. });
  350. };
  351. SliderTimelineView.prototype._renderAxisLabel = function (layoutInfo, group, axis, timelineModel) {
  352. var _this = this;
  353. var labelModel = axis.getLabelModel();
  354. if (!labelModel.get('show')) {
  355. return;
  356. }
  357. var data = timelineModel.getData();
  358. var labels = axis.getViewLabels();
  359. this._tickLabels = [];
  360. each(labels, function (labelItem) {
  361. if (labelItem.tick.offInterval) {
  362. return;
  363. }
  364. // The tickValue is dataIndex, see the customized scale.
  365. var dataIndex = labelItem.tick.value;
  366. var itemModel = data.getItemModel(dataIndex);
  367. var normalLabelModel = itemModel.getModel('label');
  368. var hoverLabelModel = itemModel.getModel(['emphasis', 'label']);
  369. var progressLabelModel = itemModel.getModel(['progress', 'label']);
  370. var tickCoord = axis.dataToCoord(dataIndex);
  371. var textEl = new graphic.Text({
  372. x: tickCoord,
  373. y: 0,
  374. rotation: layoutInfo.labelRotation - layoutInfo.rotation,
  375. onclick: bind(_this._changeTimeline, _this, dataIndex),
  376. silent: false,
  377. style: createTextStyle(normalLabelModel, {
  378. text: labelItem.formattedLabel,
  379. align: layoutInfo.labelAlign,
  380. verticalAlign: layoutInfo.labelBaseline
  381. })
  382. });
  383. textEl.ensureState('emphasis').style = createTextStyle(hoverLabelModel);
  384. textEl.ensureState('progress').style = createTextStyle(progressLabelModel);
  385. group.add(textEl);
  386. enableHoverEmphasis(textEl);
  387. labelDataIndexStore(textEl).dataIndex = dataIndex;
  388. _this._tickLabels.push(textEl);
  389. });
  390. };
  391. SliderTimelineView.prototype._renderControl = function (layoutInfo, group, axis, timelineModel) {
  392. var controlSize = layoutInfo.controlSize;
  393. var rotation = layoutInfo.rotation;
  394. var itemStyle = timelineModel.getModel('controlStyle').getItemStyle();
  395. var hoverStyle = timelineModel.getModel(['emphasis', 'controlStyle']).getItemStyle();
  396. var playState = timelineModel.getPlayState();
  397. var inverse = timelineModel.get('inverse', true);
  398. makeBtn(layoutInfo.nextBtnPosition, 'next', bind(this._changeTimeline, this, inverse ? '-' : '+'));
  399. makeBtn(layoutInfo.prevBtnPosition, 'prev', bind(this._changeTimeline, this, inverse ? '+' : '-'));
  400. makeBtn(layoutInfo.playPosition, playState ? 'stop' : 'play', bind(this._handlePlayClick, this, !playState), true);
  401. function makeBtn(position, iconName, onclick, willRotate) {
  402. if (!position) {
  403. return;
  404. }
  405. var iconSize = parsePercent(retrieve2(timelineModel.get(['controlStyle', iconName + 'BtnSize']), controlSize), controlSize);
  406. var rect = [0, -iconSize / 2, iconSize, iconSize];
  407. var btn = makeControlIcon(timelineModel, iconName + 'Icon', rect, {
  408. x: position[0],
  409. y: position[1],
  410. originX: controlSize / 2,
  411. originY: 0,
  412. rotation: willRotate ? -rotation : 0,
  413. rectHover: true,
  414. style: itemStyle,
  415. onclick: onclick
  416. });
  417. btn.ensureState('emphasis').style = hoverStyle;
  418. group.add(btn);
  419. enableHoverEmphasis(btn);
  420. }
  421. };
  422. SliderTimelineView.prototype._renderCurrentPointer = function (layoutInfo, group, axis, timelineModel) {
  423. var data = timelineModel.getData();
  424. var currentIndex = timelineModel.getCurrentIndex();
  425. var pointerModel = data.getItemModel(currentIndex).getModel('checkpointStyle');
  426. var me = this;
  427. var callback = {
  428. onCreate: function (pointer) {
  429. pointer.draggable = true;
  430. pointer.drift = bind(me._handlePointerDrag, me);
  431. pointer.ondragend = bind(me._handlePointerDragend, me);
  432. pointerMoveTo(pointer, me._progressLine, currentIndex, axis, timelineModel, true);
  433. },
  434. onUpdate: function (pointer) {
  435. pointerMoveTo(pointer, me._progressLine, currentIndex, axis, timelineModel);
  436. }
  437. };
  438. // Reuse when exists, for animation and drag.
  439. this._currentPointer = giveSymbol(pointerModel, pointerModel, this._mainGroup, {}, this._currentPointer, callback);
  440. };
  441. SliderTimelineView.prototype._handlePlayClick = function (nextState) {
  442. this._clearTimer();
  443. this.api.dispatchAction({
  444. type: 'timelinePlayChange',
  445. playState: nextState,
  446. from: this.uid
  447. });
  448. };
  449. SliderTimelineView.prototype._handlePointerDrag = function (dx, dy, e) {
  450. this._clearTimer();
  451. this._pointerChangeTimeline([e.offsetX, e.offsetY]);
  452. };
  453. SliderTimelineView.prototype._handlePointerDragend = function (e) {
  454. this._pointerChangeTimeline([e.offsetX, e.offsetY], true);
  455. };
  456. SliderTimelineView.prototype._pointerChangeTimeline = function (mousePos, trigger) {
  457. var toCoord = this._toAxisCoord(mousePos)[0];
  458. var axis = this._axis;
  459. var axisExtent = numberUtil.asc(axis.getExtent().slice());
  460. toCoord > axisExtent[1] && (toCoord = axisExtent[1]);
  461. toCoord < axisExtent[0] && (toCoord = axisExtent[0]);
  462. this._currentPointer.x = toCoord;
  463. this._currentPointer.markRedraw();
  464. var progressLine = this._progressLine;
  465. if (progressLine) {
  466. progressLine.shape.x2 = toCoord;
  467. progressLine.dirty();
  468. }
  469. var targetDataIndex = this._findNearestTick(toCoord);
  470. var timelineModel = this.model;
  471. if (trigger || targetDataIndex !== timelineModel.getCurrentIndex() && timelineModel.get('realtime')) {
  472. this._changeTimeline(targetDataIndex);
  473. }
  474. };
  475. SliderTimelineView.prototype._doPlayStop = function () {
  476. var _this = this;
  477. this._clearTimer();
  478. if (this.model.getPlayState()) {
  479. this._timer = setTimeout(function () {
  480. // Do not cache
  481. var timelineModel = _this.model;
  482. _this._changeTimeline(timelineModel.getCurrentIndex() + (timelineModel.get('rewind', true) ? -1 : 1));
  483. }, this.model.get('playInterval'));
  484. }
  485. };
  486. SliderTimelineView.prototype._toAxisCoord = function (vertex) {
  487. var trans = this._mainGroup.getLocalTransform();
  488. return graphic.applyTransform(vertex, trans, true);
  489. };
  490. SliderTimelineView.prototype._findNearestTick = function (axisCoord) {
  491. var data = this.model.getData();
  492. var dist = Infinity;
  493. var targetDataIndex;
  494. var axis = this._axis;
  495. data.each(['value'], function (value, dataIndex) {
  496. var coord = axis.dataToCoord(value);
  497. var d = Math.abs(coord - axisCoord);
  498. if (d < dist) {
  499. dist = d;
  500. targetDataIndex = dataIndex;
  501. }
  502. });
  503. return targetDataIndex;
  504. };
  505. SliderTimelineView.prototype._clearTimer = function () {
  506. if (this._timer) {
  507. clearTimeout(this._timer);
  508. this._timer = null;
  509. }
  510. };
  511. SliderTimelineView.prototype._changeTimeline = function (nextIndex) {
  512. var currentIndex = this.model.getCurrentIndex();
  513. if (nextIndex === '+') {
  514. nextIndex = currentIndex + 1;
  515. } else if (nextIndex === '-') {
  516. nextIndex = currentIndex - 1;
  517. }
  518. this.api.dispatchAction({
  519. type: 'timelineChange',
  520. currentIndex: nextIndex,
  521. from: this.uid
  522. });
  523. };
  524. SliderTimelineView.prototype._updateTicksStatus = function () {
  525. var currentIndex = this.model.getCurrentIndex();
  526. var tickSymbols = this._tickSymbols;
  527. var tickLabels = this._tickLabels;
  528. if (tickSymbols) {
  529. for (var i = 0; i < tickSymbols.length; i++) {
  530. tickSymbols && tickSymbols[i] && tickSymbols[i].toggleState('progress', i < currentIndex);
  531. }
  532. }
  533. if (tickLabels) {
  534. for (var i = 0; i < tickLabels.length; i++) {
  535. tickLabels && tickLabels[i] && tickLabels[i].toggleState('progress', labelDataIndexStore(tickLabels[i]).dataIndex <= currentIndex);
  536. }
  537. }
  538. };
  539. SliderTimelineView.type = 'timeline.slider';
  540. return SliderTimelineView;
  541. }(TimelineView);
  542. function getViewRect(model, api) {
  543. return layout.getLayoutRect(model.getBoxLayoutParams(), layout.createBoxLayoutReference(model, api).refContainer, model.get('padding'));
  544. }
  545. function makeControlIcon(timelineModel, objPath, rect, opts) {
  546. var style = opts.style;
  547. var icon = graphic.createIcon(timelineModel.get(['controlStyle', objPath]), opts || {}, new BoundingRect(rect[0], rect[1], rect[2], rect[3]));
  548. // TODO createIcon won't use style in opt.
  549. if (style) {
  550. icon.setStyle(style);
  551. }
  552. return icon;
  553. }
  554. /**
  555. * Create symbol or update symbol
  556. * opt: basic position and event handlers
  557. */
  558. function giveSymbol(hostModel, itemStyleModel, group, opt, symbol, callback) {
  559. var color = itemStyleModel.get('color');
  560. if (!symbol) {
  561. var symbolType = hostModel.get('symbol');
  562. symbol = createSymbol(symbolType, -1, -1, 2, 2, color);
  563. symbol.setStyle('strokeNoScale', true);
  564. group.add(symbol);
  565. callback && callback.onCreate(symbol);
  566. } else {
  567. symbol.setColor(color);
  568. group.add(symbol); // Group may be new, also need to add.
  569. callback && callback.onUpdate(symbol);
  570. }
  571. // Style
  572. var itemStyle = itemStyleModel.getItemStyle(['color']);
  573. symbol.setStyle(itemStyle);
  574. // Transform and events.
  575. opt = merge({
  576. rectHover: true,
  577. z2: 100
  578. }, opt, true);
  579. var symbolSize = normalizeSymbolSize(hostModel.get('symbolSize'));
  580. opt.scaleX = symbolSize[0] / 2;
  581. opt.scaleY = symbolSize[1] / 2;
  582. var symbolOffset = normalizeSymbolOffset(hostModel.get('symbolOffset'), symbolSize);
  583. if (symbolOffset) {
  584. opt.x = (opt.x || 0) + symbolOffset[0];
  585. opt.y = (opt.y || 0) + symbolOffset[1];
  586. }
  587. var symbolRotate = hostModel.get('symbolRotate');
  588. opt.rotation = (symbolRotate || 0) * Math.PI / 180 || 0;
  589. symbol.attr(opt);
  590. // FIXME
  591. // (1) When symbol.style.strokeNoScale is true and updateTransform is not performed,
  592. // getBoundingRect will return wrong result.
  593. // (This is supposed to be resolved in zrender, but it is a little difficult to
  594. // leverage performance and auto updateTransform)
  595. // (2) All of ancesters of symbol do not scale, so we can just updateTransform symbol.
  596. symbol.updateTransform();
  597. return symbol;
  598. }
  599. function pointerMoveTo(pointer, progressLine, dataIndex, axis, timelineModel, noAnimation) {
  600. if (pointer.dragging) {
  601. return;
  602. }
  603. var pointerModel = timelineModel.getModel('checkpointStyle');
  604. var toCoord = axis.dataToCoord(timelineModel.getData().get('value', dataIndex));
  605. if (noAnimation || !pointerModel.get('animation', true)) {
  606. pointer.attr({
  607. x: toCoord,
  608. y: 0
  609. });
  610. progressLine && progressLine.attr({
  611. shape: {
  612. x2: toCoord
  613. }
  614. });
  615. } else {
  616. var animationCfg = {
  617. duration: pointerModel.get('animationDuration', true),
  618. easing: pointerModel.get('animationEasing', true)
  619. };
  620. pointer.stopAnimation(null, true);
  621. pointer.animateTo({
  622. x: toCoord,
  623. y: 0
  624. }, animationCfg);
  625. progressLine && progressLine.animateTo({
  626. shape: {
  627. x2: toCoord
  628. }
  629. }, animationCfg);
  630. }
  631. }
  632. export default SliderTimelineView;