Skip to content

Commit da3c848

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

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

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

Lines changed: 26 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,9 @@ function pieChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['
2944
},
3045
transform: [{ type: 'stackY' }],
3146
coordinate: { type: 'theta' },
47+
animate: {
48+
enter: { type: 'waveIn' },
49+
},
3250
};
3351
return spec;
3452
}
@@ -46,6 +64,9 @@ function donutChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice
4664
},
4765
transform: [{ type: 'stackY' }],
4866
coordinate: { type: 'theta', innerRadius: 0.6 },
67+
animate: {
68+
enter: { type: 'waveIn' },
69+
},
4970
};
5071
return spec;
5172
}
@@ -67,7 +88,7 @@ function lineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
6788
type: 'line',
6889
data,
6990
encode: {
70-
x: field4X.name,
91+
x: processDateEncode(field4X.name, dataProps),
7192
y: field4Y.name,
7293
},
7394
};
@@ -87,7 +108,7 @@ function stepLineChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Adv
87108
type: 'line',
88109
data,
89110
encode: {
90-
x: field4X.name,
111+
x: processDateEncode(field4X.name, dataProps),
91112
y: field4Y.name,
92113
shape: 'hvh',
93114
},
@@ -120,7 +141,7 @@ function areaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice[
120141
type: 'area',
121142
data,
122143
encode: {
123-
x: field4X.name,
144+
x: processDateEncode(field4X.name, dataProps),
124145
y: field4Y.name,
125146
},
126147
};
@@ -136,7 +157,7 @@ function stackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvice[]):
136157
type: 'area',
137158
data,
138159
encode: {
139-
x: field4X.name,
160+
x: processDateEncode(field4X.name, dataProps),
140161
y: field4Y.name,
141162
color: field4Series.name,
142163
},
@@ -154,7 +175,7 @@ function percentStackedAreaChart(data: Data, dataProps: BasicDataPropertyForAdvi
154175
type: 'area',
155176
data,
156177
encode: {
157-
x: field4X.name,
178+
x: processDateEncode(field4X.name, dataProps),
158179
y: field4Y.name,
159180
color: field4Series.name,
160181
},

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)