SliderZoomView.js 31 KB

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