HTML5绘制饼图实例(二)
本文接上一讲继续来说明
我们继续来制作饼图示例,结合CSS3。先说一下需求,就是一个展示的页面,用饼图来展示数据,下方给出各个扇形的含义,最后给一个按钮点击进入详情。饼图的上方有标题和简单的文字介绍,这些内容竖式结构,页面上显示散列,就是这么简单。
我们首先来设置背景样式,用CSS3的渐变,那么首先页面代码需要先修改,我们一步一步来,首先看下HTML代码:
<!DOCTYPE html><html><head> <meta charset="UTF-8" /> <title>HTML5 Canvas Demo</title><style type="text/css">body{margin:0px;padding:0px;}.bg{ position:absolute; height:100%; width:100%;overflow-x: hidden;overflow-y:hidden; background-image: -moz-linear-gradient(top,#77D1F6, #2F368F); background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, #77D1F6),color-stop(1, #2F368F));}</style></head><body></html>
<div name="code">.panelBg{ position:absolute; height:600px; width:800px; left:250px; top:20px; border-radius: 12px; background-color:#000000; opacity:0.5;}
<div name="code">.panel{ position:absolute; height:550px; width:750px; left:275px; top:45px; border-radius: 12px; background-image: -moz-linear-gradient(top,#EBEBEB, #BFBFBF); background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #EBEBEB),color-stop(1, #BFBFBF));}
<div name="code">#section1{ border:1px solid red; position:relative; float:left; width:235px; height:530px; top:10px; left:10px;}#section2{ border:1px solid red; position:relative; float:left; width:235px; height:530px; top:10px; left:20px;}#section3{border:1px solid red; position:relative; float:left; width:235px; height:530px; top:10px; left:30px;}
<div id="section1"><div name="code">.title{ border:1px solid red; position:relative; margin:5px; font-family:微软雅黑; font-size:22px; font-weight:bold; text-align:center; color:#58595B;}.subTitle{ border:1px solid red; margin:5px; font-family:微软雅黑; font-size:14px;height:70px; font-weight:bold;text-indent:2em; color:#6D6E71;}
<div id="section1"><div width="225" height="168"></canvas></div></div>
.piechart{ border:1px solid red; margin:5px; height:170px;}
<script type="text/javascript">var color = ["#27255F","#2F368F","#3666B0","#2CA8E0","#77D1F6"];function drawCircle(){var data = [5,30,15,30,20];var canvas = document.getElementById("piechart1");var ctx = canvas.getContext("2d");var startPoint= 1.5 * Math.PI;for(var i=0;i<data.length;i++){ctx.fillStyle = color[i];ctx.strokeStyle = color[i];ctx.beginPath();ctx.moveTo(112,84);ctx.arc(112,84,84,startPoint,startPoint-Math.PI*2*(data[i]/100),true);ctx.fill();ctx.stroke();startPoint -= Math.PI*2*(data[i]/100);}}drawCircle();</script>
<div style="background-color: rgb(119, 209, 246); "></div><div style="background-color: rgb(44, 168, 224); "></div><div style="background-color: rgb(54, 102, 176); "></div><div style="background-color: rgb(47, 54, 143); "></div><div style="background-color: rgb(39, 37, 95); "></div><div name="code">.description{ border:1px solid #CCCCCC; border-color: #636263 #464647 #A1A3A5; margin:10px 5px; height:165px; border-radius: 4px;}.scroll-item {position: relative;width: 100%;height: 32px;border-bottom:1px solid gray;cursor: pointer;}.item-even {background-color: #E7E8EC;}.item-odd {background-color: #E0ECF6;}.rect {float: left;margin-top: 5px;margin-left: 5px;width: 20px;height: 20px;border-radius: 3px;}.item-text{margin-left: 5px;height: 100%;float: left;font-size: 14px;font-family: 微软雅黑;vertical-align: middle;display: inline-block;line-height: 30px;}
<div name="code">.button{ border:1px solid #cccccc; cursor:pointer; margin:10px 5px; height:40px; text-align:center; border-radius: 4px; border-color: #636263 #464647 #A1A3A5; text-shadow: 0 1px 1px #F6F6F6; background-image: -moz-linear-gradient(center top, #D9D9D9, #A6A6A6 49%, #A6A6A6 50%); background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #D9D9D9),color-stop(1, #A6A6A6));}.buttonText{ position:relative; font-weight:bold; top:10px; font-family:微软雅黑; color:#58595B;}