@antv/component 2.1.4 → 2.1.5

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.
@@ -24,7 +24,11 @@ import {
24
24
  scaleToPixel,
25
25
  select,
26
26
  subStyleProps,
27
+ deepAssign,
27
28
  } from '../../../util';
29
+ import { Poptip } from '../../poptip';
30
+ import type { PoptipStyleProps } from '../../poptip/types';
31
+ import type { PoptipRender } from './items';
28
32
 
29
33
  type ItemMarkerStyle = { size?: number } & PathStyleProps;
30
34
  type ItemTextStyle = Omit<TextStyleProps, 'text'>;
@@ -45,6 +49,7 @@ export type CategoryItemStyleProps = GroupStyleProps &
45
49
  width?: number;
46
50
  x?: number;
47
51
  y?: number;
52
+ poptip?: PoptipStyleProps & PoptipRender;
48
53
  };
49
54
 
50
55
  export type CategoryItemOptions = ComponentOptions<CategoryItemStyleProps>;
@@ -64,6 +69,25 @@ const CLASS_NAMES = classNames(
64
69
  'legend-category-item'
65
70
  );
66
71
 
72
+ const DEFAULT_POPTIP_PROPS: PoptipStyleProps = {
73
+ offset: [0, 20],
74
+ domStyles: {
75
+ '.component-poptip': {
76
+ opacity: '1',
77
+ padding: '8px 12px',
78
+ background: '#fff',
79
+ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.15)',
80
+ },
81
+ '.component-poptip-arrow': {
82
+ display: 'none',
83
+ },
84
+ '.component-poptip-text': {
85
+ color: '#000',
86
+ lineHeight: '20px',
87
+ },
88
+ },
89
+ };
90
+
67
91
  function styleOfMarker(group: Group): BaseStyleProps {
68
92
  const marker = group.querySelector(CLASS_NAMES.marker.class);
69
93
  if (marker) return marker.style;
@@ -85,6 +109,8 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
85
109
  });
86
110
  }
87
111
 
112
+ private poptipGroup!: Poptip;
113
+
88
114
  private markerGroup!: Selection<Group>;
89
115
 
90
116
  private labelGroup!: Selection<Group>;
@@ -224,6 +250,48 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
224
250
  });
225
251
  }
226
252
 
253
+ private createPoptip() {
254
+ const { poptip } = this.attributes;
255
+ const { render, ...poptipStyle } = poptip || {};
256
+ const poptipGroup = new Poptip({ style: deepAssign(DEFAULT_POPTIP_PROPS, poptipStyle) });
257
+ this.poptipGroup = poptipGroup;
258
+ return poptipGroup;
259
+ }
260
+
261
+ private bindPoptip(node: DisplayObject) {
262
+ const { poptip } = this.attributes;
263
+ if (!poptip) return;
264
+ const poptipGroup = this.poptipGroup || this.createPoptip();
265
+ poptipGroup.bind(node, () => {
266
+ const { labelText, valueText, markerFill } = this.attributes;
267
+ const label = typeof labelText === 'string' ? labelText : (labelText as DisplayObject)?.attr('text');
268
+ const value = typeof valueText === 'string' ? valueText : (valueText as DisplayObject)?.attr('text');
269
+ if (typeof poptip.render === 'function') {
270
+ return { html: poptip.render({ label, value, color: markerFill as string }) };
271
+ }
272
+ let html = '';
273
+ if (typeof label === 'string' || typeof label === 'number') {
274
+ html += `<div class="component-poptip-label">${label}</div>`;
275
+ }
276
+ if (typeof value === 'string' || typeof value === 'number') {
277
+ html += `<div class="component-poptip-value">${value}</div>`;
278
+ }
279
+ return { html };
280
+ });
281
+ }
282
+
283
+ private renderPoptip(ctn: Selection) {
284
+ const { poptip } = this.attributes;
285
+ if (!poptip) return;
286
+ const valueNode = ctn.maybeAppendByClassName(CLASS_NAMES.value, 'g').node();
287
+ const labelNode = ctn.maybeAppendByClassName(CLASS_NAMES.label, 'g').node();
288
+ [valueNode, labelNode].forEach((node) => {
289
+ if (node) {
290
+ this.bindPoptip(node);
291
+ }
292
+ });
293
+ }
294
+
227
295
  private renderBackground(container: Selection) {
228
296
  const { width, height } = this.shape;
229
297
  const style = subStyleProps(this.attributes, 'background');
@@ -264,6 +332,7 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
264
332
  this.renderLabel(ctn);
265
333
  this.renderValue(ctn);
266
334
  this.renderBackground(ctn);
335
+ this.renderPoptip(ctn);
267
336
  this.adjustLayout();
268
337
  }
269
338
  }
@@ -20,6 +20,7 @@ import { Navigator } from '../../navigator';
20
20
  import { ifHorizontal } from '../utils';
21
21
  import type { CategoryItemStyleProps } from './item';
22
22
  import { CategoryItem } from './item';
23
+ import type { PoptipStyleProps } from '../../poptip/types';
23
24
 
24
25
  interface CategoryItemsDatum {
25
26
  [keys: string]: any;
@@ -30,6 +31,10 @@ type CallableItemStyle = CallableStyleProps<
30
31
  CallbackParameter<CategoryItemsDatum>
31
32
  >;
32
33
 
34
+ export type PoptipRender = {
35
+ render?: (params: { label: string | number; value: string | number; color?: string }) => string;
36
+ };
37
+
33
38
  export type CategoryItemsStyleProps = GroupStyleProps &
34
39
  PrefixStyleProps<CallableItemStyle, 'item'> &
35
40
  PrefixStyleProps<NavigatorStyleProps, 'nav'> & {
@@ -49,6 +54,7 @@ export type CategoryItemsStyleProps = GroupStyleProps &
49
54
  click?: (el: Selection) => void;
50
55
  mouseenter?: (el: Selection) => void;
51
56
  mouseleave?: (el: Selection) => void;
57
+ poptip?: PoptipStyleProps & PoptipRender;
52
58
  };
53
59
 
54
60
  export type CategoryItemsOptions = ComponentOptions<CategoryItemsStyleProps>;
@@ -123,7 +129,7 @@ export class CategoryItems extends Component<CategoryItemsStyleProps> {
123
129
  }
124
130
 
125
131
  private get renderData() {
126
- const { data, layout } = this.attributes;
132
+ const { data, layout, poptip } = this.attributes;
127
133
  const style = subStyleProps<CategoryItemStyleProps>(this.attributes, 'item');
128
134
 
129
135
  const d = data.map((datum, index) => {
@@ -135,6 +141,7 @@ export class CategoryItems extends Component<CategoryItemsStyleProps> {
135
141
  layout,
136
142
  labelText,
137
143
  valueText,
144
+ poptip,
138
145
  ...Object.fromEntries(
139
146
  Object.entries(style).map(([key, val]) => [key, getCallbackValue(val, [datum, index, data])])
140
147
  ),