Skip to content

Commit

Permalink
fix: area and line shares the same curve generator(#5877)
Browse files Browse the repository at this point in the history
* docs: update description of mark shape options

* fix: let area and line use the same curve smoothing strategy

* test: update tests for modification of the area curve strategy

* test: add tests for modification of the area curve strategy

---------

Co-authored-by: taoliujun <taoliujun@qq.com>
  • Loading branch information
taoliujun and taoliujun authored Dec 5, 2023
1 parent 046ea5f commit b212242
Show file tree
Hide file tree
Showing 19 changed files with 1,365 additions and 270 deletions.
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

0 comments on commit b212242

Please sign in to comment.