SliderZoomView.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 { bind, each, isFunction, isString, indexOf } from 'zrender/lib/core/util.js';
  42. import * as eventTool from 'zrender/lib/core/event.js';
  43. import * as graphic from '../../util/graphic.js';
  44. import * as throttle from '../../util/throttle.js';
  45. import DataZoomView from './DataZoomView.js';
  46. import { linearMap, asc, parsePercent } from '../../util/number.js';
  47. import * as layout from '../../util/layout.js';
  48. import sliderMove from '../helper/sliderMove.js';
  49. import { getAxisMainType, collectReferCoordSysModelInfo } from './helper.js';
  50. import { enableHoverEmphasis } from '../../util/states.js';
  51. import { createSymbol, symbolBuildProxies } from '../../util/symbol.js';
  52. import { deprecateLog } from '../../util/log.js';
  53. import { createTextStyle } from '../../label/labelStyle.js';
  54. import tokens from '../../visual/tokens.js';
  55. var Rect = graphic.Rect;
  56. // Constants
  57. var DEFAULT_FRAME_BORDER_WIDTH = 1;
  58. var DEFAULT_FILLER_SIZE = 30;
  59. var DEFAULT_MOVE_HANDLE_SIZE = 7;
  60. var HORIZONTAL = 'horizontal';
  61. var VERTICAL = 'vertical';
  62. var LABEL_GAP = 5;
  63. var SHOW_DATA_SHADOW_SERIES_TYPE = ['line', 'bar', 'candlestick', 'scatter'];
  64. var REALTIME_ANIMATION_CONFIG = {
  65. easing: 'cubicOut',
  66. duration: 100,
  67. delay: 0
  68. };
  69. var SliderZoomView = /** @class */function (_super) {
  70. __extends(SliderZoomView, _super);
  71. function SliderZoomView() {
  72. var _this = _super !== null && _super.apply(this, arguments) || this;
  73. _this.type = SliderZoomView.type;
  74. _this._displayables = {};
  75. return _this;
  76. }
  77. SliderZoomView.prototype.init = function (ecModel, api) {
  78. this.api = api;
  79. // A unique handler for each dataZoom component
  80. this._onBrush = bind(this._onBrush, this);
  81. this._onBrushEnd = bind(this._onBrushEnd, this);
  82. };
  83. SliderZoomView.prototype.render = function (dataZoomModel, ecModel, api, payload) {
  84. _super.prototype.render.apply(this, arguments);
  85. throttle.createOrUpdate(this, '_dispatchZoomAction', dataZoomModel.get('throttle'), 'fixRate');
  86. this._orient = dataZoomModel.getOrient();
  87. if (dataZoomModel.get('show') === false) {
  88. this.group.removeAll();
  89. return;
  90. }
  91. if (dataZoomModel.noTarget()) {
  92. this._clear();
  93. this.group.removeAll();
  94. return;
  95. }
  96. // Notice: this._resetInterval() should not be executed when payload.type
  97. // is 'dataZoom', origin this._range should be maintained, otherwise 'pan'
  98. // or 'zoom' info will be missed because of 'throttle' of this.dispatchAction,
  99. if (!payload || payload.type !== 'dataZoom' || payload.from !== this.uid) {
  100. this._buildView();
  101. }
  102. this._updateView();
  103. };
  104. SliderZoomView.prototype.dispose = function () {
  105. this._clear();
  106. _super.prototype.dispose.apply(this, arguments);
  107. };
  108. SliderZoomView.prototype._clear = function () {
  109. throttle.clear(this, '_dispatchZoomAction');
  110. var zr = this.api.getZr();
  111. zr.off('mousemove', this._onBrush);
  112. zr.off('mouseup', this._onBrushEnd);
  113. };
  114. SliderZoomView.prototype._buildView = function () {
  115. var thisGroup = this.group;
  116. thisGroup.removeAll();
  117. this._brushing = false;
  118. this._displayables.brushRect = null;
  119. this._resetLocation();
  120. this._resetInterval();
  121. var barGroup = this._displayables.sliderGroup = new graphic.Group();
  122. this._renderBackground();
  123. this._renderHandle();
  124. this._renderDataShadow();
  125. thisGroup.add(barGroup);
  126. this._positionGroup();
  127. };
  128. SliderZoomView.prototype._resetLocation = function () {
  129. var dataZoomModel = this.dataZoomModel;
  130. var api = this.api;
  131. var showMoveHandle = dataZoomModel.get('brushSelect');
  132. var moveHandleSize = showMoveHandle ? DEFAULT_MOVE_HANDLE_SIZE : 0;
  133. var refContainer = layout.createBoxLayoutReference(dataZoomModel, api).refContainer;
  134. // If some of x/y/width/height are not specified,
  135. // auto-adapt according to target grid.
  136. var coordRect = this._findCoordRect();
  137. var edgeGap = dataZoomModel.get('defaultLocationEdgeGap', true) || 0;
  138. // Default align by coordinate system rect.
  139. var positionInfo = this._orient === HORIZONTAL ? {
  140. // Why using 'right', because right should be used in vertical,
  141. // and it is better to be consistent for dealing with position param merge.
  142. right: refContainer.width - coordRect.x - coordRect.width,
  143. top: refContainer.height - DEFAULT_FILLER_SIZE - edgeGap - moveHandleSize,
  144. width: coordRect.width,
  145. height: DEFAULT_FILLER_SIZE
  146. } : {
  147. right: edgeGap,
  148. top: coordRect.y,
  149. width: DEFAULT_FILLER_SIZE,
  150. height: coordRect.height
  151. };
  152. // Do not write back to option and replace value 'ph', because
  153. // the 'ph' value should be recalculated when resize.
  154. var layoutParams = layout.getLayoutParams(dataZoomModel.option);
  155. // Replace the placeholder value.
  156. each(['right', 'top', 'width', 'height'], function (name) {
  157. if (layoutParams[name] === 'ph') {
  158. layoutParams[name] = positionInfo[name];
  159. }
  160. });
  161. var layoutRect = layout.getLayoutRect(layoutParams, refContainer);
  162. this._location = {
  163. x: layoutRect.x,
  164. y: layoutRect.y
  165. };
  166. this._size = [layoutRect.width, layoutRect.height];
  167. this._orient === VERTICAL && this._size.reverse();
  168. };
  169. SliderZoomView.prototype._positionGroup = function () {
  170. var thisGroup = this.group;
  171. var location = this._location;
  172. var orient = this._orient;
  173. // Just use the first axis to determine mapping.
  174. var targetAxisModel = this.dataZoomModel.getFirstTargetAxisModel();
  175. var inverse = targetAxisModel && targetAxisModel.get('inverse');
  176. var sliderGroup = this._displayables.sliderGroup;
  177. var otherAxisInverse = (this._dataShadowInfo || {}).otherAxisInverse;
  178. // Transform barGroup.
  179. sliderGroup.attr(orient === HORIZONTAL && !inverse ? {
  180. scaleY: otherAxisInverse ? 1 : -1,
  181. scaleX: 1
  182. } : orient === HORIZONTAL && inverse ? {
  183. scaleY: otherAxisInverse ? 1 : -1,
  184. scaleX: -1
  185. } : orient === VERTICAL && !inverse ? {
  186. scaleY: otherAxisInverse ? -1 : 1,
  187. scaleX: 1,
  188. rotation: Math.PI / 2
  189. }
  190. // Don't use Math.PI, considering shadow direction.
  191. : {
  192. scaleY: otherAxisInverse ? -1 : 1,
  193. scaleX: -1,
  194. rotation: Math.PI / 2
  195. });
  196. // Position barGroup
  197. var rect = thisGroup.getBoundingRect([sliderGroup]);
  198. thisGroup.x = location.x - rect.x;
  199. thisGroup.y = location.y - rect.y;
  200. thisGroup.markRedraw();
  201. };
  202. SliderZoomView.prototype._getViewExtent = function () {
  203. return [0, this._size[0]];
  204. };
  205. SliderZoomView.prototype._renderBackground = function () {
  206. var dataZoomModel = this.dataZoomModel;
  207. var size = this._size;
  208. var barGroup = this._displayables.sliderGroup;
  209. var brushSelect = dataZoomModel.get('brushSelect');
  210. barGroup.add(new Rect({
  211. silent: true,
  212. shape: {
  213. x: 0,
  214. y: 0,
  215. width: size[0],
  216. height: size[1]
  217. },
  218. style: {
  219. fill: dataZoomModel.get('backgroundColor')
  220. },
  221. z2: -40
  222. }));
  223. // Click panel, over shadow, below handles.
  224. var clickPanel = new Rect({
  225. shape: {
  226. x: 0,
  227. y: 0,
  228. width: size[0],
  229. height: size[1]
  230. },
  231. style: {
  232. fill: 'transparent'
  233. },
  234. z2: 0,
  235. onclick: bind(this._onClickPanel, this)
  236. });
  237. var zr = this.api.getZr();
  238. if (brushSelect) {
  239. clickPanel.on('mousedown', this._onBrushStart, this);
  240. clickPanel.cursor = 'crosshair';
  241. zr.on('mousemove', this._onBrush);
  242. zr.on('mouseup', this._onBrushEnd);
  243. } else {
  244. zr.off('mousemove', this._onBrush);
  245. zr.off('mouseup', this._onBrushEnd);
  246. }
  247. barGroup.add(clickPanel);
  248. };
  249. SliderZoomView.prototype._renderDataShadow = function () {
  250. var info = this._dataShadowInfo = this._prepareDataShadowInfo();
  251. this._displayables.dataShadowSegs = [];
  252. if (!info) {
  253. return;
  254. }
  255. var size = this._size;
  256. var oldSize = this._shadowSize || [];
  257. var seriesModel = info.series;
  258. var data = seriesModel.getRawData();
  259. var candlestickDim = seriesModel.getShadowDim && seriesModel.getShadowDim();
  260. var otherDim = candlestickDim && data.getDimensionInfo(candlestickDim) ? seriesModel.getShadowDim() // @see candlestick
  261. : info.otherDim;
  262. if (otherDim == null) {
  263. return;
  264. }
  265. var polygonPts = this._shadowPolygonPts;
  266. var polylinePts = this._shadowPolylinePts;
  267. // Not re-render if data doesn't change.
  268. if (data !== this._shadowData || otherDim !== this._shadowDim || size[0] !== oldSize[0] || size[1] !== oldSize[1]) {
  269. var thisDataExtent_1 = data.getDataExtent(info.thisDim);
  270. var otherDataExtent_1 = data.getDataExtent(otherDim);
  271. // Nice extent.
  272. var otherOffset = (otherDataExtent_1[1] - otherDataExtent_1[0]) * 0.3;
  273. otherDataExtent_1 = [otherDataExtent_1[0] - otherOffset, otherDataExtent_1[1] + otherOffset];
  274. var otherShadowExtent_1 = [0, size[1]];
  275. var thisShadowExtent = [0, size[0]];
  276. var areaPoints_1 = [[size[0], 0], [0, 0]];
  277. var linePoints_1 = [];
  278. var step_1 = thisShadowExtent[1] / Math.max(1, data.count() - 1);
  279. var normalizationConstant_1 = size[0] / (thisDataExtent_1[1] - thisDataExtent_1[0]);
  280. var isTimeAxis_1 = info.thisAxis.type === 'time';
  281. var thisCoord_1 = -step_1;
  282. // Optimize for large data shadow
  283. var stride_1 = Math.round(data.count() / size[0]);
  284. var lastIsEmpty_1;
  285. data.each([info.thisDim, otherDim], function (thisValue, otherValue, index) {
  286. if (stride_1 > 0 && index % stride_1) {
  287. if (!isTimeAxis_1) {
  288. thisCoord_1 += step_1;
  289. }
  290. return;
  291. }
  292. thisCoord_1 = isTimeAxis_1 ? (+thisValue - thisDataExtent_1[0]) * normalizationConstant_1 : thisCoord_1 + step_1;
  293. // FIXME
  294. // Should consider axis.min/axis.max when drawing dataShadow.
  295. // FIXME
  296. // 应该使用统一的空判断?还是在list里进行空判断?
  297. var isEmpty = otherValue == null || isNaN(otherValue) || otherValue === '';
  298. // See #4235.
  299. var otherCoord = isEmpty ? 0 : linearMap(otherValue, otherDataExtent_1, otherShadowExtent_1, true);
  300. // Attempt to draw data shadow precisely when there are empty value.
  301. if (isEmpty && !lastIsEmpty_1 && index) {
  302. areaPoints_1.push([areaPoints_1[areaPoints_1.length - 1][0], 0]);
  303. linePoints_1.push([linePoints_1[linePoints_1.length - 1][0], 0]);
  304. } else if (!isEmpty && lastIsEmpty_1) {
  305. areaPoints_1.push([thisCoord_1, 0]);
  306. linePoints_1.push([thisCoord_1, 0]);
  307. }
  308. if (!isEmpty) {
  309. areaPoints_1.push([thisCoord_1, otherCoord]);
  310. linePoints_1.push([thisCoord_1, otherCoord]);
  311. }
  312. lastIsEmpty_1 = isEmpty;
  313. });
  314. polygonPts = this._shadowPolygonPts = areaPoints_1;
  315. polylinePts = this._shadowPolylinePts = linePoints_1;
  316. }
  317. this._shadowData = data;
  318. this._shadowDim = otherDim;
  319. this._shadowSize = [size[0], size[1]];
  320. var dataZoomModel = this.dataZoomModel;
  321. function createDataShadowGroup(isSelectedArea) {
  322. var model = dataZoomModel.getModel(isSelectedArea ? 'selectedDataBackground' : 'dataBackground');
  323. var group = new graphic.Group();
  324. var polygon = new graphic.Polygon({
  325. shape: {
  326. points: polygonPts
  327. },
  328. segmentIgnoreThreshold: 1,
  329. style: model.getModel('areaStyle').getAreaStyle(),
  330. silent: true,
  331. z2: -20
  332. });
  333. var polyline = new graphic.Polyline({
  334. shape: {
  335. points: polylinePts
  336. },
  337. segmentIgnoreThreshold: 1,
  338. style: model.getModel('lineStyle').getLineStyle(),
  339. silent: true,
  340. z2: -19
  341. });
  342. group.add(polygon);
  343. group.add(polyline);
  344. return group;
  345. }
  346. // let dataBackgroundModel = dataZoomModel.getModel('dataBackground');
  347. for (var i = 0; i < 3; i++) {
  348. var group = createDataShadowGroup(i === 1);
  349. this._displayables.sliderGroup.add(group);
  350. this._displayables.dataShadowSegs.push(group);
  351. }
  352. };
  353. SliderZoomView.prototype._prepareDataShadowInfo = function () {
  354. var dataZoomModel = this.dataZoomModel;
  355. var showDataShadow = dataZoomModel.get('showDataShadow');
  356. if (showDataShadow === false) {
  357. return;
  358. }
  359. // Find a representative series.
  360. var result;
  361. var ecModel = this.ecModel;
  362. dataZoomModel.eachTargetAxis(function (axisDim, axisIndex) {
  363. var seriesModels = dataZoomModel.getAxisProxy(axisDim, axisIndex).getTargetSeriesModels();
  364. each(seriesModels, function (seriesModel) {
  365. if (result) {
  366. return;
  367. }
  368. if (showDataShadow !== true && indexOf(SHOW_DATA_SHADOW_SERIES_TYPE, seriesModel.get('type')) < 0) {
  369. return;
  370. }
  371. var thisAxis = ecModel.getComponent(getAxisMainType(axisDim), axisIndex).axis;
  372. var otherDim = getOtherDim(axisDim);
  373. var otherAxisInverse;
  374. var coordSys = seriesModel.coordinateSystem;
  375. if (otherDim != null && coordSys.getOtherAxis) {
  376. otherAxisInverse = coordSys.getOtherAxis(thisAxis).inverse;
  377. }
  378. otherDim = seriesModel.getData().mapDimension(otherDim);
  379. var thisDim = seriesModel.getData().mapDimension(axisDim);
  380. result = {
  381. thisAxis: thisAxis,
  382. series: seriesModel,
  383. thisDim: thisDim,
  384. otherDim: otherDim,
  385. otherAxisInverse: otherAxisInverse
  386. };
  387. }, this);
  388. }, this);
  389. return result;
  390. };
  391. SliderZoomView.prototype._renderHandle = function () {
  392. var thisGroup = this.group;
  393. var displayables = this._displayables;
  394. var handles = displayables.handles = [null, null];
  395. var handleLabels = displayables.handleLabels = [null, null];
  396. var sliderGroup = this._displayables.sliderGroup;
  397. var size = this._size;
  398. var dataZoomModel = this.dataZoomModel;
  399. var api = this.api;
  400. var borderRadius = dataZoomModel.get('borderRadius') || 0;
  401. var brushSelect = dataZoomModel.get('brushSelect');
  402. var filler = displayables.filler = new Rect({
  403. silent: brushSelect,
  404. style: {
  405. fill: dataZoomModel.get('fillerColor')
  406. },
  407. textConfig: {
  408. position: 'inside'
  409. }
  410. });
  411. sliderGroup.add(filler);
  412. // Frame border.
  413. sliderGroup.add(new Rect({
  414. silent: true,
  415. subPixelOptimize: true,
  416. shape: {
  417. x: 0,
  418. y: 0,
  419. width: size[0],
  420. height: size[1],
  421. r: borderRadius
  422. },
  423. style: {
  424. // deprecated option
  425. stroke: dataZoomModel.get('dataBackgroundColor') || dataZoomModel.get('borderColor'),
  426. lineWidth: DEFAULT_FRAME_BORDER_WIDTH,
  427. fill: tokens.color.transparent
  428. }
  429. }));
  430. // Left and right handle to resize
  431. each([0, 1], function (handleIndex) {
  432. var iconStr = dataZoomModel.get('handleIcon');
  433. if (!symbolBuildProxies[iconStr] && iconStr.indexOf('path://') < 0 && iconStr.indexOf('image://') < 0) {
  434. // Compatitable with the old icon parsers. Which can use a path string without path://
  435. iconStr = 'path://' + iconStr;
  436. if (process.env.NODE_ENV !== 'production') {
  437. deprecateLog('handleIcon now needs \'path://\' prefix when using a path string');
  438. }
  439. }
  440. var path = createSymbol(iconStr, -1, 0, 2, 2, null, true);
  441. path.attr({
  442. cursor: getCursor(this._orient),
  443. draggable: true,
  444. drift: bind(this._onDragMove, this, handleIndex),
  445. ondragend: bind(this._onDragEnd, this),
  446. onmouseover: bind(this._showDataInfo, this, true),
  447. onmouseout: bind(this._showDataInfo, this, false),
  448. z2: 5
  449. });
  450. var bRect = path.getBoundingRect();
  451. var handleSize = dataZoomModel.get('handleSize');
  452. this._handleHeight = parsePercent(handleSize, this._size[1]);
  453. this._handleWidth = bRect.width / bRect.height * this._handleHeight;
  454. path.setStyle(dataZoomModel.getModel('handleStyle').getItemStyle());
  455. path.style.strokeNoScale = true;
  456. path.rectHover = true;
  457. path.ensureState('emphasis').style = dataZoomModel.getModel(['emphasis', 'handleStyle']).getItemStyle();
  458. enableHoverEmphasis(path);
  459. var handleColor = dataZoomModel.get('handleColor'); // deprecated option
  460. // Compatitable with previous version
  461. if (handleColor != null) {
  462. path.style.fill = handleColor;
  463. }
  464. sliderGroup.add(handles[handleIndex] = path);
  465. var textStyleModel = dataZoomModel.getModel('textStyle');
  466. var handleLabel = dataZoomModel.get('handleLabel') || {};
  467. var handleLabelShow = handleLabel.show || false;
  468. thisGroup.add(handleLabels[handleIndex] = new graphic.Text({
  469. silent: true,
  470. invisible: !handleLabelShow,
  471. style: createTextStyle(textStyleModel, {
  472. x: 0,
  473. y: 0,
  474. text: '',
  475. verticalAlign: 'middle',
  476. align: 'center',
  477. fill: textStyleModel.getTextColor(),
  478. font: textStyleModel.getFont()
  479. }),
  480. z2: 10
  481. }));
  482. }, this);
  483. // Handle to move. Only visible when brushSelect is set true.
  484. var actualMoveZone = filler;
  485. if (brushSelect) {
  486. var moveHandleHeight = parsePercent(dataZoomModel.get('moveHandleSize'), size[1]);
  487. var moveHandle_1 = displayables.moveHandle = new graphic.Rect({
  488. style: dataZoomModel.getModel('moveHandleStyle').getItemStyle(),
  489. silent: true,
  490. shape: {
  491. r: [0, 0, 2, 2],
  492. y: size[1] - 0.5,
  493. height: moveHandleHeight
  494. }
  495. });
  496. var iconSize = moveHandleHeight * 0.8;
  497. var moveHandleIcon = displayables.moveHandleIcon = createSymbol(dataZoomModel.get('moveHandleIcon'), -iconSize / 2, -iconSize / 2, iconSize, iconSize, tokens.color.neutral00, true);
  498. moveHandleIcon.silent = true;
  499. moveHandleIcon.y = size[1] + moveHandleHeight / 2 - 0.5;
  500. moveHandle_1.ensureState('emphasis').style = dataZoomModel.getModel(['emphasis', 'moveHandleStyle']).getItemStyle();
  501. var moveZoneExpandSize = Math.min(size[1] / 2, Math.max(moveHandleHeight, 10));
  502. actualMoveZone = displayables.moveZone = new graphic.Rect({
  503. invisible: true,
  504. shape: {
  505. y: size[1] - moveZoneExpandSize,
  506. height: moveHandleHeight + moveZoneExpandSize
  507. }
  508. });
  509. actualMoveZone.on('mouseover', function () {
  510. api.enterEmphasis(moveHandle_1);
  511. }).on('mouseout', function () {
  512. api.leaveEmphasis(moveHandle_1);
  513. });
  514. sliderGroup.add(moveHandle_1);
  515. sliderGroup.add(moveHandleIcon);
  516. sliderGroup.add(actualMoveZone);
  517. }
  518. actualMoveZone.attr({
  519. draggable: true,
  520. cursor: 'default',
  521. drift: bind(this._onDragMove, this, 'all'),
  522. ondragstart: bind(this._showDataInfo, this, true),
  523. ondragend: bind(this._onDragEnd, this),
  524. onmouseover: bind(this._showDataInfo, this, true),
  525. onmouseout: bind(this._showDataInfo, this, false)
  526. });
  527. };
  528. SliderZoomView.prototype._resetInterval = function () {
  529. var range = this._range = this.dataZoomModel.getPercentRange();
  530. var viewExtent = this._getViewExtent();
  531. this._handleEnds = [linearMap(range[0], [0, 100], viewExtent, true), linearMap(range[1], [0, 100], viewExtent, true)];
  532. };
  533. SliderZoomView.prototype._updateInterval = function (handleIndex, delta) {
  534. var dataZoomModel = this.dataZoomModel;
  535. var handleEnds = this._handleEnds;
  536. var viewExtend = this._getViewExtent();
  537. var minMaxSpan = dataZoomModel.findRepresentativeAxisProxy().getMinMaxSpan();
  538. var percentExtent = [0, 100];
  539. sliderMove(delta, handleEnds, viewExtend, dataZoomModel.get('zoomLock') ? 'all' : handleIndex, minMaxSpan.minSpan != null ? linearMap(minMaxSpan.minSpan, percentExtent, viewExtend, true) : null, minMaxSpan.maxSpan != null ? linearMap(minMaxSpan.maxSpan, percentExtent, viewExtend, true) : null);
  540. var lastRange = this._range;
  541. var range = this._range = asc([linearMap(handleEnds[0], viewExtend, percentExtent, true), linearMap(handleEnds[1], viewExtend, percentExtent, true)]);
  542. return !lastRange || lastRange[0] !== range[0] || lastRange[1] !== range[1];
  543. };
  544. SliderZoomView.prototype._updateView = function (nonRealtime) {
  545. var displaybles = this._displayables;
  546. var handleEnds = this._handleEnds;
  547. var handleInterval = asc(handleEnds.slice());
  548. var size = this._size;
  549. each([0, 1], function (handleIndex) {
  550. // Handles
  551. var handle = displaybles.handles[handleIndex];
  552. var handleHeight = this._handleHeight;
  553. handle.attr({
  554. scaleX: handleHeight / 2,
  555. scaleY: handleHeight / 2,
  556. // This is a trick, by adding an extra tiny offset to let the default handle's end point align to the drag window.
  557. // NOTE: It may affect some custom shapes a bit. But we prefer to have better result by default.
  558. x: handleEnds[handleIndex] + (handleIndex ? -1 : 1),
  559. y: size[1] / 2 - handleHeight / 2
  560. });
  561. }, this);
  562. // Filler
  563. displaybles.filler.setShape({
  564. x: handleInterval[0],
  565. y: 0,
  566. width: handleInterval[1] - handleInterval[0],
  567. height: size[1]
  568. });
  569. var viewExtent = {
  570. x: handleInterval[0],
  571. width: handleInterval[1] - handleInterval[0]
  572. };
  573. // Move handle
  574. if (displaybles.moveHandle) {
  575. displaybles.moveHandle.setShape(viewExtent);
  576. displaybles.moveZone.setShape(viewExtent);
  577. // Force update path on the invisible object
  578. displaybles.moveZone.getBoundingRect();
  579. displaybles.moveHandleIcon && displaybles.moveHandleIcon.attr('x', viewExtent.x + viewExtent.width / 2);
  580. }
  581. // update clip path of shadow.
  582. var dataShadowSegs = displaybles.dataShadowSegs;
  583. var segIntervals = [0, handleInterval[0], handleInterval[1], size[0]];
  584. for (var i = 0; i < dataShadowSegs.length; i++) {
  585. var segGroup = dataShadowSegs[i];
  586. var clipPath = segGroup.getClipPath();
  587. if (!clipPath) {
  588. clipPath = new graphic.Rect();
  589. segGroup.setClipPath(clipPath);
  590. }
  591. clipPath.setShape({
  592. x: segIntervals[i],
  593. y: 0,
  594. width: segIntervals[i + 1] - segIntervals[i],
  595. height: size[1]
  596. });
  597. }
  598. this._updateDataInfo(nonRealtime);
  599. };
  600. SliderZoomView.prototype._updateDataInfo = function (nonRealtime) {
  601. var dataZoomModel = this.dataZoomModel;
  602. var displaybles = this._displayables;
  603. var handleLabels = displaybles.handleLabels;
  604. var orient = this._orient;
  605. var labelTexts = ['', ''];
  606. // FIXME
  607. // date型,支持formatter,autoformatter(ec2 date.getAutoFormatter)
  608. if (dataZoomModel.get('showDetail')) {
  609. var axisProxy = dataZoomModel.findRepresentativeAxisProxy();
  610. if (axisProxy) {
  611. var axis = axisProxy.getAxisModel().axis;
  612. var range = this._range;
  613. var dataInterval = nonRealtime
  614. // See #4434, data and axis are not processed and reset yet in non-realtime mode.
  615. ? axisProxy.calculateDataWindow({
  616. start: range[0],
  617. end: range[1]
  618. }).valueWindow : axisProxy.getDataValueWindow();
  619. labelTexts = [this._formatLabel(dataInterval[0], axis), this._formatLabel(dataInterval[1], axis)];
  620. }
  621. }
  622. var orderedHandleEnds = asc(this._handleEnds.slice());
  623. setLabel.call(this, 0);
  624. setLabel.call(this, 1);
  625. function setLabel(handleIndex) {
  626. // Label
  627. // Text should not transform by barGroup.
  628. // Ignore handlers transform
  629. var barTransform = graphic.getTransform(displaybles.handles[handleIndex].parent, this.group);
  630. var direction = graphic.transformDirection(handleIndex === 0 ? 'right' : 'left', barTransform);
  631. var offset = this._handleWidth / 2 + LABEL_GAP;
  632. var textPoint = graphic.applyTransform([orderedHandleEnds[handleIndex] + (handleIndex === 0 ? -offset : offset), this._size[1] / 2], barTransform);
  633. handleLabels[handleIndex].setStyle({
  634. x: textPoint[0],
  635. y: textPoint[1],
  636. verticalAlign: orient === HORIZONTAL ? 'middle' : direction,
  637. align: orient === HORIZONTAL ? direction : 'center',
  638. text: labelTexts[handleIndex]
  639. });
  640. }
  641. };
  642. SliderZoomView.prototype._formatLabel = function (value, axis) {
  643. var dataZoomModel = this.dataZoomModel;
  644. var labelFormatter = dataZoomModel.get('labelFormatter');
  645. var labelPrecision = dataZoomModel.get('labelPrecision');
  646. if (labelPrecision == null || labelPrecision === 'auto') {
  647. labelPrecision = axis.getPixelPrecision();
  648. }
  649. var valueStr = value == null || isNaN(value) ? ''
  650. // FIXME Glue code
  651. : axis.type === 'category' || axis.type === 'time' ? axis.scale.getLabel({
  652. value: Math.round(value)
  653. })
  654. // param of toFixed should less then 20.
  655. : value.toFixed(Math.min(labelPrecision, 20));
  656. return isFunction(labelFormatter) ? labelFormatter(value, valueStr) : isString(labelFormatter) ? labelFormatter.replace('{value}', valueStr) : valueStr;
  657. };
  658. /**
  659. * @param isEmphasis true: show, false: hide
  660. */
  661. SliderZoomView.prototype._showDataInfo = function (isEmphasis) {
  662. var handleLabel = this.dataZoomModel.get('handleLabel') || {};
  663. var normalShow = handleLabel.show || false;
  664. var emphasisHandleLabel = this.dataZoomModel.getModel(['emphasis', 'handleLabel']);
  665. var emphasisShow = emphasisHandleLabel.get('show') || false;
  666. // Dragging is considered as emphasis, unless emphasisShow is false
  667. var toShow = isEmphasis || this._dragging ? emphasisShow : normalShow;
  668. var displayables = this._displayables;
  669. var handleLabels = displayables.handleLabels;
  670. handleLabels[0].attr('invisible', !toShow);
  671. handleLabels[1].attr('invisible', !toShow);
  672. // Highlight move handle
  673. displayables.moveHandle && this.api[toShow ? 'enterEmphasis' : 'leaveEmphasis'](displayables.moveHandle, 1);
  674. };
  675. SliderZoomView.prototype._onDragMove = function (handleIndex, dx, dy, event) {
  676. this._dragging = true;
  677. // For mobile device, prevent screen slider on the button.
  678. eventTool.stop(event.event);
  679. // Transform dx, dy to bar coordination.
  680. var barTransform = this._displayables.sliderGroup.getLocalTransform();
  681. var vertex = graphic.applyTransform([dx, dy], barTransform, true);
  682. var changed = this._updateInterval(handleIndex, vertex[0]);
  683. var realtime = this.dataZoomModel.get('realtime');
  684. this._updateView(!realtime);
  685. // Avoid dispatch dataZoom repeatly but range not changed,
  686. // which cause bad visual effect when progressive enabled.
  687. changed && realtime && this._dispatchZoomAction(true);
  688. };
  689. SliderZoomView.prototype._onDragEnd = function () {
  690. this._dragging = false;
  691. this._showDataInfo(false);
  692. // While in realtime mode and stream mode, dispatch action when
  693. // drag end will cause the whole view rerender, which is unnecessary.
  694. var realtime = this.dataZoomModel.get('realtime');
  695. !realtime && this._dispatchZoomAction(false);
  696. };
  697. SliderZoomView.prototype._onClickPanel = function (e) {
  698. var size = this._size;
  699. var localPoint = this._displayables.sliderGroup.transformCoordToLocal(e.offsetX, e.offsetY);
  700. if (localPoint[0] < 0 || localPoint[0] > size[0] || localPoint[1] < 0 || localPoint[1] > size[1]) {
  701. return;
  702. }
  703. var handleEnds = this._handleEnds;
  704. var center = (handleEnds[0] + handleEnds[1]) / 2;
  705. var changed = this._updateInterval('all', localPoint[0] - center);
  706. this._updateView();
  707. changed && this._dispatchZoomAction(false);
  708. };
  709. SliderZoomView.prototype._onBrushStart = function (e) {
  710. var x = e.offsetX;
  711. var y = e.offsetY;
  712. this._brushStart = new graphic.Point(x, y);
  713. this._brushing = true;
  714. this._brushStartTime = +new Date();
  715. // this._updateBrushRect(x, y);
  716. };
  717. SliderZoomView.prototype._onBrushEnd = function (e) {
  718. if (!this._brushing) {
  719. return;
  720. }
  721. var brushRect = this._displayables.brushRect;
  722. this._brushing = false;
  723. if (!brushRect) {
  724. return;
  725. }
  726. brushRect.attr('ignore', true);
  727. var brushShape = brushRect.shape;
  728. var brushEndTime = +new Date();
  729. // console.log(brushEndTime - this._brushStartTime);
  730. if (brushEndTime - this._brushStartTime < 200 && Math.abs(brushShape.width) < 5) {
  731. // Will treat it as a click
  732. return;
  733. }
  734. var viewExtend = this._getViewExtent();
  735. var percentExtent = [0, 100];
  736. var handleEnds = this._handleEnds = [brushShape.x, brushShape.x + brushShape.width];
  737. var minMaxSpan = this.dataZoomModel.findRepresentativeAxisProxy().getMinMaxSpan();
  738. // Restrict range.
  739. sliderMove(0, handleEnds, viewExtend, 0, minMaxSpan.minSpan != null ? linearMap(minMaxSpan.minSpan, percentExtent, viewExtend, true) : null, minMaxSpan.maxSpan != null ? linearMap(minMaxSpan.maxSpan, percentExtent, viewExtend, true) : null);
  740. this._range = asc([linearMap(handleEnds[0], viewExtend, percentExtent, true), linearMap(handleEnds[1], viewExtend, percentExtent, true)]);
  741. this._updateView();
  742. this._dispatchZoomAction(false);
  743. };
  744. SliderZoomView.prototype._onBrush = function (e) {
  745. if (this._brushing) {
  746. // For mobile device, prevent screen slider on the button.
  747. eventTool.stop(e.event);
  748. this._updateBrushRect(e.offsetX, e.offsetY);
  749. }
  750. };
  751. SliderZoomView.prototype._updateBrushRect = function (mouseX, mouseY) {
  752. var displayables = this._displayables;
  753. var dataZoomModel = this.dataZoomModel;
  754. var brushRect = displayables.brushRect;
  755. if (!brushRect) {
  756. brushRect = displayables.brushRect = new Rect({
  757. silent: true,
  758. style: dataZoomModel.getModel('brushStyle').getItemStyle()
  759. });
  760. displayables.sliderGroup.add(brushRect);
  761. }
  762. brushRect.attr('ignore', false);
  763. var brushStart = this._brushStart;
  764. var sliderGroup = this._displayables.sliderGroup;
  765. var endPoint = sliderGroup.transformCoordToLocal(mouseX, mouseY);
  766. var startPoint = sliderGroup.transformCoordToLocal(brushStart.x, brushStart.y);
  767. var size = this._size;
  768. endPoint[0] = Math.max(Math.min(size[0], endPoint[0]), 0);
  769. brushRect.setShape({
  770. x: startPoint[0],
  771. y: 0,
  772. width: endPoint[0] - startPoint[0],
  773. height: size[1]
  774. });
  775. };
  776. /**
  777. * This action will be throttled.
  778. */
  779. SliderZoomView.prototype._dispatchZoomAction = function (realtime) {
  780. var range = this._range;
  781. this.api.dispatchAction({
  782. type: 'dataZoom',
  783. from: this.uid,
  784. dataZoomId: this.dataZoomModel.id,
  785. animation: realtime ? REALTIME_ANIMATION_CONFIG : null,
  786. start: range[0],
  787. end: range[1]
  788. });
  789. };
  790. SliderZoomView.prototype._findCoordRect = function () {
  791. // Find the grid corresponding to the first axis referred by dataZoom.
  792. var rect;
  793. var coordSysInfoList = collectReferCoordSysModelInfo(this.dataZoomModel).infoList;
  794. if (!rect && coordSysInfoList.length) {
  795. var coordSys = coordSysInfoList[0].model.coordinateSystem;
  796. rect = coordSys.getRect && coordSys.getRect();
  797. }
  798. if (!rect) {
  799. var width = this.api.getWidth();
  800. var height = this.api.getHeight();
  801. rect = {
  802. x: width * 0.2,
  803. y: height * 0.2,
  804. width: width * 0.6,
  805. height: height * 0.6
  806. };
  807. }
  808. return rect;
  809. };
  810. SliderZoomView.type = 'dataZoom.slider';
  811. return SliderZoomView;
  812. }(DataZoomView);
  813. function getOtherDim(thisDim) {
  814. // FIXME
  815. // 这个逻辑和getOtherAxis里一致,但是写在这里是否不好
  816. var map = {
  817. x: 'y',
  818. y: 'x',
  819. radius: 'angle',
  820. angle: 'radius'
  821. };
  822. return map[thisDim];
  823. }
  824. function getCursor(orient) {
  825. return orient === 'vertical' ? 'ns-resize' : 'ew-resize';
  826. }
  827. export default SliderZoomView;