@antv/g2 5.1.4 → 5.1.5-beta.0

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 (98) hide show
  1. package/dist/g2-lite.min.js +1 -1
  2. package/dist/g2.min.js +1 -1
  3. package/esm/api/chart.d.ts +1 -0
  4. package/esm/api/library.d.ts +1 -0
  5. package/esm/api/utils.d.ts +1 -0
  6. package/esm/api/utils.js +25 -6
  7. package/esm/api/utils.js.map +1 -1
  8. package/esm/composition/repeatMatrix.js +4 -1
  9. package/esm/composition/repeatMatrix.js.map +1 -1
  10. package/esm/interaction/fisheye.js +3 -1
  11. package/esm/interaction/fisheye.js.map +1 -1
  12. package/esm/interaction/legendFilter.d.ts +1 -1
  13. package/esm/interaction/legendFilter.js +72 -31
  14. package/esm/interaction/legendFilter.js.map +1 -1
  15. package/esm/interaction/tooltip.d.ts +2 -2
  16. package/esm/interaction/tooltip.js +37 -20
  17. package/esm/interaction/tooltip.js.map +1 -1
  18. package/esm/lib/core.d.ts +1 -0
  19. package/esm/lib/core.js +2 -1
  20. package/esm/lib/core.js.map +1 -1
  21. package/esm/lib/std.d.ts +1 -0
  22. package/esm/runtime/plot.js +11 -4
  23. package/esm/runtime/plot.js.map +1 -1
  24. package/esm/shape/connector/connector.js +1 -1
  25. package/esm/shape/connector/connector.js.map +1 -1
  26. package/esm/theme/academy.js +30 -358
  27. package/esm/theme/academy.js.map +1 -1
  28. package/esm/theme/classic.js +4 -433
  29. package/esm/theme/classic.js.map +1 -1
  30. package/esm/theme/classicDark.js +3 -404
  31. package/esm/theme/classicDark.js.map +1 -1
  32. package/esm/theme/create.d.ts +437 -0
  33. package/esm/theme/create.js +421 -0
  34. package/esm/theme/create.js.map +1 -0
  35. package/esm/theme/dark.d.ts +3 -0
  36. package/esm/theme/dark.js +76 -0
  37. package/esm/theme/dark.js.map +1 -0
  38. package/esm/theme/index.d.ts +1 -0
  39. package/esm/theme/index.js +1 -0
  40. package/esm/theme/index.js.map +1 -1
  41. package/esm/theme/light.js +52 -470
  42. package/esm/theme/light.js.map +1 -1
  43. package/lib/api/chart.d.ts +1 -0
  44. package/lib/api/library.d.ts +1 -0
  45. package/lib/api/utils.d.ts +1 -0
  46. package/lib/api/utils.js +26 -7
  47. package/lib/api/utils.js.map +1 -1
  48. package/lib/composition/repeatMatrix.js +4 -1
  49. package/lib/composition/repeatMatrix.js.map +1 -1
  50. package/lib/interaction/fisheye.js +3 -1
  51. package/lib/interaction/fisheye.js.map +1 -1
  52. package/lib/interaction/legendFilter.d.ts +1 -1
  53. package/lib/interaction/legendFilter.js +72 -31
  54. package/lib/interaction/legendFilter.js.map +1 -1
  55. package/lib/interaction/tooltip.d.ts +2 -2
  56. package/lib/interaction/tooltip.js +37 -20
  57. package/lib/interaction/tooltip.js.map +1 -1
  58. package/lib/lib/core.d.ts +1 -0
  59. package/lib/lib/core.js +1 -0
  60. package/lib/lib/core.js.map +1 -1
  61. package/lib/lib/std.d.ts +1 -0
  62. package/lib/runtime/plot.js +11 -4
  63. package/lib/runtime/plot.js.map +1 -1
  64. package/lib/shape/connector/connector.js +1 -1
  65. package/lib/shape/connector/connector.js.map +1 -1
  66. package/lib/theme/academy.js +30 -358
  67. package/lib/theme/academy.js.map +1 -1
  68. package/lib/theme/classic.js +4 -433
  69. package/lib/theme/classic.js.map +1 -1
  70. package/lib/theme/classicDark.js +3 -404
  71. package/lib/theme/classicDark.js.map +1 -1
  72. package/lib/theme/create.d.ts +437 -0
  73. package/lib/theme/create.js +425 -0
  74. package/lib/theme/create.js.map +1 -0
  75. package/lib/theme/dark.d.ts +3 -0
  76. package/lib/theme/dark.js +80 -0
  77. package/lib/theme/dark.js.map +1 -0
  78. package/lib/theme/index.d.ts +1 -0
  79. package/lib/theme/index.js +3 -1
  80. package/lib/theme/index.js.map +1 -1
  81. package/lib/theme/light.js +52 -470
  82. package/lib/theme/light.js.map +1 -1
  83. package/package.json +1 -1
  84. package/src/api/utils.ts +22 -6
  85. package/src/composition/repeatMatrix.ts +11 -2
  86. package/src/interaction/fisheye.ts +3 -1
  87. package/src/interaction/legendFilter.ts +81 -48
  88. package/src/interaction/tooltip.ts +56 -32
  89. package/src/lib/core.ts +2 -1
  90. package/src/runtime/plot.ts +34 -7
  91. package/src/shape/connector/connector.ts +1 -1
  92. package/src/theme/academy.ts +74 -399
  93. package/src/theme/classic.ts +11 -438
  94. package/src/theme/classicDark.ts +10 -409
  95. package/src/theme/create.ts +439 -0
  96. package/src/theme/dark.ts +85 -0
  97. package/src/theme/index.ts +1 -0
  98. package/src/theme/light.ts +53 -473
@@ -198,9 +198,69 @@ function legendFilterContinuous(_, { legend, filter, emitter, channel }) {
198
198
  };
199
199
  }
200
200
 
201
+ async function filterView(
202
+ context, // View instance,
203
+ {
204
+ legend, // Legend instance.
205
+ channel, // Filter Channel.
206
+ value, // Filtered Values.
207
+ ordinal, // Data type of the legend.
208
+ channels, // Channels for this legend.
209
+ allChannels, // Channels for all legends.
210
+ facet = false, // For facet.
211
+ },
212
+ ) {
213
+ const { view, update, setState } = context;
214
+ setState(legend, (viewOptions) => {
215
+ const { marks } = viewOptions;
216
+ // Add filter transform for every marks,
217
+ // which will skip for mark without color channel.
218
+ const newMarks = marks.map((mark) => {
219
+ if (mark.type === 'legends') return mark;
220
+
221
+ // Inset after aggregate transform, such as group, and bin.
222
+ const { transform = [] } = mark;
223
+ const index = transform.findIndex(
224
+ ({ type }) => type.startsWith('group') || type.startsWith('bin'),
225
+ );
226
+ const newTransform = [...transform];
227
+ newTransform.splice(index + 1, 0, {
228
+ type: 'filter',
229
+ [channel]: { value, ordinal },
230
+ });
231
+
232
+ // Set domain of scale to preserve encoding.
233
+ const newScale = Object.fromEntries(
234
+ channels.map((channel) => [
235
+ channel,
236
+ { domain: view.scale[channel].getOptions().domain },
237
+ ]),
238
+ );
239
+ return deepMix({}, mark, {
240
+ transform: newTransform,
241
+ scale: newScale,
242
+ ...(!ordinal && { animate: false }),
243
+ legend: facet
244
+ ? false
245
+ : Object.fromEntries(allChannels.map((d) => [d, { preserve: true }])),
246
+ });
247
+ });
248
+ return { ...viewOptions, marks: newMarks };
249
+ });
250
+ await update();
251
+ }
252
+
253
+ function filterFacets(facets, options) {
254
+ for (const facet of facets) {
255
+ filterView(facet, { ...options, facet: true });
256
+ }
257
+ }
258
+
201
259
  export function LegendFilter() {
202
- return (context, _, emitter) => {
203
- const { container, view, update, setState } = context;
260
+ return (context, contexts, emitter) => {
261
+ const { container } = context;
262
+ const facets = contexts.filter((d) => d !== context);
263
+ const isFacet = facets.length > 0;
204
264
 
205
265
  const channelsOf = (legend) => {
206
266
  return dataOf(legend).scales.map((d) => d.name);
@@ -211,54 +271,19 @@ export function LegendFilter() {
211
271
  ];
212
272
  const allChannels = legends.flatMap(channelsOf);
213
273
 
214
- const filter = throttle(
215
- async (legend, channel, value, ordinal: boolean, channels) => {
216
- setState(legend, (viewOptions) => {
217
- const { marks } = viewOptions;
218
- // Add filter transform for every marks,
219
- // which will skip for mark without color channel.
220
- const newMarks = marks.map((mark) => {
221
- if (mark.type === 'legends') return mark;
222
-
223
- // Inset after aggregate transform, such as group, and bin.
224
- const { transform = [] } = mark;
225
- const index = transform.findIndex(
226
- ({ type }) => type.startsWith('group') || type.startsWith('bin'),
227
- );
228
- const newTransform = [...transform];
229
- newTransform.splice(index + 1, 0, {
230
- type: 'filter',
231
- [channel]: { value, ordinal },
232
- });
233
-
234
- // Set domain of scale to preserve encoding.
235
- const newScale = Object.fromEntries(
236
- channels.map((channel) => [
237
- channel,
238
- { domain: view.scale[channel].getOptions().domain },
239
- ]),
240
- );
241
-
242
- return deepMix({}, mark, {
243
- transform: newTransform,
244
- scale: newScale,
245
- ...(!ordinal && { animate: false }),
246
- legend: Object.fromEntries(
247
- allChannels.map((d) => [d, { preserve: true }]),
248
- ),
249
- });
250
- });
251
- return { ...viewOptions, marks: newMarks };
252
- });
253
- await update();
254
- },
255
- 50,
256
- { trailing: true },
257
- );
274
+ const filter = isFacet
275
+ ? throttle(filterFacets, 50, { trailing: true })
276
+ : throttle(filterView, 50, { trailing: true });
258
277
 
259
278
  const removes = legends.map((legend) => {
260
279
  const { name: channel, domain } = dataOf(legend).scales[0];
261
280
  const channels = channelsOf(legend);
281
+ const common = {
282
+ legend,
283
+ channel,
284
+ channels,
285
+ allChannels,
286
+ };
262
287
  if (legend.className === CATEGORY_LEGEND_CLASS_NAME) {
263
288
  return legendFilterOrdinal(container, {
264
289
  legends: itemsOf,
@@ -269,7 +294,11 @@ export function LegendFilter() {
269
294
  const { index } = datum;
270
295
  return domain[index];
271
296
  },
272
- filter: (value) => filter(legend, channel, value, true, channels),
297
+ filter: (value) => {
298
+ const options = { ...common, value, ordinal: true };
299
+ if (isFacet) filter(facets, options);
300
+ else filter(context, options);
301
+ },
273
302
  state: legend.attributes.state,
274
303
  channel,
275
304
  emitter,
@@ -277,7 +306,11 @@ export function LegendFilter() {
277
306
  } else {
278
307
  return legendFilterContinuous(container, {
279
308
  legend,
280
- filter: (value) => filter(legend, channel, value, false, channels),
309
+ filter: (value) => {
310
+ const options = { ...common, value, ordinal: false };
311
+ if (isFacet) filter(facets, options);
312
+ else filter(context, options);
313
+ },
281
314
  emitter,
282
315
  channel,
283
316
  });
@@ -2,7 +2,7 @@ import { Circle, DisplayObject, IElement, Line } from '@antv/g';
2
2
  import { sort, group, mean, bisector, minIndex } from 'd3-array';
3
3
  import { deepMix, lowerFirst, throttle } from '@antv/util';
4
4
  import { Tooltip as TooltipComponent } from '@antv/gui';
5
- import { Constant, Identity, Band } from '@antv/scale';
5
+ import { Constant, Band } from '@antv/scale';
6
6
  import { defined, subObject } from '../utils/helper';
7
7
  import { isTranspose, isPolar } from '../utils/coordinate';
8
8
  import { angle, sub } from '../utils/vector';
@@ -200,7 +200,10 @@ function groupNameOf(scale, datum) {
200
200
  );
201
201
  };
202
202
  // For non constant color channel.
203
- if (invertAble(scaleSeries)) return scaleSeries.invert(series);
203
+ if (invertAble(scaleSeries)) {
204
+ const cloned = scaleSeries.clone();
205
+ return cloned.invert(series);
206
+ }
204
207
  if (
205
208
  series &&
206
209
  scaleSeries instanceof Band &&
@@ -236,6 +239,7 @@ function groupItems(
236
239
  scale,
237
240
  groupName,
238
241
  data = elements.map((d) => d['__data__']),
242
+ theme: Record<string, any> = {},
239
243
  ) {
240
244
  const key = (d) => (d instanceof Date ? +d : d);
241
245
  const T = unique(
@@ -253,10 +257,9 @@ function groupItems(
253
257
  groupName !== undefined ? groupName : items.length <= 1 ? true : false;
254
258
 
255
259
  return definedItems.map(
256
- ({ color = itemColorOf(element), name, ...item }) => {
257
- const name1 = useGroupName
258
- ? groupNameOf(scale, datum) || name
259
- : name || groupNameOf(scale, datum);
260
+ ({ color = itemColorOf(element) || theme.color, name, ...item }) => {
261
+ const groupName = groupNameOf(scale, datum);
262
+ const name1 = useGroupName ? groupName || name : name || groupName;
260
263
  return {
261
264
  ...item,
262
265
  color,
@@ -347,30 +350,34 @@ function hideRuleY(root) {
347
350
  }
348
351
  }
349
352
 
350
- // @todo Fill for composite g component.
351
- function updateMarker(root, { data, style }) {
353
+ function updateMarker(root, { data, style, theme }) {
352
354
  if (root.markers) root.markers.forEach((d) => d.remove());
353
- const markers = data.map((d) => {
354
- const [{ color, element }, point] = d;
355
- const fill =
356
- color || // encode value
357
- element.style.fill ||
358
- element.style.stroke ||
359
- '#000';
360
-
361
- const shape = new Circle({
362
- style: {
363
- cx: point[0],
364
- cy: point[1],
365
- fill,
366
- r: 4,
367
- stroke: '#fff',
368
- strokeWidth: 2,
369
- ...style,
370
- },
355
+ const markers = data
356
+ .filter((d) => {
357
+ const [{ x, y }] = d;
358
+ return defined(x) && defined(y);
359
+ })
360
+ .map((d) => {
361
+ const [{ color, element }, point] = d;
362
+ const fill =
363
+ color || // encode value
364
+ element.style.fill ||
365
+ element.style.stroke ||
366
+ theme.color;
367
+
368
+ const shape = new Circle({
369
+ style: {
370
+ cx: point[0],
371
+ cy: point[1],
372
+ fill,
373
+ r: 4,
374
+ stroke: '#fff',
375
+ strokeWidth: 2,
376
+ ...style,
377
+ },
378
+ });
379
+ return shape;
371
380
  });
372
- return shape;
373
- });
374
381
  for (const marker of markers) root.appendChild(marker);
375
382
  root.markers = markers;
376
383
  }
@@ -432,8 +439,10 @@ export function seriesTooltip(
432
439
  enterable,
433
440
  mount,
434
441
  bounding,
442
+ theme,
435
443
  disableNative = false,
436
444
  marker = true,
445
+ preserve = false,
437
446
  style: _style = {},
438
447
  css = {},
439
448
  ...rest
@@ -597,6 +606,7 @@ export function seriesTooltip(
597
606
  scale,
598
607
  groupName,
599
608
  selectedData,
609
+ theme,
600
610
  );
601
611
 
602
612
  // Sort items and filter items.
@@ -653,6 +663,7 @@ export function seriesTooltip(
653
663
  updateMarker(root, {
654
664
  data: filteredSeriesData,
655
665
  style: markerStyles,
666
+ theme,
656
667
  });
657
668
  }
658
669
 
@@ -732,7 +743,11 @@ export function seriesTooltip(
732
743
  emitter.off('tooltip:hide', onTooltipHide);
733
744
  emitter.off('tooltip:disable', onTooltipDisable);
734
745
  emitter.off('tooltip:enable', onTooltipEnable);
735
- destroy();
746
+ if (preserve) {
747
+ hideTooltip({ root, single, emitter, nativeEvent: false });
748
+ } else {
749
+ destroy();
750
+ }
736
751
  };
737
752
  }
738
753
 
@@ -760,9 +775,11 @@ export function tooltip(
760
775
  view,
761
776
  mount,
762
777
  bounding,
778
+ theme,
763
779
  shared = false,
764
780
  body = true,
765
781
  disableNative = false,
782
+ preserve = false,
766
783
  css = {},
767
784
  }: Record<string, any>,
768
785
  ) {
@@ -782,7 +799,7 @@ export function tooltip(
782
799
  const data =
783
800
  group.length === 1 && !shared
784
801
  ? singleItem(group[0])
785
- : groupItems(group, scale, groupName);
802
+ : groupItems(group, scale, groupName, undefined, theme);
786
803
 
787
804
  // Sort items and sort.
788
805
  if (sortFunction) {
@@ -888,7 +905,11 @@ export function tooltip(
888
905
  removeEventListeners();
889
906
  emitter.off('tooltip:show', onTooltipShow);
890
907
  emitter.off('tooltip:hide', onTooltipHide);
891
- destroyTooltip({ root, single });
908
+ if (preserve) {
909
+ hideTooltip({ root, single, emitter, nativeEvent: false });
910
+ } else {
911
+ destroyTooltip({ root, single });
912
+ }
892
913
  };
893
914
  }
894
915
 
@@ -904,7 +925,7 @@ export function Tooltip(options) {
904
925
  } = options;
905
926
  return (target, viewInstances, emitter) => {
906
927
  const { container, view } = target;
907
- const { scale, markState, coordinate } = view;
928
+ const { scale, markState, coordinate, theme } = view;
908
929
  // Get default value from mark states.
909
930
  const defaultSeries = interactionKeyof(markState, 'seriesTooltip');
910
931
  const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs');
@@ -915,6 +936,7 @@ export function Tooltip(options) {
915
936
  if (isSeries && hasSeries(markState) && !facet) {
916
937
  return seriesTooltip(plotArea, {
917
938
  ...rest,
939
+ theme,
918
940
  elements: selectG2Elements,
919
941
  scale,
920
942
  coordinate,
@@ -943,6 +965,7 @@ export function Tooltip(options) {
943
965
  // @ts-ignore
944
966
  return seriesTooltip(plotArea.parentNode.parentNode, {
945
967
  ...rest,
968
+ theme,
946
969
  elements: () => elements,
947
970
  scale,
948
971
  coordinate,
@@ -964,6 +987,7 @@ export function Tooltip(options) {
964
987
  item,
965
988
  emitter,
966
989
  view,
990
+ theme,
967
991
  shared,
968
992
  });
969
993
  };
package/src/lib/core.ts CHANGED
@@ -50,7 +50,7 @@ import {
50
50
  Sequential as ScaleSequential,
51
51
  Constant as ScaleConstant,
52
52
  } from '../scale';
53
- import { Classic, ClassicDark, Academy, Light } from '../theme';
53
+ import { Classic, ClassicDark, Academy, Light, Dark } from '../theme';
54
54
  import {
55
55
  AxisX,
56
56
  AxisY,
@@ -252,6 +252,7 @@ export function corelib() {
252
252
  'theme.classicDark': ClassicDark,
253
253
  'theme.academy': Academy,
254
254
  'theme.light': Light,
255
+ 'theme.dark': Dark,
255
256
  'component.axisX': AxisX,
256
257
  'component.axisY': AxisY,
257
258
  'component.legendCategory': LegendCategory,
@@ -278,7 +278,12 @@ export async function plot<T extends G2ViewTree>(
278
278
  for (const typeOption of inferInteraction(options)) {
279
279
  const [type, option] = typeOption;
280
280
  if (option) {
281
- const interaction = useInteraction({ type, ...(option as any) });
281
+ const interaction = useThemeInteraction(
282
+ target.view,
283
+ type,
284
+ option as Record<string, any>,
285
+ useInteraction,
286
+ );
282
287
  const destroy = interaction(
283
288
  target,
284
289
  enterViewInstances,
@@ -303,7 +308,12 @@ export async function plot<T extends G2ViewTree>(
303
308
 
304
309
  // Apply new interaction.
305
310
  if (option) {
306
- const interaction = useInteraction({ type, ...(option as any) });
311
+ const interaction = useThemeInteraction(
312
+ target.view,
313
+ type,
314
+ option as Record<string, any>,
315
+ useInteraction,
316
+ );
307
317
  const destroy = interaction(
308
318
  target,
309
319
  updateViewInstances,
@@ -402,17 +412,20 @@ function updateTooltip(
402
412
  if (!tooltipOptions[1]) return;
403
413
 
404
414
  // Apply new tooltip interaction.
405
- const applyTooltip = useInteraction({
406
- type: 'tooltip',
407
- ...(tooltipOptions[1] as any),
408
- });
415
+ const applyTooltip = useThemeInteraction(
416
+ view,
417
+ 'tooltip',
418
+ tooltipOptions[1] as any,
419
+ useInteraction,
420
+ );
409
421
  const target = {
410
422
  options,
411
423
  view,
412
424
  container: selection.node(),
413
425
  update: (options) => Promise.resolve(options),
414
426
  };
415
- applyTooltip(target, [], context.emitter);
427
+ const newTooltip = applyTooltip(target, [], context.emitter);
428
+ nameInteraction.set('tooltip', newTooltip);
416
429
  }
417
430
 
418
431
  async function initializeView(
@@ -593,6 +606,20 @@ async function initializeMarks(
593
606
  return markState;
594
607
  }
595
608
 
609
+ function useThemeInteraction(
610
+ view: G2ViewDescriptor,
611
+ type: string,
612
+ option: Record<string, any>,
613
+ useInteraction: (options: G2InteractionOptions, context?: any) => Interaction,
614
+ ): Interaction {
615
+ const theme = view.theme;
616
+ const defaults = typeof type === 'string' ? theme[type] || {} : {};
617
+ const interaction = useInteraction(
618
+ deepMix(defaults, { type, ...(option as any) }),
619
+ );
620
+ return interaction;
621
+ }
622
+
596
623
  function initializeState(
597
624
  markState: Map<G2Mark, G2MarkState>,
598
625
  options: G2View,
@@ -83,7 +83,7 @@ export const Connector: SC<ConnectorOptions> = (options, context) => {
83
83
  points,
84
84
  offset1,
85
85
  offset2,
86
- length1 || connectLength1,
86
+ length1 ?? connectLength1,
87
87
  );
88
88
  const makerStyle = subObject({ ...style, ...defaults }, 'endMarker');
89
89