diff --git a/__tests__/integration/snapshots/static/AutoMark.png b/__tests__/integration/snapshots/static/AutoMark.png new file mode 100644 index 0000000000..ada457939f Binary files /dev/null and b/__tests__/integration/snapshots/static/AutoMark.png differ diff --git a/__tests__/plots/static/auto-mark.ts b/__tests__/plots/static/auto-mark.ts new file mode 100644 index 0000000000..bfffa50073 --- /dev/null +++ b/__tests__/plots/static/auto-mark.ts @@ -0,0 +1,12 @@ +import { G2Spec } from '../../../src'; +import { Auto } from '../../../src/mark/auto'; + +export function AutoMark(): G2Spec { + return { + type: Auto, + data: { + type: 'fetch', + value: 'data/alphabet.csv', + }, + }; +} diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index 5a996f5d3c..0c7d856a79 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -237,6 +237,7 @@ export { aaplIntervalDateEncodeX } from './aapl-interval-date-encode-x'; export { athletesRectBinLegendStyle } from './athletes-rect-bin-legend-style'; export { HeatmapHeatmapBasic } from './heatmap-heatmap-basic'; export { DiamondHeatmapDensity } from './diamond-heatmap-density'; +export { AutoMark } from './auto-mark'; export { basicLineNullLabel } from './basic-line-null-label'; export { alphabetIntervalViewStyle } from './alphabet-interval-view-style'; export { vennBasic } from './venn-basic'; diff --git a/__tests__/unit/lib/auto.spec.ts b/__tests__/unit/lib/auto.spec.ts new file mode 100644 index 0000000000..4b9360344d --- /dev/null +++ b/__tests__/unit/lib/auto.spec.ts @@ -0,0 +1,10 @@ +import { autolib } from '../../../src/lib'; +import { Auto } from '../../../src/mark'; + +describe('autolib', () => { + it('autolib() should returns expected autolib components.', () => { + expect(autolib()).toEqual({ + 'mark.auto': Auto, + }); + }); +}); diff --git a/__tests__/unit/lib/std.spec.ts b/__tests__/unit/lib/std.spec.ts index 4d91192fe2..e05e7c6d3f 100644 --- a/__tests__/unit/lib/std.spec.ts +++ b/__tests__/unit/lib/std.spec.ts @@ -42,6 +42,7 @@ import { Density as DensityGeometry, Heatmap, Liquid, + Auto, } from '../../../src/mark'; import { Category10, Category20 } from '../../../src/palette'; import { diff --git a/bundle/g2.std.ts b/bundle/g2.std.ts index e5542a8d9d..afdcbe034f 100644 --- a/bundle/g2.std.ts +++ b/bundle/g2.std.ts @@ -1,11 +1,4 @@ -import { - corelib, - plotlib, - graphlib, - geolib, - stdlib, - threedlib, -} from '../src/lib'; +import { corelib, plotlib, graphlib, geolib, stdlib } from '../src/lib'; import { extend, Runtime } from '../src/api'; import { API, CompositionAPI } from '../src/api/extend'; import { G2Spec } from '../src/spec'; diff --git a/package.json b/package.json index 9525c4157e..b731d95648 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "test:unit": "node --expose-gc --max-old-space-size=4096 --unhandled-rejections=strict node_modules/jest/bin/jest __tests__/unit/ --coverage -i --logHeapUsage", "test:integration": "node --expose-gc --max-old-space-size=4096 --unhandled-rejections=strict node_modules/jest/bin/jest __tests__/integration/ --coverage -i --logHeapUsage", "preview": "vite preview", - "build:umd": "rimraf ./dist && rollup -c && npm run size", + "build:umd": "rimraf ./dist && node --expose-gc --max-old-space-size=4096 node_modules/rollup/dist/bin/rollup -c && npm run size", "build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib", "build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm", "build": "run-p build:*", @@ -56,6 +56,7 @@ "animation" ], "dependencies": { + "@antv/ava": "^3.0.7", "@antv/coord": "^0.4.1", "@antv/event-emitter": "^0.1.3", "@antv/g": "^5.18.6", @@ -90,7 +91,9 @@ "@antv/g-webgl": "^1.9.8", "@commitlint/cli": "^11.0.0", "@commitlint/config-conventional": "^17.4.4", + "@optimize-lodash/rollup-plugin": "^4.0.3", "@rollup/plugin-commonjs": "^21.0.2", + "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.1", "@rollup/plugin-terser": "^0.4.3", "@types/d3-array": "3.0.5", diff --git a/rollup.config.js b/rollup.config.js index b798a62435..0c808c3ec2 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,6 +4,8 @@ import terser from '@rollup/plugin-terser'; import commonjs from '@rollup/plugin-commonjs'; import resolve from '@rollup/plugin-node-resolve'; import typescript from 'rollup-plugin-typescript2'; +import json from '@rollup/plugin-json'; +import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin'; const isBundleVis = !!process.env.BUNDLE_VIS; @@ -21,7 +23,8 @@ export default [ treeshake: { preset: 'smallest', // Set `src/exports` as a sideEffects file. - moduleSideEffects: (id, external) => id.includes('src/exports.ts') ? true : false, + moduleSideEffects: (id, external) => + id.includes('src/exports.ts') ? true : false, }, output: [ { @@ -39,8 +42,10 @@ export default [ typescript({ useTsconfigDeclarationDir: true, }), + optimizeLodashImports(), + json(), terser(), ], context: 'window', // Disable 'THIS_IS_UNDEFINED' warnings - })) + })), ]; diff --git a/src/lib/auto.ts b/src/lib/auto.ts new file mode 100644 index 0000000000..db2638d806 --- /dev/null +++ b/src/lib/auto.ts @@ -0,0 +1,7 @@ +import { Auto } from '../mark/auto'; + +export function autolib() { + return { + 'mark.auto': Auto, + } as const; +} diff --git a/src/lib/index.ts b/src/lib/index.ts index e254b20b6c..4fab8408bc 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -5,3 +5,4 @@ export { plotlib } from './plot'; export { threedlib } from './threed'; export { stdlib } from './std'; export { litelib } from './lite'; +export { autolib } from './auto'; diff --git a/src/mark/auto.ts b/src/mark/auto.ts new file mode 100644 index 0000000000..2c4f5de93f --- /dev/null +++ b/src/mark/auto.ts @@ -0,0 +1,22 @@ +import { Advisor } from '@antv/ava'; +import { deepMix } from '@antv/util'; + +export const Auto = (options) => { + const chartAdvisor = new Advisor(); + const { + data, + dataProps, + fields, + options: o, + colorOptions, + ...rest + } = options; + const results = chartAdvisor.advise({ + data, + dataProps, + fields, + options: o, + colorOptions, + }); + return deepMix({}, rest, results?.[0].spec); +}; diff --git a/src/mark/index.ts b/src/mark/index.ts index 8a47de62e8..8edfa5ae79 100644 --- a/src/mark/index.ts +++ b/src/mark/index.ts @@ -32,6 +32,7 @@ export { Gauge } from './gauge'; export { Density } from './density'; export { Heatmap } from './heatmap'; export { Liquid } from './liquid'; +export { Auto } from './auto'; export type { IntervalOptions } from './interval'; export type { RectOptions } from './rect'; diff --git a/src/spec/mark.ts b/src/spec/mark.ts index 1e5095afba..bc0118e96d 100644 --- a/src/spec/mark.ts +++ b/src/spec/mark.ts @@ -1,3 +1,4 @@ +import type { AdviseParams } from '@antv/ava'; import { MarkComponent } from '../runtime'; import { Encode } from './encode'; import { Transform } from './transform'; @@ -18,6 +19,7 @@ import { import { Closeable, Literal2Object, Padding } from './utils'; export type Mark = + | AutoMark | IntervalMark | RectMark | LineMark @@ -51,6 +53,7 @@ export type Mark = | CompositeMark; export type MarkTypes = + | 'auto' | 'interval' | 'rect' | 'line' @@ -391,3 +394,4 @@ export type HeatmapMark = BaseMark<'heatmap'>; export type LiquidMark = BaseMark<'liquid'>; export type CustomMark = BaseMark; +export type AutoMark = BaseMark<'auto'> & AdviseParams;