@antv/g2 5.4.2 → 5.4.3

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.
@@ -5,6 +5,7 @@ import { ANNOTATION_MARKS } from '../component/constant';
5
5
  import { useState, setCursor, restoreCursor } from './utils';
6
6
 
7
7
  export const CATEGORY_LEGEND_CLASS_NAME = 'legend-category';
8
+ export const CATEGORY_LEGEND_CLASS_NAME_HTML = 'legend-html';
8
9
 
9
10
  export const CONTINUOUS_LEGEND_CLASS_NAME = 'legend-continuous';
10
11
 
@@ -35,6 +36,9 @@ export function itemsOf(root) {
35
36
  export function legendsOf(root) {
36
37
  return root.getElementsByClassName(CATEGORY_LEGEND_CLASS_NAME);
37
38
  }
39
+ export function legendsHtmlOf(root) {
40
+ return root.getElementsByClassName(CATEGORY_LEGEND_CLASS_NAME_HTML);
41
+ }
38
42
 
39
43
  export function legendsContinuousOf(root) {
40
44
  return root.getElementsByClassName(CONTINUOUS_LEGEND_CLASS_NAME);
@@ -254,22 +258,215 @@ function legendFilterOrdinal(
254
258
  if (focusIcon) {
255
259
  focusIcon.removeEventListener('click', focusIconClick.get(item));
256
260
  }
257
-
258
- emitter.on('legend:focus', onFocus);
259
- emitter.off('legend:filter', onFilter);
260
- emitter.off('legend:reset', onEnd);
261
261
  }
262
+ emitter.off('legend:focus', onFocus);
263
+ emitter.off('legend:filter', onFilter);
264
+ emitter.off('legend:reset', onEnd);
265
+ };
266
+ }
267
+
268
+ function legendFilterOrdinalHtml(
269
+ root: DisplayObject,
270
+ { domain, filter, defaultSelect, emitter, channel },
271
+ ) {
272
+ // HTML DOM event handlers.
273
+ const htmlItemClick = new Map();
274
+ const htmlItemPointerenter = new Map();
275
+ const htmlItemPointerout = new Map();
276
+
277
+ let selectedValues = [...domain];
278
+
279
+ // Helper function to get chart container element.
280
+ const getChartContainer = () => {
281
+ // Use the same approach as tooltip.ts to get container.
282
+ const view = root.ownerDocument?.defaultView;
283
+ if (!view) return document.body;
284
+
285
+ const canvas: any = view.getContextService().getDomElement();
286
+ return (canvas.parentElement as unknown as HTMLElement) || document.body;
287
+ };
288
+
289
+ // Helper function to bind HTML DOM events.
290
+ const bindHtmlDomEvents = () => {
291
+ const chartContainer = getChartContainer();
292
+
293
+ // Find HTML legend containers within this chart's container.
294
+ const htmlContainer = chartContainer.querySelector('.legend-html');
295
+
296
+ const htmlClick = async (event) => {
297
+ // Find the element with legend-value attribute by traversing up from the target.
298
+ let targetElement = event.target as Element;
299
+ while (targetElement && !targetElement.hasAttribute('legend-value')) {
300
+ targetElement = targetElement.parentElement;
301
+ if (targetElement === htmlContainer) break; // Stop if we reach the container.
302
+ }
303
+
304
+ if (!targetElement || !targetElement.hasAttribute('legend-value')) return;
305
+
306
+ event.preventDefault();
307
+ event.stopPropagation();
308
+
309
+ const value = targetElement.getAttribute('legend-value');
310
+ if (!value) return;
311
+
312
+ const index = selectedValues.indexOf(value);
313
+ if (index === -1) selectedValues.push(value);
314
+ else selectedValues.splice(index, 1);
315
+
316
+ await filter(selectedValues);
317
+ updateHtmlLegendState();
318
+
319
+ if (selectedValues.length === domain.length) {
320
+ emitter.emit('legend:reset', { nativeEvent: true });
321
+ } else {
322
+ emitter.emit('legend:filter', {
323
+ nativeEvent: true,
324
+ data: {
325
+ channel,
326
+ values: selectedValues,
327
+ },
328
+ });
329
+ }
330
+ };
331
+
332
+ // Bind HTML DOM events to the container using event delegation.
333
+ htmlContainer.addEventListener('click', htmlClick);
334
+
335
+ // Store handlers for cleanup.
336
+ htmlItemClick.set(htmlContainer, htmlClick);
337
+ };
338
+
339
+ // Helper function to update HTML legend visual state.
340
+ const updateHtmlLegendState = () => {
341
+ const chartContainer = getChartContainer();
342
+ const htmlLegendItems = chartContainer.querySelectorAll('[legend-value]');
343
+
344
+ htmlLegendItems.forEach((htmlItem) => {
345
+ const value = htmlItem.getAttribute('legend-value');
346
+ if (!value) return;
347
+
348
+ // Check if this value exists in the domain (belongs to this chart instance).
349
+ if (!domain.includes(value)) return;
350
+
351
+ const isSelected = selectedValues.includes(value);
352
+ const htmlElement = htmlItem as HTMLElement;
353
+
354
+ if (!isSelected) {
355
+ // Apply unselected style.
356
+ // User can override style via CSS.
357
+ htmlElement.style.opacity = '0.4';
358
+ htmlElement.classList.add('legend-item-inactive');
359
+ } else {
360
+ // Apply selected style.
361
+ htmlElement.style.opacity = '1';
362
+ htmlElement.classList.remove('legend-item-inactive');
363
+ }
364
+ });
365
+ };
366
+
367
+ // Bind HTML DOM events.
368
+ bindHtmlDomEvents();
369
+
370
+ const onFocus = async (event) => {
371
+ const { nativeEvent } = event;
372
+ if (nativeEvent) return;
373
+
374
+ const { data } = event;
375
+ const { channel: specifiedChannel, value } = data;
376
+ if (specifiedChannel !== channel) return;
377
+
378
+ selectedValues = [value];
379
+
380
+ await filter(selectedValues);
381
+ updateHtmlLegendState();
382
+ };
383
+
384
+ const onFilter = async (event) => {
385
+ const { nativeEvent } = event;
386
+ if (nativeEvent) return;
387
+ const { data } = event;
388
+ const { channel: specifiedChannel, values } = data;
389
+ if (specifiedChannel !== channel) return;
390
+ selectedValues = values;
391
+ await filter(selectedValues);
392
+ updateHtmlLegendState();
393
+ };
394
+
395
+ const onEnd = async (event) => {
396
+ const { nativeEvent } = event;
397
+ if (nativeEvent) return;
398
+ selectedValues = [...domain];
399
+ await filter(selectedValues);
400
+ updateHtmlLegendState();
401
+ };
402
+
403
+ emitter.on('legend:filter', onFilter);
404
+ emitter.on('legend:focus', onFocus);
405
+ emitter.on('legend:reset', onEnd);
406
+
407
+ if (defaultSelect) {
408
+ emitter.emit('legend:filter', {
409
+ data: { channel, values: defaultSelect },
410
+ });
411
+ }
412
+
413
+ return () => {
414
+ // Clean up HTML DOM event listeners.
415
+ const chartContainer = getChartContainer();
416
+ const htmlLegendItems = chartContainer.querySelectorAll('[legend-value]');
417
+
418
+ htmlLegendItems.forEach((htmlItem) => {
419
+ const value = htmlItem.getAttribute('legend-value');
420
+ if (!value) return;
421
+
422
+ // Only clean up items that belong to this chart instance.
423
+ if (!domain.includes(value)) return;
424
+
425
+ const clickHandler = htmlItemClick.get(htmlItem);
426
+ const pointerenterHandler = htmlItemPointerenter.get(htmlItem);
427
+ const pointeroutHandler = htmlItemPointerout.get(htmlItem);
428
+
429
+ if (clickHandler) {
430
+ htmlItem.removeEventListener('click', clickHandler);
431
+ }
432
+ if (pointerenterHandler) {
433
+ htmlItem.removeEventListener('pointerenter', pointerenterHandler);
434
+ }
435
+ if (pointeroutHandler) {
436
+ htmlItem.removeEventListener('pointerout', pointeroutHandler);
437
+ }
438
+ });
439
+
440
+ // Clear HTML DOM handler maps.
441
+ htmlItemClick.clear();
442
+ htmlItemPointerenter.clear();
443
+ htmlItemPointerout.clear();
444
+
445
+ // Clean up emitter listeners.
446
+ emitter.off('legend:filter', onFilter);
447
+ emitter.off('legend:focus', onFocus);
448
+ emitter.off('legend:reset', onEnd);
262
449
  };
263
450
  }
264
451
 
265
452
  function legendFilterContinuous(_, { legend, filter, emitter, channel }) {
266
- const onValueChange = ({ detail: { value } }) => {
267
- filter(value);
453
+ const { attributes } = legend;
454
+ const onValueChange = (data) => {
455
+ const { value } = data.detail;
456
+ const domainValue = value.map((d) => {
457
+ const matchRealValue = attributes.data?.find((item) => item.value === d);
458
+ // For threshold/quantile scale, use domain value instead of threshold index.
459
+ if (matchRealValue) return matchRealValue.domainValue ?? d;
460
+
461
+ return d;
462
+ });
463
+
464
+ filter(domainValue);
268
465
  emitter.emit({
269
466
  nativeEvent: true,
270
467
  data: {
271
468
  channel,
272
- values: value,
469
+ values: domainValue,
273
470
  },
274
471
  });
275
472
  };
@@ -353,6 +550,7 @@ export function LegendFilter() {
353
550
  };
354
551
  const legends = [
355
552
  ...legendsOf(container),
553
+ ...legendsHtmlOf(container),
356
554
  ...legendsContinuousOf(container),
357
555
  ];
358
556
  const allChannels = legends.flatMap(channelsOf);
@@ -370,6 +568,7 @@ export function LegendFilter() {
370
568
  channels,
371
569
  allChannels,
372
570
  };
571
+
373
572
  if (legend.className === CATEGORY_LEGEND_CLASS_NAME) {
374
573
  return legendFilterOrdinal(container, {
375
574
  legends: itemsOf,
@@ -390,6 +589,18 @@ export function LegendFilter() {
390
589
  channel,
391
590
  emitter,
392
591
  });
592
+ } else if (legend.className === CATEGORY_LEGEND_CLASS_NAME_HTML) {
593
+ return legendFilterOrdinalHtml(container, {
594
+ domain,
595
+ filter: (value) => {
596
+ const options = { ...common, value, ordinal: true };
597
+ if (isFacet) filter(facets, options);
598
+ else filter(context, options);
599
+ },
600
+ defaultSelect: legend.attributes.defaultSelect,
601
+ channel,
602
+ emitter,
603
+ });
393
604
  } else {
394
605
  return legendFilterContinuous(container, {
395
606
  legend,
@@ -948,6 +948,7 @@ export function seriesTooltip(
948
948
  style: _style = {},
949
949
  css = {},
950
950
  clickLock = false,
951
+ disableAutoHide = false,
951
952
  ...rest
952
953
  }: Record<string, any>,
953
954
  ) {
@@ -1095,7 +1096,8 @@ export function seriesTooltip(
1095
1096
  ) as (...args: any[]) => void;
1096
1097
 
1097
1098
  const hide = (event: MouseEvent) => {
1098
- if (clickLock && root.getAttribute(LOCKED_SYMBOL)) return;
1099
+ if ((clickLock && root.getAttribute(LOCKED_SYMBOL)) || disableAutoHide)
1100
+ return;
1099
1101
  hideTooltip({ root, single, emitter, event });
1100
1102
  };
1101
1103
 
@@ -1220,6 +1222,7 @@ export function tooltip(
1220
1222
  preserve = false,
1221
1223
  css = {},
1222
1224
  clickLock = false,
1225
+ disableAutoHide = false,
1223
1226
  }: Record<string, any>,
1224
1227
  ) {
1225
1228
  const elements = elementsof(root);
@@ -1236,7 +1239,9 @@ export function tooltip(
1236
1239
  shared,
1237
1240
  });
1238
1241
  if (!element) {
1239
- hideTooltip({ root, single, emitter, event });
1242
+ if (!disableAutoHide) {
1243
+ hideTooltip({ root, single, emitter, event });
1244
+ }
1240
1245
  return;
1241
1246
  }
1242
1247
  const k = groupKey(element);
@@ -1258,7 +1263,9 @@ export function tooltip(
1258
1263
  }
1259
1264
 
1260
1265
  if (isEmptyTooltipData(data)) {
1261
- hideTooltip({ root, single, emitter, event });
1266
+ if (!disableAutoHide) {
1267
+ hideTooltip({ root, single, emitter, event });
1268
+ }
1262
1269
  return;
1263
1270
  }
1264
1271
 
@@ -1295,6 +1302,7 @@ export function tooltip(
1295
1302
  ) as (...args: any[]) => void;
1296
1303
 
1297
1304
  const pointerleave = (event) => {
1305
+ if (disableAutoHide) return;
1298
1306
  hideTooltip({ root, single, emitter, event });
1299
1307
  };
1300
1308
 
@@ -189,6 +189,7 @@ export type TooltipInteraction = {
189
189
  marker?: boolean;
190
190
  groupName?: boolean;
191
191
  disableNative?: boolean;
192
+ disableAutoHide?: boolean;
192
193
  offset?: [number, number];
193
194
  position?: TooltipStyleProps['position'];
194
195
  bounding?: BBox;