@antv/component 0.8.32-beta.0 → 0.9.1

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 +44 -1
  6. package/esm/slider/constant.js +48 -4
  7. package/esm/slider/constant.js.map +1 -1
  8. package/esm/slider/foreground.d.ts +71 -0
  9. package/esm/slider/foreground.js +104 -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 +16 -4
  15. package/esm/slider/slider.js +218 -40
  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 +9 -1
  27. package/esm/util/event.js +12 -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 +34 -0
  31. package/esm/util/mask.js.map +1 -0
  32. package/esm/util/util.d.ts +7 -0
  33. package/esm/util/util.js +59 -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 +44 -1
  40. package/lib/slider/constant.js +49 -5
  41. package/lib/slider/constant.js.map +1 -1
  42. package/lib/slider/foreground.d.ts +71 -0
  43. package/lib/slider/foreground.js +107 -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 +16 -4
  49. package/lib/slider/slider.js +215 -37
  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 +9 -1
  61. package/lib/util/event.js +14 -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 +40 -0
  65. package/lib/util/mask.js.map +1 -0
  66. package/lib/util/util.d.ts +7 -0
  67. package/lib/util/util.js +61 -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 +55 -4
  73. package/src/slider/foreground.ts +165 -0
  74. package/src/slider/handler.ts +10 -20
  75. package/src/slider/slider.ts +249 -55
  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 +14 -1
  80. package/src/util/mask.ts +41 -0
  81. package/src/util/util.ts +64 -2
@@ -0,0 +1,165 @@
1
+ import { IGroup, LooseObject } from '@antv/g-base';
2
+ import GroupComponent from '../abstract/group-component';
3
+ import { GroupComponentCfg } from '../types';
4
+ import {
5
+ FOREGROUND_STYLE,
6
+ SLICER_BAR_LINE_STYLE,
7
+ SLIDER_BAR_HEIGHT,
8
+ SLIDER_BAR_LINE_GAP,
9
+ SLIDER_BAR_STYLE,
10
+ } from './constant';
11
+
12
+ interface IStyle {
13
+ fill?: string;
14
+ stroke?: string;
15
+ radius?: number;
16
+ opacity?: number;
17
+ cursor?: string;
18
+ highLightFill?: string;
19
+ }
20
+
21
+ export interface ForegroundCfg extends GroupComponentCfg {
22
+ // position size
23
+ readonly x: number;
24
+ readonly y: number;
25
+ readonly width: number;
26
+ readonly height: number;
27
+ // style
28
+ readonly foregroundStyle?: IStyle;
29
+ }
30
+
31
+ export class Foreground extends GroupComponent<ForegroundCfg> {
32
+ public getDefaultCfg() {
33
+ const cfg = super.getDefaultCfg();
34
+ return {
35
+ ...cfg,
36
+ name: 'foreground',
37
+ // x: 0,
38
+ // y: 0,
39
+ width: 100,
40
+ height: 16,
41
+ style: FOREGROUND_STYLE,
42
+ barStyle: SLIDER_BAR_STYLE,
43
+ };
44
+ }
45
+
46
+ protected renderInner(group: IGroup) {
47
+ const { x, height, width, style, barStyle } = this.cfg;
48
+
49
+ // 上方brush区域
50
+ this.addShape(group, {
51
+ id: this.getElementId('foreground-brush'),
52
+ name: 'foreground-brush',
53
+ type: 'rect',
54
+ attrs: {
55
+ x,
56
+ y: 0,
57
+ width,
58
+ height: (height * 3) / 5,
59
+ cursor: 'crosshair',
60
+ ...style,
61
+ },
62
+ });
63
+
64
+ // 下方移动区域
65
+ this.addShape(group, {
66
+ id: this.getElementId('foreground-scroll'),
67
+ name: 'foreground-scroll',
68
+ type: 'rect',
69
+ attrs: {
70
+ x,
71
+ y: (height * 3) / 5,
72
+ width,
73
+ height: (height * 2) / 5,
74
+ cursor: 'move',
75
+ ...style,
76
+ },
77
+ });
78
+
79
+ // 下方Bar区域
80
+ const barGroup = this.addGroup(group, {
81
+ id: this.getElementId('foreground-bar'),
82
+ name: 'foreground-bar',
83
+ });
84
+ // 外层矩形
85
+ this.addShape(barGroup, {
86
+ id: this.getElementId('foreground-bar-rect'),
87
+ name: 'foreground-bar-rect',
88
+ type: 'rect',
89
+ attrs: {
90
+ x,
91
+ y: height,
92
+ width,
93
+ height: SLIDER_BAR_HEIGHT,
94
+ cursor: 'move',
95
+ ...barStyle,
96
+ },
97
+ });
98
+ // 三条小竖线
99
+ const centerX = width / 2 + x;
100
+ const leftX = centerX - SLIDER_BAR_LINE_GAP;
101
+ const rightX = centerX + SLIDER_BAR_LINE_GAP;
102
+ const y1 = height + (1 / 5) * 6;
103
+ const y2 = height + (4 / 5) * 6;
104
+ const publicAttrs = {
105
+ y1,
106
+ y2,
107
+ ...SLICER_BAR_LINE_STYLE,
108
+ };
109
+ [leftX, centerX, rightX].forEach((x) => {
110
+ this.drawBarLine(group, x, publicAttrs);
111
+ });
112
+
113
+ this.addShape(group, {
114
+ id: this.getElementId('foreground-border'),
115
+ name: 'foreground-border',
116
+ type: 'rect',
117
+ attrs: {
118
+ x,
119
+ y: 0,
120
+ width,
121
+ height,
122
+ stroke: '#4E83CD',
123
+ opacity: 0.3,
124
+ lineWidth: 0.8,
125
+ },
126
+ });
127
+ }
128
+
129
+ private drawBarLine(group: IGroup, x: number, attrs: LooseObject) {
130
+ this.addShape(group, {
131
+ id: this.getElementId(`line-${x}`),
132
+ name: `line-${x}`,
133
+ type: 'line',
134
+ attrs: {
135
+ x1: x,
136
+ x2: x,
137
+ ...attrs,
138
+ },
139
+ });
140
+ }
141
+
142
+ protected initEvent() {
143
+ this.bindEvents();
144
+ }
145
+
146
+ private bindEvents() {
147
+ const { highLightFill, fill } = this.get('barStyle');
148
+ const barRectShape = this.get('group').findById(this.getElementId('foreground-bar-rect'));
149
+ this.get('group').on('foreground-bar:mouseenter', () => {
150
+ barRectShape.attr('fill', highLightFill);
151
+ this.draw();
152
+ });
153
+ this.get('group').on('foreground-bar:mouseleave', () => {
154
+ barRectShape.attr('fill', fill);
155
+ this.draw();
156
+ });
157
+ }
158
+
159
+ private draw() {
160
+ const canvas = this.get('container').get('canvas');
161
+ if (canvas) {
162
+ canvas.draw();
163
+ }
164
+ }
165
+ }
@@ -1,6 +1,7 @@
1
1
  import { IGroup } from '@antv/g-base';
2
2
  import GroupComponent from '../abstract/group-component';
3
3
  import { GroupComponentCfg } from '../types';
4
+ import { DEFAULT_HANDLER_HEIGHT, DEFAULT_HANDLER_WIDTH, HANDLER_STYLE } from './constant';
4
5
 
5
6
  interface IStyle {
6
7
  fill?: string;
@@ -21,16 +22,6 @@ export interface HandlerCfg extends GroupComponentCfg {
21
22
  readonly style?: IStyle;
22
23
  }
23
24
 
24
- export const DEFAULT_HANDLER_STYLE = {
25
- fill: '#F7F7F7',
26
- stroke: '#BFBFBF',
27
- radius: 2,
28
- opacity: 1,
29
- cursor: 'ew-resize',
30
- // 高亮的颜色
31
- highLightFill: '#FFF',
32
- };
33
-
34
25
  export class Handler extends GroupComponent<HandlerCfg> {
35
26
  public getDefaultCfg() {
36
27
  const cfg = super.getDefaultCfg();
@@ -39,9 +30,9 @@ export class Handler extends GroupComponent<HandlerCfg> {
39
30
  name: 'handler',
40
31
  x: 0,
41
32
  y: 0,
42
- width: 10,
43
- height: 24,
44
- style: DEFAULT_HANDLER_STYLE,
33
+ width: DEFAULT_HANDLER_WIDTH,
34
+ height: DEFAULT_HANDLER_HEIGHT,
35
+ style: HANDLER_STYLE,
45
36
  };
46
37
  }
47
38
  protected renderInner(group: IGroup) {
@@ -58,7 +49,6 @@ export class Handler extends GroupComponent<HandlerCfg> {
58
49
  width,
59
50
  height,
60
51
  fill,
61
- stroke,
62
52
  radius,
63
53
  opacity,
64
54
  cursor,
@@ -112,16 +102,16 @@ export class Handler extends GroupComponent<HandlerCfg> {
112
102
 
113
103
  private bindEvents() {
114
104
  this.get('group').on('mouseenter', () => {
115
- const { highLightFill } = this.get('style');
116
- this.getElementByLocalId('background').attr('fill', highLightFill);
117
-
105
+ const { highLightStroke } = this.get('style');
106
+ this.getElementByLocalId('line-left').attr('stroke', highLightStroke);
107
+ this.getElementByLocalId('line-right').attr('stroke', highLightStroke);
118
108
  this.draw();
119
109
  });
120
110
 
121
111
  this.get('group').on('mouseleave', () => {
122
- const { fill } = this.get('style');
123
- this.getElementByLocalId('background').attr('fill', fill);
124
-
112
+ const { stroke } = this.get('style');
113
+ this.getElementByLocalId('line-left').attr('stroke', stroke);
114
+ this.getElementByLocalId('line-right').attr('stroke', stroke);
125
115
  this.draw();
126
116
  });
127
117
  }
@@ -1,18 +1,27 @@
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
+ ACTIVE_TREND_STYLE,
9
10
  BACKGROUND_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 { clipText, getTextWidth } from '../util/util';
24
+ import { Foreground } from './foreground';
16
25
 
17
26
  export interface TrendCfg {
18
27
  // 数据
@@ -62,11 +71,19 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
62
71
 
63
72
  private minHandler: Handler;
64
73
  private maxHandler: Handler;
74
+ private foreground: Foreground;
65
75
  private trend: Trend;
66
76
  private currentTarget: string;
67
77
  private prevX: number;
68
78
  private prevY: number;
69
79
 
80
+ // Brush 起始点坐标记录
81
+ private brushPrevX: number = null;
82
+ // 标识开始和结束 brush 操作
83
+ private enableBrush: boolean = false;
84
+ // Brush 框样式Shape
85
+ private brushMaskShape: IShape = null;
86
+
70
87
  public setRange(min: number, max: number) {
71
88
  this.set('minLimit', min);
72
89
  this.set('maxLimit', max);
@@ -140,6 +157,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
140
157
  validCfg.end = clamp(end, 0, 1);
141
158
  }
142
159
  super.update(validCfg);
160
+ this.foreground = this.getChildComponentById(this.getElementId('foreground'));
143
161
  this.minHandler = this.getChildComponentById(this.getElementId('minHandler'));
144
162
  this.maxHandler = this.getChildComponentById(this.getElementId('maxHandler'));
145
163
  this.trend = this.getChildComponentById(this.getElementId('trend'));
@@ -154,11 +172,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
154
172
  public render() {
155
173
  super.render();
156
174
 
157
- this.updateUI(
158
- this.getElementByLocalId('foreground'),
159
- this.getElementByLocalId('minText'),
160
- this.getElementByLocalId('maxText')
161
- );
175
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
162
176
  }
163
177
 
164
178
  protected renderInner(group: IGroup) {
@@ -175,12 +189,13 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
175
189
  textStyle = {},
176
190
  } = this.cfg;
177
191
 
178
- const handlerStyle = deepMix({}, DEFAULT_HANDLER_STYLE, this.cfg.handlerStyle);
192
+ const handlerStyle = deepMix({}, HANDLER_STYLE, this.cfg.handlerStyle);
179
193
 
180
194
  const min = start * width;
181
195
  const max = end * width;
182
196
 
183
- // 趋势图数据
197
+ // default 趋势图数据
198
+ const defaultTrendStyle = deepMix({}, DEFAULT_TREND_STYLE, trendCfg);
184
199
  if (size(get(trendCfg, 'data'))) {
185
200
  this.trend = this.addComponent(group, {
186
201
  component: Trend,
@@ -189,7 +204,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
189
204
  y: 0,
190
205
  width,
191
206
  height,
192
- ...trendCfg,
207
+ ...defaultTrendStyle,
193
208
  });
194
209
  }
195
210
 
@@ -202,11 +217,21 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
202
217
  y: 0,
203
218
  width,
204
219
  height,
220
+ cursor: 'crosshair',
205
221
  ...backgroundStyle,
206
222
  },
207
223
  });
208
224
 
209
- // 2. 左右文字
225
+ // 2. 前景 选中背景框
226
+ this.foreground = this.addComponent(group, {
227
+ component: Foreground,
228
+ id: this.getElementId('foreground'),
229
+ name: 'foreground',
230
+ height,
231
+ style: foregroundStyle,
232
+ });
233
+
234
+ // 3. 左右文字
210
235
  const minTextShape = this.addShape(group, {
211
236
  id: this.getElementId('minText'),
212
237
  type: 'text',
@@ -219,7 +244,6 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
219
244
  ...textStyle,
220
245
  },
221
246
  });
222
-
223
247
  const maxTextShape = this.addShape(group, {
224
248
  id: this.getElementId('maxText'),
225
249
  type: 'text',
@@ -233,23 +257,9 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
233
257
  },
234
258
  });
235
259
 
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
260
  // 滑块相关的大小信息
251
261
  const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
252
- const handlerHeight = get(handlerStyle, 'height', 24);
262
+ const handlerHeight = get(handlerStyle, 'height', DEFAULT_HANDLER_HEIGHT);
253
263
 
254
264
  // 4. 左右滑块
255
265
  this.minHandler = this.addComponent(group, {
@@ -288,7 +298,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
288
298
  this.bindEvents();
289
299
  }
290
300
 
291
- private updateUI(foregroundShape: IShape, minTextShape: IShape, maxTextShape: IShape) {
301
+ private updateUI(minTextShape: IShape, maxTextShape: IShape) {
292
302
  const { start, end, width, minText, maxText, handlerStyle, height } = this.cfg as SliderCfg;
293
303
  const min = start * width;
294
304
  const max = end * width;
@@ -303,9 +313,15 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
303
313
  }
304
314
  }
305
315
 
306
- // 1. foreground
307
- foregroundShape.attr('x', min);
308
- foregroundShape.attr('width', max - min);
316
+ if (this.foreground) {
317
+ this.foreground.update({
318
+ x: min,
319
+ width: max - min,
320
+ });
321
+ if (!this.get('updateAutoRender')) {
322
+ this.foreground.render();
323
+ }
324
+ }
309
325
 
310
326
  // 滑块相关的大小信息
311
327
  const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
@@ -348,10 +364,16 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
348
364
  group.on('handler-max:mousedown', this.onMouseDown('maxHandler'));
349
365
  group.on('handler-max:touchstart', this.onMouseDown('maxHandler'));
350
366
 
351
- // 3. 前景选中区域
352
- const foreground = group.findById(this.getElementId('foreground'));
353
- foreground.on('mousedown', this.onMouseDown('foreground'));
354
- foreground.on('touchstart', this.onMouseDown('foreground'));
367
+ // 3.背景区域框选
368
+ const background = group.findById(this.getElementId('background'));
369
+ background.on('mousedown', this.onMouseDown('background'));
370
+
371
+ // 4. 前景选中区域
372
+ // 选中区域拖拽
373
+ group.on('foreground-scroll:mousedown', this.onMouseDown('foreground'));
374
+ group.on('foreground-bar:mousedown', this.onMouseDown('foreground'));
375
+ // 选中区域框选
376
+ group.on('foreground-brush:mousedown', this.onMouseDown('foreground-brush'));
355
377
  }
356
378
 
357
379
  private onMouseDown = (target: string) => (e: Event) => {
@@ -367,9 +389,20 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
367
389
  this.prevX = get(event, 'touches.0.pageX', event.pageX);
368
390
  this.prevY = get(event, 'touches.0.pageY', event.pageY);
369
391
 
370
- // 3. 开始滑动的时候,绑定 move 和 up 事件
371
392
  const containerDOM = this.getContainerDOM();
372
393
 
394
+ // 框选事件
395
+ if (['background', 'foreground-brush'].includes(target)) {
396
+ this.brushPrevX = event.offsetX;
397
+ this.enableBrush = true;
398
+ // 绑定框选事件
399
+ containerDOM.addEventListener('mousemove', this.onBrushMouseMove);
400
+ containerDOM.addEventListener('mouseup', this.onBrushMouseUp);
401
+ containerDOM.addEventListener('mouseleave', this.onBrushMouseUp);
402
+ return;
403
+ }
404
+
405
+ // 3. 开始滑动的时候,绑定 move 和 up 事件
373
406
  containerDOM.addEventListener('mousemove', this.onMouseMove);
374
407
  containerDOM.addEventListener('mouseup', this.onMouseUp);
375
408
  containerDOM.addEventListener('mouseleave', this.onMouseUp);
@@ -398,11 +431,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
398
431
  // 更新 start end range 范围
399
432
  this.updateStartEnd(offsetXRange);
400
433
  // 更新 ui
401
- this.updateUI(
402
- this.getElementByLocalId('foreground'),
403
- this.getElementByLocalId('minText'),
404
- this.getElementByLocalId('maxText')
405
- );
434
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
406
435
 
407
436
  this.prevX = x;
408
437
  this.prevY = y;
@@ -457,6 +486,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
457
486
 
458
487
  return Math.min(max, Math.max(min, offsetRange));
459
488
  }
489
+ case 'foreground-brush':
460
490
  case 'foreground': {
461
491
  const min = 0 - start;
462
492
  const max = 1 - end;
@@ -476,7 +506,9 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
476
506
  case 'maxHandler':
477
507
  end += offsetRange;
478
508
  break;
509
+
479
510
  case 'foreground':
511
+ case 'foreground-brush':
480
512
  start += offsetRange;
481
513
  end += offsetRange;
482
514
  break;
@@ -490,11 +522,8 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
490
522
  * 根据位置,调整返回新的位置
491
523
  * @param range
492
524
  */
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
-
525
+ private _dodgeText(range: [number, number], minTextShape: IShape, maxTextShape: IShape): [object, object] {
526
+ const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
498
527
  let [min, max] = range;
499
528
  let sorted = false;
500
529
 
@@ -509,19 +538,113 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
509
538
  const minBBox = minTextShape.getBBox();
510
539
  const maxBBox = maxTextShape.getBBox();
511
540
 
512
- const minAttrs =
513
- minBBox.width > min - PADDING
514
- ? { x: min + handlerWidth / 2 + PADDING, textAlign: 'left' }
515
- : { x: min - handlerWidth / 2 - PADDING, textAlign: 'right' };
541
+ const { attrs: minAttrs, textWidth: minTextWidth } = this.setTextAttrs(minBBox, minTextShape, [min, max], 'min');
542
+ const { attrs: maxAttrs, textWidth: maxTextWidth } = this.setTextAttrs(maxBBox, maxTextShape, [min, max], 'max');
516
543
 
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' };
544
+ // 两端文字存在安全间距为4px, 如果小于该距离则两端均不显示文字
545
+ const gap = this.cfg.width - handlerWidth * 2 - TEXT_PADDING * 2 - minTextWidth - maxTextWidth;
546
+ if (gap < TEXT_SAFE_WIDTH) {
547
+ const newMinAttrs = { ...minAttrs, text: '' };
548
+ const newMaxAttrs = { ...maxAttrs, text: '' };
549
+ return !sorted ? [newMinAttrs, newMaxAttrs] : [newMaxAttrs, newMinAttrs];
550
+ }
551
+
552
+ // 一侧不滑动(文字仍显示在中间),另一侧滑动的情况下。
553
+ // 检测中间宽度是否能展示当前不滑动文字的文本,如果距离不够,则隐藏不滑动一侧的文字。
554
+ if (minAttrs.textAlign === 'left' || maxAttrs.textAlign === 'right') {
555
+ if (minAttrs.textAlign === 'left') {
556
+ const foregroundWidth = this.foreground?.get('width');
557
+ const gap = foregroundWidth - TEXT_PADDING;
558
+ if (gap < minTextWidth) {
559
+ return !sorted ? [{ ...minAttrs, text: '' }, { ...maxAttrs }] : [{ ...maxAttrs }, { ...minAttrs, text: '' }];
560
+ } else {
561
+ return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
562
+ }
563
+ }
564
+ if (maxAttrs.textAlign === 'right') {
565
+ const foregroundWidth = this.foreground?.get('width');
566
+ const gap = foregroundWidth - TEXT_PADDING;
567
+ if (gap < maxTextWidth) {
568
+ return !sorted ? [{ ...minAttrs }, { ...maxAttrs, text: '' }] : [{ ...maxAttrs, text: '' }, { ...minAttrs }];
569
+ } else {
570
+ return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
571
+ }
572
+ }
573
+ }
521
574
 
522
575
  return !sorted ? [minAttrs, maxAttrs] : [maxAttrs, minAttrs];
523
576
  }
524
577
 
578
+ private setTextAttrs(
579
+ bbox: BBox,
580
+ textShape: IShape,
581
+ range: [number, number],
582
+ direction: 'min' | 'max'
583
+ ): {
584
+ attrs: {
585
+ textAlign?: string;
586
+ x?: string;
587
+ text?: string;
588
+ };
589
+ textWidth: number;
590
+ } {
591
+ const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
592
+ const [min, max] = range;
593
+ const { width } = bbox;
594
+ const text = textShape.attr('text');
595
+ const font = textShape.attr('font');
596
+ let textWidth = 0;
597
+ let attrs = {};
598
+ if (width > MAX_TEXT_WIDTH) {
599
+ textWidth = MAX_TEXT_WIDTH;
600
+ if (direction === 'min') {
601
+ attrs =
602
+ width / 2 > min - TEXT_PADDING
603
+ ? {
604
+ x: min + handlerWidth / 2 + TEXT_PADDING,
605
+ textAlign: 'left',
606
+ text: clipText(text, MAX_TEXT_WIDTH, font),
607
+ }
608
+ : {
609
+ x: min - handlerWidth / 2 - TEXT_PADDING,
610
+ textAlign: 'right',
611
+ text: clipText(text, MAX_TEXT_WIDTH, font),
612
+ };
613
+ } else {
614
+ attrs =
615
+ width / 2 > this.cfg.width - max - TEXT_PADDING
616
+ ? {
617
+ x: max - handlerWidth / 2 + TEXT_PADDING,
618
+ textAlign: 'right',
619
+ text: clipText(text, MAX_TEXT_WIDTH, font),
620
+ }
621
+ : {
622
+ x: max + handlerWidth / 2 - TEXT_PADDING,
623
+ textAlign: 'left',
624
+ text: clipText(text, MAX_TEXT_WIDTH, font),
625
+ };
626
+ }
627
+ } else {
628
+ textWidth = getTextWidth(text, font);
629
+ if (direction === 'min') {
630
+ attrs =
631
+ width > min - TEXT_PADDING
632
+ ? { x: min + handlerWidth / 2 + TEXT_PADDING, textAlign: 'left' }
633
+ : { x: min - handlerWidth / 2 - TEXT_PADDING, textAlign: 'right' };
634
+ } else {
635
+ attrs =
636
+ width > this.cfg.width - max - TEXT_PADDING
637
+ ? { x: max - handlerWidth / 2 - TEXT_PADDING, textAlign: 'right' }
638
+ : { x: max + handlerWidth / 2 + TEXT_PADDING, textAlign: 'left' };
639
+ }
640
+ }
641
+
642
+ return {
643
+ attrs,
644
+ textWidth,
645
+ };
646
+ }
647
+
525
648
  public draw() {
526
649
  const container = this.get('container');
527
650
  const canvas = container && container.get('canvas');
@@ -536,6 +659,77 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
536
659
 
537
660
  return canvas && canvas.get('container');
538
661
  }
662
+
663
+ private onBrushMouseMove = (event: MouseEvent) => {
664
+ if (!this.enableBrush) return;
665
+
666
+ const { x } = this.getBBox();
667
+ const curX = get(event, 'touches.0.pageX', event.offsetX);
668
+ // 绘制 brush mask
669
+ if (!isNumber(this.brushPrevX)) {
670
+ this.brushPrevX = curX;
671
+ }
672
+ const maskAttrs = getRectMaskAttrs(
673
+ {
674
+ x: this.brushPrevX - x,
675
+ y: 0,
676
+ },
677
+ {
678
+ x: curX - x,
679
+ y: this.cfg.height,
680
+ }
681
+ );
682
+
683
+ if (!this.brushMaskShape) {
684
+ // 新建 brush mask
685
+ this.brushMaskShape = createMask(this.get('group'), 'rect', maskAttrs);
686
+ } else {
687
+ // 更新 brush mask
688
+ updateMask(this.brushMaskShape, maskAttrs);
689
+ }
690
+ };
691
+
692
+ private onBrushMouseUp = (event: MouseEvent) => {
693
+ if (!this.enableBrush) return;
694
+
695
+ // 没有拖动偏移量时,取消框选操作
696
+ const curX = get(event, 'touches.0.pageX', event.offsetX);
697
+ if (this.brushPrevX === curX) {
698
+ this.cancelBrush();
699
+ return;
700
+ }
701
+
702
+ const { x } = this.getBBox();
703
+ const { width } = this.cfg;
704
+ const start = (Math.min(curX, this.brushPrevX) - x) / width;
705
+ const end = (Math.max(curX, this.brushPrevX) - x) / width;
706
+ const originValue = [this.get('start'), this.get('end')];
707
+
708
+ // 更新 start end
709
+ this.set('start', start);
710
+ this.set('end', end);
711
+ // 更新 ui
712
+ this.updateUI(this.getElementByLocalId('minText'), this.getElementByLocalId('maxText'));
713
+ this.draw();
714
+ this.emit(SLIDER_CHANGE, [this.get('start'), this.get('end')].sort());
715
+ this.delegateEmit('valuechanged', {
716
+ originValue,
717
+ value: [this.get('start'), this.get('end')],
718
+ });
719
+
720
+ // 销毁当前 brush mask 图形
721
+ this.cancelBrush();
722
+ };
723
+
724
+ private cancelBrush() {
725
+ // 销毁当前 brush mask 图形
726
+ if (this.brushMaskShape) {
727
+ this.brushMaskShape.remove();
728
+ this.brushMaskShape = null;
729
+ }
730
+ this.enableBrush = false;
731
+ this.brushMaskShape = null;
732
+ }
539
733
  }
540
734
 
541
735
  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';