【发布时间】:2019-11-22 08:54:38
【问题描述】:
我需要制作一个带有三个值的动画的 svg 饼图,并且在代码中找不到我的错误。当我删除 setTimeout(function(){ circle.setAttribute("stroke-dasharray", p + " 100"); }, 10);从 JavaScript 来看,饼图看起来不错,但没有动画。当我添加那段代码时,它是动画的,但不会加载所有值,而是逆时针加载而不是顺时针加载。我不知道任何 javascript,但需要向现有饼图添加两个额外的值,并使用给定的 javascript 我创建了两个额外的圆圈。在只有一个圆圈的情况下,动画效果很好。我注意到最终结果(动画结束后保留的粉红色饼图)来自 .pie div 35 中的值,但如果我将 div 留空,整个饼图就会消失。
HTML
<div class="widgets">
<div class="left box stats">
<div class="content">
<div class="widget-row">
<div class="stat stat--pie stat--productivity">
<div class="pie">35</div>
</div>
</div>
</div>
</div>
</div>
CSS
.box.stats .stat.stat--pie {
width: 170px;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.box.stats .stat.stat--pie .pie {
position: relative;
height: 170px;
width: 170px;
}
.box.stats .stat.stat--pie .pie > svg {
-webkit-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
transform: rotate(-90deg);
width: 170px;
height: 170px;
}
.box.stats .stat.stat--pie .pie > svg circle {
-webkit-transition: all 1.2s ease-out;
-o-transition: all 1.2s ease-out;
transition: all 1.2s ease-out;
}
.box.stats .content .stat.stat--productivity .pie svg {
border-radius: 50%;
}
.box.stats .content .stat.stat--productivity .pie svg circle {
fill: none;
stroke: #283250;
stroke-width: 32;
}
.box.stats .content .stat.stat--productivity .pie svg .circle2 {
fill: none;
stroke: #E8E05A;
stroke-width: 32;
}
.box.stats .content .stat.stat--productivity .pie svg .circle3 {
fill: none;
stroke: #E24264;
stroke-width: 32;
}
JavaScript
function $$(selector, context) {
context = context || document;
var elements = context.querySelectorAll(selector);
return Array.prototype.slice.call(elements);
}
$$('.stat--productivity .pie').forEach(function(pie) {
var p = parseFloat(pie.textContent);
var NS = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(NS, "svg");
var circle = document.createElementNS(NS, "circle");
var circle2 = document.createElementNS(NS, "circle");
var circle3 = document.createElementNS(NS, "circle");
var digit = document.createElement("H1");
var percent = document.createElement("span");
circle.setAttribute("r", 16);
circle.setAttribute("cx", 16);
circle.setAttribute("cy", 16);
circle.setAttribute("stroke-dasharray", "100 100");
setTimeout(function(){
circle.setAttribute("stroke-dasharray", p + " 100");
}, 10);
circle2.classList.add('circle2');
circle2.setAttribute("r", 16);
circle2.setAttribute("cx", 16);
circle2.setAttribute("cy", 16);
circle2.setAttribute("stroke-dasharray", "90 100");
setTimeout(function(){
circle2.setAttribute("stroke-dasharray", p + " 100");
}, 10);
circle3.classList.add('circle3');
circle3.setAttribute("r", 16);
circle3.setAttribute("cx", 16);
circle3.setAttribute("cy", 16);
circle3.setAttribute("stroke-dasharray", "50 100");
setTimeout(function(){
circle3.setAttribute("stroke-dasharray", p + " 100");
}, 10);
svg.setAttribute("viewBox", "0 0 32 32");
digit.textContent = pie.textContent;
percent.textContent = '%';
pie.textContent = '';
pie.appendChild(svg);
svg.appendChild(circle);
svg.appendChild(circle2);
svg.appendChild(circle3);
pie.appendChild(digit);
pie.appendChild(percent);
});
Link to codepen: https://codepen.io/anon/pen/jjRYwR
【问题讨论】:
-
您的
div的值被javascript 读取为p变量以设置圆形动画的最终值。即使只剩下第一个圆圈(在javascript中注释掉第二个和第三个圆圈),动画仍然是逆时针的。内部调用setAttribute("stroke-dasharray", ...)先将顺序更改为p+ " 100"(与“35 100”相同),然后再调用"100 100",您将获得顺时针动画。 -
您能否添加更多关于我需要更改的详细信息? Coudln真的不明白,它没有用。
-
我很困惑,jsfiddle 上的示例如您所描述的那样工作。您需要从 jsfiddle 复制内容并更新您的 JS 和 CSS,然后更新 HTML div 的内容,如描述中所述。如果您无法处理答案,我不确定社区可以为您做什么......
-
对不起,我没有看到其他答案。谢谢!
标签: javascript html css svg charts