Skip to content

Commit 94bd2b7

Browse files
author
逍为
committed
feat(ava): add polar animate, and encode date column
1 parent e45d587 commit 94bd2b7

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

packages/ava/src/advisor/advise-pipeline/spec-mapping.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ function splitAngleColor(dataProps: BasicDataPropertyForAdvice[]): [ReturnField,
1616
return [field4Color, field4Angle];
1717
}
1818

19+
/**
20+
* Process date column to new Date().
21+
* @param field
22+
* @param dataProps
23+
* @returns
24+
*/
25+
function processDateEncode(field: string, dataProps: BasicDataPropertyForAdvice[]) {
26+
const dp = dataProps.find((dataProp) => dataProp.name === field);
27+
28+
if (dp?.recommendation === 'date') {
29+
return (d) => new Date(d[field]);
30+
}
31+
return field;
32+
}
33+
1934
function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] {
2035
const [field4Color, field4Angle] = splitAngleColor(dataProps);
2136
if (!field4Angle || !field4Color) return null;
@@ -29,6 +44,10 @@ function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['
2944
},
3045
transform: [{ type: 'stackY' }],
3146
coordinate: { type: 'theta' },
47+
animate: {
48+
enter: { type: 'waveIn' },
49+
update: { type: 'waveIn' },
50+
},
3251
};
3352
return spec;
3453
}
@@ -46,6 +65,10 @@ function donutChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice
4665
},
4766
transform: [{ type: 'stackY' }],
4867
coordinate: { type: 'theta', innerRadius: 0.6 },
68+
animate: {
69+
enter: { type: 'waveIn' },
70+
update: { type: 'waveIn' },
71+
},
4972
};
5073
return spec;
5174
}
@@ -67,7 +90,7 @@ function lineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
6790
type: 'line',
6891
data,
6992
encode: {
70-
x: field4X.name,
93+
x: processDateEncode(field4X.name, dataProps),
7194
y: field4Y.name,
7295
},
7396
};
@@ -87,7 +110,7 @@ function stepLineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Adv
87110
type: 'line',
88111
data,
89112
encode: {
90-
x: field4X.name,
113+
x: processDateEncode(field4X.name, dataProps),
91114
y: field4Y.name,
92115
shape: 'hvh',
93116
},
@@ -120,7 +143,7 @@ function areaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
120143
type: 'area',
121144
data,
122145
encode: {
123-
x: field4X.name,
146+
x: processDateEncode(field4X.name, dataProps),
124147
y: field4Y.name,
125148
},
126149
};
@@ -136,7 +159,7 @@ function stackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]):
136159
type: 'area',
137160
data,
138161
encode: {
139-
x: field4X.name,
162+
x: processDateEncode(field4X.name, dataProps),
140163
y: field4Y.name,
141164
color: field4Series.name,
142165
},
@@ -154,7 +177,7 @@ function percentStackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvi
154177
type: 'area',
155178
data,
156179
encode: {
157-
x: field4X.name,
180+
x: processDateEncode(field4X.name, dataProps),
158181
y: field4Y.name,
159182
color: field4Series.name,
160183
},

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@antv/auto-chart": "^2.0.3",
4444
"@antv/chart-advisor": "^2.0.3",
4545
"@antv/ckb": "^2.0.3",
46-
"@antv/g2": "5.0.0",
46+
"@antv/g2": "^5.0.0",
4747
"@antv/g2plot": "^2.0.9",
4848
"@antv/g6": "^4.3.11",
4949
"@antv/lite-insight": "^2.0.3",

playground/src/DevPlayground/ChartAdvisor/Chart.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22

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

55
export const Chart = ({ id, spec }: any) => {
66
const containerRef = useRef<HTMLElement>(null);
7+
78
useEffect(() => {
89
if (containerRef.current) {
910
const container = document.getElementById('container');
@@ -23,5 +24,5 @@ export const Chart = ({ id, spec }: any) => {
2324
}
2425
}, []);
2526
// @ts-ignore 待 g2 确认渲染方式
26-
return <div ref={container} id={id} style={{ width: '100%', height: 200, margin: 'auto' }}></div>;
27+
return <div ref={containerRef} id={id} style={{ width: '100%', height: 200, margin: 'auto' }}></div>;
2728
};

0 commit comments

Comments
 (0)