Skip to content
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
31 changes: 26 additions & 5 deletions packages/ava/src/advisor/advise-pipeline/spec-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ function splitAngleColor(dataProps: BasicDataPropertyForAdvice[]): [ReturnField,
return [field4Color, field4Angle];
}

/**
* Process date column to new Date().
* @param field
* @param dataProps
* @returns
*/
function processDateEncode(field: string, dataProps: BasicDataPropertyForAdvice[]) {
const dp = dataProps.find((dataProp) => dataProp.name === field);

if (dp?.recommendation === 'date') {
return (d) => new Date(d[field]);
Copy link
Member

Choose a reason for hiding this comment

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

要不判断下是否能转成 date 吧,如果是 invalid date,还是输出原 field

Copy link
Member Author

Choose a reason for hiding this comment

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

那会不会有些转了,有些没转?

Copy link
Member

@chenluli chenluli Nov 14, 2023

Choose a reason for hiding this comment

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

那会不会有些转了,有些没转?

嗯 如果部分转了,部分没转确实也会有问题 但不判断的话,可能会导致值都被转成 invalid date 了,严谨点的话需要判断所有的都能转,才做这个转换?

Copy link
Member Author

Choose a reason for hiding this comment

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

已经判断了 date 类型的,还可能转化失败吗?

}
return field;
}

function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
const [field4Color, field4Angle] = splitAngleColor(dataProps);
if (!field4Angle || !field4Color) return null;
Expand All @@ -29,6 +44,9 @@ function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['
},
transform: [{ type: 'stackY' }],
coordinate: { type: 'theta' },
animate: {
enter: { type: 'waveIn' },
},
};
return spec;
}
Expand All @@ -46,6 +64,9 @@ function donutChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice
},
transform: [{ type: 'stackY' }],
coordinate: { type: 'theta', innerRadius: 0.6 },
animate: {
enter: { type: 'waveIn' },
},
};
return spec;
}
Expand All @@ -67,7 +88,7 @@ function lineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
type: 'line',
data,
encode: {
x: field4X.name,
x: processDateEncode(field4X.name, dataProps),
y: field4Y.name,
},
};
Expand All @@ -87,7 +108,7 @@ function stepLineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Adv
type: 'line',
data,
encode: {
x: field4X.name,
x: processDateEncode(field4X.name, dataProps),
y: field4Y.name,
shape: 'hvh',
},
Expand Down Expand Up @@ -120,7 +141,7 @@ function areaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
type: 'area',
data,
encode: {
x: field4X.name,
x: processDateEncode(field4X.name, dataProps),
y: field4Y.name,
},
};
Expand All @@ -136,7 +157,7 @@ function stackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]):
type: 'area',
data,
encode: {
x: field4X.name,
x: processDateEncode(field4X.name, dataProps),
y: field4Y.name,
color: field4Series.name,
},
Expand All @@ -154,7 +175,7 @@ function percentStackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvi
type: 'area',
data,
encode: {
x: field4X.name,
x: processDateEncode(field4X.name, dataProps),
y: field4Y.name,
color: field4Series.name,
},
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@antv/auto-chart": "^2.0.3",
"@antv/chart-advisor": "^2.0.3",
"@antv/ckb": "^2.0.3",
"@antv/g2": "5.0.0",
"@antv/g2": "^5.0.0",
"@antv/g2plot": "^2.0.9",
"@antv/g6": "^4.3.11",
"@antv/lite-insight": "^2.0.3",
Expand Down
5 changes: 3 additions & 2 deletions playground/src/DevPlayground/ChartAdvisor/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useRef } from 'react';
import React, { useEffect, useRef } from 'react';

import { render } from '@antv/g2';

export const Chart = ({ id, spec }: any) => {
const containerRef = useRef<HTMLElement>(null);

useEffect(() => {
if (containerRef.current) {
const container = document.getElementById('container');
Expand All @@ -23,5 +24,5 @@ export const Chart = ({ id, spec }: any) => {
}
}, []);
// @ts-ignore 待 g2 确认渲染方式
return <div ref={container} id={id} style={{ width: '100%', height: 200, margin: 'auto' }}></div>;
return <div ref={containerRef} id={id} style={{ width: '100%', height: 200, margin: 'auto' }}></div>;
};