3-5 形状篇 圆饼图

知识储备

1.svg 去W3School学习学习

http://www.w3school.com.cn/svg/svg_circle.asp

小测试

示例代码1
html

<div class="pie">10</div>

css

.pie{   
   width: 100px; 
   height: 100px;  
   border-radius: 50%; 
   background: yellowgreen;    
   background-image: linear-gradient(90deg, transparent 50%,#655 0);
}

.pie::before{  
   content: ''; 
   display: block; 
   margin-left: 50%;   
   height: 100%;  
   background-color: inherit;  
   transform-origin: 0 50%;  
   border-radius: 0 100% 100% 0 / 50%;   
   /*transform: rotate(.5turn);*/  
   animation: spin 50s linear infinite,bg 100s step-end infinite; 
   animation-play-state: paused;   
   animation-delay: inherit;
}

这一节我们需要引入JS来更好的实现代码额可用性
js

<script>  
  var pieList =document.getElementsByClassName('pie');
  for(var i=0,len=pieList.length; i<len; i++){  
    var percent = parseFloat(pieList[i].textContent);
    pieList[i].style.animationDelay = '-' + percent + 's';
  }
</script>

svg解法

html

<div class="pie">90</div>

css

svg{    
  width: 300px; 
  height: 300px; 
  transform: rotate(-90deg);    
  background: yellowgreen;  
  border-radius: 50%;
}

circle{  
  fill: yellowgreen;  
  stroke: #655; 
  stroke-width: 32; 
  stroke-dasharray: 50 100;  
  /*animation: fillup 5s linear infinite;*/
}

js

<script>
var pieList =document.getElementsByClassName('pie');
for(i=0,len=pieList.length; i<len; i++){  
  var percent = parseFloat(pieList[i].textContent);    
  var NS = "http://www.w3.org/2000/svg";  
  var svg = document.createElementNS(NS,"svg");    
  var circle = document.createElementNS(NS,"circle"); 
  var title = document.createElementNS(NS,"title");
  circle.setAttribute('r',16);
  circle.setAttribute('cx', 16);  
  circle.setAttribute('cy',16);  
  circle.setAttribute('stroke-dasharray',percent + " 100"); 
  svg.setAttribute('viewBox', '0 0 32 32');  
  title.textContent = pieList[i].textContent;   
  pieList[i].textContent = '';   
  svg.appendChild(title);   
  svg.appendChild(circle); 
  pieList[i].appendChild(svg);
}
</script>

效果图:

圆饼图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 每个人心里都有一片常人无法到达的森林,有野兽的嘶吼和困兽的哀鸣,但除了黑夜还有光明值得等待。 有些人的心里是一片戈...
    swaggie阅读 1,033评论 0 3
  • 《奇葩说》马东最近在混沌学院的演讲几乎是刷爆了我的朋友圈,没办法,宝宝身边都是学霸,可惜我是个学渣。他提到了一个词...
    郝志阳阅读 7,330评论 0 0
  • 内当家 标签: 日记 2007-06-18 08:49 阅读(144)评论(41) ——这是女儿在初中一年级时发表...
    零星往事阅读 4,023评论 0 0
  • 《四》小姐也这么蠢 当初,我把齐阳的照片,给几个要好的姐妹看过,为的也是能够多一些寻他的途径。 不过,我只有齐阳小...
    CC_f4c8阅读 2,224评论 0 0