sliderAxis.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. "use strict";
  2. // import { ElementEvent, Line, CustomEvent, IAnimation } from '../../shapes';
  3. // import type { DisplayObjectConfig } from '../../shapes';
  4. // import { clamp, deepMix } from '@antv/util';
  5. // import { Point as PointScale } from '@antv/scale';
  6. // import { GUI } from '../../core';
  7. // import { applyStyle, maybeAppend, select, Selection } from '../../util';
  8. // import { Axis, AxisStyleProps as AxisOptions } from '../axis';
  9. // import {
  10. // AxisBase,
  11. // AxisStyleProps,
  12. // DEFAULT_AXIS_CFG,
  13. // DEFAULT_STYLE as BASE_DEFAULT_STYLE,
  14. // normalSelection,
  15. // } from './playAxis';
  16. // type SliderAxisOptions = DisplayObjectConfig<AxisStyleProps>;
  17. // function getScale(data: any[], range: number[]) {
  18. // return new PointScale({
  19. // domain: data.map((_, idx) => idx),
  20. // range,
  21. // // todo, 是否开放配置
  22. // padding: 0.1,
  23. // });
  24. // }
  25. // function getOffsetIntervalByOffsetDistance(offset: number, totalLength: number, data: any[]): number {
  26. // const scale = getScale(data, [0, totalLength]);
  27. // const step = scale.getStep();
  28. // const round = offset > 0 ? Math.ceil : Math.floor;
  29. // return step > 0 ? round(offset / step) : 0;
  30. // }
  31. // function getPositionByIndex(index: number, totalLength: number, data: any[]) {
  32. // const scale = getScale(data, [0, totalLength]);
  33. // return scale.map(index);
  34. // }
  35. // export class SliderAxis extends AxisBase<AxisStyleProps> {
  36. // private startHandleAnimation: IAnimation | null = null;
  37. // private endHandleAnimation: IAnimation | null = null;
  38. // public static defaultOptions: SliderAxisOptions = {
  39. // style: deepMix({}, BASE_DEFAULT_STYLE, {
  40. // tag: 'slider-axis',
  41. // size: 3,
  42. // handleStyle: {
  43. // r: 3,
  44. // stroke: '#8AADF3',
  45. // strokeOpacity: 0.75,
  46. // lineWidth: 1,
  47. // fill: '#fff',
  48. // fillOpacity: 1,
  49. // },
  50. // backgroundStyle: {
  51. // fill: '#416180',
  52. // fillOpacity: 0.1,
  53. // },
  54. // selectionStyle: {
  55. // cursor: 'grabbing',
  56. // },
  57. // }),
  58. // };
  59. // constructor(options: SliderAxisOptions) {
  60. // super(deepMix({}, SliderAxis.defaultOptions, options));
  61. // this.selection = normalSelection(this.style.selection, this.style.singleMode);
  62. // }
  63. // private get styles(): Required<AxisStyleProps> {
  64. // return deepMix({}, SliderAxis.defaultOptions.style, this.attributes);
  65. // }
  66. // protected setSelection(newSelection: { start?: number; end?: number }) {
  67. // const { start: startIndex, end: endIndex } = newSelection;
  68. // const { data = [], playInterval } = this.styles;
  69. // const animationOptions = {
  70. // duration: playInterval! / 2,
  71. // easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)', // 缓动函数
  72. // fill: 'both' as any,
  73. // };
  74. // const posName = this.ifH('cx', 'cy');
  75. // const startHandle = select(this).select('.slider-start-handle').node();
  76. // if (startIndex !== undefined) {
  77. // const pos = getPositionByIndex(startIndex, this.style.length!, data);
  78. // this.startHandleAnimation = startHandle.animate(
  79. // [{ [posName]: startHandle.style[posName] }, { [posName]: pos }],
  80. // animationOptions
  81. // );
  82. // }
  83. // if (endIndex !== undefined) {
  84. // const endHandle = select(this).select('.slider-end-handle').node();
  85. // const pos1 = getPositionByIndex(endIndex, this.style.length!, data);
  86. // this.endHandleAnimation = endHandle.animate(
  87. // [{ [posName]: endHandle.style[posName] }, { [posName]: pos1 }],
  88. // animationOptions
  89. // );
  90. // }
  91. // this.selection = [startIndex ?? this.selection[0], endIndex ?? this.selection[1]];
  92. // this.dispatchEvent(new CustomEvent('selectionChanged', { detail: { selection: this.selection } }));
  93. // }
  94. // protected render() {
  95. // const styles = this.styles;
  96. // const [width, height] = this.ifH([styles.length, styles.size], [styles.size, styles.length]);
  97. // const bg = maybeAppend(this, '.slider-background', 'line')
  98. // .attr('className', 'slider-background')
  99. // .style('x1', 0)
  100. // .style('y1', 0)
  101. // .style('x2', this.ifH(width, 0))
  102. // .style('y2', this.ifH(0, height))
  103. // .style('lineWidth', styles.size)
  104. // .style('lineCap', 'round')
  105. // .style('stroke', styles.backgroundStyle?.fill)
  106. // .style('strokeOpacity', styles.backgroundStyle?.fillOpacity)
  107. // .node();
  108. // const tickScale = getScale(styles.data, [0, 1]);
  109. // const [st = 0, et = st] = this.selection.map((d) => tickScale.map(d)) as number[];
  110. // const x1 = this.ifH(st * width, 0);
  111. // const x2 = this.ifH(et * width, 0);
  112. // const y1 = this.ifH(0, st * height);
  113. // const y2 = this.ifH(0, et * height);
  114. // maybeAppend(bg, '.slider-selection', 'line')
  115. // .attr('className', 'slider-selection')
  116. // .style('x1', x1)
  117. // .style('y1', y1)
  118. // .style('x2', x2)
  119. // .style('y2', y2)
  120. // .style('lineWidth', styles.size)
  121. // .style('lineCap', 'round')
  122. // .style('stroke', styles.selectionStyle?.fill)
  123. // .style('strokeOpacity', styles.selectionStyle?.fillOpacity)
  124. // .style('visibility', styles.singleMode ? 'hidden' : 'visible')
  125. // .call(applyStyle, styles.selectionStyle);
  126. // if (this.startHandleAnimation?.playState === 'running') this.startHandleAnimation.pause();
  127. // maybeAppend(bg, '.slider-start-handle', 'circle')
  128. // .attr('className', 'slider-start-handle')
  129. // .style('cx', x1)
  130. // .style('cy', y1)
  131. // .call(applyStyle, styles.handleStyle);
  132. // if (this.startHandleAnimation?.playState === 'paused') this.startHandleAnimation.play();
  133. // if (this.endHandleAnimation?.playState === 'running') this.endHandleAnimation.pause();
  134. // maybeAppend(bg, '.slider-end-handle', 'circle')
  135. // .attr('className', 'slider-end-handle')
  136. // .style('cx', x2)
  137. // .style('cy', y2)
  138. // .style('visibility', styles.singleMode ? 'hidden' : 'visible')
  139. // .call(applyStyle, styles.handleStyle);
  140. // if (this.endHandleAnimation?.playState === 'paused') this.endHandleAnimation.play();
  141. // const ticks = styles.data.map((tick, idx) => ({ value: tickScale.map(idx), text: tick.date }));
  142. // const { position: verticalFactor = -1, tickLine: tickLineCfg, ...axisLabelCfg } = styles.label || {};
  143. // maybeAppend(bg, '.slider-axis', () => new Axis({ className: 'slider-axis' })).call((selection) =>
  144. // (selection.node() as GUI<AxisOptions>).update(
  145. // deepMix({}, DEFAULT_AXIS_CFG, {
  146. // startPos: [verticalFactor * this.ifH(0, width + 2), verticalFactor * this.ifH(height + 2, 0)],
  147. // endPos: [
  148. // this.ifH(width, 0) + verticalFactor * this.ifH(0, width + 2),
  149. // this.ifH(0, height) + verticalFactor * this.ifH(height + 2, 0),
  150. // ],
  151. // ticks,
  152. // verticalFactor,
  153. // tickLine: styles.label === null ? null : tickLineCfg,
  154. // label: styles.label === null ? null : axisLabelCfg,
  155. // })
  156. // )
  157. // );
  158. // }
  159. // private bindEvents() {
  160. // const target = select(this).select('.slider-background');
  161. // this.dragHandle(target, 'start');
  162. // this.dragHandle(target, 'end');
  163. // this.dragSelection(target);
  164. // const selection = target.select('.slider-selection').node() as Line;
  165. // const startHandle = target.select('.slider-start-handle').node();
  166. // const endHandle = target.select('.slider-end-handle').node();
  167. // startHandle.addEventListener(ElementEvent.ATTR_MODIFIED, ({ attrName, newValue, prevValue }: any) => {
  168. // if (attrName === 'cx' || attrName === 'cy') {
  169. // const value = parseFloat(newValue) || 0;
  170. // selection.style[attrName === 'cx' ? 'x1' : 'y1'] = value;
  171. // }
  172. // });
  173. // endHandle.addEventListener(ElementEvent.ATTR_MODIFIED, ({ attrName, newValue, prevValue }: any) => {
  174. // if (attrName === 'cx' || attrName === 'cy') {
  175. // const value = parseFloat(newValue) || 0;
  176. // selection.style[attrName === 'cx' ? 'x2' : 'y2'] = value;
  177. // }
  178. // });
  179. // }
  180. // private dragHandle(selection: Selection, type: 'start' | 'end') {
  181. // let dragging = false; // 拖拽状态
  182. // let lastPosition: any; // 保存上次位置
  183. // let firstPosition: number[] | undefined; // 保存首次位置
  184. // const onDragStart = (event: any) => {
  185. // if (this.playTimer) {
  186. // clearInterval(this.playTimer);
  187. // }
  188. // const shape = selection.select(`.slider-${type}-handle`).node();
  189. // if (event.target === shape) {
  190. // dragging = true;
  191. // firstPosition = [event.x, event.y];
  192. // lastPosition = [event.x, event.y];
  193. // }
  194. // };
  195. // const onDragEnd = (event: any) => {
  196. // if (!dragging) return;
  197. // dragging = false;
  198. // const offset = firstPosition ? this.ifH(event.x - firstPosition[0], event.y - firstPosition[1]) : 0;
  199. // const interval = getOffsetIntervalByOffsetDistance(offset, this.style.length!, this.style.data);
  200. // firstPosition = undefined;
  201. // const [startIndex, endIndex] = this.selection;
  202. // const max = this.style.data.length - 1;
  203. // if (type === 'start') {
  204. // if (startIndex === endIndex && interval > 0) {
  205. // const start = clamp(0, startIndex + interval, max);
  206. // this.setSelection({ start, end: start });
  207. // } else {
  208. // this.setSelection({ start: clamp(startIndex + interval, 0, endIndex) });
  209. // }
  210. // } else if (startIndex === endIndex && interval < 0) {
  211. // const end = clamp(0, endIndex + interval, max);
  212. // this.setSelection({ start: end, end });
  213. // } else {
  214. // this.setSelection({ end: clamp(endIndex + interval, startIndex, max) });
  215. // }
  216. // if (this.playTimer) {
  217. // this.play();
  218. // }
  219. // };
  220. // const onDragMove = (event: any) => {
  221. // if (dragging) {
  222. // const length = this.styles.length!;
  223. // const shape = select(this).select(`.slider-${type}-handle`).node();
  224. // const offset = this.ifH(event.x - lastPosition[0], event.y - lastPosition[1]);
  225. // const position = shape.getLocalPosition();
  226. // if (this.orientation === 'vertical') {
  227. // shape.style.cy = clamp(position[1] + offset, 0, length);
  228. // } else {
  229. // shape.style.cx = clamp(position[0] + offset, 0, length);
  230. // }
  231. // lastPosition = [event.x, event.y];
  232. // }
  233. // };
  234. // selection
  235. // // events for drag start
  236. // .on('pointerdown', onDragStart.bind(this))
  237. // // events for drag end
  238. // .on('pointerup', onDragEnd)
  239. // .on('mouseupoutside', onDragEnd)
  240. // .on('touchendoutside', onDragEnd)
  241. // // events for drag move
  242. // .on('pointermove', onDragMove);
  243. // }
  244. // private dragSelection(selection: Selection) {
  245. // const shape = selection.select('.slider-selection').node() as Line;
  246. // const startHandle = selection.select('.slider-start-handle').node();
  247. // const endHandle = selection.select('.slider-end-handle').node();
  248. // let dragging = false; // 拖拽状态
  249. // let lastPosition: any; // 保存上次位置
  250. // let firstPosition: number[] | undefined; // 保存首次位置
  251. // const onDragStart = (event: any) => {
  252. // if (this.playTimer) {
  253. // clearInterval(this.playTimer);
  254. // }
  255. // if (event.target === shape) {
  256. // dragging = true;
  257. // firstPosition = [event.x, event.y];
  258. // lastPosition = [event.x, event.y];
  259. // }
  260. // };
  261. // const onDragEnd = (event: any) => {
  262. // if (!dragging) return;
  263. // dragging = false;
  264. // const offset = firstPosition ? this.ifH(event.x - firstPosition[0], event.y - firstPosition[1]) : 0;
  265. // const moveInterval = getOffsetIntervalByOffsetDistance(offset, this.style.length!, this.style.data);
  266. // firstPosition = undefined;
  267. // const range = this.selection[1] - this.selection[0];
  268. // const endIndex = clamp(this.selection[1] + moveInterval, range, this.style.data.length - 1);
  269. // this.setSelection({ start: endIndex - range, end: endIndex });
  270. // if (this.playTimer) {
  271. // this.play();
  272. // }
  273. // };
  274. // const onDragMove = (event: any) => {
  275. // if (dragging) {
  276. // const length = this.styles.length!;
  277. // const offset = this.ifH(event.x - lastPosition[0], event.y - lastPosition[1]);
  278. // const [cx0, cy0] = startHandle.getLocalPosition();
  279. // const [cx1, cy1] = endHandle.getLocalPosition();
  280. // if (this.orientation === 'vertical') {
  281. // const height = cy1 - cy0;
  282. // startHandle.style.cy = clamp(cy0 + offset, 0, length - height);
  283. // endHandle.style.cy = startHandle.style.cy + height;
  284. // } else {
  285. // const width = cx1 - cx0;
  286. // startHandle.style.cx = clamp(cx0 + offset, 0, length);
  287. // endHandle.style.cx = startHandle.style.cx + width;
  288. // }
  289. // lastPosition = [event.x, event.y];
  290. // }
  291. // };
  292. // selection
  293. // // events for drag start
  294. // .on('mousedown', onDragStart.bind(this))
  295. // .on('touchstart', onDragStart.bind(this))
  296. // // events for drag end
  297. // .on('mouseup', onDragEnd.bind(this))
  298. // .on('mouseupoutside', onDragEnd.bind(this))
  299. // .on('touchend', onDragEnd.bind(this))
  300. // .on('touchendoutside', onDragEnd.bind(this))
  301. // // events for drag move
  302. // .on('mousemove', onDragMove.bind(this))
  303. // .on('touchmove', onDragMove.bind(this));
  304. // }
  305. // }
  306. //# sourceMappingURL=sliderAxis.js.map