【发布时间】:2021-10-17 12:39:19
【问题描述】:
我在订购 SVG 元素时遇到了麻烦。我编写了一个脚本,它将数组中每个数字的比率与其中数字的总和相结合,并针对这些数字创建一个饼图。现在,我正在尝试添加一些动画,但本质上,当我尝试扩大切片的大小时,它似乎在以下路径后面。
我已尝试添加 z-index,但显然该属性不适用于 SVG 元素。我曾尝试使用 3D 变换,但我了解到这也不适用于 SVG。我该如何克服这个问题?
我的代码:
var radius = 100
var slices = [3,4,3]
var sumSlices = slices.reduce((last,num) => last + num)
var degrees = Array.from(slices, num => (360*num)/sumSlices)
var colorPalet = ["#e668a0", "#53e1a0", "#53a7e1", "#f8a1c8", "#95f2c7", "#94caf0"]
var pie
var obj = new Array
var t, f1, f2, g1, g2
t = 0
degrees.forEach((degree, i) => {
obj[i] = new Object
f1 = radius * Math.cos((Math.PI/180)*t)
g1 = -(radius * Math.sin((Math.PI/180) * t))
obj[i]["lGo"] = f1 + " " + g1
t = degree + t
f2 = (radius * Math.cos((Math.PI/180) * t)) - f1
g2 = -(g1 + (radius * Math.sin((Math.PI/180) * t)))
if(degree < 180) obj[i]["aGo"] = radius + " " + radius + " 0 0 0" + f2 + " " + g2
else obj[i]["aGo"] = radius + " " + radius + " 0 1 0" + f2 + " " + g2
})
var tags = new Array
obj.forEach((sl, i) => {
tags[i] = '<path d="M'+ radius + " " + radius +' l'+ sl["lGo"] +' a'+ sl["aGo"] +' Z" fill="'+ colorPalet[i] +'" />'
})
document.getElementById("dd").innerHTML = tags.reduce((a, b) => a + " " + b)
body {margin:0}
.con {
position: absolute;
top:50%;
left:50%
}
.conIn {
position: absolute;
transform: translate(-50%, -50%);
}
svg {
overflow: initial;
}
path {
transition: 1s;
position: relative;
z-index: 1;
transform-origin: center
}
path:hover {
transform:scale(1.4);
}
<div class="con">
<div class="conIn">
<svg id="dd" width="100" height="100" >
</svg>
</div>
</div>
【问题讨论】:
标签: javascript css svg charts