@antv/g2 5.1.18 → 5.1.19

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 (50) hide show
  1. package/dist/g2.lite.min.js +1 -1
  2. package/dist/g2.min.js +1 -1
  3. package/esm/data/utils/d3-cloud/index.js +1 -0
  4. package/esm/data/utils/d3-cloud/index.js.map +1 -1
  5. package/esm/data/wordCloud.d.ts +100 -2
  6. package/esm/data/wordCloud.js +20 -5
  7. package/esm/data/wordCloud.js.map +1 -1
  8. package/esm/index.d.ts +4 -2
  9. package/esm/lib/core.d.ts +2 -0
  10. package/esm/lib/core.js +4 -2
  11. package/esm/lib/core.js.map +1 -1
  12. package/esm/lib/std.d.ts +2 -1
  13. package/esm/mark/text.js +4 -3
  14. package/esm/mark/text.js.map +1 -1
  15. package/esm/mark/wordCloud.js +11 -12
  16. package/esm/mark/wordCloud.js.map +1 -1
  17. package/esm/shape/index.d.ts +2 -0
  18. package/esm/shape/index.js +1 -0
  19. package/esm/shape/index.js.map +1 -1
  20. package/esm/shape/text/tag.d.ts +7 -0
  21. package/esm/shape/text/tag.js +40 -0
  22. package/esm/shape/text/tag.js.map +1 -0
  23. package/lib/data/utils/d3-cloud/index.js +1 -0
  24. package/lib/data/utils/d3-cloud/index.js.map +1 -1
  25. package/lib/data/wordCloud.d.ts +100 -2
  26. package/lib/data/wordCloud.js +20 -5
  27. package/lib/data/wordCloud.js.map +1 -1
  28. package/lib/index.d.ts +4 -2
  29. package/lib/lib/core.d.ts +2 -0
  30. package/lib/lib/core.js +2 -0
  31. package/lib/lib/core.js.map +1 -1
  32. package/lib/lib/std.d.ts +2 -1
  33. package/lib/mark/text.js +3 -2
  34. package/lib/mark/text.js.map +1 -1
  35. package/lib/mark/wordCloud.js +11 -12
  36. package/lib/mark/wordCloud.js.map +1 -1
  37. package/lib/shape/index.d.ts +2 -0
  38. package/lib/shape/index.js +4 -2
  39. package/lib/shape/index.js.map +1 -1
  40. package/lib/shape/text/tag.d.ts +7 -0
  41. package/lib/shape/text/tag.js +44 -0
  42. package/lib/shape/text/tag.js.map +1 -0
  43. package/package.json +3 -2
  44. package/src/data/utils/d3-cloud/index.ts +1 -0
  45. package/src/data/wordCloud.ts +112 -7
  46. package/src/lib/core.ts +4 -0
  47. package/src/mark/text.ts +4 -3
  48. package/src/mark/wordCloud.ts +16 -18
  49. package/src/shape/index.ts +2 -0
  50. package/src/shape/text/tag.ts +44 -0
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tag = void 0;
4
+ const g_1 = require("@antv/g");
5
+ const utils_1 = require("../../shape/utils");
6
+ const selection_1 = require("../../utils/selection");
7
+ /**
8
+ * @todo autoRotate when in polar coordinate
9
+ * Tag shape for Text mark, used in wordCloud plot.
10
+ */
11
+ const Tag = (options, context) => {
12
+ const { coordinate } = context;
13
+ return (points, value, defaults) => {
14
+ const { color, text = '', fontSize, rotate = 0, transform = '' } = value;
15
+ const textStyle = {
16
+ text: String(text),
17
+ stroke: color,
18
+ fill: color,
19
+ fontSize,
20
+ textAlign: 'center',
21
+ textBaseline: 'middle',
22
+ };
23
+ const [[x0, y0]] = points;
24
+ const n = (0, selection_1.select)(new g_1.Text())
25
+ .style('x', x0)
26
+ .style('y', y0)
27
+ .call(utils_1.applyStyle, defaults)
28
+ .style('transformOrigin', 'center center')
29
+ .style('transform', `${transform}rotate(${rotate}deg)`)
30
+ .style('coordCenter', coordinate.getCenter())
31
+ .call(utils_1.applyStyle, textStyle)
32
+ .call(utils_1.applyStyle, options)
33
+ .node();
34
+ return n;
35
+ };
36
+ };
37
+ exports.Tag = Tag;
38
+ exports.Tag.props = {
39
+ defaultMarker: 'point',
40
+ defaultEnterAnimation: 'fadeIn',
41
+ defaultUpdateAnimation: 'morphing',
42
+ defaultExitAnimation: 'fadeOut',
43
+ };
44
+ //# sourceMappingURL=tag.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tag.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AyBd;AAEF,WAAG,CAAC,KAAK,GAAG;IACV,aAAa,EAAE,OAAO;IACtB,qBAAqB,EAAE,QAAQ;IAC/B,sBAAsB,EAAE,UAAU;IAClC,oBAAoB,EAAE,SAAS;CAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/g2",
3
- "version": "5.1.18",
3
+ "version": "5.1.19",
4
4
  "description": "the Grammar of Graphics in Javascript",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -42,6 +42,7 @@
42
42
  "build": "run-p build:*",
43
43
  "bundle-vis": "cross-env BUNDLE_VIS=1 run-p build:umd",
44
44
  "ci": "run-s lint test build",
45
+ "prepublishOnly": "npm run ci",
45
46
  "prepare": "husky install",
46
47
  "upload": "node scripts/upload/snapshot.js",
47
48
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
@@ -153,7 +154,7 @@
153
154
  "limit-size": [
154
155
  {
155
156
  "path": "dist/g2.min.js",
156
- "limit": "280 Kb",
157
+ "limit": "285 Kb",
157
158
  "gzip": true
158
159
  },
159
160
  {
@@ -357,6 +357,7 @@ export function tagCloud() {
357
357
  const context = canvas.getContext('2d') as CanvasRenderingContext2D;
358
358
  context.fillStyle = context.strokeStyle = 'red';
359
359
  context.textAlign = 'center';
360
+ context.textBaseline = 'middle';
360
361
  return { context, ratio };
361
362
  }
362
363
 
@@ -1,15 +1,112 @@
1
1
  import { min, max } from 'd3-array';
2
2
  import { DataComponent as DC } from '../runtime';
3
- import { tagCloud } from './utils/d3-cloud';
4
3
  import { flow } from './utils/flow';
4
+ import { tagCloud } from './utils/d3-cloud';
5
5
 
6
- export type WordCloudOptions = Omit<Record<string, any>, 'type'>;
6
+ type Callable<T> = T | ((datum: any, i: number) => T);
7
+
8
+ /**
9
+ * See the document here: https://github.com/jasondavies/d3-cloud.
10
+ */
11
+ export type WordCloudOptions = {
12
+ /**
13
+ * Internally, the layout uses setInterval to avoid locking up the browser’s event loop.
14
+ * If specified, time is the maximum amount of time that can be spent during the current timestep.
15
+ * If not specified, returns the current maximum time interval, which defaults to Infinity.
16
+ */
17
+ timeInterval: number;
18
+ /**
19
+ * If specified, sets the words array. If not specified, returns the current words array, which defaults to [].
20
+ */
21
+ words: any[];
22
+ /**
23
+ * If specified, sets the rectangular [width, height] of the layout.
24
+ * If not specified, returns the current size, which defaults to [1, 1].
25
+ */
26
+ size: [number, number];
27
+ /**
28
+ * If specified, sets the font accessor function, which indicates the font face for each word.
29
+ * If not specified, returns the current font accessor function, which defaults to "serif".
30
+ * A constant may be specified instead of a function.
31
+ */
32
+ font: Callable<CSSStyleDeclaration['fontFamily']>;
33
+ /**
34
+ * TODO: Cname of font.
35
+ */
36
+ fontFamily: Callable<CSSStyleDeclaration['fontFamily']>;
37
+ /**
38
+ * If specified, sets the fontStyle accessor function, which indicates the font style for each word.
39
+ * If not specified, returns the current fontStyle accessor function, which defaults to "normal".
40
+ * A constant may be specified instead of a function.
41
+ */
42
+ fontStyle: Callable<CSSStyleDeclaration['fontFamily']>;
43
+ /**
44
+ * If specified, sets the fontWeight accessor function, which indicates the font weight for each word.
45
+ * If not specified, returns the current fontWeight accessor function, which defaults to "normal".
46
+ * A constant may be specified instead of a function.
47
+ */
48
+ fontWeight: Callable<CSSStyleDeclaration['fontFamily']>;
49
+ /**
50
+ * If specified, sets the fontSize accessor function, which indicates the numerical font size for each word.
51
+ * If not specified, returns the current fontSize accessor function, which defaults to:
52
+ *
53
+ * > function(d) { return Math.sqrt(d.value); }
54
+ *
55
+ * A constant may be specified instead of a function.
56
+ *
57
+ * If the fontSize is an array, it will be normalized to the range of [min, max] of the data value.
58
+ * If the fontSize is a function, it will be called with the data value.
59
+ * If the fontSize is a number, it will be used as the constant value.
60
+ */
61
+ fontSize: number | [number, number] | ((d: any) => number);
62
+ /**
63
+ * If specified, sets the rotate accessor function, which indicates the rotation angle (in degrees) for each word.
64
+ * If not specified, returns the current rotate accessor function, which defaults to:
65
+ *
66
+ * function() { return (~~(Math.random() * 6) - 3) * 30; }
67
+ *
68
+ * A constant may be specified instead of a function.
69
+ */
70
+ rotate: Callable<number>;
71
+ /**
72
+ * If specified, sets the text accessor function, which indicates the text for each word.
73
+ * If not specified, returns the current text accessor function, which defaults to:
74
+ *
75
+ * function(d) { return d.text; }
76
+ *
77
+ * A constant may be specified instead of a function.
78
+ */
79
+ text: Callable<string>;
80
+ /**
81
+ * If specified, sets the padding accessor function, which indicates the numerical padding for each word.
82
+ * If not specified, returns the current padding, which defaults to 1.
83
+ */
84
+ padding: Callable<number>;
85
+
86
+ /**
87
+ * If specified, sets the internal random number generator, used for selecting the initial position of each word,
88
+ * and the clockwise/counterclockwise direction of the spiral for each word.
89
+ * This should return a number in the range [0, 1).
90
+ * If not specified, returns the current random number generator, which defaults to Math.random.
91
+ */
92
+ random: () => number;
93
+ spiral: any;
94
+ };
7
95
 
8
- const DEFAULT_OPTIONS: Partial<WordCloudOptions> = {
9
- size: [500, 500],
10
- fontSize: [14, 28],
96
+ const DEFAULT_OPTIONS = {
97
+ fontSize: [20, 60],
98
+ font: 'Impact',
99
+ padding: 2,
100
+ rotate: function () {
101
+ return (~~(Math.random() * 6) - 3) * 30;
102
+ },
11
103
  };
12
104
 
105
+ /**
106
+ * Process the image mask of wordCloud.
107
+ * @param img
108
+ * @returns
109
+ */
13
110
  export function processImageMask(
14
111
  img: HTMLImageElement | string,
15
112
  ): Promise<HTMLImageElement> {
@@ -33,6 +130,12 @@ export function processImageMask(
33
130
  });
34
131
  }
35
132
 
133
+ /**
134
+ * normalize fontSize range to d3-cloud fontSize function.
135
+ * @param fontSize
136
+ * @param range
137
+ * @returns
138
+ */
36
139
  export function normalizeFontSize(fontSize: any, range?: [number, number]) {
37
140
  if (typeof fontSize === 'function') return fontSize;
38
141
 
@@ -48,7 +151,7 @@ export function normalizeFontSize(fontSize: any, range?: [number, number]) {
48
151
  return () => fontSize;
49
152
  }
50
153
 
51
- export const WordCloud: DC<WordCloudOptions> = (options) => {
154
+ export const WordCloud: DC<Partial<WordCloudOptions>> = (options) => {
52
155
  return async (data) => {
53
156
  const cloudOptions = Object.assign({}, DEFAULT_OPTIONS, options);
54
157
  const layout = tagCloud();
@@ -80,12 +183,14 @@ export const WordCloud: DC<WordCloudOptions> = (options) => {
80
183
  { x: 0, y: 0 },
81
184
  { x: cw, y: ch },
82
185
  ];
186
+
83
187
  const { _bounds: bounds = defaultBounds, _tags, hasImage } = result;
84
188
 
85
- const tags = _tags.map(({ x, y, ...rest }) => ({
189
+ const tags = _tags.map(({ x, y, font, ...rest }) => ({
86
190
  ...rest,
87
191
  x: x + cw / 2,
88
192
  y: y + ch / 2,
193
+ fontFamily: font,
89
194
  }));
90
195
 
91
196
  // Append two data to replace the corner of top-left and bottom-right, avoid calculate the actual bounds will occur some error.
package/src/lib/core.ts CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  Shape,
33
33
  Density,
34
34
  Heatmap,
35
+ WordCloud as WordCloudGeometry,
35
36
  } from '../mark';
36
37
  import { Category10, Category20 } from '../palette';
37
38
  import {
@@ -153,6 +154,7 @@ import {
153
154
  Sort as DataSort,
154
155
  KDE as DataKDE,
155
156
  Log as DataLog,
157
+ WordCloud,
156
158
  } from '../data';
157
159
  import {
158
160
  OverlapDodgeY,
@@ -177,6 +179,7 @@ export function corelib() {
177
179
  'data.join': DataJoin,
178
180
  'data.kde': DataKDE,
179
181
  'data.log': DataLog,
182
+ 'data.wordCloud': WordCloud,
180
183
  'transform.stackY': StackY,
181
184
  'transform.binX': BinX,
182
185
  'transform.bin': Bin,
@@ -236,6 +239,7 @@ export function corelib() {
236
239
  'mark.shape': Shape,
237
240
  'mark.density': Density,
238
241
  'mark.heatmap': Heatmap,
242
+ 'mark.wordCloud': WordCloudGeometry,
239
243
  'palette.category10': Category10,
240
244
  'palette.category20': Category20,
241
245
  'scale.linear': ScaleLinear,
package/src/mark/text.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { MarkComponent as MC, Vector2, Mark } from '../runtime';
2
2
  import { TextMark } from '../spec';
3
- import { TextShape, TextBadge } from '../shape';
3
+ import { TextShape, TextBadge, TextTag } from '../shape';
4
4
  import { MaybeTuple, MaybeVisualPosition } from '../transform';
5
5
  import {
6
6
  baseGeometryChannels,
@@ -14,6 +14,7 @@ import {
14
14
  const shape = {
15
15
  text: TextShape,
16
16
  badge: TextBadge,
17
+ tag: TextTag,
17
18
  };
18
19
 
19
20
  export type TextOptions = Omit<TextMark, 'type'>;
@@ -42,8 +43,8 @@ Text.props = {
42
43
  { name: 'x', required: true },
43
44
  { name: 'y', required: true },
44
45
  { name: 'text', scale: 'identity' },
45
- { name: 'fontSize' },
46
- { name: 'rotate' },
46
+ { name: 'fontSize', scale: 'identity' },
47
+ { name: 'rotate', scale: 'identity' },
47
48
  ],
48
49
  preInference: [
49
50
  ...basePreInference(),
@@ -1,7 +1,6 @@
1
1
  import { deepMix } from '@antv/util';
2
2
  import { CompositeMarkComponent as CC } from '../runtime';
3
3
  import { WordCloudMark } from '../spec';
4
- import { WordCloud as WordCloudTransform } from '../data';
5
4
 
6
5
  export type WordCloudOptions = Omit<WordCloudMark, 'type'>;
7
6
 
@@ -14,11 +13,7 @@ function initializeData(data, encode) {
14
13
  }));
15
14
  }
16
15
 
17
- const GET_DEFAULT_LAYOUT_OPTIONS = (width, height) => ({
18
- size: [width, height],
19
- });
20
-
21
- const GET_DEFAULT_OPTIONS = (width, height) => ({
16
+ const GET_DEFAULT_OPTIONS = () => ({
22
17
  axis: false,
23
18
  type: 'text',
24
19
  encode: {
@@ -27,15 +22,14 @@ const GET_DEFAULT_OPTIONS = (width, height) => ({
27
22
  text: 'text',
28
23
  rotate: 'rotate',
29
24
  fontSize: 'size',
25
+ shape: 'tag',
30
26
  },
31
27
  scale: {
32
- x: { domain: [0, width], range: [0, 1] },
33
- y: { domain: [0, height], range: [0, 1] },
34
- fontSize: { type: 'identity' },
35
- rotate: { type: 'identity' },
28
+ x: { range: [0, 1] },
29
+ y: { range: [0, 1] },
36
30
  },
37
31
  style: {
38
- textAlign: 'center',
32
+ fontFamily: (d) => d.fontFamily,
39
33
  },
40
34
  });
41
35
 
@@ -53,13 +47,17 @@ export const WordCloud: CC<WordCloudOptions> = async (options, context) => {
53
47
 
54
48
  const initializedData = initializeData(data, encode);
55
49
 
56
- const transformData = await WordCloudTransform({
57
- ...GET_DEFAULT_LAYOUT_OPTIONS(width, height),
58
- ...layout,
59
- })(initializedData);
60
-
61
- return deepMix({}, GET_DEFAULT_OPTIONS(width, height), {
62
- data: transformData,
50
+ return deepMix({}, GET_DEFAULT_OPTIONS(), {
51
+ data: {
52
+ value: initializedData,
53
+ transform: [
54
+ {
55
+ type: 'wordCloud',
56
+ size: [width, height],
57
+ ...layout,
58
+ },
59
+ ],
60
+ },
63
61
  encode,
64
62
  scale,
65
63
  style,
@@ -34,6 +34,7 @@ export { Circle as PointCircle } from './point/circle';
34
34
  export { Vector as VectorShape } from './vector/vector';
35
35
  export { Text as TextShape } from './text/text';
36
36
  export { Badge as TextBadge } from './text/badge';
37
+ export { Tag as TextTag } from './text/tag';
37
38
  export { Area as AreaShape } from './area/area';
38
39
  export { Smooth as AreaSmooth } from './area/smooth';
39
40
  export { HVH as AreaHVH } from './area/hvh';
@@ -94,6 +95,7 @@ export type { TriangleDownOptions as PointTriangleDownOptions } from './point/tr
94
95
  export type { VectorOptions as VectorShapeOptions } from './vector/vector';
95
96
  export type { TextOptions as TextShapeOptions } from './text/text';
96
97
  export type { BadgeOptions as TextBadgeOptions } from './text/badge';
98
+ export type { TagOptions as TextTagOptions } from './text/tag';
97
99
  export type { AreaOptions as AreaShapeOptions } from './area/area';
98
100
  export type { SmoothOptions as AreaSmoothOptions } from './area/smooth';
99
101
  export type { HVHOptions as AreaHVHAreaOptions } from './area/hvh';
@@ -0,0 +1,44 @@
1
+ import { Text } from '@antv/g';
2
+ import { ShapeComponent as SC } from '../../runtime';
3
+ import { applyStyle } from '../../shape/utils';
4
+ import { select } from '../../utils/selection';
5
+
6
+ export type TagOptions = Record<string, any>;
7
+
8
+ /**
9
+ * @todo autoRotate when in polar coordinate
10
+ * Tag shape for Text mark, used in wordCloud plot.
11
+ */
12
+ export const Tag: SC<TagOptions> = (options, context) => {
13
+ const { coordinate } = context;
14
+ return (points, value, defaults) => {
15
+ const { color, text = '', fontSize, rotate = 0, transform = '' } = value;
16
+ const textStyle = {
17
+ text: String(text),
18
+ stroke: color,
19
+ fill: color,
20
+ fontSize,
21
+ textAlign: 'center',
22
+ textBaseline: 'middle',
23
+ };
24
+ const [[x0, y0]] = points;
25
+ const n = select(new Text())
26
+ .style('x', x0)
27
+ .style('y', y0)
28
+ .call(applyStyle, defaults)
29
+ .style('transformOrigin', 'center center')
30
+ .style('transform', `${transform}rotate(${rotate}deg)`)
31
+ .style('coordCenter', coordinate.getCenter())
32
+ .call(applyStyle, textStyle)
33
+ .call(applyStyle, options)
34
+ .node();
35
+ return n;
36
+ };
37
+ };
38
+
39
+ Tag.props = {
40
+ defaultMarker: 'point',
41
+ defaultEnterAnimation: 'fadeIn',
42
+ defaultUpdateAnimation: 'morphing',
43
+ defaultExitAnimation: 'fadeOut',
44
+ };