【问题标题】:How can I make an animated svg pie chart如何制作动画 svg 饼图
【发布时间】: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


【解决方案1】:

这里是顺时针展开的多值图表的工作示例:https://jsfiddle.net/bowkugsq/

对原始代码进行了多项更改,但我尝试尽可能保留以让 OP 了解更改并应用于自己的解决方案。

CSS 为每个带有索引后缀的circle 枚举了类。 labels(图例)还有一组类来反映饼图的颜色并进行进一步的自定义。如果想在图表中添加下一张,则需要再添加一个 circle 和 label 类。

最复杂的重构是对饼图的生成进行的(解释如下):

$$('.stat--productivity .pie').forEach(function(pie) {
  var percents = pie.textContent.split(",");
  percents = percents.map(function(pct, idx) {
    return {
      value: parseFloat(pct),
      sum: 0,
      idx: idx + 1
    }
  });
  for (var i = 0; i < percents.length; i++) {
    var prevSum = (i > 0) ? percents[i - 1].sum : 1;
    percents[i].sum = prevSum + percents[i].value;
  };
  percents.reverse();
  var NS = "http://www.w3.org/2000/svg";
  var svg = document.createElementNS(NS, "svg");
  pie.textContent = ''; //clear div
  svg.setAttribute("viewBox", "0 0 32 32");
  pie.appendChild(svg);
  percents.forEach(function(pct, idx) {
    var circle = document.createElementNS(NS, "circle");
    circle.classList.add("circle" + pct.idx);
    circle.setAttribute("r", 16);
    circle.setAttribute("cx", 16);
    circle.setAttribute("cy", 16);
    circle.setAttribute("stroke-dasharray", "0 100");
    setTimeout(function() {
      circle.setAttribute("stroke-dasharray", pct.sum + " 100");
    });
    svg.appendChild(circle);
  });
  percents.forEach(function(pct, idx) {
    var digit = document.createElement("span");
    digit.textContent = pct.value + " %";
    digit.classList.add("label" + (idx + 1));
    pie.appendChild(digit);
  });
});

首先,主饼图 div 现在包含逗号分隔的百分比列表,如下所示:&lt;div class="pie"&gt;25,15,60&lt;/div&gt;。它们的总和必须达到 100%,否则圈子不会闭合。最初,文本内容用逗号分割成percents 数组,该数组随后被转换为具有额外索引和部分总和的对象数组;对于给定的样本,它将产生[{value:25,sum:25,idx:1}, {value:15,sum:40,idx:2}, {value:60,sum:100,idx:3}]。事实上,sum 上移了额外的 1%,因为“stroke-dasharray”的范围“100 100”导致小槽没有闭合完整的圆圈。这样的对象阵列被反转以使动画片段不相互重叠。接下来为每个百分比创建circle 元素,就像在原始 OP 代码中一样。最后percent数组被处理为spans加上带有原始值的标签。

免责声明:代码结构可以更好,但与原始方法的相似性是主要驱动力。

【讨论】:

    【解决方案2】:

    如果您想学习新知识并提高编码技能,可以尝试自己编写代码,但如果您出于任何其他原因需要此功能,我建议您不要尝试重新发明轮子并使用“现成”的东西-使用'

    看看这个:PieGraph

    【讨论】:

    • 我更正了我的问题:我需要在现有的饼图中再添加两个值并使用给定的代码。在添加额外的两个值之前,图表起初看起来不错,但我不知道我的错误在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2019-11-04
    相关资源
    最近更新 更多