@antv/component 0.8.32 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/esm/abstract/html-component.js +1 -9
  2. package/esm/abstract/html-component.js.map +1 -1
  3. package/esm/legend/category.js +12 -3
  4. package/esm/legend/category.js.map +1 -1
  5. package/esm/slider/constant.d.ts +46 -1
  6. package/esm/slider/constant.js +50 -4
  7. package/esm/slider/constant.js.map +1 -1
  8. package/esm/slider/foreground.d.ts +78 -0
  9. package/esm/slider/foreground.js +102 -0
  10. package/esm/slider/foreground.js.map +1 -0
  11. package/esm/slider/handler.d.ts +3 -8
  12. package/esm/slider/handler.js +8 -15
  13. package/esm/slider/handler.js.map +1 -1
  14. package/esm/slider/slider.d.ts +29 -4
  15. package/esm/slider/slider.js +243 -46
  16. package/esm/slider/slider.js.map +1 -1
  17. package/esm/tooltip/css-const.d.ts +0 -1
  18. package/esm/tooltip/css-const.js +0 -1
  19. package/esm/tooltip/css-const.js.map +1 -1
  20. package/esm/tooltip/html-theme.d.ts +0 -22
  21. package/esm/tooltip/html-theme.js +0 -7
  22. package/esm/tooltip/html-theme.js.map +1 -1
  23. package/esm/tooltip/html.d.ts +0 -23
  24. package/esm/tooltip/html.js +11 -27
  25. package/esm/tooltip/html.js.map +1 -1
  26. package/esm/util/event.d.ts +8 -0
  27. package/esm/util/event.js +11 -0
  28. package/esm/util/event.js.map +1 -1
  29. package/esm/util/mask.d.ts +9 -0
  30. package/esm/util/mask.js +26 -0
  31. package/esm/util/mask.js.map +1 -0
  32. package/esm/util/util.d.ts +10 -0
  33. package/esm/util/util.js +62 -1
  34. package/esm/util/util.js.map +1 -1
  35. package/lib/abstract/html-component.js +1 -9
  36. package/lib/abstract/html-component.js.map +1 -1
  37. package/lib/legend/category.js +12 -3
  38. package/lib/legend/category.js.map +1 -1
  39. package/lib/slider/constant.d.ts +46 -1
  40. package/lib/slider/constant.js +51 -5
  41. package/lib/slider/constant.js.map +1 -1
  42. package/lib/slider/foreground.d.ts +78 -0
  43. package/lib/slider/foreground.js +105 -0
  44. package/lib/slider/foreground.js.map +1 -0
  45. package/lib/slider/handler.d.ts +3 -8
  46. package/lib/slider/handler.js +9 -16
  47. package/lib/slider/handler.js.map +1 -1
  48. package/lib/slider/slider.d.ts +29 -4
  49. package/lib/slider/slider.js +240 -43
  50. package/lib/slider/slider.js.map +1 -1
  51. package/lib/tooltip/css-const.d.ts +0 -1
  52. package/lib/tooltip/css-const.js +1 -2
  53. package/lib/tooltip/css-const.js.map +1 -1
  54. package/lib/tooltip/html-theme.d.ts +0 -22
  55. package/lib/tooltip/html-theme.js +0 -7
  56. package/lib/tooltip/html-theme.js.map +1 -1
  57. package/lib/tooltip/html.d.ts +0 -23
  58. package/lib/tooltip/html.js +11 -27
  59. package/lib/tooltip/html.js.map +1 -1
  60. package/lib/util/event.d.ts +8 -0
  61. package/lib/util/event.js +13 -1
  62. package/lib/util/event.js.map +1 -1
  63. package/lib/util/mask.d.ts +9 -0
  64. package/lib/util/mask.js +32 -0
  65. package/lib/util/mask.js.map +1 -0
  66. package/lib/util/util.d.ts +10 -0
  67. package/lib/util/util.js +64 -1
  68. package/lib/util/util.js.map +1 -1
  69. package/package.json +6 -18
  70. package/src/abstract/html-component.ts +1 -12
  71. package/src/legend/category.ts +21 -12
  72. package/src/slider/constant.ts +54 -4
  73. package/src/slider/foreground.ts +158 -0
  74. package/src/slider/handler.ts +10 -20
  75. package/src/slider/slider.ts +275 -60
  76. package/src/tooltip/css-const.ts +0 -1
  77. package/src/tooltip/html-theme.ts +0 -8
  78. package/src/tooltip/html.ts +11 -29
  79. package/src/util/event.ts +12 -0
  80. package/src/util/mask.ts +32 -0
  81. package/src/util/util.ts +67 -2
@@ -1,18 +1,28 @@
1
- import { Event, IGroup, IShape } from '@antv/g-base';
2
- import { clamp, deepMix, each, get, isArray, isNil, size } from '@antv/util';
1
+ import { BBox, Event, IGroup, IShape } from '@antv/g-base';
2
+ import { clamp, deepMix, each, get, isArray, isNil, isNumber, size } from '@antv/util';
3
3
  import GroupComponent from '../abstract/group-component';
4
4
  import { ISlider } from '../interfaces';
5
5
  import { Trend } from '../trend/trend';
6
- import { DEFAULT_HANDLER_STYLE, Handler, HandlerCfg } from './handler';
6
+ import { Handler, HandlerCfg } from './handler';
7
7
  import { GroupComponentCfg, Range } from '../types';
8
8
  import {
9
9
  BACKGROUND_STYLE,
10
+ BAR_STYLE,
11
+ DEFAULT_HANDLER_HEIGHT,
10
12
  DEFAULT_HANDLER_WIDTH,
13
+ DEFAULT_TREND_STYLE,
11
14
  FOREGROUND_STYLE,
12
15
  HANDLER_STYLE,
16
+ MAX_TEXT_WIDTH,
13
17
  SLIDER_CHANGE,
18
+ TEXT_PADDING,
19
+ TEXT_SAFE_WIDTH,
14
20
  TEXT_STYLE,
15
21
  } from './constant';
22
+ import { createMask, getRectMaskAttrs, updateMask } from '../util/mask';
23
+ import { clipTextTwoLines, getTextWidth } from '../util/util';
24
+ import { Foreground } from './foreground';
25
+ import { getCurrentPoint } from '../util/event';
16
26
 
17
27
  export interface TrendCfg {
18
28
  // 数据
@@ -23,6 +33,7 @@ export interface TrendCfg {
23
33
  readonly backgroundStyle?: object;
24
34
  readonly lineStyle?: object;
25
35
  readonly areaStyle?: object;
36
+ readonly barStyle?: object;
26
37
  }
27
38
 
28
39
  /**
@@ -62,11 +73,19 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
62
73
 
63
74
  private minHandler: Handler;
64
75
  private maxHandler: Handler;
76
+ private foreground: Foreground;
65
77
  private trend: Trend;
66
78
  private currentTarget: string;
67
79
  private prevX: number;
68
80
  private prevY: number;
69
81
 
82
+ // Brush 起始点坐标记录
83
+ private brushPrevX: number = null;
84
+ // 标识开始和结束 brush 操作
85
+ private enableBrush: boolean = false;
86
+ // Brush 框样式Shape
87
+ private brushMaskShape: IShape = null;
88
+
70
89
  public setRange(min: number, max: number) {
71
90
  this.set('minLimit', min);
72
91
  this.set('maxLimit', max);
@@ -121,11 +140,13 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
121
140
  foregroundStyle: {},
122
141
  handlerStyle: {},
123
142
  textStyle: {},
143
+ barStyle: {},
124
144
  defaultCfg: {
125
145
  backgroundStyle: BACKGROUND_STYLE,
126
146
  foregroundStyle: FOREGROUND_STYLE,
127
147
  handlerStyle: HANDLER_STYLE,
128
148
  textStyle: TEXT_STYLE,
149
+ barStyle: BAR_STYLE,
129
150
  },
130
151
  };
131
152
  }
@@ -140,6 +161,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
140
161
  validCfg.end = clamp(end, 0, 1);
141
162
  }
142
163
  super.update(validCfg);
164
+ this.foreground = this.getChildComponentById(this.getElementId('foreground'));
143
165
  this.minHandler = this.getChildComponentById(this.getElementId('minHandler'));
144
166
  this.maxHandler = this.getChildComponentById(this.getElementId('maxHandler'));
145
167
  this.trend = this.getChildComponentById(this.getElementId('trend'));
@@ -154,11 +176,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
154
176
  public render() {
155
177
  super.render();
156
178
 
157
- this.updateUI(
158
- this.getElementByLocalId('foreground'),
159
- this.getElementByLocalId('minText'),
160
- this.getElementByLocalId('maxText')
161
- );
179
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
162
180
  }
163
181
 
164
182
  protected renderInner(group: IGroup) {
@@ -173,14 +191,14 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
173
191
  backgroundStyle = {},
174
192
  foregroundStyle = {},
175
193
  textStyle = {},
194
+ barStyle = {},
176
195
  } = this.cfg;
177
196
 
178
- const handlerStyle = deepMix({}, DEFAULT_HANDLER_STYLE, this.cfg.handlerStyle);
179
-
180
197
  const min = start * width;
181
198
  const max = end * width;
182
199
 
183
- // 趋势图数据
200
+ // default 趋势图数据
201
+ const defaultTrendStyle = deepMix({}, DEFAULT_TREND_STYLE, trendCfg);
184
202
  if (size(get(trendCfg, 'data'))) {
185
203
  this.trend = this.addComponent(group, {
186
204
  component: Trend,
@@ -189,7 +207,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
189
207
  y: 0,
190
208
  width,
191
209
  height,
192
- ...trendCfg,
210
+ ...defaultTrendStyle,
193
211
  });
194
212
  }
195
213
 
@@ -202,11 +220,22 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
202
220
  y: 0,
203
221
  width,
204
222
  height,
223
+ cursor: 'crosshair',
205
224
  ...backgroundStyle,
206
225
  },
207
226
  });
208
227
 
209
- // 2. 左右文字
228
+ // 2. 前景 选中背景框
229
+ this.foreground = this.addComponent(group, {
230
+ component: Foreground,
231
+ id: this.getElementId('foreground'),
232
+ name: 'foreground',
233
+ height,
234
+ foregroundStyle,
235
+ barStyle,
236
+ });
237
+
238
+ // 3. 左右文字
210
239
  const minTextShape = this.addShape(group, {
211
240
  id: this.getElementId('minText'),
212
241
  type: 'text',
@@ -219,7 +248,6 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
219
248
  ...textStyle,
220
249
  },
221
250
  });
222
-
223
251
  const maxTextShape = this.addShape(group, {
224
252
  id: this.getElementId('maxText'),
225
253
  type: 'text',
@@ -233,23 +261,10 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
233
261
  },
234
262
  });
235
263
 
236
- // 3. 前景 选中背景框
237
- const foregroundShape = this.addShape(group, {
238
- id: this.getElementId('foreground'),
239
- name: 'foreground',
240
- type: 'rect',
241
- attrs: {
242
- // x: 0,
243
- y: 0,
244
- // width: 0,
245
- height,
246
- ...foregroundStyle,
247
- },
248
- });
249
-
250
264
  // 滑块相关的大小信息
265
+ const handlerStyle = deepMix({}, HANDLER_STYLE, this.cfg.handlerStyle);
251
266
  const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
252
- const handlerHeight = get(handlerStyle, 'height', 24);
267
+ const handlerHeight = get(handlerStyle, 'height', DEFAULT_HANDLER_HEIGHT);
253
268
 
254
269
  // 4. 左右滑块
255
270
  this.minHandler = this.addComponent(group, {
@@ -288,7 +303,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
288
303
  this.bindEvents();
289
304
  }
290
305
 
291
- private updateUI(foregroundShape: IShape, minTextShape: IShape, maxTextShape: IShape) {
306
+ private updateUI(minTextShape: IShape, maxTextShape: IShape) {
292
307
  const { start, end, width, minText, maxText, handlerStyle, height } = this.cfg as SliderCfg;
293
308
  const min = start * width;
294
309
  const max = end * width;
@@ -303,9 +318,15 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
303
318
  }
304
319
  }
305
320
 
306
- // 1. foreground
307
- foregroundShape.attr('x', min);
308
- foregroundShape.attr('width', max - min);
321
+ if (this.foreground) {
322
+ this.foreground.update({
323
+ x: min,
324
+ width: max - min,
325
+ });
326
+ if (!this.get('updateAutoRender')) {
327
+ this.foreground.render();
328
+ }
329
+ }
309
330
 
310
331
  // 滑块相关的大小信息
311
332
  const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
@@ -348,10 +369,18 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
348
369
  group.on('handler-max:mousedown', this.onMouseDown('maxHandler'));
349
370
  group.on('handler-max:touchstart', this.onMouseDown('maxHandler'));
350
371
 
351
- // 3. 前景选中区域
352
- const foreground = group.findById(this.getElementId('foreground'));
353
- foreground.on('mousedown', this.onMouseDown('foreground'));
354
- foreground.on('touchstart', this.onMouseDown('foreground'));
372
+ // 3.背景区域框选
373
+ const background = group.findById(this.getElementId('background'));
374
+ background.on('mousedown', this.onMouseDown('background'));
375
+
376
+ // 4. 前景选中区域
377
+ // 选中区域拖拽
378
+ group.on('foreground-scroll:mousedown', this.onMouseDown('foreground'));
379
+ group.on('foreground-bar:mousedown', this.onMouseDown('foreground'));
380
+ // 选中区域框选
381
+ group.on('foreground-brush:mousedown', this.onMouseDown('foreground-brush'));
382
+ // 移动端:只允许拖拽
383
+ group.on('foreground:touchstart', this.onMouseDown('foreground'));
355
384
  }
356
385
 
357
386
  private onMouseDown = (target: string) => (e: Event) => {
@@ -364,12 +393,24 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
364
393
  event.preventDefault();
365
394
 
366
395
  // 兼容移动端获取数据
367
- this.prevX = get(event, 'touches.0.pageX', event.pageX);
368
- this.prevY = get(event, 'touches.0.pageY', event.pageY);
396
+ const { x, y } = getCurrentPoint(event);
397
+ this.prevX = x;
398
+ this.prevY = y;
369
399
 
370
- // 3. 开始滑动的时候,绑定 move 和 up 事件
371
400
  const containerDOM = this.getContainerDOM();
372
401
 
402
+ // 框选事件
403
+ if (['background', 'foreground-brush'].includes(target)) {
404
+ this.brushPrevX = event.offsetX;
405
+ this.enableBrush = true;
406
+ // 绑定框选事件
407
+ containerDOM.addEventListener('mousemove', this.onBrushMouseMove);
408
+ containerDOM.addEventListener('mouseup', this.onBrushMouseUp);
409
+ containerDOM.addEventListener('mouseleave', this.onBrushMouseUp);
410
+ return;
411
+ }
412
+
413
+ // 3. 开始滑动的时候,绑定 move 和 up 事件
373
414
  containerDOM.addEventListener('mousemove', this.onMouseMove);
374
415
  containerDOM.addEventListener('mouseup', this.onMouseUp);
375
416
  containerDOM.addEventListener('mouseleave', this.onMouseUp);
@@ -387,8 +428,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
387
428
  event.stopPropagation();
388
429
  event.preventDefault();
389
430
 
390
- const x = get(event, 'touches.0.pageX', event.pageX);
391
- const y = get(event, 'touches.0.pageY', event.pageY);
431
+ const { x, y } = getCurrentPoint(event);
392
432
 
393
433
  // 横向的 slider 只处理 x
394
434
  const offsetX = x - this.prevX;
@@ -398,11 +438,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
398
438
  // 更新 start end range 范围
399
439
  this.updateStartEnd(offsetXRange);
400
440
  // 更新 ui
401
- this.updateUI(
402
- this.getElementByLocalId('foreground'),
403
- this.getElementByLocalId('minText'),
404
- this.getElementByLocalId('maxText')
405
- );
441
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
406
442
 
407
443
  this.prevX = x;
408
444
  this.prevY = y;
@@ -457,6 +493,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
457
493
 
458
494
  return Math.min(max, Math.max(min, offsetRange));
459
495
  }
496
+ case 'foreground-brush':
460
497
  case 'foreground': {
461
498
  const min = 0 - start;
462
499
  const max = 1 - end;
@@ -476,7 +513,9 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
476
513
  case 'maxHandler':
477
514
  end += offsetRange;
478
515
  break;
516
+
479
517
  case 'foreground':
518
+ case 'foreground-brush':
480
519
  start += offsetRange;
481
520
  end += offsetRange;
482
521
  break;
@@ -490,11 +529,8 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
490
529
  * 根据位置,调整返回新的位置
491
530
  * @param range
492
531
  */
493
- private _dodgeText(range: [number, number], minTextShape, maxTextShape): [object, object] {
494
- const { handlerStyle, width } = this.cfg as SliderCfg;
495
- const PADDING = 2;
496
- const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
497
-
532
+ private _dodgeText(range: [number, number], minTextShape: IShape, maxTextShape: IShape): [object, object] {
533
+ const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
498
534
  let [min, max] = range;
499
535
  let sorted = false;
500
536
 
@@ -509,19 +545,127 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
509
545
  const minBBox = minTextShape.getBBox();
510
546
  const maxBBox = maxTextShape.getBBox();
511
547
 
512
- const minAttrs =
513
- minBBox.width > min - PADDING
514
- ? { x: min + handlerWidth / 2 + PADDING, textAlign: 'left' }
515
- : { x: min - handlerWidth / 2 - PADDING, textAlign: 'right' };
548
+ const { attrs: minAttrs, textWidth: minTextWidth } = this.setTextAttrs(minBBox, minTextShape, [min, max], 'min');
549
+ const { attrs: maxAttrs, textWidth: maxTextWidth } = this.setTextAttrs(maxBBox, maxTextShape, [min, max], 'max');
516
550
 
517
- const maxAttrs =
518
- maxBBox.width > width - max - PADDING
519
- ? { x: max - handlerWidth / 2 - PADDING, textAlign: 'right' }
520
- : { x: max + handlerWidth / 2 + PADDING, textAlign: 'left' };
551
+ // 两端文字存在安全间距为4px, 如果小于该距离则两端均不显示文字
552
+ const gap = this.cfg.width - handlerWidth * 2 - TEXT_PADDING * 2 - minTextWidth - maxTextWidth;
553
+ if (gap <= TEXT_SAFE_WIDTH) {
554
+ // text: '' 不显示文字
555
+ const newMinAttrs = { ...minAttrs, text: '' };
556
+ const newMaxAttrs = { ...maxAttrs, text: '' };
557
+ return !sorted ? [newMinAttrs, newMaxAttrs] : [newMaxAttrs, newMinAttrs];
558
+ }
559
+
560
+ // 一侧不滑动(文字仍显示在中间),另一侧滑动的情况下。
561
+ // 检测中间宽度是否能展示当前不滑动文字的文本,如果距离不够,则隐藏不滑动一侧的文字。
562
+ if (minAttrs.textAlign === 'left' || maxAttrs.textAlign === 'right') {
563
+ const foregroundWidth = this.foreground?.get('width');
564
+ const gap = foregroundWidth - handlerWidth - TEXT_PADDING * 2;
565
+ if (minAttrs.textAlign === 'left') {
566
+ if (gap <= minTextWidth) {
567
+ return !sorted ? [{ ...minAttrs, text: '' }, { ...maxAttrs }] : [{ ...maxAttrs }, { ...minAttrs, text: '' }];
568
+ } else {
569
+ return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
570
+ }
571
+ }
572
+ if (maxAttrs.textAlign === 'right') {
573
+ if (gap <= maxTextWidth) {
574
+ return !sorted ? [{ ...minAttrs }, { ...maxAttrs, text: '' }] : [{ ...maxAttrs, text: '' }, { ...minAttrs }];
575
+ } else {
576
+ return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
577
+ }
578
+ }
579
+ }
521
580
 
522
581
  return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
523
582
  }
524
583
 
584
+ private setTextAttrs(
585
+ bbox: BBox,
586
+ textShape: IShape,
587
+ range: [number, number],
588
+ direction: 'min' | 'max'
589
+ ): {
590
+ attrs: {
591
+ textAlign?: string;
592
+ x?: string;
593
+ text?: string;
594
+ };
595
+ textWidth: number;
596
+ } {
597
+ const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
598
+ const gapWidth = handlerWidth / 2 + TEXT_PADDING;
599
+ const [min, max] = range;
600
+ const { width } = bbox;
601
+ const text = textShape.attr('text');
602
+ const font = textShape.attr('font');
603
+ let textWidth = 0;
604
+ let attrs = {};
605
+ // 字体长度>最大长度,换行处理
606
+ if (width > MAX_TEXT_WIDTH) {
607
+ textWidth = MAX_TEXT_WIDTH;
608
+ if (direction === 'min') {
609
+ attrs =
610
+ // 如果 最大单行宽度 > 左侧能塞下的宽度,则放在左侧
611
+ MAX_TEXT_WIDTH > min - gapWidth
612
+ ? {
613
+ x: min + gapWidth,
614
+ textAlign: 'left',
615
+ text: clipTextTwoLines(text, MAX_TEXT_WIDTH, font),
616
+ }
617
+ : {
618
+ x: min - gapWidth,
619
+ textAlign: 'right',
620
+ text: clipTextTwoLines(text, MAX_TEXT_WIDTH, font),
621
+ };
622
+ } else {
623
+ attrs =
624
+ MAX_TEXT_WIDTH > this.cfg.width - max - gapWidth
625
+ ? {
626
+ x: max - gapWidth,
627
+ textAlign: 'right',
628
+ text: clipTextTwoLines(text, MAX_TEXT_WIDTH, font),
629
+ }
630
+ : {
631
+ x: max + gapWidth,
632
+ textAlign: 'left',
633
+ text: clipTextTwoLines(text, MAX_TEXT_WIDTH, font),
634
+ };
635
+ }
636
+ } else {
637
+ textWidth = width;
638
+ if (direction === 'min') {
639
+ attrs =
640
+ width > min - gapWidth
641
+ ? {
642
+ x: min + gapWidth,
643
+ textAlign: 'left',
644
+ }
645
+ : {
646
+ x: min - gapWidth,
647
+ textAlign: 'right',
648
+ };
649
+ } else {
650
+ attrs =
651
+ width > this.cfg.width - max - gapWidth
652
+ ? {
653
+ x: max - gapWidth,
654
+ textAlign: 'right',
655
+ }
656
+ : {
657
+ x: max + gapWidth,
658
+ textAlign: 'left',
659
+ };
660
+ }
661
+ }
662
+
663
+ return {
664
+ attrs,
665
+ textWidth,
666
+ };
667
+ }
668
+
525
669
  public draw() {
526
670
  const container = this.get('container');
527
671
  const canvas = container && container.get('canvas');
@@ -536,6 +680,77 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
536
680
 
537
681
  return canvas && canvas.get('container');
538
682
  }
683
+
684
+ private onBrushMouseMove = (event: MouseEvent) => {
685
+ if (!this.enableBrush) return;
686
+
687
+ const { x } = this.getBBox();
688
+ const curX = event.offsetX;
689
+ // 绘制 brush mask
690
+ if (!isNumber(this.brushPrevX)) {
691
+ this.brushPrevX = curX;
692
+ }
693
+ const maskAttrs = getRectMaskAttrs(
694
+ {
695
+ x: this.brushPrevX - x,
696
+ y: 0,
697
+ },
698
+ {
699
+ x: curX - x,
700
+ y: this.cfg.height,
701
+ }
702
+ );
703
+
704
+ if (!this.brushMaskShape) {
705
+ // 新建 brush mask
706
+ this.brushMaskShape = createMask(this.get('group'), 'rect', maskAttrs);
707
+ } else {
708
+ // 更新 brush mask
709
+ updateMask(this.brushMaskShape, maskAttrs);
710
+ }
711
+ };
712
+
713
+ private onBrushMouseUp = (event: MouseEvent) => {
714
+ if (!this.enableBrush) return;
715
+
716
+ // 没有拖动偏移量时,取消框选操作
717
+ const curX = event.offsetX;
718
+ if (this.brushPrevX === curX) {
719
+ this.cancelBrush();
720
+ return;
721
+ }
722
+
723
+ const { x } = this.getBBox();
724
+ const { width } = this.cfg;
725
+ const start = (Math.min(curX, this.brushPrevX) - x) / width;
726
+ const end = (Math.max(curX, this.brushPrevX) - x) / width;
727
+ const originValue = [this.get('start'), this.get('end')];
728
+
729
+ // 更新 start end
730
+ this.set('start', start);
731
+ this.set('end', end);
732
+ // 更新 ui
733
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
734
+ this.draw();
735
+ this.emit(SLIDER_CHANGE, [this.get('start'), this.get('end')].sort());
736
+ this.delegateEmit('valuechanged', {
737
+ originValue,
738
+ value: [this.get('start'), this.get('end')],
739
+ });
740
+
741
+ // 销毁当前 brush mask 图形
742
+ this.cancelBrush();
743
+ };
744
+
745
+ private cancelBrush() {
746
+ // 销毁当前 brush mask 图形
747
+ if (this.brushMaskShape) {
748
+ this.brushMaskShape.remove();
749
+ this.brushMaskShape = null;
750
+ }
751
+ this.enableBrush = false;
752
+ this.brushMaskShape = null;
753
+ }
539
754
  }
540
755
 
541
756
  export default Slider;
@@ -1,5 +1,4 @@
1
1
  export const CONTAINER_CLASS = 'g2-tooltip';
2
- export const CONTAINER_CLASS_CUSTOM = 'g2-tooltip-custom';
3
2
  export const TITLE_CLASS = 'g2-tooltip-title';
4
3
  export const LIST_CLASS = 'g2-tooltip-list';
5
4
  export const LIST_ITEM_CLASS = 'g2-tooltip-list-item';
@@ -24,14 +24,6 @@ export default {
24
24
  lineHeight: '20px',
25
25
  padding: '10px 10px 6px 10px',
26
26
  },
27
- [`${CssConst.CONTAINER_CLASS_CUSTOM}`]: {
28
- position: 'absolute',
29
- zIndex: 8,
30
- transition:
31
- 'visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1), ' +
32
- 'left 0.4s cubic-bezier(0.23, 1, 0.32, 1), ' +
33
- 'top 0.4s cubic-bezier(0.23, 1, 0.32, 1)',
34
- },
35
27
  [`${CssConst.TITLE_CLASS}`]: {
36
28
  marginBottom: '4px',
37
29
  },
@@ -10,8 +10,6 @@ import TooltipTheme from './html-theme';
10
10
 
11
11
  import { ILocation } from '../interfaces';
12
12
  import { getAlignPoint } from '../util/align';
13
- import { hasClass } from '../util/util';
14
- import { CONTAINER_CLASS, CONTAINER_CLASS_CUSTOM } from './css-const';
15
13
 
16
14
  function hasOneKey(obj, keys) {
17
15
  let result = false;
@@ -133,13 +131,6 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
133
131
  });
134
132
  }
135
133
 
136
- private setCustomContainer(): void {
137
- const customContainer = document.createElement('div');
138
- customContainer.className = CONTAINER_CLASS_CUSTOM;
139
- this.set('container', customContainer);
140
- this.set('containerClassName', CONTAINER_CLASS_CUSTOM);
141
- }
142
-
143
134
  // 如有 customContent 则根据 customContent 设置 container
144
135
  protected initContainer() {
145
136
  super.initContainer();
@@ -147,11 +138,9 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
147
138
  if (this.get('container')) {
148
139
  this.get('container').remove();
149
140
  }
150
- this.setCustomContainer();
151
- const newContainer = this.getHtmlContentNode();
152
- const customContainer: HTMLElement = this.get('container');
153
- customContainer.appendChild(newContainer);
154
- this.get('parent').appendChild(customContainer);
141
+ const container = this.getHtmlContentNode();
142
+ this.get('parent').appendChild(container);
143
+ this.set('container', container);
155
144
  this.resetStyles();
156
145
  this.applyStyles();
157
146
  }
@@ -208,22 +197,15 @@ class Tooltip<T extends TooltipCfg = TooltipCfg> extends HtmlComponent implement
208
197
 
209
198
  // 根据 customContent 渲染
210
199
  private renderCustomContent() {
211
- const newContainer = this.getHtmlContentNode();
212
- let oldContainer: HTMLElement = this.get('container');
213
- // 如果一个图表的 tooltip 一开始是默认的, 而后手动设置了 customContent 方法, 则会导致 oldContainer 是 'g2-tooltip', 但其实应该 'g2-tooltip-custom'
214
- let existG2TooltipClass = hasClass(oldContainer, CONTAINER_CLASS);
215
- if (existG2TooltipClass) {
216
- this.setCustomContainer();
217
- oldContainer = this.get('container');
218
- const parent: HTMLElement = this.get('parent');
219
- const target = parent.querySelector(`.${CONTAINER_CLASS}`);
220
- parent.removeChild(target);
221
- }
222
- oldContainer.innerHTML = '';
223
- oldContainer.appendChild(newContainer);
224
- if (existG2TooltipClass) {
225
- this.get('parent').appendChild(oldContainer);
200
+ const node = this.getHtmlContentNode();
201
+ const parent: HTMLElement = this.get('parent');
202
+ const curContainer: HTMLElement = this.get('container');
203
+ if (curContainer && curContainer.parentNode === parent) {
204
+ parent.replaceChild(node, curContainer);
205
+ } else {
206
+ parent.appendChild(node);
226
207
  }
208
+ this.set('container', node);
227
209
  this.resetStyles();
228
210
  this.applyStyles();
229
211
  }
package/src/util/event.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { IGroup } from '@antv/g-base';
2
2
  import { Event as GraphEvent } from '@antv/g-base';
3
+ import { get } from '@antv/util';
3
4
  import { LooseObject } from '../types';
4
5
 
5
6
  /**
@@ -22,3 +23,14 @@ export function propagationDelegate(group: IGroup, eventName: string, eventObjec
22
23
  parent = parent.getParent() as IGroup;
23
24
  }
24
25
  }
26
+
27
+ /**
28
+ *
29
+ * @param e 事件对象
30
+ */
31
+ export function getCurrentPoint(event: MouseEvent) {
32
+ return {
33
+ x: get(event, 'touches.0.pageX', event.pageX),
34
+ y: get(event, 'touches.0.pageY', event.pageY),
35
+ };
36
+ }
@@ -0,0 +1,32 @@
1
+ import { IGroup, IShape, LooseObject, Point } from '@antv/g-base';
2
+
3
+ export function createMask(group: IGroup, maskType: string, maskAttrs: LooseObject) {
4
+ const maskShape = group.addShape({
5
+ type: maskType,
6
+ name: 'component-mask',
7
+ attrs: {
8
+ fill: '#569CFF',
9
+ opacity: 0.16,
10
+ ...maskAttrs,
11
+ },
12
+ });
13
+ return maskShape;
14
+ }
15
+
16
+ export function updateMask(shape: IShape, maskAttrs: LooseObject) {
17
+ shape.attr(maskAttrs);
18
+ return shape;
19
+ }
20
+
21
+ export function getRectMaskAttrs(start: Point, end: Point) {
22
+ const x = Math.min(start.x, end.x);
23
+ const y = Math.min(start.y, end.y);
24
+ const width = Math.abs(end.x - start.x);
25
+ const height = Math.abs(end.y - start.y);
26
+ return {
27
+ x,
28
+ y,
29
+ width,
30
+ height,
31
+ };
32
+ }