首页 > 代码库 > html5 canvas画饼
html5 canvas画饼
1. [图片] lxdpie.jpg
?2. [文件] lqdpie.html ~ 801B 下载(7)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="ru">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script type="text/javascript" src="http://www.mamicode.com/pie.js"></script>
</head>
<body>
<fieldset style=‘position:relative;width:500px;height:400px;‘>
<legend> 刘强东吃饼</legend>
<canvas id="p" width="500" height="400" >Your browser does not support the HTML5 canvas tag.</canvas>
</fieldset>
<script>
var config = {
id: ‘p‘,
type: ‘%‘,
data: [
[‘刘强东‘,18.4],[‘老虎基金‘,22.1],[‘HHGL‘,15.8],[‘DST‘,11.2],[‘BAI‘,9.5],[‘Fortune ising‘,5.3],[‘Kingdom‘,5.0],[‘红杉资本‘,2.0],
]
};
var p= pie(config);
p.render();
</script>
</body>
</html>
3. [文件] pie.js ~ 3KB
(function() {
var Pie = (function() {
var F = function(conf) {
this.type=null;
this.id=null;
this.total=0;
this.container=null;
this.data=http://www.mamicode.com/[];
this.init(conf);
return this;
};
F.prototype = {
defaultBgcolor: [‘deeppink‘, ‘mediumslateblue‘, ‘chartreuse‘, ‘goldenrod‘, "#ffff00", "#2F368F", "#F6A25D", "#2CA8E0", "#77D1F6", ‘#181818‘, ‘#45AB35‘, "#336699", "#5fD1F6"],
init: function(options) {
for (var p in options) {
this[p] = options[p];
}
this.container = document.getElementById(this.id);
},
percentize: function() {
if (this.type && this.type == ‘%‘) {
var sum = 0;
for (var i = 0; i < this.data.length; i++) {
sum += this.data[i][1];
if (this.data[i + 1] && (sum + this.data[i + 1][1]) > 100) {
break;
}
}
if (i != this.data.length) {
this.data = http://www.mamicode.com/this.data.splice(0, i + 1);
}
if (sum != 100) {
this.data.push([‘?‘, Math.ceil(100 - sum), ‘#282828‘]);
}
} else {
var sum = 0;
for (var i = 0; i < this.data.length; i++) {
sum += this.data[i][1];
}
if (0 == this.total) {
this.total = sum;
}
if (this.total - sum > 0) {
this.data.push([‘?‘, this.total - sum, ‘#282828‘]);
}http://www.enterdesk.com/special/shouhui/?
for (var i = 0; i < this.data.length; i++) {
this.data[i][1] = Math.round((this.data[i][1] / this.total) * 100);
}手绘图片
}
},
renderPie: function() {
var x = this.container.clientWidth * .33;
var y = this.container.clientHeight * .5;
var radius = (x > y) ? y : x;
var ctx = this.container.getContext("2d");
var startPoint = 0;
for (var i = 0; i < this.data.length; i++) {
if (null == this.data[i][2]) {
this.data[i][2] = this.defaultBgcolor[i % this.defaultBgcolor.length];
}
ctx.fillStyle = this.data[i][2];
ctx.beginPath();
ctx.moveTo(x, y);
ctx.arc(x, y, radius, startPoint, startPoint + Math.PI * 2 * (this.data[i][1] / 100), false);
ctx.fill();
startPoint += Math.PI * 2 * (this.data[i][1] / 100);
}
return true;
},
renderLabel: function() {
var table = [‘<table >‘];
for (var i = 0; i < this.data.length; i++) {
table.push(‘<tr><td bgcolor="‘);
table.push(this.data[i][2]);
table.push(‘"> </td><td>‘);
table.push(this.data[i][0]);
table.push("</td><td align=right>");
if (this.type && this.type == ‘%‘) {
table.push(this.data[i][1] + "%");
} else {
table.push(Math.ceil(this.total * this.data[i][1] / 100));
}
table.push("</td></tr>");
}
table.push("</table>");
this.container.insertAdjacentHTML("afterEnd", ‘<div > ‘ + table.join("") + ‘</div>‘);
},
render: function() {
this.percentize();
this.renderPie();
this.renderLabel();
return true;
}
};
var Pie = function(conf) {
return new F(conf);
},
r;
return Pie;
})();
window.pie = Pie;
})(window);
html5 canvas画饼
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。