canvas绘制直线图

Canvas

canvas 最早由Apple引入WebKit,用于Mac OS X 的 Dashboard,后来又在Safari和Google Chrome被实现。
基于 Gecko 1.8的浏览器,比如 Firefox 1.5, 同样支持这个元素。
<canvas> 元素是WhatWG Web applications 1.0规范的一部分,也包含于HTML 5中。
下面使用canvas通过获取数据 来绘制简单的折线统计图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas width="600" height="400" style="border: 1px solid gray;"></canvas>

  <script>


    var LineChar = 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.dottedSzie = 6;
      // 坐标原点
      this.x0 = this.space;
      this.y0 = this.canvasHeight - this.space;
    }
    // 行为方法
    LineChar.prototype.init = function (data) {
      this.drawGrid();
      this.drawAxis();
      this.drwaDotted(data);
    }
    // 绘制网格
    LineChar.prototype.drawGrid = function () {
      var xLineTotal = Math.floor(this.canvasHeight / this.gridSize);
      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 = "gray";
        this.ctx.stroke();
      }
      var yLineTotal = Math.floor(this.canvasWidth / this.gridSize);
      for (var i = 0; i <= yLineTotal; i++) {
        // 初始化 y轴
        this.ctx.beginPath();
        this.ctx.moveTo(i * this.gridSize - 0.5, 0);
        this.ctx.lineTo(i * this.gridSize - 0.5, this.canvasHeight);
        this.ctx.strokeStyle = "gray";
        this.ctx.stroke();
      }
    }
    // 绘制坐标系
    LineChar.prototype.drawAxis = function () {
      //x轴
      this.ctx.beginPath();
      this.ctx.moveTo(this.x0, this.y0);
      this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
      this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 + this.arrowSize / 2);
      this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 - this.arrowSize / 2);
      this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
      this.ctx.fill();
      this.ctx.stroke();
      // 绘制y轴
      this.ctx.beginPath();
      this.ctx.moveTo(this.x0, this.y0);
      this.ctx.lineTo(this.space, this.space);
      this.ctx.lineTo(this.space + this.arrowSize / 2, this.space + this.arrowSize);
      this.ctx.lineTo(this.space - this.arrowSize / 2, this.space + this.arrowSize);
      this.ctx.lineTo(this.space, this.space);
      this.ctx.fill();
      this.ctx.stroke();
    }
    // 绘制所有点
    LineChar.prototype.drwaDotted = function (data) {
      var that = this;
      // 记录当前坐标 为下次坐标使用
      var precanvasX = 0;
      var precanvasY = 0;
      data.forEach(function (item, i) {
        var canvasX = that.x0 + item.x;
        var canvasY = that.y0 - item.y;
        // 遍历所有的坐标点
        that.ctx.beginPath();
        that.ctx.moveTo(canvasX - that.dottedSzie / 2, canvasY - that.dottedSzie / 2);
        that.ctx.lineTo(canvasX + that.dottedSzie / 2, canvasY - that.dottedSzie / 2);
        that.ctx.lineTo(canvasX + that.dottedSzie / 2, canvasY + that.dottedSzie / 2);
        that.ctx.lineTo(canvasX - that.dottedSzie / 2, canvasY + that.dottedSzie / 2);
        that.ctx.closePath();
        that.ctx.fill();
        //开始连线
        if (i == 0) {
          that.ctx.beginPath();
          that.ctx.moveTo(that.x0, that.y0);
          that.ctx.lineTo(canvasX, canvasY);
          that.ctx.stroke();
        } else {
          that.ctx.beginPath();
          that.ctx.moveTo(precanvasX, precanvasY);
          that.ctx.lineTo(canvasX, canvasY);
          that.ctx.stroke();
        }
        precanvasX = canvasX;
        precanvasY = canvasY;

      });
    }
    //输入数据
    var data = [
      {
        x: 100,
        y: 120
      }, {
        x: 200,
        y: 160
      }, {
        x: 300,
        y: 200
      }, {
        x: 400,
        y: 260
      },{
        x:500,
        y:100
      }
    ]

    var LineChar = new LineChar();
    LineChar.init(data);
  </script>
</body>

</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘴巴嘟嘟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值