@antv/component 0.9.2 → 0.9.4

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.
@@ -11,6 +11,8 @@ interface IStyle {
11
11
  opacity?: number;
12
12
  cursor?: string;
13
13
  highLightFill?: string;
14
+ /** 框选占比 */
15
+ brushRatio?: number;
14
16
  }
15
17
 
16
18
  export interface ForegroundCfg extends GroupComponentCfg {
@@ -38,38 +40,39 @@ export class Foreground extends GroupComponent<ForegroundCfg> {
38
40
 
39
41
  protected renderInner(group: IGroup) {
40
42
  const { x, height, width, foregroundStyle, barStyle } = this.cfg;
43
+ const { brushRatio } = foregroundStyle;
41
44
 
42
- // 上方brush区域
45
+ // 上方移动区域
43
46
  this.addShape(group, {
44
- id: this.getElementId('foreground-brush'),
45
- name: 'foreground-brush',
47
+ id: this.getElementId('foreground-scroll'),
48
+ name: 'foreground-scroll',
46
49
  type: 'rect',
47
50
  attrs: {
48
51
  x,
49
52
  y: 0,
50
53
  width,
51
- height: (height * 3) / 5,
52
- cursor: 'crosshair',
54
+ height: height * (1 - brushRatio),
55
+ cursor: 'move',
53
56
  ...omit(foregroundStyle, ['stroke']),
54
57
  },
55
58
  });
56
59
 
57
- // 下方移动区域
60
+ // 下方brush区域
58
61
  this.addShape(group, {
59
- id: this.getElementId('foreground-scroll'),
60
- name: 'foreground-scroll',
62
+ id: this.getElementId('foreground-brush'),
63
+ name: 'foreground-brush',
61
64
  type: 'rect',
62
65
  attrs: {
63
66
  x,
64
- y: (height * 3) / 5,
67
+ y: height * (1 - brushRatio),
65
68
  width,
66
- height: (height * 2) / 5,
67
- cursor: 'move',
69
+ height: height * brushRatio,
70
+ cursor: 'crosshair',
68
71
  ...omit(foregroundStyle, ['stroke']),
69
72
  },
70
73
  });
71
74
 
72
- // 下方Bar区域
75
+ // 上方Bar区域
73
76
  const barGroup = this.addGroup(group, {
74
77
  id: this.getElementId('foreground-bar'),
75
78
  name: 'foreground-bar',
@@ -81,19 +84,19 @@ export class Foreground extends GroupComponent<ForegroundCfg> {
81
84
  type: 'rect',
82
85
  attrs: {
83
86
  x,
84
- y: height,
87
+ y: -barStyle.height,
85
88
  width,
86
89
  height: barStyle.height,
87
90
  cursor: 'move',
88
- ...barStyle,
91
+ ...omit(barStyle, ['stroke']),
89
92
  },
90
93
  });
91
94
  // 三条小竖线
92
95
  const centerX = width / 2 + x;
93
96
  const leftX = centerX - SLIDER_BAR_LINE_GAP;
94
97
  const rightX = centerX + SLIDER_BAR_LINE_GAP;
95
- const y1 = height + (1 / 4) * barStyle.height;
96
- const y2 = height + (3 / 4) * barStyle.height;
98
+ const y1 = -(1 / 4) * barStyle.height;
99
+ const y2 = -(3 / 4) * barStyle.height;
97
100
  const publicAttrs = {
98
101
  y1,
99
102
  y2,
@@ -103,6 +106,7 @@ export class Foreground extends GroupComponent<ForegroundCfg> {
103
106
  this.drawBarLine(group, x, publicAttrs);
104
107
  });
105
108
 
109
+ // 背景边框
106
110
  this.addShape(group, {
107
111
  id: this.getElementId('foreground-border'),
108
112
  name: 'foreground-border',
@@ -122,7 +126,7 @@ export class Foreground extends GroupComponent<ForegroundCfg> {
122
126
  private drawBarLine(group: IGroup, x: number, attrs: LooseObject) {
123
127
  this.addShape(group, {
124
128
  id: this.getElementId(`line-${x}`),
125
- name: `line-${x}`,
129
+ name: `foreground-line`,
126
130
  type: 'line',
127
131
  attrs: {
128
132
  x1: x,
@@ -143,10 +147,18 @@ export class Foreground extends GroupComponent<ForegroundCfg> {
143
147
  barRectShape.attr('fill', highLightFill);
144
148
  this.draw();
145
149
  });
150
+ this.get('group').on('foreground-scroll:mouseenter', () => {
151
+ barRectShape.attr('fill', highLightFill);
152
+ this.draw();
153
+ });
146
154
  this.get('group').on('foreground-bar:mouseleave', () => {
147
155
  barRectShape.attr('fill', fill);
148
156
  this.draw();
149
157
  });
158
+ this.get('group').on('foreground-scroll:mouseleave', () => {
159
+ barRectShape.attr('fill', fill);
160
+ this.draw();
161
+ });
150
162
  }
151
163
 
152
164
  private draw() {
@@ -1,4 +1,5 @@
1
1
  import { IGroup } from '@antv/g-base';
2
+ import { omit } from '@antv/util';
2
3
  import GroupComponent from '../abstract/group-component';
3
4
  import { GroupComponentCfg } from '../types';
4
5
  import { DEFAULT_HANDLER_HEIGHT, DEFAULT_HANDLER_WIDTH, HANDLER_STYLE } from './constant';
@@ -10,6 +11,7 @@ interface IStyle {
10
11
  opacity?: number;
11
12
  cursor?: string;
12
13
  highLightFill?: string;
14
+ highLightStroke?: string;
13
15
  }
14
16
 
15
17
  export interface HandlerCfg extends GroupComponentCfg {
@@ -20,6 +22,7 @@ export interface HandlerCfg extends GroupComponentCfg {
20
22
  readonly height: number;
21
23
  // style
22
24
  readonly style?: IStyle;
25
+ readonly sliderHeight?: number;
23
26
  }
24
27
 
25
28
  export class Handler extends GroupComponent<HandlerCfg> {
@@ -36,10 +39,23 @@ export class Handler extends GroupComponent<HandlerCfg> {
36
39
  };
37
40
  }
38
41
  protected renderInner(group: IGroup) {
39
- const { width, height, style } = this.cfg as HandlerCfg;
40
- const { fill, stroke, radius, opacity, cursor } = style;
42
+ const { width, height, style, sliderHeight } = this.cfg;
43
+ const { stroke, cursor } = style;
41
44
 
42
- // 按钮框框
45
+ // 滑块柄竖线
46
+ this.addShape(group, {
47
+ type: 'line',
48
+ id: this.getElementId('background-line'),
49
+ attrs: {
50
+ x1: (1 / 2) * width,
51
+ y1: -(sliderHeight - height) / 2,
52
+ x2: (1 / 2) * width,
53
+ y2: (height + sliderHeight) / 2,
54
+ stroke,
55
+ },
56
+ });
57
+
58
+ // 滑块柄框框
43
59
  this.addShape(group, {
44
60
  type: 'rect',
45
61
  id: this.getElementId('background'),
@@ -48,10 +64,7 @@ export class Handler extends GroupComponent<HandlerCfg> {
48
64
  y: 0,
49
65
  width,
50
66
  height,
51
- fill,
52
- radius,
53
- opacity,
54
- cursor,
67
+ ...omit(style, ['stroke']),
55
68
  },
56
69
  });
57
70
 
@@ -64,6 +77,7 @@ export class Handler extends GroupComponent<HandlerCfg> {
64
77
 
65
78
  this.addShape(group, {
66
79
  id: this.getElementId('line-left'),
80
+ name: 'handler-line',
67
81
  type: 'line',
68
82
  attrs: {
69
83
  x1,
@@ -77,6 +91,7 @@ export class Handler extends GroupComponent<HandlerCfg> {
77
91
 
78
92
  this.addShape(group, {
79
93
  id: this.getElementId('line-right'),
94
+ name: 'handler-line',
80
95
  type: 'line',
81
96
  attrs: {
82
97
  x1: x2,
@@ -103,6 +118,7 @@ export class Handler extends GroupComponent<HandlerCfg> {
103
118
  private bindEvents() {
104
119
  this.get('group').on('mouseenter', () => {
105
120
  const { highLightStroke } = this.get('style');
121
+ this.getElementByLocalId('background-line').attr('stroke', highLightStroke);
106
122
  this.getElementByLocalId('line-left').attr('stroke', highLightStroke);
107
123
  this.getElementByLocalId('line-right').attr('stroke', highLightStroke);
108
124
  this.draw();
@@ -110,6 +126,7 @@ export class Handler extends GroupComponent<HandlerCfg> {
110
126
 
111
127
  this.get('group').on('mouseleave', () => {
112
128
  const { stroke } = this.get('style');
129
+ this.getElementByLocalId('background-line').attr('stroke', stroke);
113
130
  this.getElementByLocalId('line-left').attr('stroke', stroke);
114
131
  this.getElementByLocalId('line-right').attr('stroke', stroke);
115
132
  this.draw();
@@ -15,12 +15,11 @@ import {
15
15
  HANDLER_STYLE,
16
16
  MAX_TEXT_WIDTH,
17
17
  SLIDER_CHANGE,
18
- TEXT_PADDING,
19
18
  TEXT_SAFE_WIDTH,
20
19
  TEXT_STYLE,
21
20
  } from './constant';
22
21
  import { createMask, getRectMaskAttrs, updateMask } from '../util/mask';
23
- import { clipTextTwoLines, getTextWidth } from '../util/util';
22
+ import { clipTextTwoLines } from '../util/util';
24
23
  import { Foreground } from './foreground';
25
24
  import { getCurrentPoint } from '../util/event';
26
25
 
@@ -277,6 +276,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
277
276
  height: handlerHeight,
278
277
  cursor: 'ew-resize',
279
278
  style: handlerStyle,
279
+ sliderHeight: height,
280
280
  });
281
281
 
282
282
  this.maxHandler = this.addComponent(group, {
@@ -289,6 +289,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
289
289
  height: handlerHeight,
290
290
  cursor: 'ew-resize',
291
291
  style: handlerStyle,
292
+ sliderHeight: height,
292
293
  });
293
294
  }
294
295
 
@@ -304,7 +305,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
304
305
  }
305
306
 
306
307
  private updateUI(minTextShape: IShape, maxTextShape: IShape) {
307
- const { start, end, width, minText, maxText, handlerStyle, height } = this.cfg as SliderCfg;
308
+ const { start, end, width, minText, maxText, handlerStyle, height, barStyle } = this.cfg;
308
309
  const min = start * width;
309
310
  const max = end * width;
310
311
 
@@ -319,27 +320,37 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
319
320
  }
320
321
 
321
322
  if (this.foreground) {
323
+ const newBarStyle = ['foreground-bar', 'foreground'].includes(this.currentTarget)
324
+ ? { ...barStyle, fill: barStyle.highLightFill }
325
+ : barStyle;
322
326
  this.foreground.update({
323
327
  x: min,
324
328
  width: max - min,
329
+ barStyle: newBarStyle,
325
330
  });
326
331
  if (!this.get('updateAutoRender')) {
327
332
  this.foreground.render();
328
333
  }
329
334
  }
330
-
331
335
  // 滑块相关的大小信息
332
336
  const handlerWidth = get(handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
333
337
 
334
338
  // 设置文本
335
339
  minTextShape.attr('text', minText);
336
340
  maxTextShape.attr('text', maxText);
337
-
338
341
  const [minAttrs, maxAttrs] = this._dodgeText([min, max], minTextShape, maxTextShape);
339
342
  // 2. 左侧滑块和文字位置
340
343
  if (this.minHandler) {
344
+ const newStyle =
345
+ this.currentTarget === 'minHandler'
346
+ ? {
347
+ ...handlerStyle,
348
+ stroke: handlerStyle.highLightStroke,
349
+ }
350
+ : handlerStyle;
341
351
  this.minHandler.update({
342
352
  x: min - handlerWidth / 2,
353
+ style: newStyle,
343
354
  });
344
355
  if (!this.get('updateAutoRender')) {
345
356
  this.minHandler.render();
@@ -349,8 +360,16 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
349
360
 
350
361
  // 3. 右侧滑块和文字位置
351
362
  if (this.maxHandler) {
363
+ const newStyle =
364
+ this.currentTarget === 'maxHandler'
365
+ ? {
366
+ ...handlerStyle,
367
+ stroke: handlerStyle.highLightStroke,
368
+ }
369
+ : handlerStyle;
352
370
  this.maxHandler.update({
353
371
  x: max - handlerWidth / 2,
372
+ style: newStyle,
354
373
  });
355
374
  if (!this.get('updateAutoRender')) {
356
375
  this.maxHandler.render();
@@ -459,6 +478,25 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
459
478
  this.currentTarget = undefined;
460
479
  }
461
480
 
481
+ // 清空handler的高亮状态
482
+ const { handlerStyle } = this.cfg;
483
+ if (this.minHandler && this.minHandler.cfg.style.stroke === handlerStyle.highLightStroke) {
484
+ this.minHandler.update({
485
+ style: handlerStyle,
486
+ });
487
+ if (!this.get('updateAutoRender')) {
488
+ this.minHandler.render();
489
+ }
490
+ }
491
+ if (this.maxHandler && this.maxHandler.cfg.style.stroke === handlerStyle.highLightStroke) {
492
+ this.maxHandler.update({
493
+ style: handlerStyle,
494
+ });
495
+ if (!this.get('updateAutoRender')) {
496
+ this.maxHandler.render();
497
+ }
498
+ }
499
+
462
500
  const containerDOM = this.getContainerDOM();
463
501
  if (containerDOM) {
464
502
  containerDOM.removeEventListener('mousemove', this.onMouseMove);
@@ -530,7 +568,13 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
530
568
  * @param range
531
569
  */
532
570
  private _dodgeText(range: [number, number], minTextShape: IShape, maxTextShape: IShape): [object, object] {
571
+ const dodgeText = get(this.cfg.textStyle, 'dodgeText', null);
572
+ if (dodgeText) {
573
+ return dodgeText(this.cfg, range, minTextShape, maxTextShape);
574
+ }
575
+
533
576
  const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
577
+ const textPadding = get(this.cfg.textStyle, 'padding');
534
578
  let [min, max] = range;
535
579
  let sorted = false;
536
580
 
@@ -549,7 +593,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
549
593
  const { attrs: maxAttrs, textWidth: maxTextWidth } = this.setTextAttrs(maxBBox, maxTextShape, [min, max], 'max');
550
594
 
551
595
  // 两端文字存在安全间距为4px, 如果小于该距离则两端均不显示文字
552
- const gap = this.cfg.width - handlerWidth * 2 - TEXT_PADDING * 2 - minTextWidth - maxTextWidth;
596
+ const gap = this.cfg.width - handlerWidth * 2 - textPadding * 2 - minTextWidth - maxTextWidth;
553
597
  if (gap <= TEXT_SAFE_WIDTH) {
554
598
  // text: '' 不显示文字
555
599
  const newMinAttrs = { ...minAttrs, text: '' };
@@ -561,7 +605,7 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
561
605
  // 检测中间宽度是否能展示当前不滑动文字的文本,如果距离不够,则隐藏不滑动一侧的文字。
562
606
  if (minAttrs.textAlign === 'left' || maxAttrs.textAlign === 'right') {
563
607
  const foregroundWidth = this.foreground?.get('width');
564
- const gap = foregroundWidth - handlerWidth - TEXT_PADDING * 2;
608
+ const gap = foregroundWidth - handlerWidth - textPadding * 2;
565
609
  if (minAttrs.textAlign === 'left') {
566
610
  if (gap <= minTextWidth) {
567
611
  return !sorted ? [{ ...minAttrs, text: '' }, { ...maxAttrs }] : [{ ...maxAttrs }, { ...minAttrs, text: '' }];
@@ -595,7 +639,8 @@ export class Slider extends GroupComponent<SliderCfg> implements ISlider {
595
639
  textWidth: number;
596
640
  } {
597
641
  const handlerWidth = get(this.cfg.handlerStyle, 'width', DEFAULT_HANDLER_WIDTH);
598
- const gapWidth = handlerWidth / 2 + TEXT_PADDING;
642
+ const textPadding = get(this.cfg.textStyle, 'padding');
643
+ const gapWidth = handlerWidth / 2 + textPadding;
599
644
  const [min, max] = range;
600
645
  const { width } = bbox;
601
646
  const text = textShape.attr('text');