初探canvas制作一个折线图——(面向对象)

本文介绍了如何利用HTML5的canvas元素,通过面向对象的方式创建一个折线图。从设置画布到展示效果,详细阐述了绘制过程。
摘要由CSDN通过智能技术生成

canvas

  • 准备一块画布
 <style>
        canvas {
    
            border: 1px solid #ccc;
        }
    </style>
<canvas width="600" height="400"></canvas>

起步——start

<script>
    var myCanvas = document.querySelector('canvas');
    var ctx = myCanvas.getContext('2d');

    //构造函数
    var lineChart=function (ctx) {
   
        //获取绘图工具,上下文
       this.ctx=ctx||document.querySelector('canvas').getContext('2d');
       this.canvasWidth=this.ctx.canvas.width;
       this.canvasHeight=this.ctx.canvas.height;
       //网格大小
        this.gridSize=10;
        //坐标系的间距
        this.space=20;
        //箭头大小
        this.arrowSize=10;
        //原点
        this.x0=this.space;
        this.y0=this.canvasHeight-this.space;
        //绘制点
        this.dottedSize=6;
        //点的坐标——和数据有关系,数据可视化

    };
    //行为方法
    lineChart.prototype.init=function (data) {
   
        this.drawGrid();
        this.drawAxis();
        this.drawDotted(data)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值