Skip to content

Commit 2af40c1

Browse files
authored
fix(#1909): opacity 配置不生效 (#1920)
1 parent 3ab3a06 commit 2af40c1

File tree

2 files changed

+80
-58
lines changed

2 files changed

+80
-58
lines changed

__tests__/bugs/issue-1909-spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Liquid } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('#1909', () => {
5+
it('liquidStyle', () => {
6+
const liquidPlot = new Liquid(createDiv(), {
7+
percent: 0.75,
8+
liquidStyle: {
9+
fill: 'red',
10+
},
11+
});
12+
liquidPlot.render();
13+
14+
// @ts-ignore
15+
expect(liquidPlot.chart.geometries[0].container.getChildren()[0].getChildren()[0].attr('opacity')).toBe(0.6);
16+
17+
liquidPlot.update({
18+
liquidStyle: {
19+
opacity: 0.1,
20+
},
21+
});
22+
23+
// @ts-ignore
24+
expect(liquidPlot.chart.geometries[0].container.getChildren()[0].getChildren()[0].attr('opacity')).toBe(0.06);
25+
26+
liquidPlot.destroy();
27+
});
28+
});

src/plots/liquid/shapes/liquid.ts

+52-58
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,46 @@ import { IGroup } from '@antv/g-base';
33
import { reduce, isNumber, mix } from '@antv/util';
44
import { transform } from '../../../utils/matrix';
55
import { Point } from '../../../types';
6-
import { ShapeStyle } from '../../../types';
7-
8-
function addFillAttrs(attrs: ShapeStyle, cfg: any) {
9-
if (cfg.color && !attrs.fill) {
10-
attrs.fill = cfg.color;
11-
}
12-
if (isNumber(cfg.opacity)) {
13-
attrs.opacity = attrs.fillOpacity = cfg.opacity;
14-
}
15-
}
16-
17-
function addStrokeAttrs(attrs: ShapeStyle, cfg: any) {
18-
if (cfg.color && !attrs.stroke) {
19-
attrs.stroke = cfg.color;
20-
}
21-
if (isNumber(cfg.opacity)) {
22-
attrs.opacity = attrs.strokeOpacity = cfg.opacity;
23-
}
24-
}
256

267
function lerp(a: number, b: number, factor: number) {
278
return (1 - factor) * a + factor * b;
289
}
2910

30-
const getFillAttrs = (cfg) => {
31-
const defaultAttrs = {
32-
lineWidth: 0,
33-
fillOpacity: 0.85,
34-
};
35-
const attrs = mix({}, defaultAttrs, cfg.style);
36-
addFillAttrs(attrs, cfg);
37-
if (cfg.color && !attrs.stroke) {
38-
attrs.stroke = attrs.stroke || cfg.color;
11+
/**
12+
* 波浪的 attrs
13+
* @param cfg
14+
*/
15+
function getFillAttrs(cfg) {
16+
const attrs = { opacity: 1, ...cfg.style };
17+
18+
if (cfg.color && !attrs.fill) {
19+
attrs.fill = cfg.color;
3920
}
21+
4022
return attrs;
41-
};
23+
}
4224

43-
const getLineAttrs = (cfg) => {
25+
/**
26+
* 圆圈的 attrs
27+
* @param cfg
28+
*/
29+
function getLineAttrs(cfg) {
4430
const defaultAttrs = {
4531
fill: '#fff',
4632
fillOpacity: 0,
4733
lineWidth: 2,
4834
};
4935
const attrs = mix({}, defaultAttrs, cfg.style);
50-
addStrokeAttrs(attrs, cfg);
36+
37+
if (cfg.color && !attrs.stroke) {
38+
attrs.stroke = cfg.color;
39+
}
40+
if (isNumber(cfg.opacity)) {
41+
attrs.opacity = attrs.strokeOpacity = cfg.opacity;
42+
}
43+
5144
return attrs;
52-
};
45+
}
5346

5447
/**
5548
* 用贝塞尔曲线模拟正弦波
@@ -65,11 +58,11 @@ const getLineAttrs = (cfg) => {
6558
*
6659
* whose positions are a: (0, 0), b: (0.5, 0.5), c: (1, 1), d: (PI / 2, 1)
6760
*
68-
* @param {number} x x position of the left-most point (a)
69-
* @param {number} stage 0-3, stating which part of the wave it is
70-
* @param {number} waveLength wave length of the sine wave
71-
* @param {number} amplitude wave amplitude
72-
* @return {Array} 正弦片段曲线
61+
* @param x x position of the left-most point (a)
62+
* @param stage 0-3, stating which part of the wave it is
63+
* @param waveLength wave length of the sine wave
64+
* @param amplitude wave amplitude
65+
* @return 正弦片段曲线
7366
*/
7467
function getWaterWavePositions(x: number, stage: number, waveLength: number, amplitude: number) {
7568
if (stage === 0) {
@@ -101,14 +94,14 @@ function getWaterWavePositions(x: number, stage: number, waveLength: number, amp
10194
}
10295
/**
10396
* 获取水波路径
104-
* @param {number} radius 半径
105-
* @param {number} waterLevel 水位
106-
* @param {number} waveLength 波长
107-
* @param {number} phase 相位
108-
* @param {number} amplitude 震幅
109-
* @param {number} cx 圆心x
110-
* @param {number} cy 圆心y
111-
* @return {Array} path 路径
97+
* @param radius 半径
98+
* @param waterLevel 水位
99+
* @param waveLength 波长
100+
* @param phase 相位
101+
* @param amplitude 震幅
102+
* @param cx 圆心x
103+
* @param cy 圆心y
104+
* @return path 路径
112105
* @reference http://gitlab.alipay-inc.com/datavis/g6/blob/1.2.0/src/graph/utils/path.js#L135
113106
*/
114107
function getWaterWavePath(
@@ -191,16 +184,17 @@ function getWaterWavePath(
191184

192185
/**
193186
* 添加水波
194-
* @param {number} x 中心x
195-
* @param {number} y 中心y
196-
* @param {number} level 水位等级 0~1
197-
* @param {number} waveCount 水波数
198-
* @param {number} colors 色值
199-
* @param {number} group 图组
200-
* @param {number} clip 用于剪切的图形
201-
* @param {number} radius 绘制图形的高度
187+
* @param x 中心x
188+
* @param y 中心y
189+
* @param level 水位等级 0~1
190+
* @param waveCount 水波数
191+
* @param waveAttrs 色值
192+
* @param group 图组
193+
* @param clip 用于剪切的图形
194+
* @param radius 绘制图形的高度
202195
*/
203-
function addWaterWave(x, y, level, waveCount, color, group, clip, radius) {
196+
function addWaterWave(x, y, level, waveCount, waveAttrs, group, clip, radius) {
197+
const { fill, opacity } = waveAttrs;
204198
const bbox = clip.getBBox();
205199
const width = bbox.maxX - bbox.minX;
206200
const height = bbox.maxY - bbox.minY;
@@ -211,8 +205,8 @@ function addWaterWave(x, y, level, waveCount, color, group, clip, radius) {
211205
name: `wave-path-${i}`,
212206
attrs: {
213207
path: getWaterWavePath(radius, bbox.minY + height * level, width / 4, 0, width / lerp(56, 64, factor), x, y),
214-
fill: color,
215-
opacity: lerp(0.6, 0.3, factor),
208+
fill,
209+
opacity: lerp(0.6, 0.3, factor) * opacity,
216210
},
217211
});
218212

@@ -255,7 +249,7 @@ registerShape('interval', 'liquid-fill-gauge', {
255249

256250
// 保证半径是 画布宽高最小值的 radius 值
257251
const radius = Math.min(halfWidth, minXPoint.y * radio);
258-
const { fill } = getFillAttrs(cfg);
252+
const waveAttrs = getFillAttrs(cfg);
259253

260254
const waves = container.addGroup({
261255
name: 'waves',
@@ -275,7 +269,7 @@ registerShape('interval', 'liquid-fill-gauge', {
275269
center.y,
276270
1 - (cfg.points[1] as Point).y, // cfg.y / (2 * cp.y),
277271
3,
278-
fill,
272+
waveAttrs,
279273
waves,
280274
clipCircle,
281275
radius * 4

0 commit comments

Comments
 (0)