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

fix: 统一线与面积的平滑采样曲线 #5881

Closed
wants to merge 2 commits into from
Closed
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,036 changes: 1,036 additions & 0 deletions __tests__/integration/snapshots/static/mockLineAreaSmooth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,4 @@ export { mockLineCloseX } from './mock-line-close-x';
export { premierLeagueTable } from './premier-league-table';
export { singlePointBasic } from './single-point-basic';
export { populationFlowChordDefault } from './population-flow-chord-default';
export { mockLineAreaSmooth } from './mock-line-area-smooth';
50 changes: 50 additions & 0 deletions __tests__/plots/static/mock-line-area-smooth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { G2Spec } from '../../../src';

export function mockLineAreaSmooth(): G2Spec {
return {
type: 'view',
data: [
{ date: '2-1', close: 1200 },
{ date: '2-2', close: 400 },
{ date: '2-3', close: 600 },
{ date: '2-4', close: 800 },
{ date: '2-5', close: 420 },
{ date: '2-6', close: 1120 },
],
children: [
{
type: 'area',
encode: {
x: 'date',
y: 'close',
shape: 'smooth',
},
transform: [
{
type: 'sample',
thresholds: 100,
strategy: 'lttb',
},
],
style: {
fillOpacity: 0.5,
},
},
{
type: 'line',
encode: {
x: 'date',
y: 'close',
shape: 'smooth',
},
transform: [
{
type: 'sample',
thresholds: 100,
strategy: 'lttb',
},
],
},
],
};
}
6 changes: 1 addition & 5 deletions site/docs/spec/mark/area.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ chart.render();

### smooth

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

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

### vh

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
23 changes: 15 additions & 8 deletions src/shape/area/smooth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { curveCatmullRomClosed, curveCatmullRom } from 'd3-shape';
import { isPolar } from '../../utils/coordinate';
import {
curveCatmullRomClosed,
curveMonotoneY,
curveMonotoneX,
curveCatmullRom,
} 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