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

feat: area chart support smooth shape #1275

Merged
merged 4 commits into from
Nov 18, 2021
Merged
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
44 changes: 42 additions & 2 deletions packages/f2/src/components/area/areaView.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
import { jsx } from '../../jsx';
import { Smooth, BBox } from '@antv/f2-graphic';

export default (props: any) => {
const { records } = props;
const { records, shape } = props;
const isSmooth = shape === 'smooth';
return (
<group>
{records.map((record) => {
const { key, children } = record;
return (
<group key={key}>
{children.map((child) => {
const { points, color, shape } = child;
const { points, bottomPoints, color, shape } = child;
if (isSmooth) {
return (
<custom
attrs={{
points,
lineWidth: '2px',
fill: color,
...shape,
}}
createPath={(context) => {
const constaint = [
Copy link
Member

Choose a reason for hiding this comment

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

感觉这段逻辑应该放到 polygon 里,让 polygon 也支持 smooth 属性会比较好

Copy link
Contributor Author

Choose a reason for hiding this comment

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

改 graphic 吗

Copy link
Member

Choose a reason for hiding this comment

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

那先这样吧,后面再看吧

[ 0, 0 ],
[ 1, 1 ]
];
const bottomPointsLen = bottomPoints?.length || 0;
const topPoints = points.slice(0, points.length - bottomPointsLen);
const topSps = Smooth.smooth(topPoints, false, constaint);
context.beginPath();
context.moveTo(topPoints[0].x, topPoints[0].y);
for (let i = 0, n = topSps.length; i < n; i++) {
const sp = topSps[i];
context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
}

if (bottomPointsLen) {
const bottomSps = Smooth.smooth(bottomPoints, false, constaint);
context.lineTo(bottomPoints[0].x, bottomPoints[0].y);
for (let i = 0, n = bottomSps.length; i < n; i++) {
const sp = bottomSps[i];
context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
}
}
context.closePath();
}}
calculateBox={() => BBox.getBBoxFromPoints(points)}
/>
);
}
return (
<polygon
attrs={{
Expand Down
6 changes: 2 additions & 4 deletions packages/f2/test/components/area/area.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,15 @@ describe('面积图', () => {
>
<Axis field="time" />
<Axis field="tem" />
<Area x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" />
<Line x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" />
<Area x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" shape="smooth" />
<Line x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" shape="smooth" />
</Chart>
</Canvas>
);
// @ts-ignore
const chart = new type(props);
chart.render();
});

// TODO(@buli): 曲线面积图
});

describe('层叠面积图', () => {
Expand Down
9 changes: 1 addition & 8 deletions packages/f2/test/components/line/line.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,6 @@ describe('折线图', () => {
pixelRatio={window.devicePixelRatio}
width={offsetWidth}
height={height}
// 方式一:通过 theme 设置全局 line style
// theme={{
// shapes: {
// line: ['smooth'],
// },
// }}
>
<Chart
ref={chartRef}
Expand All @@ -462,8 +456,7 @@ describe('折线图', () => {
}}
/>
<Axis field="tem" />
{/* 方式二:通过 props style 传入 */}
<Line x="time" y="tem" ref={lineRef} style={{ smooth: true }} />
<Line x="time" y="tem" ref={lineRef} shape="smooth" />
<Tooltip />
</Chart>
</Canvas>
Expand Down
4 changes: 3 additions & 1 deletion packages/graphic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Matrix from './util/matrix';
import Vector2 from './util/vector2';
import * as Smooth from './util/smooth';
import * as BBox from './util/bbox';

import { Canvas, Group, Shape } from './engine';

Expand All @@ -21,5 +23,5 @@ function createCanvas(cfg) {
return new G.Canvas(cfg);
}

export { registerEngine, getEngine, createCanvas, Canvas, Group, Shape, Matrix, Vector2 };
export { registerEngine, getEngine, createCanvas, Canvas, Group, Shape, Matrix, Vector2, Smooth, BBox };
export * from './types';
11 changes: 8 additions & 3 deletions packages/site/examples/area/area/demo/gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ const { props } = (
},
}}
>
<Axis field="time" />
<Axis
field="time"
style={{
label: { align: 'between' },
}}
/>
<Axis field="tem" />
<Area x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" />
<Line x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" />
<Area x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" shape="smooth" />
<Line x="time" y="tem" color="l(90) 0:#1890FF 1:#f7f7f7" shape="smooth" />
</Chart>
</Canvas>
);
Expand Down
9 changes: 1 addition & 8 deletions packages/site/examples/line/line/demo/smooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ const LineChart = (
<Chart
data={data}
scale={scale}
// 方式一:通过 theme 设置全局 line style
// theme={{
// shapes: {
// line: ['smooth'],
// },
// }}
>
<Axis
field="time"
Expand All @@ -62,8 +56,7 @@ const LineChart = (
}}
/>
<Axis field="tem" />
{/* 方式二:通过 props style 传入 */}
<Line x="time" y="tem" style={{ smooth: true }} />
<Line x="time" y="tem" shape="smooth" />
<Tooltip />
</Chart>
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ const { props } = (
y="value"
size={{
field: 'tag',
map: (val) => (val ? 4 : 0),
callback: (val) => (val ? 4 : 0),
}}
color={{
field: 'tag',
map: (val) => (val === 2 ? '#518DFE' : '#F35833'),
callback: (val) => (val === 2 ? '#518DFE' : '#F35833'),
}}
/>
<Legend
Expand Down