@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
package/src/api/chart.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { IRenderer, RendererPlugin, Canvas as GCanvas } from '@antv/g';
2
2
  import { Renderer as CanvasRenderer } from '@antv/g-canvas';
3
3
  import { Plugin as DragAndDropPlugin } from '@antv/g-plugin-dragndrop';
4
- import { debounce, deepMix } from '@antv/util';
4
+ import { debounce } from '@antv/util';
5
5
  import EventEmitter from '@antv/event-emitter';
6
6
  import { G2Context, render, destroy } from '../runtime';
7
7
  import { ViewComposition } from '../spec';
@@ -27,6 +27,8 @@ import {
27
27
  removeContainer,
28
28
  sizeOf,
29
29
  optionsOf,
30
+ updateRoot,
31
+ createEmptyPromise,
30
32
  } from './utils';
31
33
 
32
34
  export const G2_CHART_KEY = 'G2_CHART_KEY';
@@ -84,13 +86,16 @@ export class Chart extends View<ChartOptions> {
84
86
  private _container: HTMLElement;
85
87
  private _context: G2Context;
86
88
  private _emitter: EventEmitter;
87
- private _options: G2ViewTree;
88
89
  private _width: number;
89
90
  private _height: number;
90
91
  private _renderer: IRenderer;
91
92
  private _plugins: RendererPlugin[];
92
93
  // Identifies whether bindAutoFit.
93
94
  private _hasBindAutoFit = false;
95
+ private _rendering = false;
96
+ private _trailing = false;
97
+ private _trailingResolve = null;
98
+ private _trailingReject = null;
94
99
 
95
100
  constructor(options: ChartOptions) {
96
101
  const { container, canvas, renderer, plugins, ...rest } = options || {};
@@ -103,45 +108,24 @@ export class Chart extends View<ChartOptions> {
103
108
  }
104
109
 
105
110
  render(): Promise<Chart> {
111
+ if (this._rendering) return this._addToTrailing();
112
+ if (!this._context.canvas) this._createCanvas();
106
113
  this._bindAutoFit();
107
-
108
- if (!this._context.canvas) {
109
- // Init width and height.
110
- const { width, height } = sizeOf(this.options(), this._container);
111
- // Create canvas if it does not exist.
112
- // DragAndDropPlugin is for interaction.
113
- // It is OK to register more than one time, G will handle this.
114
- this._plugins.push(new DragAndDropPlugin());
115
- this._plugins.forEach((d) => this._renderer.registerPlugin(d));
116
- this._context.canvas = new GCanvas({
117
- container: this._container,
118
- width,
119
- height,
120
- renderer: this._renderer,
121
- });
122
- }
123
-
124
- return new Promise((resolve, reject) => {
125
- try {
126
- const options = this.options();
127
- const { key = G2_CHART_KEY } = options;
128
- const { width, height } = sizeOf(options, this._container);
129
-
130
- // Update actual size and key.
131
- this._width = width;
132
- this._height = height;
133
- this._key = key;
134
-
135
- render(
136
- { key: this._key, ...options, width, height },
137
- this._context,
138
- () => resolve(this),
139
- reject,
140
- );
141
- } catch (e) {
142
- reject(e);
143
- }
144
- });
114
+ this._rendering = true;
115
+ const finished = new Promise<Chart>((resolve, reject) =>
116
+ render(
117
+ this._computedOptions(),
118
+ this._context,
119
+ this._createResolve(resolve),
120
+ this._createReject(reject),
121
+ ),
122
+ );
123
+ const [finished1, resolve, reject] = createEmptyPromise<Chart>();
124
+ finished
125
+ .then(resolve)
126
+ .catch(reject)
127
+ .then(() => this._renderTrailing());
128
+ return finished1;
145
129
  }
146
130
 
147
131
  /**
@@ -161,20 +145,11 @@ export class Chart extends View<ChartOptions> {
161
145
  * @returns {Chart|G2ViewTree}
162
146
  */
163
147
  options(options?: G2ViewTree): Chart | G2ViewTree {
164
- if (arguments.length === 0) {
165
- return this._options || optionsOf(this);
166
- }
167
- this._options = deepMix(this._options || optionsOf(this), options);
148
+ if (arguments.length === 0) return optionsOf(this);
149
+ updateRoot(this, options);
168
150
  return this;
169
151
  }
170
152
 
171
- // @todo Remove it when implement updateRoot.
172
- changeData(data: any): Promise<Chart> {
173
- // Update options data.
174
- this.options({ data });
175
- return super.changeData(data);
176
- }
177
-
178
153
  getContainer(): HTMLElement {
179
154
  return this._container;
180
155
  }
@@ -206,7 +181,7 @@ export class Chart extends View<ChartOptions> {
206
181
  clear() {
207
182
  const options = this.options();
208
183
  this.emit(ChartEvent.BEFORE_CLEAR);
209
- this._options = {};
184
+ this._reset();
210
185
  destroy(options, this._context, false);
211
186
  this.emit(ChartEvent.AFTER_CLEAR);
212
187
  }
@@ -215,16 +190,21 @@ export class Chart extends View<ChartOptions> {
215
190
  const options = this.options();
216
191
  this.emit(ChartEvent.BEFORE_DESTROY);
217
192
  this._unbindAutoFit();
218
- this._options = {};
193
+ this._reset();
219
194
  destroy(options, this._context, true);
220
195
  removeContainer(this._container);
221
196
  this.emit(ChartEvent.AFTER_DESTROY);
222
197
  }
223
198
 
224
199
  forceFit() {
200
+ // Don't fit if size do not change.
201
+ this.options['autoFit'] = true;
202
+ const { width, height } = sizeOf(this.options(), this._container);
203
+ if (width === this._width && height === this._height) {
204
+ return Promise.resolve(this);
205
+ }
206
+
225
207
  // Don't call changeSize to prevent update width and height of options.
226
- // @ts-ignore
227
- this.options.autoFit = true;
228
208
  this.emit(ChartEvent.BEFORE_CHANGE_SIZE);
229
209
  const finished = this.render();
230
210
  finished.then(() => {
@@ -247,6 +227,82 @@ export class Chart extends View<ChartOptions> {
247
227
  return finished;
248
228
  }
249
229
 
230
+ private _reset() {
231
+ this.type = 'view';
232
+ this.value = {};
233
+ this.children = [];
234
+ }
235
+
236
+ private _renderTrailing() {
237
+ if (!this._trailing) return;
238
+ this._trailing = false;
239
+ this.render()
240
+ .then(() => {
241
+ const trailingResolve = this._trailingResolve.bind(this);
242
+ this._trailingResolve = null;
243
+ trailingResolve(this);
244
+ })
245
+ .catch((error) => {
246
+ const trailingReject = this._trailingReject.bind(this);
247
+ this._trailingReject = null;
248
+ trailingReject(error);
249
+ });
250
+ }
251
+
252
+ private _createResolve(resolve: (chart: Chart) => void) {
253
+ return () => {
254
+ this._rendering = false;
255
+ resolve(this);
256
+ };
257
+ }
258
+
259
+ private _createReject(reject: (error: Error) => void) {
260
+ return (error: Error) => {
261
+ this._rendering = false;
262
+ reject(error);
263
+ };
264
+ }
265
+
266
+ // Update actual size and key.
267
+ private _computedOptions() {
268
+ const options = this.options();
269
+ const { key = G2_CHART_KEY } = options;
270
+ const { width, height } = sizeOf(options, this._container);
271
+ this._width = width;
272
+ this._height = height;
273
+ this._key = key;
274
+ return { key: this._key, ...options, width, height };
275
+ }
276
+
277
+ // Create canvas if it does not exist.
278
+ // DragAndDropPlugin is for interaction.
279
+ // It is OK to register more than one time, G will handle this.
280
+ private _createCanvas() {
281
+ const { width, height } = sizeOf(this.options(), this._container);
282
+ this._plugins.push(new DragAndDropPlugin());
283
+ this._plugins.forEach((d) => this._renderer.registerPlugin(d));
284
+ this._context.canvas = new GCanvas({
285
+ container: this._container,
286
+ width,
287
+ height,
288
+ renderer: this._renderer,
289
+ });
290
+ }
291
+
292
+ private _addToTrailing(): Promise<Chart> {
293
+ // Resolve previous promise, and give up this task.
294
+ this._trailingResolve?.(this);
295
+
296
+ // Create new task.
297
+ this._trailing = true;
298
+ const promise = new Promise<Chart>((resolve, reject) => {
299
+ this._trailingResolve = resolve;
300
+ this._trailingReject = reject;
301
+ });
302
+
303
+ return promise;
304
+ }
305
+
250
306
  private _onResize = debounce(() => {
251
307
  this.forceFit();
252
308
  }, 300);
@@ -21,6 +21,7 @@ import {
21
21
  Sankey,
22
22
  Treemap,
23
23
  Boxplot,
24
+ Density,
24
25
  Shape,
25
26
  Pack,
26
27
  ForceGraph,
@@ -53,6 +54,7 @@ export interface Mark {
53
54
  sankey(): Sankey;
54
55
  treemap(): Treemap;
55
56
  boxplot(): Boxplot;
57
+ density(): Density;
56
58
  shape(): Shape;
57
59
  pack(): Pack;
58
60
  forceGraph(): ForceGraph;
@@ -86,6 +88,7 @@ export const mark = {
86
88
  sankey: Sankey,
87
89
  treemap: Treemap,
88
90
  boxplot: Boxplot,
91
+ density: Density,
89
92
  shape: Shape,
90
93
  pack: Pack,
91
94
  forceGraph: ForceGraph,
@@ -19,6 +19,7 @@ import {
19
19
  ConnectorMark,
20
20
  SankeyMark,
21
21
  BoxPlotMark,
22
+ DensityMark,
22
23
  ShapeMark,
23
24
  TreemapMark,
24
25
  ForceGraphMark,
@@ -113,6 +114,10 @@ export interface Boxplot extends API<Concrete<BoxPlotMark>, Boxplot> {
113
114
  type: 'boxplot';
114
115
  }
115
116
 
117
+ export interface Density extends API<Concrete<DensityMark>, Density> {
118
+ type: 'density';
119
+ }
120
+
116
121
  export interface Shape extends API<Concrete<ShapeMark>, Shape> {
117
122
  type: 'shape';
118
123
  }
@@ -323,6 +328,13 @@ export class Boxplot extends MarkNode<BoxPlotMark> {
323
328
  }
324
329
  }
325
330
 
331
+ @defineProps(props)
332
+ export class Density extends MarkNode<DensityMark> {
333
+ constructor() {
334
+ super({}, 'density');
335
+ }
336
+ }
337
+
326
338
  @defineProps([...props, { name: 'layout', type: 'value' }])
327
339
  export class Pack extends MarkNode<PackMark> {
328
340
  constructor() {
package/src/api/node.ts CHANGED
@@ -6,7 +6,7 @@ import { Chart } from './chart';
6
6
  function bfs(node: Node, callback?: (...args: any[]) => void) {
7
7
  const discovered: Node[] = [node];
8
8
  while (discovered.length) {
9
- const currentNode = discovered.pop();
9
+ const currentNode = discovered.shift();
10
10
  callback && callback(currentNode);
11
11
  const children = currentNode.children || [];
12
12
  for (const child of children) {
@@ -115,6 +115,15 @@ export class Node<
115
115
  return nodes;
116
116
  }
117
117
 
118
+ getNodeByType(type: string): Node {
119
+ let node = null;
120
+ bfs(this, (current: Node) => {
121
+ if (node) return;
122
+ if (type === current.type) node = current;
123
+ });
124
+ return node;
125
+ }
126
+
118
127
  /**
119
128
  * Apply specified callback to the node value.
120
129
  */
package/src/api/utils.ts CHANGED
@@ -1,5 +1,33 @@
1
+ import { deepMix } from '@antv/util';
2
+ import { G2ViewTree } from '../runtime';
1
3
  import { getContainerSize } from '../utils/size';
2
4
  import { Node } from './node';
5
+ import { mark } from './mark';
6
+ import { composition } from './composition';
7
+
8
+ // Keys can specified by new Chart({...}).
9
+ // Keys can bubble form mark-level options to view-level options.
10
+ export const VIEW_KEYS = [
11
+ 'width',
12
+ 'height',
13
+ 'padding',
14
+ 'paddingLeft',
15
+ 'paddingRight',
16
+ 'paddingBottom',
17
+ 'paddingTop',
18
+ 'inset',
19
+ 'insetLeft',
20
+ 'insetRight',
21
+ 'insetTop',
22
+ 'insetBottom',
23
+ 'margin',
24
+ 'marginLeft',
25
+ 'marginRight',
26
+ 'marginTop',
27
+ 'marginBottom',
28
+ 'autoFit',
29
+ 'theme',
30
+ ];
3
31
 
4
32
  export function normalizeContainer(
5
33
  container: string | HTMLElement,
@@ -22,25 +50,7 @@ export function removeContainer(container: HTMLElement) {
22
50
  export function normalizeRoot(node: Node) {
23
51
  if (node.type !== null) return node;
24
52
  const root = node.children[node.children.length - 1];
25
- root.attr('width', node.attr('width'));
26
- root.attr('height', node.attr('height'));
27
- root.attr('paddingLeft', node.attr('paddingLeft'));
28
- root.attr('paddingTop', node.attr('paddingTop'));
29
- root.attr('paddingBottom', node.attr('paddingBottom'));
30
- root.attr('paddingRight', node.attr('paddingRight'));
31
- root.attr('insetLeft', node.attr('insetLeft'));
32
- root.attr('insetRight', node.attr('insetRight'));
33
- root.attr('insetBottom', node.attr('insetBottom'));
34
- root.attr('insetTop', node.attr('insetTop'));
35
- root.attr('marginLeft', node.attr('marginLeft'));
36
- root.attr('marginBottom', node.attr('marginBottom'));
37
- root.attr('marginTop', node.attr('marginTop'));
38
- root.attr('marginRight', node.attr('marginRight'));
39
- root.attr('autoFit', node.attr('autoFit'));
40
- root.attr('padding', node.attr('padding'));
41
- root.attr('margin', node.attr('margin'));
42
- root.attr('inset', node.attr('inset'));
43
- root.attr('theme', node.attr('theme'));
53
+ for (const key of VIEW_KEYS) root.attr(key, node.attr(key));
44
54
  return root;
45
55
  }
46
56
 
@@ -66,7 +76,8 @@ export function optionsOf(node: Node): Record<string, any> {
66
76
  while (discovered.length) {
67
77
  const node = discovered.pop();
68
78
  const value = nodeValue.get(node);
69
- for (const child of node.children) {
79
+ const { children = [] } = node;
80
+ for (const child of children) {
70
81
  const childValue = valueOf(child);
71
82
  const { children = [] } = value;
72
83
  children.push(childValue);
@@ -77,3 +88,111 @@ export function optionsOf(node: Node): Record<string, any> {
77
88
  }
78
89
  return nodeValue.get(root);
79
90
  }
91
+
92
+ function isMark(type: string): boolean {
93
+ return new Set(Object.keys(mark)).has(type);
94
+ }
95
+
96
+ function normalizeRootOptions(node: Node, options: G2ViewTree) {
97
+ const { type: oldType } = node;
98
+ const { type = oldType } = options;
99
+ if (type === 'view') return options;
100
+ if (typeof type !== 'string') return options;
101
+ if (!isMark(type)) return options;
102
+ const view = { type: 'view' };
103
+ const mark = { ...options };
104
+ for (const key of VIEW_KEYS) {
105
+ if (mark[key] !== undefined) {
106
+ view[key] = mark[key];
107
+ delete mark[key];
108
+ }
109
+ }
110
+ return { ...view, children: [mark] };
111
+ }
112
+
113
+ function typeCtor(type: string): new () => Node {
114
+ const node = { ...mark, ...composition };
115
+ const ctor = node[type];
116
+ if (!ctor) throw new Error(`Unknown mark: ${type}.`);
117
+ return ctor;
118
+ }
119
+
120
+ // Create node from options.
121
+ function createNode(options: G2ViewTree): Node {
122
+ const { type, children, ...value } = options;
123
+ if (typeof type !== 'string') return;
124
+ const Ctor = typeCtor(type);
125
+ const node = new Ctor();
126
+ node.value = value;
127
+ return node;
128
+ }
129
+
130
+ // Update node by options.
131
+ function updateNode(node: Node, newOptions: G2ViewTree) {
132
+ const { type, children, ...value } = newOptions;
133
+ if (node.type === type || type === undefined) {
134
+ // Update node.
135
+ node.value = deepMix(node.value, value);
136
+ } else if (typeof type === 'string') {
137
+ // Transform node.
138
+ node.type = type;
139
+ node.value = value;
140
+ }
141
+ }
142
+
143
+ // Create a nested node tree from newOptions, and append it to the parent.
144
+ function appendNode(parent: Node, newOptions: G2ViewTree) {
145
+ if (!parent) return;
146
+ const discovered = [[parent, newOptions]];
147
+ while (discovered.length) {
148
+ const [parent, nodeOptions] = discovered.shift();
149
+ const node = createNode(nodeOptions);
150
+ if (Array.isArray(parent.children)) parent.children.push(node);
151
+ const { children } = nodeOptions;
152
+ if (Array.isArray(children)) {
153
+ for (const child of children) {
154
+ discovered.push([node, child]);
155
+ }
156
+ }
157
+ }
158
+ }
159
+
160
+ // Update node tree from options.
161
+ export function updateRoot(node: Node, options: G2ViewTree) {
162
+ const rootOptions = normalizeRootOptions(node, options);
163
+ const discovered: [Node, Node, G2ViewTree][] = [[null, node, rootOptions]];
164
+ while (discovered.length) {
165
+ const [parent, oldNode, newNode] = discovered.shift();
166
+ // If there is no oldNode, create a node tree directly.
167
+ if (!oldNode) {
168
+ appendNode(parent, newNode);
169
+ } else {
170
+ updateNode(oldNode, newNode);
171
+ const { children: newChildren } = newNode;
172
+ const { children: oldChildren } = oldNode;
173
+ if (Array.isArray(newChildren) && Array.isArray(oldChildren)) {
174
+ // Only update node specified in newChildren,
175
+ // the extra oldChildren will remain still.
176
+ for (let i = 0; i < newChildren.length; i++) {
177
+ const newChild = newChildren[i];
178
+ const oldChild = oldChildren[i];
179
+ discovered.push([oldNode, oldChild, newChild]);
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ export function createEmptyPromise<T>(): [
187
+ Promise<T>,
188
+ (reason?: any) => void,
189
+ (value: T | PromiseLike<T>) => void,
190
+ ] {
191
+ let reject: (reason?: any) => void;
192
+ let resolve: (value: T | PromiseLike<T>) => void;
193
+ const cloned = new Promise<T>((res, rej) => {
194
+ resolve = res;
195
+ reject = rej;
196
+ });
197
+ return [cloned, resolve, reject];
198
+ }
@@ -148,7 +148,12 @@ function getData(
148
148
 
149
149
  const ticks = ticksOf(scale, domain, tickMethod);
150
150
  const filteredTicks = tickFilter ? ticks.filter(tickFilter) : ticks;
151
- const toString = (d) => (typeof d === 'object' && !!d ? d : String(d));
151
+ const toString = (d) =>
152
+ d instanceof Date
153
+ ? String(d)
154
+ : typeof d === 'object' && !!d
155
+ ? d
156
+ : String(d);
152
157
  const labelFormatter =
153
158
  defaultTickFormatter || scale.getFormatter?.() || toString;
154
159
  const applyInset = createInset(position, coordinate);
@@ -266,7 +271,7 @@ function inferArcStyle(
266
271
  ...common,
267
272
  labelAlign: 'parallel',
268
273
  labelDirection: 'negative',
269
- labelSpacing: 8,
274
+ labelSpacing: 4,
270
275
  tickDirection: 'negative',
271
276
  gridDirection: 'positive',
272
277
  };
@@ -397,6 +402,7 @@ const ArcAxisComponent: GCC<AxisOptions> = (options) => {
397
402
  );
398
403
 
399
404
  const { axis: axisTheme } = theme;
405
+
400
406
  return new AxisComponent({
401
407
  style: adaptor(
402
408
  deepMix({}, axisTheme, defaultStyle, {
@@ -477,6 +483,7 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
477
483
  tickFilter,
478
484
  tickMethod,
479
485
  transform,
486
+ indexBBox,
480
487
  ...userDefinitions
481
488
  } = options;
482
489
 
@@ -506,19 +513,34 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
506
513
  bbox,
507
514
  coordinate,
508
515
  );
516
+
517
+ const data = getData(
518
+ scale,
519
+ domain,
520
+ tickCount,
521
+ labelFormatter,
522
+ tickFilter,
523
+ tickMethod,
524
+ position,
525
+ coordinate,
526
+ );
527
+
528
+ // Bind computed bbox if exists.
529
+ const labels = indexBBox
530
+ ? data.map((d, i) => {
531
+ const bbox = indexBBox.get(i);
532
+ if (!bbox) return d;
533
+ // bbox: [label, bbox]
534
+ // Make than indexBBox can match current label.
535
+ if (bbox[0] !== d.label) return d;
536
+ return { ...d, bbox: bbox[1] };
537
+ })
538
+ : data;
539
+
509
540
  const finalAxisStyle = {
510
541
  ...internalAxisStyle,
511
542
  type: 'linear' as const,
512
- data: getData(
513
- scale,
514
- domain,
515
- tickCount,
516
- labelFormatter,
517
- tickFilter,
518
- tickMethod,
519
- position,
520
- coordinate,
521
- ),
543
+ data: labels,
522
544
  crossSize: size,
523
545
  titleText: titleContent(title),
524
546
  labelOverlap: inferLabelOverlap(transform, internalAxisStyle),
@@ -526,6 +548,7 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
526
548
  gridLength,
527
549
  // Always showLine, make title could align the end of axis.
528
550
  line: true,
551
+ indexBBox,
529
552
  ...(!internalAxisStyle.line ? { lineOpacity: 0 } : null),
530
553
  ...overrideStyle,
531
554
  ...important,
@@ -158,11 +158,16 @@ export function adaptor<T>(style: T) {
158
158
  'title',
159
159
  'trunc',
160
160
  ];
161
+ // @ts-ignore
162
+ const { style: styles, ...rest } = style;
161
163
  const finalStyle = {};
162
- Object.entries(style).forEach(([key, value]) => {
163
- if (reservedKeys.includes(key))
164
+ Object.entries(rest).forEach(([key, value]) => {
165
+ if (reservedKeys.includes(key)) {
164
166
  finalStyle[`show${upperFirst(key)}`] = value;
165
- else finalStyle[key] = value;
167
+ } else finalStyle[key] = value;
166
168
  });
167
- return finalStyle as T;
169
+ return {
170
+ ...finalStyle,
171
+ ...styles,
172
+ };
168
173
  }
@@ -37,6 +37,7 @@ export const Mark: CC<MarkOptions> = () => {
37
37
  labelTransform,
38
38
  parentKey,
39
39
  clip,
40
+ viewStyle,
40
41
  ...mark
41
42
  } = options;
42
43
 
@@ -72,6 +73,7 @@ export const Mark: CC<MarkOptions> = () => {
72
73
  marginRight,
73
74
  parentKey,
74
75
  clip,
76
+ style: viewStyle,
75
77
  marks: [{ ...mark, key: `${key}-0`, data }],
76
78
  },
77
79
  ];
package/src/data/index.ts CHANGED
@@ -15,6 +15,7 @@ export { Arc } from './arc';
15
15
  export { WordCloud } from './wordCloud';
16
16
  export { Join } from './join';
17
17
  export { Slice } from './slice';
18
+ export { KDE } from './kde';
18
19
 
19
20
  export type { FetchOptions } from './fetch';
20
21
  export type { FoldOptions } from './fold';
@@ -33,3 +34,4 @@ export type { ArcOptions } from './arc';
33
34
  export type { WordCloudOptions } from './wordCloud';
34
35
  export type { JoinOptions } from './join';
35
36
  export type { SliceOptions } from './slice';
37
+ export type { KDEOptions } from './kde';
@@ -0,0 +1,53 @@
1
+ import pdf from 'pdfast';
2
+ import { group } from 'd3-array';
3
+ import { DataComponent as DC } from '../runtime';
4
+ import { KDEDataTransform } from '../spec';
5
+
6
+ export type KDEOptions = Omit<KDEDataTransform, 'type'>;
7
+
8
+ export function defined(d: any): boolean {
9
+ return d !== undefined && d !== null && !Number.isNaN(d);
10
+ }
11
+
12
+ /**
13
+ * Kernel Density Estimation base on [pdfast](https://www.npmjs.com/package/pdfast),
14
+ * generating probability density function (pdf) using triangular kernel,
15
+ * optimized to run in O(N + K).
16
+ */
17
+ export const KDE: DC<KDEOptions> = (options) => {
18
+ const {
19
+ field,
20
+ groupBy,
21
+ as = ['y', 'size'],
22
+ min,
23
+ max,
24
+ size = 10,
25
+ width,
26
+ } = options;
27
+ const [yField, sizeField] = as;
28
+ return (data) => {
29
+ const gs = Array.from(
30
+ group(data, (d) => groupBy.map((gb) => d[gb]).join('-')).values(),
31
+ );
32
+ return gs.map((g) => {
33
+ const pdfResult = pdf.create(
34
+ g.map((i) => i[field]),
35
+ {
36
+ min,
37
+ max,
38
+ size,
39
+ width,
40
+ },
41
+ );
42
+ const _y = pdfResult.map((result) => result.x);
43
+ const _size = pdfResult.map((result) => result.y);
44
+ return {
45
+ ...(g[0] as Record<string, unknown>),
46
+ [yField]: _y,
47
+ [sizeField]: _size,
48
+ };
49
+ });
50
+ };
51
+ };
52
+
53
+ KDE.props = {};