@antv/g2 5.0.9 → 5.0.11

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 (182) hide show
  1. package/dist/g2-lite.min.js +3 -3
  2. package/dist/g2.min.js +3 -3
  3. package/esm/api/chart.d.ts +1 -0
  4. package/esm/api/chart.js +5 -1
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/composition/geoPath.d.ts +1 -0
  7. package/esm/api/composition/geoPath.js +1 -0
  8. package/esm/api/composition/geoPath.js.map +1 -1
  9. package/esm/api/mark/index.d.ts +3 -1
  10. package/esm/api/mark/index.js +2 -1
  11. package/esm/api/mark/index.js.map +1 -1
  12. package/esm/api/mark/mark.d.ts +7 -1
  13. package/esm/api/mark/mark.js +9 -0
  14. package/esm/api/mark/mark.js.map +1 -1
  15. package/esm/api/node.d.ts +1 -0
  16. package/esm/api/node.js +5 -1
  17. package/esm/api/node.js.map +1 -1
  18. package/esm/api/utils.d.ts +1 -1
  19. package/esm/api/utils.js +10 -6
  20. package/esm/api/utils.js.map +1 -1
  21. package/esm/geo/geoPath.js +2 -1
  22. package/esm/geo/geoPath.js.map +1 -1
  23. package/esm/index.d.ts +1 -1
  24. package/esm/index.js +3 -1
  25. package/esm/index.js.map +1 -1
  26. package/esm/interaction/event.js +5 -2
  27. package/esm/interaction/event.js.map +1 -1
  28. package/esm/mark/area.js +1 -1
  29. package/esm/mark/area.js.map +1 -1
  30. package/esm/mark/heatmap.d.ts +7 -0
  31. package/esm/mark/heatmap.js +36 -0
  32. package/esm/mark/heatmap.js.map +1 -0
  33. package/esm/mark/index.d.ts +2 -0
  34. package/esm/mark/index.js +1 -0
  35. package/esm/mark/index.js.map +1 -1
  36. package/esm/mark/line.js +1 -1
  37. package/esm/mark/line.js.map +1 -1
  38. package/esm/mark/text.js +0 -2
  39. package/esm/mark/text.js.map +1 -1
  40. package/esm/mark/utils.d.ts +1 -0
  41. package/esm/mark/utils.js +1 -1
  42. package/esm/mark/utils.js.map +1 -1
  43. package/esm/runtime/plot.js +12 -10
  44. package/esm/runtime/plot.js.map +1 -1
  45. package/esm/runtime/render.d.ts +2 -2
  46. package/esm/runtime/render.js +7 -3
  47. package/esm/runtime/render.js.map +1 -1
  48. package/esm/runtime/scale.js +11 -9
  49. package/esm/runtime/scale.js.map +1 -1
  50. package/esm/runtime/types/component.d.ts +2 -2
  51. package/esm/runtime/types/options.d.ts +6 -0
  52. package/esm/shape/heatmap/heatmap.d.ts +4 -0
  53. package/esm/shape/heatmap/heatmap.js +70 -0
  54. package/esm/shape/heatmap/heatmap.js.map +1 -0
  55. package/esm/shape/heatmap/renderer/gradient.d.ts +5 -0
  56. package/esm/shape/heatmap/renderer/gradient.js +13 -0
  57. package/esm/shape/heatmap/renderer/gradient.js.map +1 -0
  58. package/esm/shape/heatmap/renderer/index.d.ts +6 -0
  59. package/esm/shape/heatmap/renderer/index.js +122 -0
  60. package/esm/shape/heatmap/renderer/index.js.map +1 -0
  61. package/esm/shape/heatmap/renderer/types.d.ts +36 -0
  62. package/esm/shape/heatmap/renderer/types.js +2 -0
  63. package/esm/shape/heatmap/renderer/types.js.map +1 -0
  64. package/esm/shape/image/image.js +8 -3
  65. package/esm/shape/image/image.js.map +1 -1
  66. package/esm/shape/index.d.ts +2 -0
  67. package/esm/shape/index.js +1 -0
  68. package/esm/shape/index.js.map +1 -1
  69. package/esm/spec/component.d.ts +1 -1
  70. package/esm/spec/composition.d.ts +9 -6
  71. package/esm/spec/mark.d.ts +2 -1
  72. package/esm/stdlib/index.js +3 -3
  73. package/esm/stdlib/index.js.map +1 -1
  74. package/esm/utils/lru.d.ts +4 -0
  75. package/esm/utils/lru.js +18 -0
  76. package/esm/utils/lru.js.map +1 -0
  77. package/lib/api/chart.d.ts +1 -0
  78. package/lib/api/chart.js +5 -1
  79. package/lib/api/chart.js.map +1 -1
  80. package/lib/api/composition/geoPath.d.ts +1 -0
  81. package/lib/api/composition/geoPath.js +1 -0
  82. package/lib/api/composition/geoPath.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 +5 -1
  91. package/lib/api/node.js.map +1 -1
  92. package/lib/api/utils.d.ts +1 -1
  93. package/lib/api/utils.js +10 -6
  94. package/lib/api/utils.js.map +1 -1
  95. package/lib/geo/geoPath.js +2 -1
  96. package/lib/geo/geoPath.js.map +1 -1
  97. package/lib/index.d.ts +1 -1
  98. package/lib/index.js +3 -1
  99. package/lib/index.js.map +1 -1
  100. package/lib/interaction/event.js +5 -2
  101. package/lib/interaction/event.js.map +1 -1
  102. package/lib/mark/area.js +1 -1
  103. package/lib/mark/area.js.map +1 -1
  104. package/lib/mark/heatmap.d.ts +7 -0
  105. package/lib/mark/heatmap.js +40 -0
  106. package/lib/mark/heatmap.js.map +1 -0
  107. package/lib/mark/index.d.ts +2 -0
  108. package/lib/mark/index.js +3 -1
  109. package/lib/mark/index.js.map +1 -1
  110. package/lib/mark/line.js +1 -1
  111. package/lib/mark/line.js.map +1 -1
  112. package/lib/mark/text.js +0 -2
  113. package/lib/mark/text.js.map +1 -1
  114. package/lib/mark/utils.d.ts +1 -0
  115. package/lib/mark/utils.js +2 -1
  116. package/lib/mark/utils.js.map +1 -1
  117. package/lib/runtime/plot.js +12 -10
  118. package/lib/runtime/plot.js.map +1 -1
  119. package/lib/runtime/render.d.ts +2 -2
  120. package/lib/runtime/render.js +7 -3
  121. package/lib/runtime/render.js.map +1 -1
  122. package/lib/runtime/scale.js +11 -9
  123. package/lib/runtime/scale.js.map +1 -1
  124. package/lib/runtime/types/component.d.ts +2 -2
  125. package/lib/runtime/types/options.d.ts +6 -0
  126. package/lib/shape/heatmap/heatmap.d.ts +4 -0
  127. package/lib/shape/heatmap/heatmap.js +74 -0
  128. package/lib/shape/heatmap/heatmap.js.map +1 -0
  129. package/lib/shape/heatmap/renderer/gradient.d.ts +5 -0
  130. package/lib/shape/heatmap/renderer/gradient.js +17 -0
  131. package/lib/shape/heatmap/renderer/gradient.js.map +1 -0
  132. package/lib/shape/heatmap/renderer/index.d.ts +6 -0
  133. package/lib/shape/heatmap/renderer/index.js +126 -0
  134. package/lib/shape/heatmap/renderer/index.js.map +1 -0
  135. package/lib/shape/heatmap/renderer/types.d.ts +36 -0
  136. package/lib/shape/heatmap/renderer/types.js +3 -0
  137. package/lib/shape/heatmap/renderer/types.js.map +1 -0
  138. package/lib/shape/image/image.js +8 -3
  139. package/lib/shape/image/image.js.map +1 -1
  140. package/lib/shape/index.d.ts +2 -0
  141. package/lib/shape/index.js +3 -1
  142. package/lib/shape/index.js.map +1 -1
  143. package/lib/spec/component.d.ts +1 -1
  144. package/lib/spec/composition.d.ts +9 -6
  145. package/lib/spec/mark.d.ts +2 -1
  146. package/lib/stdlib/index.js +1 -1
  147. package/lib/stdlib/index.js.map +1 -1
  148. package/lib/utils/lru.d.ts +4 -0
  149. package/lib/utils/lru.js +25 -0
  150. package/lib/utils/lru.js.map +1 -0
  151. package/package.json +4 -1
  152. package/src/api/chart.ts +4 -1
  153. package/src/api/composition/geoPath.ts +2 -0
  154. package/src/api/mark/index.ts +3 -0
  155. package/src/api/mark/mark.ts +12 -0
  156. package/src/api/node.ts +6 -1
  157. package/src/api/utils.ts +17 -6
  158. package/src/geo/geoPath.ts +3 -1
  159. package/src/index.ts +10 -1
  160. package/src/interaction/event.ts +7 -2
  161. package/src/mark/area.ts +1 -1
  162. package/src/mark/heatmap.ts +48 -0
  163. package/src/mark/index.ts +2 -0
  164. package/src/mark/line.ts +1 -1
  165. package/src/mark/text.ts +0 -1
  166. package/src/mark/utils.ts +1 -1
  167. package/src/runtime/plot.ts +30 -13
  168. package/src/runtime/render.ts +9 -7
  169. package/src/runtime/scale.ts +14 -9
  170. package/src/runtime/types/component.ts +2 -1
  171. package/src/runtime/types/options.ts +6 -0
  172. package/src/shape/heatmap/heatmap.ts +85 -0
  173. package/src/shape/heatmap/renderer/gradient.ts +16 -0
  174. package/src/shape/heatmap/renderer/index.ts +193 -0
  175. package/src/shape/heatmap/renderer/types.ts +38 -0
  176. package/src/shape/image/image.ts +9 -3
  177. package/src/shape/index.ts +2 -0
  178. package/src/spec/component.ts +2 -1
  179. package/src/spec/composition.ts +24 -8
  180. package/src/spec/mark.ts +2 -0
  181. package/src/stdlib/index.ts +4 -0
  182. package/src/utils/lru.ts +25 -0
package/src/api/chart.ts CHANGED
@@ -96,6 +96,7 @@ export class Chart extends View<ChartOptions> {
96
96
  private _trailing = false;
97
97
  private _trailingResolve = null;
98
98
  private _trailingReject = null;
99
+ private _previousDefinedType = null;
99
100
 
100
101
  constructor(options: ChartOptions) {
101
102
  const { container, canvas, renderer, plugins, ...rest } = options || {};
@@ -146,7 +147,9 @@ export class Chart extends View<ChartOptions> {
146
147
  */
147
148
  options(options?: G2ViewTree): Chart | G2ViewTree {
148
149
  if (arguments.length === 0) return optionsOf(this);
149
- updateRoot(this, options);
150
+ const { type } = options;
151
+ if (type) this._previousDefinedType = type;
152
+ updateRoot(this, options, this._previousDefinedType);
150
153
  return this;
151
154
  }
152
155
 
@@ -15,6 +15,7 @@ export interface GeoPath extends Mark, Composition {
15
15
  scale: ObjectAttribute<GeoPathSpec['scale'], GeoPath>;
16
16
  encode: ObjectAttribute<GeoPathSpec['encode'], GeoPath>;
17
17
  legend: ObjectAttribute<GeoPathSpec['legend'], GeoPath>;
18
+ state: ObjectAttribute<GeoPathSpec['state'], GeoPath>;
18
19
  }
19
20
 
20
21
  @defineProps([
@@ -26,6 +27,7 @@ export interface GeoPath extends Mark, Composition {
26
27
  { type: 'object', name: 'scale' },
27
28
  { type: 'object', name: 'encode' },
28
29
  { type: 'object', name: 'legend' },
30
+ { type: 'object', name: 'state' },
29
31
  ...nodeProps(mark),
30
32
  ])
31
33
  export class GeoPath extends CompositionNode<GeoPathComposition> {
@@ -22,6 +22,7 @@ import {
22
22
  Treemap,
23
23
  Boxplot,
24
24
  Density,
25
+ Heatmap,
25
26
  Shape,
26
27
  Pack,
27
28
  ForceGraph,
@@ -55,6 +56,7 @@ export interface Mark {
55
56
  treemap(): Treemap;
56
57
  boxplot(): Boxplot;
57
58
  density(): Density;
59
+ heatmap(): Heatmap;
58
60
  shape(): Shape;
59
61
  pack(): Pack;
60
62
  forceGraph(): ForceGraph;
@@ -89,6 +91,7 @@ export const mark = {
89
91
  treemap: Treemap,
90
92
  boxplot: Boxplot,
91
93
  density: Density,
94
+ heatmap: Heatmap,
92
95
  shape: Shape,
93
96
  pack: Pack,
94
97
  forceGraph: ForceGraph,
@@ -20,6 +20,7 @@ import {
20
20
  SankeyMark,
21
21
  BoxPlotMark,
22
22
  DensityMark,
23
+ HeatmapMark,
23
24
  ShapeMark,
24
25
  TreemapMark,
25
26
  ForceGraphMark,
@@ -118,6 +119,10 @@ export interface Density extends API<Concrete<DensityMark>, Density> {
118
119
  type: 'density';
119
120
  }
120
121
 
122
+ export interface Heatmap extends API<Concrete<HeatmapMark>, Heatmap> {
123
+ type: 'heatmap';
124
+ }
125
+
121
126
  export interface Shape extends API<Concrete<ShapeMark>, Shape> {
122
127
  type: 'shape';
123
128
  }
@@ -335,6 +340,13 @@ export class Density extends MarkNode<DensityMark> {
335
340
  }
336
341
  }
337
342
 
343
+ @defineProps(props)
344
+ export class Heatmap extends MarkNode<HeatmapMark> {
345
+ constructor() {
346
+ super({}, 'heatmap');
347
+ }
348
+ }
349
+
338
350
  @defineProps([...props, { name: 'layout', type: 'value' }])
339
351
  export class Pack extends MarkNode<PackMark> {
340
352
  constructor() {
package/src/api/node.ts CHANGED
@@ -74,10 +74,15 @@ export class Node<
74
74
  ): Node<ChildValue, Value> {
75
75
  const node = new Ctor({});
76
76
  node.children = [];
77
+ this.push(node);
78
+ return node;
79
+ }
80
+
81
+ push(node: Node<ChildValue, Value>): this {
77
82
  node.parentNode = this;
78
83
  node.index = this.children.length;
79
84
  this.children.push(node);
80
- return node;
85
+ return this;
81
86
  }
82
87
 
83
88
  /**
package/src/api/utils.ts CHANGED
@@ -93,9 +93,13 @@ function isMark(type: string): boolean {
93
93
  return new Set(Object.keys(mark)).has(type);
94
94
  }
95
95
 
96
- function normalizeRootOptions(node: Node, options: G2ViewTree) {
96
+ function normalizeRootOptions(
97
+ node: Node,
98
+ options: G2ViewTree,
99
+ previousType: string,
100
+ ) {
97
101
  const { type: oldType } = node;
98
- const { type = oldType } = options;
102
+ const { type = previousType || oldType } = options;
99
103
  if (type === 'view') return options;
100
104
  if (typeof type !== 'string') return options;
101
105
  if (!isMark(type)) return options;
@@ -147,7 +151,7 @@ function appendNode(parent: Node, newOptions: G2ViewTree) {
147
151
  while (discovered.length) {
148
152
  const [parent, nodeOptions] = discovered.shift();
149
153
  const node = createNode(nodeOptions);
150
- if (Array.isArray(parent.children)) parent.children.push(node);
154
+ if (Array.isArray(parent.children)) parent.push(node);
151
155
  const { children } = nodeOptions;
152
156
  if (Array.isArray(children)) {
153
157
  for (const child of children) {
@@ -158,14 +162,20 @@ function appendNode(parent: Node, newOptions: G2ViewTree) {
158
162
  }
159
163
 
160
164
  // Update node tree from options.
161
- export function updateRoot(node: Node, options: G2ViewTree) {
162
- const rootOptions = normalizeRootOptions(node, options);
165
+ export function updateRoot(
166
+ node: Node,
167
+ options: G2ViewTree,
168
+ definedType: string,
169
+ ) {
170
+ const rootOptions = normalizeRootOptions(node, options, definedType);
163
171
  const discovered: [Node, Node, G2ViewTree][] = [[null, node, rootOptions]];
164
172
  while (discovered.length) {
165
173
  const [parent, oldNode, newNode] = discovered.shift();
166
174
  // If there is no oldNode, create a node tree directly.
167
175
  if (!oldNode) {
168
176
  appendNode(parent, newNode);
177
+ } else if (!newNode) {
178
+ oldNode.remove();
169
179
  } else {
170
180
  updateNode(oldNode, newNode);
171
181
  const { children: newChildren } = newNode;
@@ -173,7 +183,8 @@ export function updateRoot(node: Node, options: G2ViewTree) {
173
183
  if (Array.isArray(newChildren) && Array.isArray(oldChildren)) {
174
184
  // Only update node specified in newChildren,
175
185
  // the extra oldChildren will remain still.
176
- for (let i = 0; i < newChildren.length; i++) {
186
+ const n = Math.max(newChildren.length, oldChildren.length);
187
+ for (let i = 0; i < n; i++) {
177
188
  const newChild = newChildren[i];
178
189
  const oldChild = oldChildren[i];
179
190
  discovered.push([oldNode, oldChild, newChild]);
@@ -8,7 +8,8 @@ export type GeoPathOptions = Omit<GeoPathComposition, 'type'>;
8
8
  */
9
9
  export const GeoPath: CC<GeoPathOptions> = () => {
10
10
  return (options) => {
11
- const { type, data, scale, encode, style, animate, key, ...rest } = options;
11
+ const { type, data, scale, encode, style, animate, key, state, ...rest } =
12
+ options;
12
13
  return [
13
14
  {
14
15
  type: 'geoView',
@@ -24,6 +25,7 @@ export const GeoPath: CC<GeoPathOptions> = () => {
24
25
  encode,
25
26
  style,
26
27
  animate,
28
+ state,
27
29
  },
28
30
  ],
29
31
  },
package/src/index.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import { runtime } from '@antv/g';
2
+
3
+ runtime.enableCSSParsing = false;
4
+
1
5
  export {
2
6
  render,
3
7
  renderToMountedElement,
@@ -10,8 +14,13 @@ export {
10
14
  LABEL_CLASS_NAME,
11
15
  AREA_CLASS_NAME,
12
16
  } from './runtime';
17
+
13
18
  export type { G2Context } from './runtime';
19
+
14
20
  export { createLibrary } from './stdlib';
21
+
15
22
  export { Chart, MarkNode, CompositionNode, register } from './api';
16
- export * from './spec';
23
+
17
24
  export { ChartEvent } from './utils/event';
25
+
26
+ export * from './spec';
@@ -17,12 +17,17 @@ export function dataOf(element, view) {
17
17
  function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
18
18
  return (e) => {
19
19
  if (!predicate(e)) return;
20
- const { target } = e;
21
- const { className: elementType, markType } = target;
22
20
 
23
21
  // Emit plot events.
24
22
  emitter.emit(`plot:${eventType}`, e);
25
23
 
24
+ const { target } = e;
25
+
26
+ // There is no target for pointerupoutside event if out of canvas.
27
+ if (!target) return;
28
+
29
+ const { className: elementType, markType } = target;
30
+
26
31
  // If target area is plot area, do not emit extra events.
27
32
  if (elementType === 'plot') return;
28
33
 
package/src/mark/area.ts CHANGED
@@ -42,7 +42,7 @@ export const Area: MC<AreaOptions> = () => {
42
42
 
43
43
  // Group data by series field.
44
44
  const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
45
- const I = series.map((group) => group[0]);
45
+ const I = series.map((group) => group[0]).filter((i) => i !== undefined);
46
46
 
47
47
  // A group of data corresponds to one area.
48
48
  const P = Array.from(series, (SI) => {
@@ -0,0 +1,48 @@
1
+ import { MarkComponent as MC, Vector2 } from '../runtime';
2
+ import { HeatmapMark } from '../spec';
3
+ import {
4
+ baseGeometryChannels,
5
+ basePostInference,
6
+ basePreInference,
7
+ tooltip2d,
8
+ } from './utils';
9
+
10
+ export type HeatmapOptions = Omit<HeatmapMark, 'type'>;
11
+
12
+ /**
13
+ * Draw heatmap with gradient.
14
+ */
15
+ export const Heatmap: MC<HeatmapOptions> = (options) => {
16
+ return (index, scale, value, coordinate) => {
17
+ const { x: X, y: Y, size: S, color: C } = value;
18
+ const P = Array.from(index, (i) => {
19
+ // Default size = 40.
20
+ const r = S ? +S[i] : 40;
21
+ // Warning: x, y, value, radius.
22
+ return [...coordinate.map([+X[i], +Y[i]]), C[i], r] as unknown as Vector2;
23
+ });
24
+
25
+ return [[0], [P]];
26
+ };
27
+ };
28
+
29
+ const shapes = ['heatmap'];
30
+
31
+ Heatmap.props = {
32
+ defaultShape: 'heatmap',
33
+ defaultLabelShape: 'label',
34
+ composite: false,
35
+ channels: [
36
+ ...baseGeometryChannels({ shapes }),
37
+ { name: 'x', required: true },
38
+ { name: 'y', required: true },
39
+ { name: 'color', scale: 'identity', required: true },
40
+ { name: 'size' },
41
+ ],
42
+ preInference: [
43
+ ...basePreInference(),
44
+ { type: 'maybeZeroY' },
45
+ { type: 'maybeZeroX' },
46
+ ],
47
+ postInference: [...basePostInference(), ...tooltip2d()],
48
+ };
package/src/mark/index.ts CHANGED
@@ -27,6 +27,7 @@ export { Tree } from './tree';
27
27
  export { WordCloud } from './wordCloud';
28
28
  export { Gauge } from './gauge';
29
29
  export { Density } from './density';
30
+ export { Heatmap } from './heatmap';
30
31
 
31
32
  export type { IntervalOptions } from './interval';
32
33
  export type { RectOptions } from './rect';
@@ -55,3 +56,4 @@ export type { TreeOptions } from './tree';
55
56
  export type { WordCloudOptions } from './wordCloud';
56
57
  export type { GaugeOptions } from './gauge';
57
58
  export type { DensityOptions } from './density';
59
+ export type { HeatmapOptions } from './heatmap';
package/src/mark/line.ts CHANGED
@@ -26,7 +26,7 @@ const line: Mark = (index, scale, value, coordinate) => {
26
26
  // Group data into series.
27
27
  // There is only one series without specified series encode.
28
28
  const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
29
- const I = series.map((group) => group[0]);
29
+ const I = series.map((group) => group[0]).filter((i) => i !== undefined);
30
30
 
31
31
  // A group of data corresponds to one line.
32
32
  const xoffset = (x?.getBandWidth?.() || 0) / 2;
package/src/mark/text.ts CHANGED
@@ -15,7 +15,6 @@ export const Text: MC<TextOptions> = (options) => {
15
15
  const { cartesian = false } = options;
16
16
  if (cartesian) return visualMark as Mark;
17
17
  return ((index, scale, value, coordinate) => {
18
- if (cartesian) return visualMark(index, scale, value, coordinate);
19
18
  const { x: X, y: Y } = value;
20
19
  const offset = createBandOffset(scale, value, options);
21
20
  const P = Array.from(index, (i) => {
package/src/mark/utils.ts CHANGED
@@ -96,7 +96,7 @@ export function createBandOffset(
96
96
  };
97
97
  }
98
98
 
99
- function p(d) {
99
+ export function p(d) {
100
100
  return parseFloat(d) / 100;
101
101
  }
102
102
 
@@ -188,12 +188,12 @@ export async function plot<T extends G2ViewTree>(
188
188
  .attr('id', (view) => view.key)
189
189
  .call(applyTranslate)
190
190
  .each(function (view) {
191
- plotView(view, select(this), transitions, library);
191
+ plotView(view, select(this), transitions, library, context);
192
192
  enterContainer.set(view, this);
193
193
  }),
194
194
  (update) =>
195
195
  update.call(applyTranslate).each(function (view) {
196
- plotView(view, select(this), transitions, library);
196
+ plotView(view, select(this), transitions, library, context);
197
197
  updateContainer.set(view, this);
198
198
  }),
199
199
  (exit) =>
@@ -320,7 +320,7 @@ function createUpdateView(
320
320
  return async (newOptions) => {
321
321
  const transitions = [];
322
322
  const [newView, newChildren] = await initializeView(newOptions, library);
323
- plotView(newView, selection, transitions, library);
323
+ plotView(newView, selection, transitions, library, context);
324
324
  updateTooltip(selection, newOptions, newView, library, context);
325
325
  for (const child of newChildren) {
326
326
  plot(child, selection, library, context);
@@ -392,11 +392,13 @@ function bubbleOptions(options: G2View): G2View {
392
392
  const {
393
393
  coordinate: viewCoordinate = {},
394
394
  interaction: viewInteraction = {},
395
+ style: viewStyle = {},
395
396
  marks,
396
397
  ...rest
397
398
  } = options;
398
399
  const markCoordinates = marks.map((d) => d.coordinate || {});
399
400
  const markInteractions = marks.map((d) => d.interaction || {});
401
+ const markViewStyles = marks.map((d) => d.viewStyle || {});
400
402
  const newCoordinate = [...markCoordinates, viewCoordinate].reduceRight(
401
403
  (prev, cur) => deepMix(prev, cur),
402
404
  {},
@@ -405,11 +407,16 @@ function bubbleOptions(options: G2View): G2View {
405
407
  (prev, cur) => deepMix(prev, cur),
406
408
  {},
407
409
  );
410
+ const newStyle = [...markViewStyles, viewStyle].reduce(
411
+ (prev, cur) => deepMix(prev, cur),
412
+ {},
413
+ );
408
414
  return {
409
415
  ...rest,
410
416
  marks,
411
417
  coordinate: newCoordinate,
412
418
  interaction: newInteraction,
419
+ style: newStyle,
413
420
  };
414
421
  }
415
422
 
@@ -671,6 +678,7 @@ async function plotView(
671
678
  selection: Selection,
672
679
  transitions: GAnimation[],
673
680
  library: G2Library,
681
+ context: G2Context,
674
682
  ): Promise<void> {
675
683
  const { components, theme, layout, markState, coordinate, key, style, clip } =
676
684
  view;
@@ -809,7 +817,13 @@ async function plotView(
809
817
  const { data } = state;
810
818
  const { key, class: cls, type } = mark;
811
819
  const viewNode = selection.select(`#${key}`);
812
- const shapeFunction = createMarkShapeFunction(mark, state, view, library);
820
+ const shapeFunction = createMarkShapeFunction(
821
+ mark,
822
+ state,
823
+ view,
824
+ library,
825
+ context,
826
+ );
813
827
  const enterFunction = createEnterFunction(mark, state, view, library);
814
828
  const updateFunction = createUpdateFunction(mark, state, view, library);
815
829
  const exitFunction = createExitFunction(mark, state, view, library);
@@ -1017,14 +1031,16 @@ function getLabels(
1017
1031
  ];
1018
1032
  }
1019
1033
  const selector = normalizeLabelSelector(label);
1020
- const F = SI.map((index: number, i: number) => ({
1021
- ...label,
1022
- key: `${seriesKey[i]}-${labelIndex}`,
1023
- bounds: [points[i]],
1024
- index,
1025
- points,
1026
- dependentElement: element,
1027
- }));
1034
+ const F = SI.filter((_, i) => points[i].every(defined)).map(
1035
+ (index: number, i: number) => ({
1036
+ ...label,
1037
+ key: `${seriesKey[i]}-${labelIndex}`,
1038
+ bounds: [points[i]],
1039
+ index,
1040
+ points,
1041
+ dependentElement: element,
1042
+ }),
1043
+ );
1028
1044
  return selector ? selector(F) : F;
1029
1045
  }
1030
1046
 
@@ -1225,6 +1241,7 @@ function createMarkShapeFunction(
1225
1241
  state: G2MarkState,
1226
1242
  view: G2ViewDescriptor,
1227
1243
  library: G2Library,
1244
+ context: G2Context,
1228
1245
  ): (
1229
1246
  data: Record<string, any>,
1230
1247
  index: number,
@@ -1262,7 +1279,7 @@ function createMarkShapeFunction(
1262
1279
  ...visualStyle,
1263
1280
  type: shapeName(mark, shape),
1264
1281
  });
1265
- return shapeFunction(points, value, coordinate, theme, point2d);
1282
+ return shapeFunction(points, value, coordinate, theme, point2d, context);
1266
1283
  };
1267
1284
  }
1268
1285
 
@@ -65,8 +65,10 @@ function Canvas(width: number, height: number): GCanvas {
65
65
  export function render<T extends G2ViewTree = G2ViewTree>(
66
66
  options: T,
67
67
  context: G2Context = {},
68
- resolve?: () => void,
69
- reject?: (e: Error) => void,
68
+ resolve = (): void => {},
69
+ reject = (e?: any): void => {
70
+ throw e;
71
+ },
70
72
  ): HTMLElement {
71
73
  // Initialize the context if it is not provided.
72
74
  const { width = 640, height = 480, theme } = options;
@@ -113,8 +115,10 @@ export function render<T extends G2ViewTree = G2ViewTree>(
113
115
  export function renderToMountedElement<T extends G2ViewTree = G2ViewTree>(
114
116
  options: T,
115
117
  context: G2Context = {},
116
- resolve?: () => void,
117
- reject?: (e: Error) => void,
118
+ resolve = () => {},
119
+ reject = (e?: any) => {
120
+ throw e;
121
+ },
118
122
  ): DisplayObject {
119
123
  // Initialize the context if it is not provided.
120
124
  const { width = 640, height = 480, on } = options;
@@ -126,9 +130,7 @@ export function renderToMountedElement<T extends G2ViewTree = G2ViewTree>(
126
130
  } = context;
127
131
 
128
132
  if (!group?.parentElement) {
129
- throw new Error(
130
- `renderToMountedElement can't render chart to unmounted group.`,
131
- );
133
+ error(`renderToMountedElement can't render chart to unmounted group.`);
132
134
  }
133
135
 
134
136
  const selection = select(group);
@@ -190,7 +190,6 @@ function inferScaleDomain(
190
190
  ): Primitive[] {
191
191
  const { domain } = options;
192
192
  if (domain !== undefined) return domain;
193
- const { domainMax, domainMin } = options;
194
193
  switch (type) {
195
194
  case 'linear':
196
195
  case 'time':
@@ -198,20 +197,16 @@ function inferScaleDomain(
198
197
  case 'pow':
199
198
  case 'sqrt':
200
199
  case 'quantize':
201
- case 'threshold': {
202
- const [d0, d1] = inferDomainQ(values, options);
203
- return [domainMin ?? d0, domainMax ?? d1];
204
- }
200
+ case 'threshold':
201
+ return maybeMinMax(inferDomainQ(values, options), options);
205
202
  case 'band':
206
203
  case 'ordinal':
207
204
  case 'point':
208
205
  return inferDomainC(values);
209
206
  case 'quantile':
210
207
  return inferDomainO(values);
211
- case 'sequential': {
212
- const [d0, d1] = inferDomainS(values);
213
- return [domainMin ?? d0, domainMax ?? d1];
214
- }
208
+ case 'sequential':
209
+ return maybeMinMax(inferDomainS(values), options);
215
210
  default:
216
211
  return [];
217
212
  }
@@ -403,6 +398,16 @@ function asQuantitativeType(name: string, range: Primitive[]) {
403
398
  return range ? 'linear' : 'sequential';
404
399
  }
405
400
 
401
+ function maybeMinMax(
402
+ domain: Primitive[],
403
+ options: G2ScaleOptions,
404
+ ): Primitive[] {
405
+ if (domain.length === 0) return domain;
406
+ const { domainMin, domainMax } = options;
407
+ const [d0, d1] = domain;
408
+ return [domainMin ?? d0, domainMax ?? d1];
409
+ }
410
+
406
411
  function inferDomainQ(values: Primitive[][], options: G2ScaleOptions) {
407
412
  const { zero = false } = options;
408
413
  let min = Infinity;
@@ -14,7 +14,7 @@ import {
14
14
  import { DataComponent } from './data';
15
15
  import { Encode, EncodeComponent } from './encode';
16
16
  import { Mark, MarkComponent } from './mark';
17
- import { G2ViewTree, G2Library, G2Mark } from './options';
17
+ import { G2ViewTree, G2Library, G2Mark, G2Context } from './options';
18
18
  import { Transform, TransformComponent } from './transform';
19
19
 
20
20
  export type G2ComponentNamespaces =
@@ -145,6 +145,7 @@ export type Shape = (
145
145
  coordinate: Coordinate,
146
146
  theme: G2Theme,
147
147
  point2d?: Vector2[][],
148
+ context?: G2Context,
148
149
  ) => DisplayObject;
149
150
  export type ShapeProps = {
150
151
  defaultMarker?: string;
@@ -57,6 +57,11 @@ export type G2Context = {
57
57
  group?: DisplayObject;
58
58
  animations?: GAnimation[];
59
59
  views?: G2ViewDescriptor[];
60
+ /**
61
+ * Tell G2 how to create a canvas-like element, some marks will use it later such as wordcloud & heatmap.
62
+ * Use `document.createElement('canvas')` instead if not provided.
63
+ */
64
+ createCanvas?: () => HTMLCanvasElement;
60
65
  };
61
66
 
62
67
  export type G2View = {
@@ -126,6 +131,7 @@ export type G2Mark = {
126
131
  labels?: Record<string, any>[];
127
132
  interaction?: Record<string, any>;
128
133
  coordinate?: Record<string, any>;
134
+ viewStyle?: Record<string, any>;
129
135
  };
130
136
 
131
137
  export type G2MarkChildrenCallback = (
@@ -0,0 +1,85 @@
1
+ import { max as d3max, min as d3min } from 'd3-array';
2
+ import { Image as GImage } from '@antv/g';
3
+ import { applyStyle, getShapeTheme } from '../utils';
4
+ import { select } from '../../utils/selection';
5
+ import { ShapeComponent as SC } from '../../runtime';
6
+ import { HeatmapRenderer } from './renderer';
7
+ import type { HeatmapRendererOptions } from './renderer/types';
8
+
9
+ export type HeatmapOptions = HeatmapRendererOptions;
10
+
11
+ function deleteKey(obj: any, fn: (v, k) => boolean) {
12
+ const r = { ...obj };
13
+ return Object.keys(obj).reduce((r, k) => {
14
+ const v = obj[k];
15
+ if (!fn(v, k)) r[k] = v;
16
+ return r;
17
+ }, {});
18
+ }
19
+
20
+ export const Heatmap: SC<HeatmapOptions> = (options) => {
21
+ const {
22
+ gradient,
23
+ opacity,
24
+ maxOpacity,
25
+ minOpacity,
26
+ blur,
27
+ useGradientOpacity,
28
+ ...style
29
+ } = options;
30
+ return (points: number[][], value, coordinate, theme, _, context) => {
31
+ const { mark, shape, defaultShape, transform } = value;
32
+ const { ...shapeTheme } = getShapeTheme(theme, mark, shape, defaultShape);
33
+ const { createCanvas } = context;
34
+
35
+ const [width, height] = coordinate.getSize();
36
+ const data = points.map((p: number[]) => ({
37
+ x: p[0],
38
+ y: p[1],
39
+ value: p[2],
40
+ radius: p[3],
41
+ }));
42
+
43
+ const min = d3min(points, (p) => p[2]);
44
+ const max = d3max(points, (p) => p[2]);
45
+
46
+ const options = {
47
+ gradient,
48
+ opacity,
49
+ minOpacity,
50
+ maxOpacity,
51
+ blur,
52
+ useGradientOpacity,
53
+ };
54
+ const ctx =
55
+ width && height
56
+ ? HeatmapRenderer(
57
+ width,
58
+ height,
59
+ min,
60
+ max,
61
+ data,
62
+ deleteKey(options, (v) => v === undefined),
63
+ createCanvas,
64
+ )
65
+ : { canvas: null };
66
+
67
+ return select(new GImage())
68
+ .call(applyStyle, shapeTheme)
69
+ .style('x', 0)
70
+ .style('y', 0)
71
+ .style('width', width)
72
+ .style('height', height)
73
+ .style('src', ctx.canvas)
74
+ .style('transform', transform)
75
+ .call(applyStyle, style)
76
+ .node();
77
+ };
78
+ };
79
+
80
+ Heatmap.props = {
81
+ defaultMarker: 'point',
82
+ defaultEnterAnimation: 'fadeIn',
83
+ defaultUpdateAnimation: 'morphing',
84
+ defaultExitAnimation: 'fadeOut',
85
+ };
@@ -0,0 +1,16 @@
1
+ import { HeatmapGradient } from './types';
2
+
3
+ /**
4
+ * Parse heatmap gradient.
5
+ */
6
+ export function parseGradient(
7
+ gradient: HeatmapGradient,
8
+ ): Array<[number, string]> {
9
+ if (typeof gradient === 'string') {
10
+ return gradient.split(' ').map((stop) => {
11
+ const [r, c] = stop.split(':');
12
+ return [+r, c];
13
+ });
14
+ }
15
+ return gradient;
16
+ }