@antv/g2 5.3.3-beta.0 → 5.3.4-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/README.zh-CN.md +8 -0
- package/dist/g2.lite.min.js +30 -22
- package/dist/g2.min.js +30 -22
- package/esm/api/runtime.d.ts +12 -1
- package/esm/api/runtime.js +89 -6
- package/esm/api/runtime.js.map +1 -1
- package/esm/data/fetch.d.ts +2 -2
- package/esm/data/fetch.js +2 -2
- package/esm/data/fetch.js.map +1 -1
- package/esm/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/esm/data/utils/venn/fmin/bisect.js +30 -0
- package/esm/data/utils/venn/fmin/bisect.js.map +1 -0
- package/esm/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/esm/data/utils/venn/fmin/blas1.js +38 -0
- package/esm/data/utils/venn/fmin/blas1.js.map +1 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js +91 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/esm/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js +66 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/esm/data/utils/venn/fmin/index.d.ts +34 -0
- package/esm/data/utils/venn/fmin/index.js +35 -0
- package/esm/data/utils/venn/fmin/index.js.map +1 -0
- package/esm/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/esm/data/utils/venn/fmin/linesearch.js +60 -0
- package/esm/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/esm/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/esm/data/utils/venn/fmin/nelderMead.js +133 -0
- package/esm/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/esm/data/utils/venn/layout.d.ts +2 -2
- package/esm/data/utils/venn/layout.js +1 -1
- package/esm/data/utils/venn/layout.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.js +2 -2
- package/esm/index.js.map +1 -1
- package/esm/interaction/elementSelect.js +32 -2
- package/esm/interaction/elementSelect.js.map +1 -1
- package/esm/interaction/tooltip.d.ts +57 -0
- package/esm/interaction/tooltip.js +147 -85
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/interaction/utils.d.ts +2 -2
- package/esm/interaction/utils.js +10 -2
- package/esm/interaction/utils.js.map +1 -1
- package/esm/mark/line.js +4 -1
- package/esm/mark/line.js.map +1 -1
- package/esm/runtime/component.js +5 -2
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/plot.js +8 -3
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.d.ts +1 -1
- package/esm/runtime/render.js +4 -2
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/types/common.d.ts +1 -0
- package/esm/shape/connector/connector.js +19 -15
- package/esm/shape/connector/connector.js.map +1 -1
- package/esm/spec/transform.d.ts +1 -0
- package/esm/theme/create.d.ts +0 -1
- package/esm/theme/create.js +0 -1
- package/esm/theme/create.js.map +1 -1
- package/esm/transform/group.d.ts +0 -1
- package/esm/transform/group.js.map +1 -1
- package/esm/transform/groupN.d.ts +1 -1
- package/esm/utils/style.js +11 -2
- package/esm/utils/style.js.map +1 -1
- package/esm/utils/traverse-elements.d.ts +2 -0
- package/esm/utils/traverse-elements.js +13 -0
- package/esm/utils/traverse-elements.js.map +1 -0
- package/lib/api/runtime.d.ts +12 -1
- package/lib/api/runtime.js +96 -13
- package/lib/api/runtime.js.map +1 -1
- package/lib/data/fetch.d.ts +2 -2
- package/lib/data/fetch.js +1 -1
- package/lib/data/fetch.js.map +1 -1
- package/lib/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/lib/data/utils/venn/fmin/bisect.js +34 -0
- package/lib/data/utils/venn/fmin/bisect.js.map +1 -0
- package/lib/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/lib/data/utils/venn/fmin/blas1.js +48 -0
- package/lib/data/utils/venn/fmin/blas1.js.map +1 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js +96 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/lib/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js +71 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/lib/data/utils/venn/fmin/index.d.ts +34 -0
- package/lib/data/utils/venn/fmin/index.js +49 -0
- package/lib/data/utils/venn/fmin/index.js.map +1 -0
- package/lib/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/lib/data/utils/venn/fmin/linesearch.js +64 -0
- package/lib/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/lib/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/lib/data/utils/venn/fmin/nelderMead.js +137 -0
- package/lib/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/lib/data/utils/venn/layout.d.ts +2 -2
- package/lib/data/utils/venn/layout.js +1 -1
- package/lib/data/utils/venn/layout.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/interaction/elementSelect.js +32 -2
- package/lib/interaction/elementSelect.js.map +1 -1
- package/lib/interaction/tooltip.d.ts +57 -0
- package/lib/interaction/tooltip.js +150 -85
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/interaction/utils.d.ts +2 -2
- package/lib/interaction/utils.js +10 -2
- package/lib/interaction/utils.js.map +1 -1
- package/lib/mark/line.js +3 -0
- package/lib/mark/line.js.map +1 -1
- package/lib/runtime/component.js +5 -2
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/plot.js +8 -3
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.d.ts +1 -1
- package/lib/runtime/render.js +4 -2
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/types/common.d.ts +1 -0
- package/lib/shape/connector/connector.js +19 -15
- package/lib/shape/connector/connector.js.map +1 -1
- package/lib/spec/transform.d.ts +1 -0
- package/lib/theme/create.d.ts +0 -1
- package/lib/theme/create.js +0 -1
- package/lib/theme/create.js.map +1 -1
- package/lib/transform/group.d.ts +0 -1
- package/lib/transform/group.js.map +1 -1
- package/lib/transform/groupN.d.ts +1 -1
- package/lib/utils/style.js +11 -2
- package/lib/utils/style.js.map +1 -1
- package/lib/utils/traverse-elements.d.ts +2 -0
- package/lib/utils/traverse-elements.js +17 -0
- package/lib/utils/traverse-elements.js.map +1 -0
- package/package.json +21 -21
- package/src/api/runtime.ts +136 -6
- package/src/data/fetch.ts +5 -4
- package/src/data/utils/venn/fmin/bisect.ts +32 -0
- package/src/data/utils/venn/fmin/blas1.ts +42 -0
- package/src/data/utils/venn/fmin/conjugateGradient.ts +106 -0
- package/src/data/utils/venn/fmin/gradientDescent.ts +75 -0
- package/src/data/utils/venn/fmin/index.ts +35 -0
- package/src/data/utils/venn/fmin/linesearch.ts +70 -0
- package/src/data/utils/venn/fmin/nelderMead.ts +147 -0
- package/src/data/utils/venn/layout.ts +1 -1
- package/src/index.ts +2 -2
- package/src/interaction/elementSelect.ts +32 -3
- package/src/interaction/tooltip.ts +216 -135
- package/src/interaction/utils.ts +12 -5
- package/src/mark/line.ts +3 -1
- package/src/runtime/component.ts +5 -3
- package/src/runtime/plot.ts +7 -1
- package/src/runtime/render.ts +5 -1
- package/src/runtime/types/common.ts +1 -0
- package/src/shape/connector/connector.ts +30 -19
- package/src/spec/transform.ts +1 -0
- package/src/theme/create.ts +0 -1
- package/src/transform/group.ts +0 -1
- package/src/transform/groupN.ts +1 -1
- package/src/utils/style.ts +11 -1
- package/src/utils/traverse-elements.ts +16 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antv/g2",
|
|
3
|
-
"version": "5.3.
|
|
3
|
+
"version": "5.3.4-alpha.0",
|
|
4
4
|
"description": "the Grammar of Graphics in Javascript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -41,10 +41,11 @@
|
|
|
41
41
|
"size": "limit-size",
|
|
42
42
|
"lint": "eslint ./src ./__tests__ && prettier ./src ./__tests__ --check ",
|
|
43
43
|
"fix": "eslint ./src ./__tests__ --fix && prettier ./src ./__tests__ --write ",
|
|
44
|
-
"test": "cross-env TZ=Asia/Shanghai
|
|
45
|
-
"test:unit": "cross-env TZ=Asia/Shanghai
|
|
46
|
-
"test:integration": "cross-env TZ=Asia/Shanghai
|
|
44
|
+
"test": "cross-env TZ=Asia/Shanghai vitest --coverage",
|
|
45
|
+
"test:unit": "cross-env TZ=Asia/Shanghai vitest __tests__/unit/",
|
|
46
|
+
"test:integration": "cross-env TZ=Asia/Shanghai vitest __tests__/integration/",
|
|
47
47
|
"preview": "vite preview",
|
|
48
|
+
"playground": "vite --mode playground",
|
|
48
49
|
"build:umd": "rimraf ./dist && node node_modules/rollup/dist/bin/rollup -c && npm run size",
|
|
49
50
|
"build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib",
|
|
50
51
|
"build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm",
|
|
@@ -67,29 +68,28 @@
|
|
|
67
68
|
"animation"
|
|
68
69
|
],
|
|
69
70
|
"dependencies": {
|
|
70
|
-
"@antv/component": "^2.1.
|
|
71
|
+
"@antv/component": "^2.1.3",
|
|
71
72
|
"@antv/coord": "^0.4.7",
|
|
72
73
|
"@antv/event-emitter": "^0.1.3",
|
|
73
74
|
"@antv/expr": "^1.0.2",
|
|
74
|
-
"@antv/g": "^6.1.
|
|
75
|
-
"@antv/g-canvas": "^2.0.
|
|
76
|
-
"@antv/g-plugin-dragndrop": "^2.0.
|
|
75
|
+
"@antv/g": "^6.1.24",
|
|
76
|
+
"@antv/g-canvas": "^2.0.43",
|
|
77
|
+
"@antv/g-plugin-dragndrop": "^2.0.35",
|
|
77
78
|
"@antv/scale": "^0.4.16",
|
|
78
79
|
"@antv/util": "^3.3.10",
|
|
79
|
-
"@antv/vendor": "^1.0.
|
|
80
|
+
"@antv/vendor": "^1.0.11",
|
|
80
81
|
"flru": "^1.0.2",
|
|
81
|
-
"fmin": "0.0.2",
|
|
82
82
|
"pdfast": "^0.2.0"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"@antv/data-set": "^0.11.8",
|
|
86
|
-
"@antv/g-pattern": "^2.0.
|
|
87
|
-
"@antv/g-plugin-3d": "^2.0.
|
|
86
|
+
"@antv/g-pattern": "^2.0.35",
|
|
87
|
+
"@antv/g-plugin-3d": "^2.0.45",
|
|
88
88
|
"@antv/g-plugin-control": "^2.0.34",
|
|
89
89
|
"@antv/g-plugin-rough-canvas-renderer": "^2.0.42",
|
|
90
90
|
"@antv/g-plugin-rough-svg-renderer": "^2.0.38",
|
|
91
|
-
"@antv/g-svg": "^2.0.
|
|
92
|
-
"@antv/g-webgl": "^2.0.
|
|
91
|
+
"@antv/g-svg": "^2.0.38",
|
|
92
|
+
"@antv/g-webgl": "^2.0.47",
|
|
93
93
|
"@antv/g2-extension-3d": "^0.1.6",
|
|
94
94
|
"@antv/g2-extension-ava": "^0.1.1",
|
|
95
95
|
"@antv/g2-extension-plot": "^0.1.2",
|
|
@@ -99,21 +99,20 @@
|
|
|
99
99
|
"@optimize-lodash/rollup-plugin": "^4.0.4",
|
|
100
100
|
"@rollup/plugin-commonjs": "^21.1.0",
|
|
101
101
|
"@rollup/plugin-json": "^6.1.0",
|
|
102
|
-
"@rollup/plugin-node-resolve": "^15.3.
|
|
102
|
+
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
103
103
|
"@rollup/plugin-terser": "^0.4.4",
|
|
104
104
|
"@types/diff": "^5.2.3",
|
|
105
|
-
"@types/jest": "^26.0.24",
|
|
106
105
|
"@types/pixelmatch": "^5.2.6",
|
|
107
106
|
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
|
108
107
|
"@typescript-eslint/parser": "^4.33.0",
|
|
108
|
+
"@vitest/coverage-istanbul": "^3.1.4",
|
|
109
109
|
"conventional-changelog-cli": "^2.2.2",
|
|
110
110
|
"cross-env": "^7.0.3",
|
|
111
111
|
"eslint": "^7.32.0",
|
|
112
112
|
"eslint-plugin-import": "^2.31.0",
|
|
113
113
|
"fecha": "^4.2.3",
|
|
114
114
|
"husky": "^7.0.4",
|
|
115
|
-
"
|
|
116
|
-
"jsdom": "^19.0.0",
|
|
115
|
+
"jsdom": "^20",
|
|
117
116
|
"limit-size": "^0.1.4",
|
|
118
117
|
"lint-staged": "^10.5.4",
|
|
119
118
|
"npm-run-all": "^4.1.5",
|
|
@@ -123,12 +122,13 @@
|
|
|
123
122
|
"rollup": "^2.79.2",
|
|
124
123
|
"rollup-plugin-polyfill-node": "^0.8.0",
|
|
125
124
|
"rollup-plugin-typescript2": "^0.35.0",
|
|
126
|
-
"rollup-plugin-visualizer": "^5.
|
|
125
|
+
"rollup-plugin-visualizer": "^5.14.0",
|
|
126
|
+
"svgo": "^3.3.2",
|
|
127
127
|
"topojson": "^3.0.2",
|
|
128
|
-
"ts-jest": "^26.5.6",
|
|
129
128
|
"tslib": "^2.8.1",
|
|
130
129
|
"typescript": "^4.9.5",
|
|
131
|
-
"vite": "^
|
|
130
|
+
"vite": "^4.5.14",
|
|
131
|
+
"vitest": "^3.1.4",
|
|
132
132
|
"xmlserializer": "^0.6.1"
|
|
133
133
|
},
|
|
134
134
|
"lint-staged": {
|
package/src/api/runtime.ts
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DisplayObject,
|
|
3
|
+
IRenderer,
|
|
4
|
+
RendererPlugin,
|
|
5
|
+
Canvas as GCanvas,
|
|
6
|
+
} from '@antv/g';
|
|
2
7
|
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
|
|
3
8
|
import { Plugin as DragAndDropPlugin } from '@antv/g-plugin-dragndrop';
|
|
4
|
-
import { debounce } from '@antv/util';
|
|
9
|
+
import { debounce, get } from '@antv/util';
|
|
5
10
|
import EventEmitter from '@antv/event-emitter';
|
|
6
|
-
import {
|
|
11
|
+
import { group } from '@antv/vendor/d3-array';
|
|
12
|
+
import { G2Element } from '../utils/selection';
|
|
13
|
+
import {
|
|
14
|
+
G2Context,
|
|
15
|
+
render,
|
|
16
|
+
destroy,
|
|
17
|
+
ELEMENT_CLASS_NAME,
|
|
18
|
+
VIEW_CLASS_NAME,
|
|
19
|
+
} from '../runtime';
|
|
7
20
|
import { G2Spec, ViewComposition } from '../spec';
|
|
8
21
|
import { ChartEvent } from '../utils/event';
|
|
9
22
|
import type { G2Library } from '../runtime/types/options';
|
|
23
|
+
import {
|
|
24
|
+
findSingleElement,
|
|
25
|
+
maybeValue,
|
|
26
|
+
findSeriesElement,
|
|
27
|
+
} from '../interaction/tooltip';
|
|
28
|
+
import { selectPlotArea } from '../interaction/utils';
|
|
10
29
|
import {
|
|
11
30
|
normalizeContainer,
|
|
12
31
|
removeContainer,
|
|
@@ -46,6 +65,7 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
46
65
|
// Identifies whether bindAutoFit.
|
|
47
66
|
private _hasBindAutoFit = false;
|
|
48
67
|
private _rendering = false;
|
|
68
|
+
private _trailingClear = null;
|
|
49
69
|
private _trailing = false;
|
|
50
70
|
private _trailingResolve = null;
|
|
51
71
|
private _trailingReject = null;
|
|
@@ -86,11 +106,27 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
86
106
|
this._createReject(reject),
|
|
87
107
|
),
|
|
88
108
|
);
|
|
109
|
+
|
|
89
110
|
const [finished1, resolve, reject] = createEmptyPromise<Runtime<Spec>>();
|
|
90
111
|
finished
|
|
91
112
|
.then(resolve)
|
|
113
|
+
.then(() => {
|
|
114
|
+
// Resolve trailing clear.
|
|
115
|
+
if (this._trailingClear) {
|
|
116
|
+
const options = this.options();
|
|
117
|
+
|
|
118
|
+
this._trailingClear();
|
|
119
|
+
|
|
120
|
+
// If clear is called during trailing, recover options for next trailing render.
|
|
121
|
+
if (this._trailing) this.options(options);
|
|
122
|
+
}
|
|
123
|
+
})
|
|
92
124
|
.catch(reject)
|
|
93
|
-
.then(() =>
|
|
125
|
+
.then(() => {
|
|
126
|
+
this._trailingClear = null;
|
|
127
|
+
this._renderTrailing();
|
|
128
|
+
});
|
|
129
|
+
|
|
94
130
|
return finished1;
|
|
95
131
|
}
|
|
96
132
|
|
|
@@ -152,11 +188,20 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
152
188
|
return this;
|
|
153
189
|
}
|
|
154
190
|
|
|
155
|
-
clear() {
|
|
191
|
+
clear(isClearEvents = true) {
|
|
192
|
+
// Clear after render, otherwise render with destroyed context will return infinite promise, which will block trialing render.
|
|
193
|
+
if (this._rendering) {
|
|
194
|
+
this._trailingClear = () => {
|
|
195
|
+
this.clear(isClearEvents);
|
|
196
|
+
};
|
|
197
|
+
// Only reset options, not destroy canvas.
|
|
198
|
+
this._reset();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
156
201
|
const options = this.options();
|
|
157
202
|
this.emit(ChartEvent.BEFORE_CLEAR);
|
|
158
203
|
this._reset();
|
|
159
|
-
destroy(options, this._context, false);
|
|
204
|
+
destroy(options, this._context, false, isClearEvents);
|
|
160
205
|
this.emit(ChartEvent.AFTER_CLEAR);
|
|
161
206
|
}
|
|
162
207
|
|
|
@@ -201,6 +246,91 @@ export class Runtime<Spec extends G2Spec = G2Spec> extends CompositionNode {
|
|
|
201
246
|
return finished;
|
|
202
247
|
}
|
|
203
248
|
|
|
249
|
+
getDataByXY(
|
|
250
|
+
point: { x: number; y: number },
|
|
251
|
+
options: {
|
|
252
|
+
shared?: boolean;
|
|
253
|
+
series?: boolean;
|
|
254
|
+
facet?: boolean;
|
|
255
|
+
startX?: number;
|
|
256
|
+
startY?: number;
|
|
257
|
+
} = {},
|
|
258
|
+
): any[] {
|
|
259
|
+
const {
|
|
260
|
+
shared = false,
|
|
261
|
+
series,
|
|
262
|
+
facet = false,
|
|
263
|
+
startX = 0,
|
|
264
|
+
startY = 0,
|
|
265
|
+
} = options;
|
|
266
|
+
const { canvas, views } = this._context;
|
|
267
|
+
const { document } = canvas;
|
|
268
|
+
const { x, y } = point;
|
|
269
|
+
// Temporarily do not handle the multi - view situation.
|
|
270
|
+
const { coordinate, scale, markState, data: dataMap, key } = views[0];
|
|
271
|
+
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
|
|
272
|
+
const groupKey = shared ? (element) => element.__data__.x : (d) => d;
|
|
273
|
+
const keyGroup = group(elements, groupKey);
|
|
274
|
+
const container = document.getElementsByClassName(
|
|
275
|
+
VIEW_CLASS_NAME,
|
|
276
|
+
)[0] as DisplayObject;
|
|
277
|
+
const root = selectPlotArea(container);
|
|
278
|
+
const hasSeriesInteraction = (markState: Map<string, any>) => {
|
|
279
|
+
return Array.from(markState.values()).some(
|
|
280
|
+
(d) =>
|
|
281
|
+
d.interaction?.['seriesTooltip'] ||
|
|
282
|
+
d.channels?.some(
|
|
283
|
+
(c) => c.name === 'series' && c.values !== undefined,
|
|
284
|
+
),
|
|
285
|
+
);
|
|
286
|
+
};
|
|
287
|
+
const isSeries = maybeValue(
|
|
288
|
+
series,
|
|
289
|
+
hasSeriesInteraction(markState as Map<string, any>),
|
|
290
|
+
);
|
|
291
|
+
const getElementData = (el: G2Element) => get(el, '__data__.data', null);
|
|
292
|
+
const getElementsData = (els: G2Element[]) => els.map(getElementData);
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
// For non-facet and series chart.
|
|
296
|
+
if (
|
|
297
|
+
isSeries &&
|
|
298
|
+
hasSeriesInteraction(markState as Map<string, any>) &&
|
|
299
|
+
!facet
|
|
300
|
+
) {
|
|
301
|
+
const { selectedData } = findSeriesElement({
|
|
302
|
+
root,
|
|
303
|
+
event: { offsetX: x, offsetY: y },
|
|
304
|
+
elements,
|
|
305
|
+
coordinate,
|
|
306
|
+
scale,
|
|
307
|
+
startX,
|
|
308
|
+
startY,
|
|
309
|
+
});
|
|
310
|
+
const viewData = dataMap.get(`${key}-0`);
|
|
311
|
+
return selectedData.map(({ index }) => viewData[index]);
|
|
312
|
+
}
|
|
313
|
+
// For single chart.
|
|
314
|
+
const element = findSingleElement({
|
|
315
|
+
root,
|
|
316
|
+
event: { offsetX: x, offsetY: y },
|
|
317
|
+
elements,
|
|
318
|
+
coordinate,
|
|
319
|
+
scale,
|
|
320
|
+
shared,
|
|
321
|
+
});
|
|
322
|
+
const k = groupKey(element);
|
|
323
|
+
const groupElements = keyGroup.get(k) as G2Element[];
|
|
324
|
+
return groupElements ? getElementsData(groupElements) : [];
|
|
325
|
+
} catch (e) {
|
|
326
|
+
const topMostElement = canvas.document.elementFromPointSync(
|
|
327
|
+
x,
|
|
328
|
+
y,
|
|
329
|
+
) as G2Element;
|
|
330
|
+
return topMostElement ? getElementData(topMostElement) : [];
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
204
334
|
private _create() {
|
|
205
335
|
const { library } = this._context;
|
|
206
336
|
|
package/src/data/fetch.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { autoType as d3AutoType, dsvFormat } from '@antv/vendor/d3-dsv';
|
|
2
|
+
import type { DataComponent as DC } from '../runtime';
|
|
3
|
+
import type { FetchConnector } from '../spec';
|
|
4
4
|
import { identity } from '../utils/helper';
|
|
5
5
|
|
|
6
6
|
export type FetchOptions = Omit<FetchConnector, 'type'>;
|
|
@@ -19,7 +19,8 @@ export const Fetch: DC<FetchOptions> = (options) => {
|
|
|
19
19
|
// @see: https://github.com/d3/d3-dsv#dsv_parse
|
|
20
20
|
const str = await response.text();
|
|
21
21
|
return dsvFormat(delimiter).parse(str, autoType ? d3AutoType : identity);
|
|
22
|
-
}
|
|
22
|
+
}
|
|
23
|
+
if (format === 'json') {
|
|
23
24
|
return await response.json();
|
|
24
25
|
}
|
|
25
26
|
throw new Error(`Unknown format: ${format}.`);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** finds the zeros of a function, given two starting points (which must
|
|
2
|
+
* have opposite signs */
|
|
3
|
+
export function bisect(f, a, b, parameters?: any) {
|
|
4
|
+
parameters = parameters || {};
|
|
5
|
+
const maxIterations = parameters.maxIterations || 100;
|
|
6
|
+
const tolerance = parameters.tolerance || 1e-10;
|
|
7
|
+
const fA = f(a);
|
|
8
|
+
const fB = f(b);
|
|
9
|
+
let delta = b - a;
|
|
10
|
+
|
|
11
|
+
if (fA * fB > 0) {
|
|
12
|
+
throw 'Initial bisect points must have opposite signs';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (fA === 0) return a;
|
|
16
|
+
if (fB === 0) return b;
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < maxIterations; ++i) {
|
|
19
|
+
delta /= 2;
|
|
20
|
+
const mid = a + delta;
|
|
21
|
+
const fMid = f(mid);
|
|
22
|
+
|
|
23
|
+
if (fMid * fA >= 0) {
|
|
24
|
+
a = mid;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (Math.abs(delta) < tolerance || fMid === 0) {
|
|
28
|
+
return mid;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return a + delta;
|
|
32
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// need some basic operations on vectors, rather than adding a dependency,
|
|
2
|
+
// just define here
|
|
3
|
+
export function zeros(x) {
|
|
4
|
+
const r = new Array(x);
|
|
5
|
+
for (let i = 0; i < x; ++i) {
|
|
6
|
+
r[i] = 0;
|
|
7
|
+
}
|
|
8
|
+
return r;
|
|
9
|
+
}
|
|
10
|
+
export function zerosM(x, y) {
|
|
11
|
+
return zeros(x).map(() => zeros(y));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function dot(a, b) {
|
|
15
|
+
let ret = 0;
|
|
16
|
+
for (let i = 0; i < a.length; ++i) {
|
|
17
|
+
ret += a[i] * b[i];
|
|
18
|
+
}
|
|
19
|
+
return ret;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function norm2(a) {
|
|
23
|
+
return Math.sqrt(dot(a, a));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function scale(ret, value, c?: any) {
|
|
27
|
+
for (let i = 0; i < value.length; ++i) {
|
|
28
|
+
ret[i] = value[i] * c;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function weightedSum(ret, w1, v1, w2, v2) {
|
|
33
|
+
for (let j = 0; j < ret.length; ++j) {
|
|
34
|
+
ret[j] = w1 * v1[j] + w2 * v2[j];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function gemv(output, A, x) {
|
|
39
|
+
for (let i = 0; i < output.length; ++i) {
|
|
40
|
+
output[i] = dot(A[i], x);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { dot, gemv, norm2, scale, weightedSum } from './blas1';
|
|
2
|
+
import { wolfeLineSearch } from './linesearch';
|
|
3
|
+
|
|
4
|
+
export function conjugateGradient(f, initial, params) {
|
|
5
|
+
// allocate all memory up front here, keep out of the loop for perfomance
|
|
6
|
+
// reasons
|
|
7
|
+
let current = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
|
|
8
|
+
let next = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
|
|
9
|
+
const yk = initial.slice();
|
|
10
|
+
let temp;
|
|
11
|
+
let a = 1;
|
|
12
|
+
|
|
13
|
+
params = params || {};
|
|
14
|
+
const maxIterations = params.maxIterations || initial.length * 20;
|
|
15
|
+
|
|
16
|
+
current.fx = f(current.x, current.fxprime);
|
|
17
|
+
const pk = current.fxprime.slice();
|
|
18
|
+
scale(pk, current.fxprime, -1);
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < maxIterations; ++i) {
|
|
21
|
+
a = wolfeLineSearch(f, pk, current, next, a);
|
|
22
|
+
|
|
23
|
+
// todo: history in wrong spot?
|
|
24
|
+
if (params.history) {
|
|
25
|
+
params.history.push({
|
|
26
|
+
x: current.x.slice(),
|
|
27
|
+
fx: current.fx,
|
|
28
|
+
fxprime: current.fxprime.slice(),
|
|
29
|
+
alpha: a,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!a) {
|
|
34
|
+
// faiiled to find point that satifies wolfe conditions.
|
|
35
|
+
// reset direction for next iteration
|
|
36
|
+
scale(pk, current.fxprime, -1);
|
|
37
|
+
} else {
|
|
38
|
+
// update direction using Polak–Ribiere CG method
|
|
39
|
+
weightedSum(yk, 1, next.fxprime, -1, current.fxprime);
|
|
40
|
+
|
|
41
|
+
const delta_k = dot(current.fxprime, current.fxprime);
|
|
42
|
+
const beta_k = Math.max(0, dot(yk, next.fxprime) / delta_k);
|
|
43
|
+
|
|
44
|
+
weightedSum(pk, beta_k, pk, -1, next.fxprime);
|
|
45
|
+
|
|
46
|
+
temp = current;
|
|
47
|
+
current = next;
|
|
48
|
+
next = temp;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (norm2(current.fxprime) <= 1e-5) {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (params.history) {
|
|
57
|
+
params.history.push({
|
|
58
|
+
x: current.x.slice(),
|
|
59
|
+
fx: current.fx,
|
|
60
|
+
fxprime: current.fxprime.slice(),
|
|
61
|
+
alpha: a,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return current;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// Solves a system of lienar equations Ax =b for x
|
|
69
|
+
/// using the conjugate gradient method.
|
|
70
|
+
export function conjugateGradientSolve(A, b, x, history?: any) {
|
|
71
|
+
const r = x.slice();
|
|
72
|
+
const Ap = x.slice();
|
|
73
|
+
let rsold;
|
|
74
|
+
let rsnew;
|
|
75
|
+
let alpha;
|
|
76
|
+
|
|
77
|
+
// r = b - A*x
|
|
78
|
+
gemv(Ap, A, x);
|
|
79
|
+
weightedSum(r, 1, b, -1, Ap);
|
|
80
|
+
const p = r.slice();
|
|
81
|
+
rsold = dot(r, r);
|
|
82
|
+
|
|
83
|
+
for (let i = 0; i < b.length; ++i) {
|
|
84
|
+
gemv(Ap, A, p);
|
|
85
|
+
alpha = rsold / dot(p, Ap);
|
|
86
|
+
if (history) {
|
|
87
|
+
history.push({ x: x.slice(), p: p.slice(), alpha: alpha });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//x=x+alpha*p;
|
|
91
|
+
weightedSum(x, 1, x, alpha, p);
|
|
92
|
+
|
|
93
|
+
// r=r-alpha*Ap;
|
|
94
|
+
weightedSum(r, 1, r, -alpha, Ap);
|
|
95
|
+
rsnew = dot(r, r);
|
|
96
|
+
if (Math.sqrt(rsnew) <= 1e-10) break;
|
|
97
|
+
|
|
98
|
+
// p=r+(rsnew/rsold)*p;
|
|
99
|
+
weightedSum(p, 1, r, rsnew / rsold, p);
|
|
100
|
+
rsold = rsnew;
|
|
101
|
+
}
|
|
102
|
+
if (history) {
|
|
103
|
+
history.push({ x: x.slice(), p: p.slice(), alpha: alpha });
|
|
104
|
+
}
|
|
105
|
+
return x;
|
|
106
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { dot, norm2, scale, weightedSum, zeros } from './blas1';
|
|
2
|
+
import { wolfeLineSearch } from './linesearch';
|
|
3
|
+
|
|
4
|
+
export function gradientDescent(f, initial, params) {
|
|
5
|
+
params = params || {};
|
|
6
|
+
const maxIterations = params.maxIterations || initial.length * 100;
|
|
7
|
+
const learnRate = params.learnRate || 0.001;
|
|
8
|
+
const current = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < maxIterations; ++i) {
|
|
11
|
+
current.fx = f(current.x, current.fxprime);
|
|
12
|
+
if (params.history) {
|
|
13
|
+
params.history.push({
|
|
14
|
+
x: current.x.slice(),
|
|
15
|
+
fx: current.fx,
|
|
16
|
+
fxprime: current.fxprime.slice(),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
weightedSum(current.x, 1, current.x, -learnRate, current.fxprime);
|
|
21
|
+
if (norm2(current.fxprime) <= 1e-5) {
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function gradientDescentLineSearch(f, initial, params) {
|
|
30
|
+
params = params || {};
|
|
31
|
+
let current = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
|
|
32
|
+
let next = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
|
|
33
|
+
const maxIterations = params.maxIterations || initial.length * 100;
|
|
34
|
+
let learnRate = params.learnRate || 1;
|
|
35
|
+
const pk = initial.slice();
|
|
36
|
+
const c1 = params.c1 || 1e-3;
|
|
37
|
+
const c2 = params.c2 || 0.1;
|
|
38
|
+
let temp;
|
|
39
|
+
let functionCalls = [];
|
|
40
|
+
|
|
41
|
+
if (params.history) {
|
|
42
|
+
// wrap the function call to track linesearch samples
|
|
43
|
+
const inner = f;
|
|
44
|
+
f = (x, fxprime) => {
|
|
45
|
+
functionCalls.push(x.slice());
|
|
46
|
+
return inner(x, fxprime);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
current.fx = f(current.x, current.fxprime);
|
|
51
|
+
for (let i = 0; i < maxIterations; ++i) {
|
|
52
|
+
scale(pk, current.fxprime, -1);
|
|
53
|
+
learnRate = wolfeLineSearch(f, pk, current, next, learnRate, c1, c2);
|
|
54
|
+
|
|
55
|
+
if (params.history) {
|
|
56
|
+
params.history.push({
|
|
57
|
+
x: current.x.slice(),
|
|
58
|
+
fx: current.fx,
|
|
59
|
+
fxprime: current.fxprime.slice(),
|
|
60
|
+
functionCalls: functionCalls,
|
|
61
|
+
learnRate: learnRate,
|
|
62
|
+
alpha: learnRate,
|
|
63
|
+
});
|
|
64
|
+
functionCalls = [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
temp = current;
|
|
68
|
+
current = next;
|
|
69
|
+
next = temp;
|
|
70
|
+
|
|
71
|
+
if (learnRate === 0 || norm2(current.fxprime) < 1e-5) break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return current;
|
|
75
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2016, Ben Frederickson
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without modification,
|
|
6
|
+
* are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* * Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
* list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
* and/or other materials provided with the distribution.
|
|
14
|
+
*
|
|
15
|
+
* * Neither the name of the author nor the names of contributors may be used to
|
|
16
|
+
* endorse or promote products derived from this software without specific prior
|
|
17
|
+
* written permission.
|
|
18
|
+
*
|
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
23
|
+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
24
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
25
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
26
|
+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
export { bisect } from './bisect';
|
|
32
|
+
export { nelderMead } from './nelderMead';
|
|
33
|
+
export { conjugateGradient, conjugateGradientSolve } from './conjugateGradient';
|
|
34
|
+
export { gradientDescent, gradientDescentLineSearch } from './gradientDescent';
|
|
35
|
+
export { zeros, zerosM, norm2, weightedSum, scale } from './blas1';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { dot, weightedSum } from './blas1';
|
|
2
|
+
|
|
3
|
+
/// searches along line 'pk' for a point that satifies the wolfe conditions
|
|
4
|
+
/// See 'Numerical Optimization' by Nocedal and Wright p59-60
|
|
5
|
+
/// f : objective function
|
|
6
|
+
/// pk : search direction
|
|
7
|
+
/// current: object containing current gradient/loss
|
|
8
|
+
/// next: output: contains next gradient/loss
|
|
9
|
+
/// returns a: step size taken
|
|
10
|
+
export function wolfeLineSearch(f, pk, current, next, a, c1?: any, c2?: any) {
|
|
11
|
+
const phi0 = current.fx;
|
|
12
|
+
const phiPrime0 = dot(current.fxprime, pk);
|
|
13
|
+
let phi = phi0;
|
|
14
|
+
let phi_old = phi0;
|
|
15
|
+
let phiPrime = phiPrime0;
|
|
16
|
+
let a0 = 0;
|
|
17
|
+
|
|
18
|
+
a = a || 1;
|
|
19
|
+
c1 = c1 || 1e-6;
|
|
20
|
+
c2 = c2 || 0.1;
|
|
21
|
+
|
|
22
|
+
function zoom(a_lo, a_high, phi_lo) {
|
|
23
|
+
for (let iteration = 0; iteration < 16; ++iteration) {
|
|
24
|
+
a = (a_lo + a_high) / 2;
|
|
25
|
+
weightedSum(next.x, 1.0, current.x, a, pk);
|
|
26
|
+
phi = next.fx = f(next.x, next.fxprime);
|
|
27
|
+
phiPrime = dot(next.fxprime, pk);
|
|
28
|
+
|
|
29
|
+
if (phi > phi0 + c1 * a * phiPrime0 || phi >= phi_lo) {
|
|
30
|
+
a_high = a;
|
|
31
|
+
} else {
|
|
32
|
+
if (Math.abs(phiPrime) <= -c2 * phiPrime0) {
|
|
33
|
+
return a;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (phiPrime * (a_high - a_lo) >= 0) {
|
|
37
|
+
a_high = a_lo;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
a_lo = a;
|
|
41
|
+
phi_lo = phi;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (let iteration = 0; iteration < 10; ++iteration) {
|
|
49
|
+
weightedSum(next.x, 1.0, current.x, a, pk);
|
|
50
|
+
phi = next.fx = f(next.x, next.fxprime);
|
|
51
|
+
phiPrime = dot(next.fxprime, pk);
|
|
52
|
+
if (phi > phi0 + c1 * a * phiPrime0 || (iteration && phi >= phi_old)) {
|
|
53
|
+
return zoom(a0, a, phi_old);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (Math.abs(phiPrime) <= -c2 * phiPrime0) {
|
|
57
|
+
return a;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (phiPrime >= 0) {
|
|
61
|
+
return zoom(a, a0, phi);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
phi_old = phi;
|
|
65
|
+
a0 = a;
|
|
66
|
+
a *= 2;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return a;
|
|
70
|
+
}
|