canvas 面向对象绘制折线图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
<canvas width="600px" height="400px"></canvas>
<script>
    //构造函数
    //属性
    var LineChart=function(ctx){
        this.myCanvas=document.querySelector("canvas");
        this.ctx=ctx || this.myCanvas.getContext("2d");
        this.canvasHeight=this.ctx.canvas.height;
        this.canvasWidth=this.ctx.canvas.width;
        //确定网格大小
        this.gridSize=10;
        //确定坐标系的间距
        this.space=20;
        //确定箭头的大小
        this.arrowSize=10;
        //点的大小
        this.dottedSize=6;
    };
    //行为方法
    LineChart.prototype.init=function(data){
        this.drawGrid();
        this.drawAxis();
        this.drawDotted(data);
    };
    //画坐网格
    LineChart.prototype.drawGrid=function(){
        //确定画几条x轴方向的线
        var xLineTotal=Math.floor(this.canvasHeight/this.gridSize);
        //确定画几条y轴方向的线
        var yLineTotal=Math.floor(this.canvasWidth/this.gridSize);
        //画x方向的线
        for(var i=0;i<xLineTotal;i++){
            this.ctx.beginPath();
            this.ctx.moveTo(0,i*this.gridSize-0.5);
            this.ctx.lineTo(this.canvasWidth,i*this.gridSize-0.5);
            this.ctx.strokeStyle="#eee";
            this.ctx.stroke();
        }
        //画y方向的线
        for(var j=0;j<yLineTotal;j++){
            this.ctx.beginPath();
            this.ctx.moveTo(j*this.gridSize-0.5,0);
            this.ctx.lineTo(j*this.gridSize-0.5,this.canvasHeight);
            this.ctx.strokeStyle="#eee";
            this.ctx.stroke();
        }
    };
    //画坐标系
    LineChart.prototype.drawAxis=function(){
        this.ctx.beginPath();
        this.ctx.moveTo(this.space,this.canvasHeight-this.space);
        this.ctx.lineTo(this.canvasWidth,this.canvasHeight-this.space);
        this.ctx.lineTo(this.canvasWidth-this.arrowSize,this.canvasHeight-this.space+this.arrowSize/2);
        this.ctx.lineTo(this.canvasWidth-this.arrowSize,this.canvasHeight-this.space-this.arrowSize/2);
        this.ctx.lineTo(this.canvasWidth,this.canvasHeight-this.space);
        this.ctx.fill();
        this.ctx.strokeStyle="black";
        this.ctx.stroke();
        this.ctx.beginPath();
        this.ctx.moveTo(this.space,this.canvasHeight-this.space);
        this.ctx.lineTo(this.space,0);
        this.ctx.lineTo(this.space-this.arrowSize/2,this.arrowSize);
        this.ctx.lineTo(this.space+this.arrowSize/2,this.arrowSize);
        this.ctx.lineTo(this.space,0);
        this.ctx.strokeStyle="black";
        this.ctx.fill();
        this.ctx.stroke();
    };
    //画坐点

    LineChart.prototype.drawDotted=function(data){
        var that=this;
        var preX=that.space;
        var preY=that.canvasHeight-that.space;
        data.forEach(function(item,i){
            //要把坐标转换成canvas的坐标
            var x0=item.x+that.space-that.dottedSize/2;
            var y0=that.canvasHeight-(item.y+that.space)-that.dottedSize/2;
            that.ctx.beginPath();
            that.ctx.moveTo(x0,y0);
            that.ctx.lineTo(x0+that.dottedSize,y0);
            that.ctx.lineTo(x0+that.dottedSize,y0+that.dottedSize);
            that.ctx.lineTo(x0,y0+that.dottedSize);
            that.ctx.lineTo(x0,y0);
            that.ctx.fill();
            that.ctx.stroke();
            that.ctx.beginPath();
            //连点
            that.ctx.moveTo(preX,preY);
            that.ctx.lineTo(item.x+that.space,that.canvasHeight-(item.y+that.space));
            that.ctx.stroke();
            preX=item.x+that.space;
            preY=that.canvasHeight-(item.y+that.space);
        });
        };
    var data = [
        {
            x:100,
            y:120
        },
        {
            x:200,
            y:160
        },
        {
            x:300,
            y:240
        },
        {
            x:400,
            y:320
        },
        {
            x:500,
            y:80
        }
    ];
    var lineChart=new LineChart();
    lineChart.init(data);
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值