【发布时间】:2018-12-19 13:05:38
【问题描述】:
我有如下圆弧图:
我想为每个单独的弧添加一个渐变,该渐变从每个单独的弧的外半径流向内半径。
我猜我需要为每条弧线创建一个单独的渐变?
let radius = 100;
for(let i = 0; i < 5; i ++) {
let grad = defs.append('radialGradient')
.attr('id', 'mygrad' + i)
.attr('gradientUnits', 'userSpaceOnUse')
.attr('cx', '0')
.attr('cy', '0')
.attr('r', radius)
grad.append('stop')
.attr('offset', '0%')
.attr('stop-color', 'white');
grad.append('stop')
.attr('offset', '100%')
.attr('stop-color', 'blue');
let arc = d3.arc()
.outerRadius( radius )
.innerRadius( radius - 50)
.startAngle( degToRad(-90) )
.endAngle( degToRad(90) );
g.append('path')
.attr('d', arc)
.attr('fill', `url(#${'mygrad' + i })`)
.attr('stroke', 'lightgrey');
radius += 50;
}
let grad = defs.append('radialGradient')
.attr('id', 'mygrad' + i)
.attr('r', '80%')
grad.append('stop')
.attr('offset', '0%')
.attr('stop-color', 'white');
grad.append('stop')
.attr('offset', '100%')
.attr('stop-color', 'blue');
【问题讨论】:
-
您有 5 个渐变,但您只使用了 1 个。将 .attr('fill',
url(#${'mygrad' + **1** })) 更改为 .attr('fill',url(#${'mygrad' + **i** })) 第 42 行 -
UPS。好的,修复它并更新图像。
标签: d3.js svg radial-gradients