真的不推荐以这种方式创建饼图。通过“那种方式”,我指的是制作笔划宽度与圆的半径相匹配的圆。准确地说,笔划宽度是圆半径的两倍。
一些浏览器(或浏览器版本)和渲染库在渲染这种形式的圆圈时存在错误。推荐的方法是为每个饼图段创建一个路径。
但是,假设您想继续使用此方法,那么您需要了解以下内容。
元素上的笔划图案<circle>
从 3 点钟开始呈现,并围绕圆圈顺时针方向进行。
这就是为什么你rotate(-90)
在上面的例子中有。-90 旋转将圆旋转 -90 度,以便笔画从顶部(12 点钟方向)开始。
破折号图案中的两个数字是<length of dash> <length of gap>
。然后重复该模式。
好的。让我们更新您的 SVG 以添加您请求的额外段。
首先,我建议进行一些更改:
- 将 移动
rotate(-90)
到父组,以便在计算新切片的旋转时不必担心它。
- 如果您使用
rotate()
带有旋转中心的版本,您会发现它会容易得多:rotate(angle, centreX, centreY)
。
- 我们需要将填充颜色 (
fill="bisque"
) 添加到单独的圆圈中。否则,您添加的每个新段的填充将与之前的段重叠。
所以我们的新起点是这样的:
<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid gray; ">
<circle r="5" cx="10" cy="10" fill="bisque" />
<g transform="rotate(-90, 10,10)" fill="none" stroke-width="10">
<circle r="5" cx="10" cy="10"
stroke="tomato"
stroke-dasharray="10.99 31.4"/>
</g>
</svg>
添加 40% 的细分
您需要的行程长度将是40% of 31.4 = 12.56
.
要旋转它以使其从第一段的末尾开始,您需要将其旋转一个等于 的角度(10.99 / 31.4) * 360deg = 126deg
。
<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid gray; ">
<circle r="5" cx="10" cy="10" fill="bisque" />
<g transform="rotate(-90, 10,10)" fill="none" stroke-width="10">
<circle r="5" cx="10" cy="10"
stroke="tomato"
stroke-dasharray="10.99 31.4"/>
<circle r="5" cx="10" cy="10"
stroke="goldenrod"
stroke-dasharray="12.56 31.4"
transform="rotate(126, 10,10)"/>
</g>
</svg>
添加 13% 的细分
您需要的行程长度将是13% of 31.4 = 4.082
.
要旋转它以使其从前一段的末尾开始,您需要将前两段的长度相加,并将其转换为角度。
((10.99 + 12.56) / 31.4) * 360deg = 0.75 * 360 = 270deg`.
<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid gray; ">
<circle r="5" cx="10" cy="10" fill="bisque" />
<g transform="rotate(-90, 10,10)" fill="none" stroke-width="10">
<circle r="5" cx="10" cy="10"
stroke="tomato"
stroke-dasharray="10.99 31.4"/>
<circle r="5" cx="10" cy="10"
stroke="goldenrod"
stroke-dasharray="12.56 31.4"
transform="rotate(126, 10,10)"/>
<circle r="5" cx="10" cy="10"
stroke="cornflowerblue"
stroke-dasharray="4.082 31.4"
transform="rotate(270, 10,10)"/>
</g>
</svg>