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

优化mark area的smooth的曲线绘制策略 #5877

Merged
merged 4 commits into from
Dec 5, 2023
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
62 changes: 28 additions & 34 deletions __tests__/integration/snapshots/animation/control/interval0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 28 additions & 34 deletions __tests__/integration/snapshots/animation/control/interval1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 28 additions & 31 deletions __tests__/integration/snapshots/animation/control/interval2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 28 additions & 31 deletions __tests__/integration/snapshots/animation/control/interval3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,067 changes: 1,067 additions & 0 deletions __tests__/integration/snapshots/static/aaplAreaLineSmoothSample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions __tests__/plots/static/aapl-area-line-smooth-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { G2Spec } from '../../../src';

export function aaplAreaLineSmoothSample(): G2Spec {
return {
type: 'view',
data: {
type: 'fetch',
value: 'data/aapl.csv',
},
children: [
{
type: 'line',
encode: {
x: 'date',
y: 'close',
shape: 'smooth',
},
style: {
strokeWidth: 4,
},
transform: [
{
type: 'sample',
thresholds: 100,
strategy: 'lttb',
},
],
},
{
type: 'area',
encode: {
x: 'date',
y: 'close',
shape: 'smooth',
},
style: {
fillOpacity: 0.5,
},
transform: [
{
type: 'sample',
thresholds: 100,
strategy: 'lttb',
},
],
},
],
};
}
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export { aaplLineBasicLabelOverlapHide } from './aapl-line-basic-label-overlap-h
export { aaplLineBasicSample } from './aapl-line-basic-sample';
export { aaplLineBasicSampleLabelFilter } from './aapl-line-basic-sample-label-filter';
export { aaplLineAreaBasicSample } from './aapl-line-area-basic-sample';
export { aaplAreaLineSmoothSample } from './aapl-area-line-smooth-sample';
export { aaplLinePointBasicSample } from './aapl-line-point-basic-sample';
export { speciesDensityBasic } from './species-density-basic';
export { speciesViolinBasic } from './species-violin-basic';
Expand Down
12 changes: 4 additions & 8 deletions site/docs/spec/mark/area.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,19 @@ chart.render();

### smooth

除了 `area` 相同配置之外,额外增加下面的属性。

| 属性 | 描述 | 类型 | 默认值 |
| ----- | ----------------------- | -------- | ------ |
| alpha | 平滑曲线的参数(0 ~ 1) | `number` | `0.5` |
和 `area` 配置相同。

### vh

和 `line` 配置相同。
和 `area` 配置相同。

### hv

和 `line` 配置相同。
和 `area` 配置相同。

### hvh

和 `line` 配置相同。
和 `area` 配置相同。

## FAQ

Expand Down
6 changes: 1 addition & 5 deletions site/docs/spec/mark/line.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ chart.render();

### smooth

除了 `line` 相同配置之外,额外增加下面的属性。

| 属性 | 描述 | 类型 | 默认值 |
| ----- | ----------------------- | -------- | ------ |
| alpha | 平滑曲线的参数(0 ~ 1) | `number` | `0.5` |
和 `line` 配置相同。

### vh

Expand Down
22 changes: 14 additions & 8 deletions src/shape/area/smooth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { curveCatmullRomClosed, curveCatmullRom } from 'd3-shape';
import { isPolar } from '../../utils/coordinate';
import {
curveCatmullRomClosed,
curveMonotoneX,
curveMonotoneY,
} from 'd3-shape';
import { isPolar, isTranspose } from '../../utils/coordinate';
import { ShapeComponent as SC } from '../../runtime';
import { Curve } from './curve';

export type SmoothOptions = {
alpha?: number;
};
export type SmoothOptions = Record<string, any>;

export const Smooth: SC<SmoothOptions> = (options, context) => {
const { alpha = 0.5, ...rest } = options;
const { ...rest } = options;
const { coordinate } = context;
return (...params) => {
const curve = isPolar(coordinate) ? curveCatmullRomClosed : curveCatmullRom;
return Curve({ curve: curve.alpha(alpha), ...rest }, context)(...params);
const curve = isPolar(coordinate)
? curveCatmullRomClosed
: isTranspose(coordinate)
? curveMonotoneY
: curveMonotoneX;
return Curve({ curve, ...rest }, context)(...params);
};
};

Expand Down
8 changes: 3 additions & 5 deletions src/shape/line/smooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import { isPolar, isTranspose } from '../../utils/coordinate';
import { ShapeComponent as SC } from '../../runtime';
import { Curve } from './curve';

export type SmoothOptions = {
alpha?: number;
};
export type SmoothOptions = Record<string, any>;

export const Smooth: SC<SmoothOptions> = (options, context) => {
const { alpha = 0.5, ...rest } = options;
const { ...rest } = options;
const { coordinate } = context;
return (...params) => {
const curve = isPolar(coordinate)
? curveCatmullRomClosed
: isTranspose(coordinate)
? curveMonotoneY
: curveMonotoneX;
return Curve({ curve: curve, ...rest }, context)(...params);
return Curve({ curve, ...rest }, context)(...params);
};
};

Expand Down
Loading