饼状图

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<canvas width="800" height="600" style="border: 1px solid gray;margin: 20px auto;display: block;"></canvas>
</body>
</html>
<script>
//    var can=document.querySelector("canvas");
//    var con=can.getContext("2d");
//    var w=con.canvas.width;
//    var h=con.canvas.height;

//1.画饼状图
//2.绘制饼状图的标题
//3.绘制饼状图的说明



//创建构造函数
    var PieChart= function () {
        this.ctx=document.querySelector("canvas").getContext("2d");
        this.w=this.ctx.canvas.width;
        this.h=this.ctx.canvas.height;
        this.x0=this.w/2+60;//往右边挪一点
        this.y0=this.h/2;
        this.r=150;
        this.outXian=20;
        //设置示例矩形的宽高 和 到画布的间距
        this.rectW=15;
        this.rectY=10;
        this.space = 20;
    }
//初始化
    PieChart.prototype.init= function () {

        this.bingTu(data);
//        this.title();
//        this.shuoMing();
    }
//    绘制饼图
    PieChart.prototype.bingTu=function(data){
    //准备数据
//   将数据转化为弧度
     var angleList = this.angle(data);
        var that=this;
        var start=0;
        angleList.forEach(function (item, i) {
            var end=item.angle+start;
            that.ctx.beginPath();
            that.ctx.moveTo(that.x0,that.y0);
            that.ctx.arc(that.x0,that.y0,that.r,start,end);
            var color = that.ctx.fillStyle=that.random();
            that.ctx.fill();

            //调用tltle
           that.title(start,item.angle,color,item.title);

            //调用说明
            that.shuoMing(i,item.title,color);
            start=end;
        })
    };
//    绘制标题
PieChart.prototype.title=function(start,angle,color,title){
    //1.从圆心到圆外的长度 就是半径加20
    //2.出去的坐标 对边 半径加20乘sin(Y)  旁边 cos(X)
    //弧度 扇形的起始弧度加对应弧度的一半

    //斜边
    var xie = this.r+this.outXian;
    //X轴方向的
    var Xbian=xie*Math.cos(start+angle/2);
    //Y轴方向的
    var Ybian=xie*Math.sin(start+angle/2);

    var outX=this.x0+Xbian;
    var outY=this.y0+Ybian;
    this.ctx.beginPath();
    this.ctx.moveTo(this.x0,this.y0);
    this.ctx.lineTo(outX,outY);
    this.ctx.strokeStyle=color;
    this.ctx.stroke();



    this.ctx.font="14px 宋体";
    var txtWidth=this.ctx.measureText(title).width;
    if(outX>this.x0){
        this.ctx.lineTo(outX+txtWidth,outY);
        this.ctx.textAlign="left";
        this.ctx.textBaseline="bottom";
        this.ctx.fillText(title,outX,outY);

    }else{
        this.ctx.lineTo(outX-txtWidth,outY);
        this.ctx.textAlign="right";
        this.ctx.textBaseline="bottom";
        this.ctx.fillText(title,outX,outY);

    }

    this.ctx.stroke();




};
//    绘制说明
PieChart.prototype.shuoMing=function(index,title,color){
    this.ctx.fillStyle=color;
    this.ctx.fillRect(this.space,this.space+index*(this.rectY+10),this.rectW,this.rectY);

    this.ctx.textBaseline="top";
    this.ctx.font="12px 黑体";
    this.ctx.textAlign="left";
    this.ctx.fillText(title,this.space+this.rectW+10,this.space+index*(this.rectY+10));

};
//转化为弧度
PieChart.prototype.angle=function(data){
    var total=0;
    data.forEach(function (item, i) {
        total+=item.num;
    });
    data.forEach(function(item,i){
        var angle=item.num/total*2*Math.PI;
        item.angle=angle;
    })
    return data;
};
PieChart.prototype.random=function(){
    var r=Math.floor(Math.random()*256);
    var g=Math.floor(Math.random()*256);
    var b=Math.floor(Math.random()*256);
    return 'rgb('+r+','+g+','+b+')';
}


    var data=[{title:"20以下",num:6},{title:"20-25",num:26},{title:"25-30",num:20},{title:"30-35",num:8}];
    var pc=new PieChart();
    pc.init();
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值