Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ava/advisor): init new advisor pipeline & built-in modules to plugin-based architecture #784

Open
wants to merge 6 commits into
base: refactor-advisor-pipeline
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/ava/__tests__/unit/advisor/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const myRule: RuleModule = {
},
trigger: (args) => {
const { chartType } = args;
return ['pie_chart'].includes(chartType);
return ['pie_chart'].includes(chartType!);
},
validator: (args) => {
let result = 1;
Expand Down Expand Up @@ -61,8 +61,8 @@ describe('init Advisor', () => {

test('data to advices with ckb config custom chart', () => {
const splitAngleColor = (dataProps: BasicDataPropertyForAdvice[]) => {
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
const field4Angle = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
const field4Color = dataProps.find((field) => hasSubset(field.levelOfMeasurements!, ['Nominal']));
const field4Angle = dataProps.find((field) => hasSubset(field.levelOfMeasurements!, ['Interval']));
return [field4Color, field4Angle];
};

Expand Down
5 changes: 3 additions & 2 deletions packages/ava/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/ava",
"version": "3.4.1",
"version": "3.5.0-alpha.1",
"description": "A framework for automated visual analytics.",
"author": {
"name": "AntV",
Expand Down Expand Up @@ -55,14 +55,15 @@
},
"dependencies": {
"@antv/antv-spec": "^0.1.0-alpha.18",
"@antv/color-schema": "^0.2.3",
"@antv/g2": "^5.0.8",
"@antv/smart-color": "^0.2.1",
"@antv/color-schema": "^0.2.3",
"bayesian-changepoint": "^1.0.1",
"csstype": "^3.1.2",
"heap-js": "^2.1.6",
"lodash": "^4.17.21",
"regression": "^2.0.1",
"tapable": "^2.2.1",
"tslib": "^2.3.1"
},
"devDependencies": {
Expand Down
18 changes: 12 additions & 6 deletions packages/ava/src/advisor/advise-pipeline/data-to-advices.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { cloneDeep, deepMix } from '../utils';

import { getChartTypeSpec } from './spec-generator';
import { getChartTypeSpec } from './plugin/presets/spec-generator';
import { DEFAULT_COLOR } from './constants';
import { getChartTypeRecommendations } from './get-chart-Type';
import { applyTheme, applyDesignRules, applySmartColor } from './spec-processors';
import { getDataProps, getSelectedData } from './data-processors';
import { getChartTypeRecommendations } from './plugin/presets/chart-type-recommend/get-chart-Type';
import { applyTheme, applyDesignRules, applySmartColor } from './plugin/presets/spec-generator/spec-processors';
import { getDataProps, getSelectedData } from './plugin/presets/data-processors';

import type { ScoringResultForChartType, Advice, AdviseResult, ChartAdviseParams } from '../types';
import type { RuleModule } from '../ruler/types';
import type { ChartKnowledgeBase } from '../../ckb';

/**
* @deprecated 已改造为 plugin 插件形式,之前的硬编码形式函数后续清理掉
* recommending charts given data and dataProps, based on CKB and RuleBase
*
* @param params input params for charts recommending
Expand Down Expand Up @@ -40,13 +41,18 @@ export function dataToAdvices({
const chartTypeRecommendations: ScoringResultForChartType[] = getChartTypeRecommendations({
dataProps,
ruleBase,
chartWIKI: ckb,
chartWiki: ckb,
});

const list: Advice[] = chartTypeRecommendations.map((result) => {
// step 2: generate spec for each chart type
const { score, chartType } = result;
const chartTypeSpec = getChartTypeSpec(chartType, filteredData, dataProps, ckb[chartType]);
const chartTypeSpec = getChartTypeSpec({
chartType,
data: filteredData,
dataProps,
chartKnowledge: ckb[chartType],
});

// step 3: apply spec processors such as design rules, theme, color, to improve spec
if (chartTypeSpec && refine) {
Expand Down
24 changes: 0 additions & 24 deletions packages/ava/src/advisor/advise-pipeline/get-chart-Type.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/ava/src/advisor/advise-pipeline/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './data-to-advices';
export * from './spec-generator';
export * from './data-processors';
export * from './plugin';
1 change: 1 addition & 0 deletions packages/ava/src/advisor/advise-pipeline/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './presets';
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { compareAdvices, scoreRules } from '../../../score-calculator';

import type { ChartKnowledge } from '../../../../../ckb';
import type {
ScoringResultForChartType,
BasicDataPropertyForAdvice,
RuleModule,
AdvisorOptions,
AdvisorPipelineContext,
} from '../../../../types';

export const getChartTypeRecommendations = ({
chartWiki,
dataProps,
ruleBase,
options,
advisorContext,
}: {
dataProps: BasicDataPropertyForAdvice[];
chartWiki: Record<string, ChartKnowledge>;
ruleBase: Record<string, RuleModule>;
options?: AdvisorOptions;
advisorContext?: Pick<AdvisorPipelineContext, 'extra'>;
}) => {
const chatTypes = Object.keys(chartWiki);
const list: ScoringResultForChartType[] = chatTypes.map((chartType) => {
return scoreRules(chartType, chartWiki, dataProps, ruleBase, options, advisorContext);
});

// filter and sorter
return list.filter((advice) => advice.score > 0).sort(compareAdvices);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './get-chart-Type';
export { chartTypeRecommendPlugin } from './plugin-config';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getChartTypeRecommendations } from './get-chart-Type';

import type {
AdvisorPipelineContext,
ChartTypeRecommendInput,
ChartTypeRecommendOutput,
AdvisorPluginType,
} from '../../../../types';

export const chartTypeRecommendPlugin: AdvisorPluginType<ChartTypeRecommendInput, ChartTypeRecommendOutput> = {
name: 'defaultChartTypeRecommend',
execute(input: ChartTypeRecommendInput, context?: AdvisorPipelineContext): ChartTypeRecommendOutput {
const { dataProps } = input;
const { advisor, options, extra } = context || {};
const chartTypeRecommendations = getChartTypeRecommendations({
dataProps,
chartWiki: advisor.ckb,
ruleBase: advisor.ruleBase,
options,
advisorContext: { extra },
});
return { chartTypeRecommendations };
},
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataFrame } from '../../../data';
import { DataFrame } from '../../../../../data';

import type { Data } from '../../../common/types';
import type { BasicDataPropertyForAdvice } from '../../types';
import type { Data } from '../../../../../common/types';
import type { BasicDataPropertyForAdvice } from '../../../../types';

/**
* A utility function that assemble dataProps with user input and DataFrame
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Data } from '../../../common/types';
import { Data } from '../../../../../common/types';

/** filter out fields that are not included for advising */
export const getSelectedData = ({ data, fields }: { data: Data; fields?: string[] }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './get-data-properties';
export * from './get-selected-data';
export { dataProcessorPlugin } from './plugin-config';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { cloneDeep } from 'lodash';

import { getDataProps } from './get-data-properties';
import { getSelectedData } from './get-selected-data';

import type {
AdvisorPipelineContext,
DataProcessorInput,
DataProcessorOutput,
AdvisorPluginType,
} from '../../../../types';

export const dataProcessorPlugin: AdvisorPluginType<DataProcessorInput, DataProcessorOutput> = {
name: 'defaultDataProcessor',
execute: (input: DataProcessorInput, context: AdvisorPipelineContext): DataProcessorOutput => {
const { data, customDataProps } = input;
const { fields } = context?.options || {};
const copyData = cloneDeep(data);
const dataProps = getDataProps(copyData, fields, customDataProps);
const filteredData = getSelectedData({ data: copyData, fields });
return {
data: filteredData,
dataProps,
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './chart-type-recommend';
export * from './data-processors';
export * from './spec-generator';
export * from './visual-encoder';
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { hasSubset, intersects } from '../../../utils';
import { splitAreaXYSeries } from '../splitFields';
import { hasSubset, intersects } from '../../../../../utils';
import { splitAreaXYSeries } from '../../visual-encoder/split-fields';
import { getLineSize } from '../../visual-encoder/utils';

import type { Data } from '../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../types';
import type { Data, Datum } from '../../../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../../../types';

export function areaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const field4X = dataProps.find((field) => intersects(field.levelOfMeasurements, ['Time', 'Ordinal']));
Expand All @@ -16,6 +17,10 @@ export function areaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]):
encode: {
x: field4X.name,
y: field4Y.name,
size: (datum: Datum) => getLineSize(datum, data, { field4X }),
},
legend: {
size: false,
},
};

Expand All @@ -33,6 +38,10 @@ export function stackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvi
x: field4X.name,
y: field4Y.name,
color: field4Series.name,
size: (datum: Datum) => getLineSize(datum, data, { field4Split: field4Series, field4X }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 size 处理是因为,在折线图数据只有单点数据时,由于默认 size 太小,一个点看不见,会误以为空白,需要手动加大 size
@hustcc 这个有其他方式么,或者要在 G2 处理吗,否则默认情况下这种离散点展示不太好,用户自己处理有点麻烦
Screenshot 2024-06-19 at 11 18 14

},
legend: {
size: false,
},
transform: [{ type: 'stackY' }],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { splitBarXYSeries } from '../splitFields';
import { splitBarXYSeries } from '../../visual-encoder/split-fields';

import type { Data } from '../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../types';
import type { Data } from '../../../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../../../types';

export function barChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const [field4X, field4Y, field4Color] = splitBarXYSeries(dataProps);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Data } from '../../../../common/types';
import { Advice, BasicDataPropertyForAdvice } from '../../../types';
import { compare, hasSubset } from '../../../utils';
import { splitColumnXYSeries } from '../splitFields';
import { Data } from '../../../../../../common/types';
import { Advice, BasicDataPropertyForAdvice } from '../../../../../types';
import { compare, hasSubset } from '../../../../../utils';
import { splitColumnXYSeries } from '../../visual-encoder/split-fields';

export function columnChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal']));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { intersects, compare, hasSubset } from '../../../utils';
import { intersects, compare, hasSubset } from '../../../../../utils';

import type { Data } from '../../../../common/types';
import type { BasicDataPropertyForAdvice, Advice } from '../../../types';
import type { Data } from '../../../../../../common/types';
import type { BasicDataPropertyForAdvice, Advice } from '../../../../../types';

export function heatmap(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const axisFields = dataProps.filter((field) => intersects(field.levelOfMeasurements, ['Nominal', 'Ordinal']));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hasSubset } from '../../../utils';
import { hasSubset } from '../../../../../utils';

import type { Data } from '../../../../common/types';
import type { BasicDataPropertyForAdvice, Advice } from '../../../types';
import type { Data } from '../../../../../../common/types';
import type { BasicDataPropertyForAdvice, Advice } from '../../../../../types';

export function histogram(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const field = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { splitLineXY } from '../splitFields';
import { splitLineXY } from '../../visual-encoder/split-fields';
import { getLineSize } from '../../visual-encoder/utils';

import type { Data } from '../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../types';
import type { Data, Datum } from '../../../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../../../types';

export function lineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const [field4X, field4Y, field4Color] = splitLineXY(dataProps);
Expand All @@ -13,6 +14,10 @@ export function lineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]):
encode: {
x: field4X.name,
y: field4Y.name,
size: (datum: Datum) => getLineSize(datum, data, { field4X }),
},
legend: {
size: false,
},
};

Expand All @@ -34,6 +39,10 @@ export function stepLineChart(data: Data, dataProps: BasicDataPropertyForAdvice[
x: field4X.name,
y: field4Y.name,
shape: 'hvh',
size: (datum: Datum) => getLineSize(datum, data, { field4X }),
},
legend: {
size: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { splitAngleColor } from '../splitFields';
import { splitAngleColor } from '../../visual-encoder/split-fields';

import type { Data } from '../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../types';
import type { Data } from '../../../../../../common/types';
import type { Advice, BasicDataPropertyForAdvice } from '../../../../../types';

export function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const [field4Color, field4Angle] = splitAngleColor(dataProps);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { pearson } from '../../../../data';
import { hasSubset, compare, intersects } from '../../../utils';
import { pearson } from '../../../../../../data';
import { hasSubset, compare, intersects } from '../../../../../utils';

import type { BasicDataPropertyForAdvice, Advice } from '../../../types';
import type { Data } from '../../../../common/types';
import type { BasicDataPropertyForAdvice, Advice } from '../../../../../types';
import type { Data } from '../../../../../../common/types';

export function scatterPlot(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const intervalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Interval']));
Expand Down
Loading