@antv/g2 5.0.7 → 5.0.9

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 (189) hide show
  1. package/dist/g2-lite.min.js +4 -4
  2. package/dist/g2.min.js +4 -4
  3. package/esm/api/chart.d.ts +11 -2
  4. package/esm/api/chart.js +98 -46
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/mark/index.d.ts +3 -1
  7. package/esm/api/mark/index.js +2 -1
  8. package/esm/api/mark/index.js.map +1 -1
  9. package/esm/api/mark/mark.d.ts +7 -1
  10. package/esm/api/mark/mark.js +9 -0
  11. package/esm/api/mark/mark.js.map +1 -1
  12. package/esm/api/node.d.ts +1 -0
  13. package/esm/api/node.js +11 -1
  14. package/esm/api/node.js.map +1 -1
  15. package/esm/api/utils.d.ts +8 -0
  16. package/esm/api/utils.js +146 -20
  17. package/esm/api/utils.js.map +1 -1
  18. package/esm/component/axis.js +23 -5
  19. package/esm/component/axis.js.map +1 -1
  20. package/esm/component/utils.d.ts +1 -1
  21. package/esm/component/utils.js +17 -3
  22. package/esm/component/utils.js.map +1 -1
  23. package/esm/composition/mark.js +2 -1
  24. package/esm/composition/mark.js.map +1 -1
  25. package/esm/data/index.d.ts +2 -0
  26. package/esm/data/index.js +1 -0
  27. package/esm/data/index.js.map +1 -1
  28. package/esm/data/kde.d.ts +10 -0
  29. package/esm/data/kde.js +30 -0
  30. package/esm/data/kde.js.map +1 -0
  31. package/esm/interaction/brushHighlight.d.ts +3 -3
  32. package/esm/interaction/brushHighlight.js +18 -13
  33. package/esm/interaction/brushHighlight.js.map +1 -1
  34. package/esm/interaction/brushXHighlight.d.ts +1 -1
  35. package/esm/interaction/brushYHighlight.d.ts +1 -1
  36. package/esm/interaction/tooltip.js +5 -2
  37. package/esm/interaction/tooltip.js.map +1 -1
  38. package/esm/mark/density.d.ts +4 -0
  39. package/esm/mark/density.js +61 -0
  40. package/esm/mark/density.js.map +1 -0
  41. package/esm/mark/index.d.ts +2 -0
  42. package/esm/mark/index.js +1 -0
  43. package/esm/mark/index.js.map +1 -1
  44. package/esm/runtime/component.d.ts +4 -2
  45. package/esm/runtime/component.js +164 -12
  46. package/esm/runtime/component.js.map +1 -1
  47. package/esm/runtime/layout.d.ts +3 -3
  48. package/esm/runtime/layout.js +40 -21
  49. package/esm/runtime/layout.js.map +1 -1
  50. package/esm/runtime/plot.js +22 -16
  51. package/esm/runtime/plot.js.map +1 -1
  52. package/esm/runtime/scale.js +1 -1
  53. package/esm/runtime/scale.js.map +1 -1
  54. package/esm/shape/box/violin.d.ts +3 -0
  55. package/esm/shape/box/violin.js +85 -0
  56. package/esm/shape/box/violin.js.map +1 -0
  57. package/esm/shape/density/density.d.ts +8 -0
  58. package/esm/shape/density/density.js +50 -0
  59. package/esm/shape/density/density.js.map +1 -0
  60. package/esm/shape/index.d.ts +4 -0
  61. package/esm/shape/index.js +2 -0
  62. package/esm/shape/index.js.map +1 -1
  63. package/esm/spec/composition.d.ts +22 -19
  64. package/esm/spec/dataTransform.d.ts +34 -2
  65. package/esm/spec/mark.d.ts +9 -8
  66. package/esm/spec/transform.d.ts +1 -1
  67. package/esm/spec/utils.d.ts +1 -0
  68. package/esm/stdlib/index.js +5 -5
  69. package/esm/stdlib/index.js.map +1 -1
  70. package/esm/theme/academy.js +2 -1
  71. package/esm/theme/academy.js.map +1 -1
  72. package/esm/theme/classic.js +2 -2
  73. package/esm/theme/classic.js.map +1 -1
  74. package/esm/theme/classicDark.js +2 -2
  75. package/esm/theme/classicDark.js.map +1 -1
  76. package/esm/utils/scale.js +5 -1
  77. package/esm/utils/scale.js.map +1 -1
  78. package/esm/utils/size.js +7 -4
  79. package/esm/utils/size.js.map +1 -1
  80. package/lib/api/chart.d.ts +11 -2
  81. package/lib/api/chart.js +96 -44
  82. package/lib/api/chart.js.map +1 -1
  83. package/lib/api/mark/index.d.ts +3 -1
  84. package/lib/api/mark/index.js +1 -0
  85. package/lib/api/mark/index.js.map +1 -1
  86. package/lib/api/mark/mark.d.ts +7 -1
  87. package/lib/api/mark/mark.js +10 -1
  88. package/lib/api/mark/mark.js.map +1 -1
  89. package/lib/api/node.d.ts +1 -0
  90. package/lib/api/node.js +11 -1
  91. package/lib/api/node.js.map +1 -1
  92. package/lib/api/utils.d.ts +8 -0
  93. package/lib/api/utils.js +149 -21
  94. package/lib/api/utils.js.map +1 -1
  95. package/lib/component/axis.js +23 -5
  96. package/lib/component/axis.js.map +1 -1
  97. package/lib/component/utils.d.ts +1 -1
  98. package/lib/component/utils.js +17 -3
  99. package/lib/component/utils.js.map +1 -1
  100. package/lib/composition/mark.js +2 -1
  101. package/lib/composition/mark.js.map +1 -1
  102. package/lib/data/index.d.ts +2 -0
  103. package/lib/data/index.js +3 -1
  104. package/lib/data/index.js.map +1 -1
  105. package/lib/data/kde.d.ts +10 -0
  106. package/lib/data/kde.js +38 -0
  107. package/lib/data/kde.js.map +1 -0
  108. package/lib/interaction/brushHighlight.d.ts +3 -3
  109. package/lib/interaction/brushHighlight.js +18 -13
  110. package/lib/interaction/brushHighlight.js.map +1 -1
  111. package/lib/interaction/brushXHighlight.d.ts +1 -1
  112. package/lib/interaction/brushYHighlight.d.ts +1 -1
  113. package/lib/interaction/tooltip.js +5 -2
  114. package/lib/interaction/tooltip.js.map +1 -1
  115. package/lib/mark/density.d.ts +4 -0
  116. package/lib/mark/density.js +65 -0
  117. package/lib/mark/density.js.map +1 -0
  118. package/lib/mark/index.d.ts +2 -0
  119. package/lib/mark/index.js +3 -1
  120. package/lib/mark/index.js.map +1 -1
  121. package/lib/runtime/component.d.ts +4 -2
  122. package/lib/runtime/component.js +163 -10
  123. package/lib/runtime/component.js.map +1 -1
  124. package/lib/runtime/layout.d.ts +3 -3
  125. package/lib/runtime/layout.js +40 -21
  126. package/lib/runtime/layout.js.map +1 -1
  127. package/lib/runtime/plot.js +22 -16
  128. package/lib/runtime/plot.js.map +1 -1
  129. package/lib/runtime/scale.js +1 -1
  130. package/lib/runtime/scale.js.map +1 -1
  131. package/lib/shape/box/violin.d.ts +3 -0
  132. package/lib/shape/box/violin.js +89 -0
  133. package/lib/shape/box/violin.js.map +1 -0
  134. package/lib/shape/density/density.d.ts +8 -0
  135. package/lib/shape/density/density.js +54 -0
  136. package/lib/shape/density/density.js.map +1 -0
  137. package/lib/shape/index.d.ts +4 -0
  138. package/lib/shape/index.js +6 -1
  139. package/lib/shape/index.js.map +1 -1
  140. package/lib/spec/composition.d.ts +22 -19
  141. package/lib/spec/dataTransform.d.ts +34 -2
  142. package/lib/spec/mark.d.ts +9 -8
  143. package/lib/spec/transform.d.ts +1 -1
  144. package/lib/spec/utils.d.ts +1 -0
  145. package/lib/stdlib/index.js +2 -2
  146. package/lib/stdlib/index.js.map +1 -1
  147. package/lib/theme/academy.js +2 -1
  148. package/lib/theme/academy.js.map +1 -1
  149. package/lib/theme/classic.js +2 -2
  150. package/lib/theme/classic.js.map +1 -1
  151. package/lib/theme/classicDark.js +2 -2
  152. package/lib/theme/classicDark.js.map +1 -1
  153. package/lib/utils/scale.js +5 -1
  154. package/lib/utils/scale.js.map +1 -1
  155. package/lib/utils/size.js +7 -4
  156. package/lib/utils/size.js.map +1 -1
  157. package/package.json +4 -3
  158. package/src/api/chart.ts +111 -55
  159. package/src/api/mark/index.ts +3 -0
  160. package/src/api/mark/mark.ts +12 -0
  161. package/src/api/node.ts +10 -1
  162. package/src/api/utils.ts +139 -20
  163. package/src/component/axis.ts +35 -12
  164. package/src/component/utils.ts +9 -4
  165. package/src/composition/mark.ts +2 -0
  166. package/src/data/index.ts +2 -0
  167. package/src/data/kde.ts +53 -0
  168. package/src/interaction/brushHighlight.ts +16 -13
  169. package/src/interaction/tooltip.ts +5 -2
  170. package/src/mark/density.ts +81 -0
  171. package/src/mark/index.ts +2 -0
  172. package/src/runtime/component.ts +210 -12
  173. package/src/runtime/layout.ts +74 -19
  174. package/src/runtime/plot.ts +44 -21
  175. package/src/runtime/scale.ts +1 -1
  176. package/src/shape/box/violin.ts +119 -0
  177. package/src/shape/density/density.ts +52 -0
  178. package/src/shape/index.ts +4 -0
  179. package/src/spec/composition.ts +22 -19
  180. package/src/spec/dataTransform.ts +35 -0
  181. package/src/spec/mark.ts +10 -6
  182. package/src/spec/transform.ts +1 -0
  183. package/src/spec/utils.ts +2 -0
  184. package/src/stdlib/index.ts +8 -0
  185. package/src/theme/academy.ts +2 -1
  186. package/src/theme/classic.ts +2 -2
  187. package/src/theme/classicDark.ts +2 -2
  188. package/src/utils/scale.ts +5 -1
  189. package/src/utils/size.ts +10 -4
@@ -230,10 +230,17 @@ export async function plot<T extends G2ViewTree>(
230
230
  target.container['nameInteraction'] = nameInteraction;
231
231
 
232
232
  // Apply interactions.
233
- for (const option of inferInteraction(options)) {
234
- const interaction = useInteraction(option);
235
- const destroy = interaction(target, enterViewInstances, context.emitter);
236
- nameInteraction.set(option.type, { destroy });
233
+ for (const typeOption of inferInteraction(options)) {
234
+ const [type, option] = typeOption;
235
+ if (option) {
236
+ const interaction = useInteraction({ type, ...(option as any) });
237
+ const destroy = interaction(
238
+ target,
239
+ enterViewInstances,
240
+ context.emitter,
241
+ );
242
+ nameInteraction.set(type, { destroy });
243
+ }
237
244
  }
238
245
  }
239
246
 
@@ -242,15 +249,23 @@ export async function plot<T extends G2ViewTree>(
242
249
  for (const target of updateViewInstances) {
243
250
  const { options, container } = target;
244
251
  const nameInteraction = container['nameInteraction'];
245
- for (const option of inferInteraction(options)) {
252
+ for (const typeOption of inferInteraction(options)) {
253
+ const [type, option] = typeOption;
254
+
246
255
  // Remove interaction for existed views.
247
- const prevInteraction = nameInteraction.get(option.type);
256
+ const prevInteraction = nameInteraction.get(type);
248
257
  if (prevInteraction) prevInteraction.destroy?.();
249
258
 
250
259
  // Apply new interaction.
251
- const interaction = useInteraction(option);
252
- const destroy = interaction(target, updateViewInstances, context.emitter);
253
- nameInteraction.set(option.type, { destroy });
260
+ if (option) {
261
+ const interaction = useInteraction({ type, ...(option as any) });
262
+ const destroy = interaction(
263
+ target,
264
+ updateViewInstances,
265
+ context.emitter,
266
+ );
267
+ nameInteraction.set(type, { destroy });
268
+ }
254
269
  }
255
270
  }
256
271
 
@@ -314,7 +329,13 @@ function createUpdateView(
314
329
  };
315
330
  }
316
331
 
317
- function updateTooltip(selection, options, view, library, context) {
332
+ function updateTooltip(
333
+ selection: Selection,
334
+ options: G2ViewTree,
335
+ view: G2ViewDescriptor,
336
+ library: G2Library,
337
+ context: G2Context,
338
+ ) {
318
339
  const [useInteraction] = useLibrary<
319
340
  G2InteractionOptions,
320
341
  InteractionComponent,
@@ -325,7 +346,7 @@ function updateTooltip(selection, options, view, library, context) {
325
346
  const container = selection.node();
326
347
  const nameInteraction = container['nameInteraction'];
327
348
  const tooltipOptions = inferInteraction(options).find(
328
- (d) => d.type === 'tooltip',
349
+ ([d]) => d === 'tooltip',
329
350
  );
330
351
 
331
352
  // Destroy older tooltip.
@@ -333,8 +354,13 @@ function updateTooltip(selection, options, view, library, context) {
333
354
  if (!tooltip) return;
334
355
  tooltip.destroy?.();
335
356
 
357
+ if (!tooltipOptions[1]) return;
358
+
336
359
  // Apply new tooltip interaction.
337
- const applyTooltip = useInteraction(tooltipOptions);
360
+ const applyTooltip = useInteraction({
361
+ type: 'tooltip',
362
+ ...(tooltipOptions[1] as any),
363
+ });
338
364
  const target = {
339
365
  options,
340
366
  view,
@@ -537,7 +563,7 @@ function initializeState(
537
563
  options,
538
564
  library,
539
565
  );
540
- const layout = computeLayout(components, options);
566
+ const layout = computeLayout(components, options, theme, library);
541
567
  const coordinate = createCoordinate(layout, options, library);
542
568
  const framedStyle = frame
543
569
  ? deepMix({ mainLineWidth: 1, mainStroke: '#000' }, style)
@@ -1082,7 +1108,7 @@ function createLabelShapeFunction(
1082
1108
  );
1083
1109
  const { shape = defaultLabelShape, text, ...style } = visualOptions;
1084
1110
  const f = typeof formatter === 'string' ? format(formatter) : formatter;
1085
- const value = { ...style, text: f(text) };
1111
+ const value = { ...style, text: f(text, datum, index, abstractData) };
1086
1112
  const shapeFunction = useShape({ type: `label.${shape}`, ...style });
1087
1113
  return shapeFunction(points, value, coordinate, theme, point2d);
1088
1114
  };
@@ -1351,7 +1377,9 @@ function inferTheme(theme: G2ThemeOptions = {}): G2ThemeOptions {
1351
1377
  /**
1352
1378
  * @todo Infer builtin tooltips.
1353
1379
  */
1354
- function inferInteraction(view: G2View): G2InteractionOptions[] {
1380
+ function inferInteraction(
1381
+ view: G2View,
1382
+ ): [string, boolean | Omit<G2InteractionOptions, 'type'>][] {
1355
1383
  const defaults = {
1356
1384
  event: true,
1357
1385
  tooltip: true,
@@ -1360,12 +1388,7 @@ function inferInteraction(view: G2View): G2InteractionOptions[] {
1360
1388
  legendFilter: true,
1361
1389
  };
1362
1390
  const { interaction = {} } = view;
1363
- return Object.entries(deepMix(defaults, interaction))
1364
- .filter((d) => !!d[1])
1365
- .map(([key, value]) => ({
1366
- type: key,
1367
- ...(value as Record<string, any>),
1368
- }));
1391
+ return Object.entries(deepMix(defaults, interaction));
1369
1392
  }
1370
1393
 
1371
1394
  async function applyTransform<T extends G2ViewTree>(
@@ -348,7 +348,7 @@ function inferOptionsQ(
348
348
  const {
349
349
  interpolate = createInterpolateValue,
350
350
  nice = false,
351
- tickCount = 10,
351
+ tickCount = 5,
352
352
  } = options;
353
353
  return { ...options, interpolate, nice, tickCount };
354
354
  }
@@ -0,0 +1,119 @@
1
+ import { Path as GPath } from '@antv/g';
2
+ import { path as d3path } from 'd3-path';
3
+ import { Coordinate } from '@antv/coord';
4
+ import { applyStyle, getShapeTheme } from '../utils';
5
+ import { select } from '../../utils/selection';
6
+ import { ShapeComponent as SC, Vector2 } from '../../runtime';
7
+ import { isPolar } from '../../utils/coordinate';
8
+ import { angle, sub, dist } from '../../utils/vector';
9
+
10
+ export type ViolinOptions = Record<string, any>;
11
+
12
+ function getPath(p: Vector2[], coordinate: Coordinate, size = 4) {
13
+ const path = d3path();
14
+
15
+ if (!isPolar(coordinate)) {
16
+ path.moveTo(...p[2]);
17
+ path.lineTo(...p[3]);
18
+
19
+ path.lineTo(p[3][0] - size, p[3][1]);
20
+ path.lineTo(p[10][0] - size, p[10][1]);
21
+ path.lineTo(p[10][0] + size, p[10][1]);
22
+ path.lineTo(p[3][0] + size, p[3][1]);
23
+ path.lineTo(...p[3]);
24
+ path.closePath();
25
+
26
+ path.moveTo(...p[10]);
27
+ path.lineTo(...p[11]);
28
+
29
+ path.moveTo(p[3][0] + size / 2, p[8][1]);
30
+ path.arc(p[3][0], p[8][1], size / 2, 0, Math.PI * 2);
31
+ path.closePath();
32
+
33
+ return path;
34
+ }
35
+
36
+ const center = coordinate.getCenter();
37
+ const [x, y] = center;
38
+
39
+ const radiusQ3 = dist(center, p[3]);
40
+ const radiusMedian = dist(center, p[8]);
41
+ const radiusQ1 = dist(center, p[10]);
42
+
43
+ const middleAngle = angle(sub(p[2], center));
44
+ const rectAngle = Math.asin(size / radiusMedian);
45
+ const startAngle = middleAngle - rectAngle;
46
+ const endAngle = middleAngle + rectAngle;
47
+
48
+ path.moveTo(...p[2]);
49
+ path.lineTo(...p[3]);
50
+
51
+ path.moveTo(
52
+ Math.cos(startAngle) * radiusQ3 + x,
53
+ Math.sin(startAngle) * radiusQ3 + y,
54
+ );
55
+ path.arc(x, y, radiusQ3, startAngle, endAngle);
56
+ path.lineTo(
57
+ Math.cos(endAngle) * radiusQ1 + x,
58
+ Math.sin(endAngle) * radiusQ1 + y,
59
+ );
60
+ path.arc(x, y, radiusQ1, endAngle, startAngle, true);
61
+ path.lineTo(
62
+ Math.cos(startAngle) * radiusQ3 + x,
63
+ Math.sin(startAngle) * radiusQ3 + y,
64
+ );
65
+ path.closePath();
66
+
67
+ path.moveTo(...p[10]);
68
+ path.lineTo(...p[11]);
69
+
70
+ const a = (startAngle + endAngle) / 2;
71
+
72
+ path.moveTo(
73
+ Math.cos(a) * (radiusMedian + size / 2) + x,
74
+ Math.sin(a) * (radiusMedian + size / 2) + y,
75
+ );
76
+ path.arc(
77
+ Math.cos(a) * radiusMedian + x,
78
+ Math.sin(a) * radiusMedian + y,
79
+ size / 2,
80
+ a,
81
+ Math.PI * 2 + a,
82
+ );
83
+ path.closePath();
84
+
85
+ return path;
86
+ }
87
+
88
+ export const Violin: SC<ViolinOptions> = (options) => {
89
+ const { ...style } = options;
90
+ return (points, value, coordinate, theme) => {
91
+ const { mark, shape, defaultShape, color, transform } = value;
92
+ // TODO: how to setting it by size channel.
93
+ const size = 4;
94
+ const {
95
+ defaultColor,
96
+ fill = defaultColor,
97
+ stroke = defaultColor,
98
+ ...shapeTheme
99
+ } = getShapeTheme(theme, mark, shape, defaultShape);
100
+
101
+ const path = getPath(points, coordinate, size);
102
+
103
+ return select(new GPath())
104
+ .call(applyStyle, shapeTheme)
105
+ .style('d', path.toString())
106
+ .style('stroke', stroke)
107
+ .style('fill', color || fill)
108
+ .style('transform', transform)
109
+ .call(applyStyle, style)
110
+ .node();
111
+ };
112
+ };
113
+
114
+ Violin.props = {
115
+ defaultMarker: 'point',
116
+ defaultEnterAnimation: 'fadeIn',
117
+ defaultUpdateAnimation: 'morphing',
118
+ defaultExitAnimation: 'fadeOut',
119
+ };
@@ -0,0 +1,52 @@
1
+ import { path as d3path } from 'd3-path';
2
+ import { Path } from '@antv/g';
3
+ import { applyStyle, getShapeTheme } from '../utils';
4
+ import { select } from '../../utils/selection';
5
+ import { ShapeComponent as SC } from '../../runtime';
6
+
7
+ export type DensityOptions = {
8
+ colorAttribute: 'fill' | 'stroke';
9
+ };
10
+
11
+ /**
12
+ * Draw density shape.
13
+ */
14
+ export const Density: SC<DensityOptions> = (options) => {
15
+ const { ...style } = options;
16
+ return (points, value, coordinate, theme) => {
17
+ const { mark, shape, defaultShape, transform } = value;
18
+ const { defaultColor, ...shapeTheme } = getShapeTheme(
19
+ theme,
20
+ mark,
21
+ shape,
22
+ defaultShape,
23
+ );
24
+ const { color = defaultColor } = value;
25
+ const [first, ...p] = points;
26
+
27
+ // todo smooth, hollow
28
+ const path = d3path();
29
+ path.moveTo(...first);
30
+ p.forEach(([x, y]) => {
31
+ path.lineTo(x, y);
32
+ });
33
+ path.closePath();
34
+
35
+ return select(new Path())
36
+ .call(applyStyle, shapeTheme)
37
+ .style('d', path.toString())
38
+ .style('stroke', color || defaultColor) // Always has stroke color.
39
+ .style('fill', color || defaultColor)
40
+ .style('fillOpacity', 0.4)
41
+ .style('transform', transform)
42
+ .call(applyStyle, style)
43
+ .node();
44
+ };
45
+ };
46
+
47
+ Density.props = {
48
+ defaultMarker: 'square',
49
+ defaultEnterAnimation: 'fadeIn',
50
+ defaultUpdateAnimation: 'morphing',
51
+ defaultExitAnimation: 'fadeOut',
52
+ };
@@ -42,11 +42,13 @@ export { Image } from './image/image';
42
42
  export { Polygon } from './polygon/polygon';
43
43
  export { Ribbon } from './polygon/ribbon';
44
44
  export { Box } from './box/box';
45
+ export { Violin } from './box/violin';
45
46
  export { Line as LineXY } from './lineXY/line';
46
47
  export { Connector } from './connector/connector';
47
48
  export { Label } from './label/label';
48
49
  export { Path } from './path/path';
49
50
  export { Hollow as HollowPath } from './path/hollow';
51
+ export { Density } from './density/density';
50
52
  export { Shape } from './shape/shape';
51
53
 
52
54
  export type { RectOptions } from './interval/rect';
@@ -93,10 +95,12 @@ export type { LinkOptions } from './link/link';
93
95
  export type { ImageOptions } from './image/image';
94
96
  export type { PolygonOptions } from './polygon/polygon';
95
97
  export type { BoxOptions } from './box/box';
98
+ export type { ViolinOptions } from './box/violin';
96
99
 
97
100
  export type { LineOptions as LineXYOptions } from './lineXY/line';
98
101
  export type { ConnectorOptions } from './connector/connector';
99
102
 
100
103
  export type { LabelOptions } from './label/label';
104
+ export type { DensityOptions } from './density/density';
101
105
 
102
106
  export type { ShapeOptions } from './shape/shape';
@@ -5,7 +5,7 @@ import { Transform } from './transform';
5
5
  import { Scale } from './scale';
6
6
  import { Data } from './data';
7
7
  import { LabelTransform } from './labelTransform';
8
- import { Literal2Object } from './utils';
8
+ import { Literal2Object, Padding } from './utils';
9
9
  import { TitleComponent } from './component';
10
10
  import { Mark } from './mark';
11
11
 
@@ -40,11 +40,11 @@ export type ViewComposition = {
40
40
  data?: Data;
41
41
  key?: string;
42
42
  class?: string;
43
- padding?: number;
44
- paddingLeft?: number;
45
- paddingRight?: number;
46
- paddingTop?: number;
47
- paddingBottom?: number;
43
+ padding?: Padding;
44
+ paddingLeft?: Padding;
45
+ paddingRight?: Padding;
46
+ paddingTop?: Padding;
47
+ paddingBottom?: Padding;
48
48
  margin?: number;
49
49
  marginLeft?: number;
50
50
  marginBottom?: number;
@@ -99,7 +99,7 @@ export type SpaceFlexComposition = {
99
99
  data?: Data;
100
100
  direction?: 'col' | 'row';
101
101
  ratio?: number[];
102
- padding?: number;
102
+ padding?: Padding;
103
103
  children?: Node[];
104
104
  };
105
105
 
@@ -118,10 +118,11 @@ export type FacetRectComposition = {
118
118
  type?: 'facetRect';
119
119
  transform?: Transform;
120
120
  data?: Data;
121
- paddingLeft?: number;
122
- paddingRight?: number;
123
- paddingTop?: number;
124
- paddingBottom?: number;
121
+ padding?: Padding;
122
+ paddingLeft?: Padding;
123
+ paddingRight?: Padding;
124
+ paddingTop?: Padding;
125
+ paddingBottom?: Padding;
125
126
  margin?: number;
126
127
  marginLeft?: number;
127
128
  marginBottom?: number;
@@ -149,10 +150,11 @@ export type FacetRectComposition = {
149
150
 
150
151
  export type RepeatMatrixComposition = {
151
152
  type?: 'repeatMatrix';
152
- paddingLeft?: number;
153
- paddingRight?: number;
154
- paddingTop?: number;
155
- paddingBottom?: number;
153
+ padding?: Padding;
154
+ paddingLeft?: Padding;
155
+ paddingRight?: Padding;
156
+ paddingTop?: Padding;
157
+ paddingBottom?: Padding;
156
158
  margin?: number;
157
159
  marginLeft?: number;
158
160
  marginBottom?: number;
@@ -181,10 +183,11 @@ export type RepeatMatrixComposition = {
181
183
 
182
184
  export type FacetCircleComposition = {
183
185
  type?: 'facetCircle';
184
- paddingLeft?: number;
185
- paddingRight?: number;
186
- paddingTop?: number;
187
- paddingBottom?: number;
186
+ padding?: Padding;
187
+ paddingLeft?: Padding;
188
+ paddingRight?: Padding;
189
+ paddingTop?: Padding;
190
+ paddingBottom?: Padding;
188
191
  margin?: number;
189
192
  marginLeft?: number;
190
193
  marginBottom?: number;
@@ -10,6 +10,7 @@ export type DataTransform =
10
10
  | FilterDataTransform
11
11
  | MapTransform
12
12
  | SliceTransform
13
+ | KDEDataTransform
13
14
  | CustomTransform;
14
15
 
15
16
  export type DataTransformTypes =
@@ -22,6 +23,7 @@ export type DataTransformTypes =
22
23
  | 'filter'
23
24
  | 'map'
24
25
  | 'slice'
26
+ | 'kde'
25
27
  | 'custom'
26
28
  | DataComponent;
27
29
 
@@ -119,6 +121,39 @@ export type CustomDataTransform = {
119
121
  callback?: (d: any) => any;
120
122
  };
121
123
 
124
+ export type KDEDataTransform = {
125
+ type?: 'kde';
126
+ /**
127
+ * Kernel Density Estimation field.
128
+ */
129
+ field: string;
130
+ /**
131
+ * Group data by fields.
132
+ */
133
+ groupBy: string[];
134
+ /**
135
+ * Generate new fieds, default: ['y', 'size']
136
+ */
137
+ as?: ['y', 'size'];
138
+ /**
139
+ * Defaults to smallest value in the array minus some threshold.
140
+ */
141
+ min?: number;
142
+ /**
143
+ * Defaults to largest value in the array plus some threshold.
144
+ */
145
+ max?: number;
146
+ /**
147
+ * Number of points to represent the pdf. Defaults to 10.
148
+ */
149
+ size?: number;
150
+ /**
151
+ * Determine how many points to the left and right does an element affect,
152
+ * similar to bandwidth in kernel density estimation. Defaults to 2.
153
+ */
154
+ width?: number;
155
+ };
156
+
122
157
  export type CustomTransform = {
123
158
  type?: DataComponent;
124
159
  [key: string]: any;
package/src/spec/mark.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  TitleComponent,
16
16
  TooltipComponent,
17
17
  } from './component';
18
- import { Closeable, Literal2Object } from './utils';
18
+ import { Closeable, Literal2Object, Padding } from './utils';
19
19
 
20
20
  export type Mark =
21
21
  | IntervalMark
@@ -46,6 +46,7 @@ export type Mark =
46
46
  | ForceGraphMark
47
47
  | TreeMark
48
48
  | WordCloudMark
49
+ | DensityMark
49
50
  | CustomMark;
50
51
 
51
52
  export type MarkTypes =
@@ -79,6 +80,7 @@ export type MarkTypes =
79
80
  | 'tree'
80
81
  | 'wordCloud'
81
82
  | 'gauge'
83
+ | 'density'
82
84
  | MarkComponent;
83
85
 
84
86
  export type ChannelTypes =
@@ -121,11 +123,11 @@ export type BaseMark<T extends MarkTypes, C extends string = ChannelTypes> = {
121
123
  y?: number;
122
124
  width?: number;
123
125
  height?: number;
124
- paddingLeft?: number;
125
- paddingRight?: number;
126
- paddingBottom?: number;
127
- paddingTop?: number;
128
- padding?: number;
126
+ paddingLeft?: Padding;
127
+ paddingRight?: Padding;
128
+ paddingBottom?: Padding;
129
+ paddingTop?: Padding;
130
+ padding?: Padding;
129
131
  inset?: number;
130
132
  insetLeft?: number;
131
133
  insetBottom?: number;
@@ -370,4 +372,6 @@ export type GaugeMark = BaseMark<
370
372
  | ChannelTypes
371
373
  >;
372
374
 
375
+ export type DensityMark = BaseMark<'density', ChannelTypes | 'series'>;
376
+
373
377
  export type CustomMark = BaseMark<MarkComponent, ChannelTypes>;
@@ -50,6 +50,7 @@ export type TransformTypes =
50
50
  | 'pack'
51
51
  | 'sample'
52
52
  | 'filter'
53
+ | 'kde'
53
54
  | TransformComponent;
54
55
 
55
56
  export type TransformOrder =
package/src/spec/utils.ts CHANGED
@@ -14,3 +14,5 @@ export type UsePrefix<
14
14
  };
15
15
 
16
16
  export type Closeable<T> = T | boolean | null;
17
+
18
+ export type Padding = number | 'auto';
@@ -39,6 +39,7 @@ import {
39
39
  ForceGraph,
40
40
  Tree as TreeGeometry,
41
41
  WordCloud as WordCloudGeometry,
42
+ Density as DensityGeometry,
42
43
  Gauge,
43
44
  } from '../mark';
44
45
  import { Category10, Category20 } from '../palette';
@@ -101,6 +102,7 @@ import {
101
102
  Polygon,
102
103
  Ribbon,
103
104
  Box,
105
+ Violin,
104
106
  Funnel,
105
107
  Pyramid,
106
108
  Vector,
@@ -108,6 +110,7 @@ import {
108
110
  Label,
109
111
  Path as PathShape,
110
112
  HollowPath,
113
+ Density as DensityShape,
111
114
  Shape as CustomShape,
112
115
  } from '../shape';
113
116
  import { Classic, ClassicDark, Academy } from '../theme';
@@ -238,6 +241,7 @@ import {
238
241
  WordCloud,
239
242
  Join,
240
243
  Sort,
244
+ KDE,
241
245
  } from '../data';
242
246
  import {
243
247
  OverlapDodgeY,
@@ -266,6 +270,7 @@ export function createLibrary(): G2Library {
266
270
  'data.arc': ArcTransform,
267
271
  'data.wordCloud': WordCloud,
268
272
  'data.join': Join,
273
+ 'data.kde': KDE,
269
274
  // 'transform.voronoi': Voronoi,
270
275
  'transform.maybeZeroY1': MaybeZeroY1,
271
276
  'transform.maybeZeroX': MaybeZeroX,
@@ -350,6 +355,7 @@ export function createLibrary(): G2Library {
350
355
  'mark.gauge': Gauge,
351
356
  'mark.tree': TreeGeometry,
352
357
  'mark.wordCloud': WordCloudGeometry,
358
+ 'mark.density': DensityGeometry,
353
359
  'palette.category10': Category10,
354
360
  'palette.category20': Category20,
355
361
  'scale.linear': Linear,
@@ -412,6 +418,7 @@ export function createLibrary(): G2Library {
412
418
  'shape.polygon.polygon': Polygon,
413
419
  'shape.polygon.ribbon': Ribbon,
414
420
  'shape.box.box': Box,
421
+ 'shape.box.violin': Violin,
415
422
  'shape.vector.vector': Vector,
416
423
  'shape.label.label': Label,
417
424
  'shape.text.badge': Badge,
@@ -424,6 +431,7 @@ export function createLibrary(): G2Library {
424
431
  'shape.rangeY.range': RectShape,
425
432
  'shape.path.path': PathShape,
426
433
  'shape.path.hollow': HollowPath,
434
+ 'shape.density.density': DensityShape,
427
435
  'theme.classic': Classic,
428
436
  'theme.classicDark': ClassicDark,
429
437
  'theme.academy': Academy,
@@ -253,11 +253,12 @@ export const Academy: TC<AcademyOptions> = (options) => {
253
253
  titlePosition: 'top',
254
254
  titleSpacing: 0,
255
255
  titleTextBaseline: 'middle',
256
+ labelSpacing: 4,
256
257
  },
257
258
  axisBottom: {
258
259
  gridDirection: 'negative',
259
260
  labelDirection: 'positive',
260
- labelSpacing: 8,
261
+ labelSpacing: 4,
261
262
  tickDirection: 'positive',
262
263
  titlePosition: 'bottom',
263
264
  titleSpacing: 4,
@@ -249,7 +249,7 @@ export const Classic: TC<ClassicOptions> = (options) => {
249
249
  axisTop: {
250
250
  gridDirection: 'positive',
251
251
  labelDirection: 'negative',
252
- labelSpacing: 8,
252
+ labelSpacing: 4,
253
253
  tickDirection: 'negative',
254
254
  titlePosition: 'top',
255
255
  titleSpacing: 0,
@@ -258,7 +258,7 @@ export const Classic: TC<ClassicOptions> = (options) => {
258
258
  axisBottom: {
259
259
  gridDirection: 'negative',
260
260
  labelDirection: 'positive',
261
- labelSpacing: 12,
261
+ labelSpacing: 4,
262
262
  labelAutoRotate: true,
263
263
  tickDirection: 'positive',
264
264
  titlePosition: 'bottom',
@@ -248,7 +248,7 @@ export const ClassicDark: TC<ClassicDarkOptions> = (options) => {
248
248
  axisTop: {
249
249
  gridDirection: 'positive',
250
250
  labelDirection: 'negative',
251
- labelSpacing: 8,
251
+ labelSpacing: 4,
252
252
  tickDirection: 'negative',
253
253
  titlePosition: 'top',
254
254
  titleSpacing: 0,
@@ -257,7 +257,7 @@ export const ClassicDark: TC<ClassicDarkOptions> = (options) => {
257
257
  axisBottom: {
258
258
  gridDirection: 'negative',
259
259
  labelDirection: 'positive',
260
- labelSpacing: 12,
260
+ labelSpacing: 4,
261
261
  tickDirection: 'positive',
262
262
  titlePosition: 'bottom',
263
263
  titleSpacing: 10,
@@ -47,7 +47,11 @@ export function pixelsOf(selection, scale, coordinate) {
47
47
  const [[minX, maxX], [minY, maxY]] = selection;
48
48
  const { x: scaleX, y: scaleY } = scale;
49
49
  const p0 = [scaleX.map(minX), scaleY.map(minY)];
50
- const p1 = [scaleX.map(maxX), scaleY.map(maxY)];
50
+ const maybeStep = (scale) => (scale.getStep ? scale.getStep() : 0);
51
+ const p1 = [
52
+ scaleX.map(maxX) + maybeStep(scaleX),
53
+ scaleY.map(maxY) + maybeStep(scaleY),
54
+ ];
51
55
  const [x, y] = coordinate.map(p0);
52
56
  const [x1, y1] = coordinate.map(p1);
53
57
  return [x, y, x1, y1];