• h5画图表


    折线:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>用canvas绘制折线图</title>
    </head>
    <body>
    <canvas id="cv"></canvas>
    </body>
    </html>
    <script>
    var cv = document.getElementById("cv");
    cv.width = 600;
    cv.height = 400;
    cv.style.border = "1px solid red";
    var ctx = cv.getContext("2d");
    var data2 = [.3, .1, .2, .4, .2, .7, .3, .9];
    var data3 = [3, 12, 14, 17, 29, 33, 40, 52];

    getBrokenLine(data2, "#f00");
    getBrokenLine(data3, "#0f0");

    //封装一个折线图的函数
    function getBrokenLine(data, color) {
    var maxNum = Math.max.apply(null, data); //求数组中的最大值
    var padding = 20, //边距
    x0 = padding, //原点x轴坐标
    y0 = cv.height - padding, //原点y轴坐标
    xArrow_x = padding, //x轴箭头处坐标x
    xArrow_y = padding, //x轴箭头处坐标y
    yArrow_x = cv.width - padding, //y轴箭头处坐标x
    yArrow_y = cv.height - padding, //y轴箭头处坐标y
    arrowWidth = 10, //箭头的宽度
    xLength = cv.width - 2*padding - arrowWidth, //x轴的长度
    yLength = cv.height - 2*padding - arrowWidth, //y轴的长度
    pointsWidth = xLength/(data.length + 1); //折线上每个点之间的距离

    ctx.beginPath();//控制绘制的折线不受坐标轴样式属性的影响
    //绘制x轴
    ctx.moveTo(x0, y0);
    ctx.lineTo(xArrow_x, xArrow_y);
    ctx.moveTo(xArrow_x, xArrow_y);
    ctx.lineTo(xArrow_x - arrowWidth, xArrow_y + arrowWidth);
    ctx.moveTo(xArrow_x, xArrow_y);
    ctx.lineTo(xArrow_x + arrowWidth, xArrow_y + arrowWidth);

    //绘制y轴
    ctx.moveTo(x0, y0);
    ctx.lineTo(yArrow_x, yArrow_y);
    ctx.moveTo(yArrow_x, yArrow_y);
    ctx.lineTo(yArrow_x - arrowWidth, yArrow_y - arrowWidth);
    ctx.moveTo(yArrow_x, yArrow_y);
    ctx.lineTo(yArrow_x - arrowWidth, yArrow_y + arrowWidth);
    ctx.strokeStyle = "#000";

    //中断(坐标轴和折线的)连接
    ctx.stroke();
    ctx.beginPath();

    //绘制折线
    for (var i = 0; i < data.length; i++) {
    var pointX = padding + (i + 1) * pointsWidth;
    var pointY = padding + arrowWidth + (1 - data[i]/maxNum) * yLength;
    ctx.lineTo(pointX, pointY);
    }
    ctx.strokeStyle = color;
    ctx.stroke();
    }
    </script>

    画直线:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>用面向对象的思想 封装 在canvas绘制直线的函数</title>
    </head>
    <body>
    <canvas id="cv"></canvas>
    </body>
    </html>
    <script>
    var cv = document.getElementById("cv");
    cv.width = 600;
    cv.height = 300;
    cv.style.border = "1px solid red";
    var ctx = cv.getContext("2d");

    //面向对象编程
    //1 创建构造函数
    //2 构造函数的原型设置
    //3 调用的时候,通过 new+构造函数 来创建一个对象(实例)

    //构造绘制直线的函数
    function drawLine(parameters) {
    this.init(parameters);
    }
    //替换原型对象实现继承
    drawLine.prototype = {
    constructor: drawLine,
    init: function (parameters) {
    this.ctx = parameters.ctx;
    this.startX = parameters.points[0];
    this.startY = parameters.points[1];
    this.endX = parameters.points[2];
    this.endY = parameters.points[3];
    //以下3个属性,可以不设置,用短路运算实现添加默认属性值
    this.lineWidth = parameters.lineWidth || 1;
    this.lineDash = parameters.lineDash || [];
    this.strokeStyle = parameters.strokeStyle || "#000";
    },
    //原型中,一般用来储存对象的方法或者共有的属性
    stroke: function () {
    this.ctx.beginPath();
    this.ctx.moveTo(this.startX, this.startY);
    this.ctx.lineTo(this.endX, this.endY);
    this.ctx.lineWidth = this.lineWidth;
    this.ctx.setLineDash(this.lineDash);
    this.ctx.strokeStyle = this.strokeStyle;
    this.ctx.stroke();
    }
    };

    //调用构造函数,传入参数
    var line = new drawLine({
    ctx: ctx,
    points: [100, 100, 300, 100],
    lineDash: [4, 2],
    strokeStyle: "red"
    });
    line.stroke();
    var line2 = new drawLine({
    ctx: ctx,
    points: [100, 200, 300, 200],
    lineWidth: 6
    });
    line2.stroke();
    </script>

  • 相关阅读:
    网络存储——数据保护:RAID
    网络存储——磁盘驱动部件
    操作系统——Linux内核完全注释011c-3.0
    信号量和互斥锁的区别
    svn安装和使用
    putty安装和使用
    linux SVN命令
    eclipse 安装配置
    宏定义中#和##的使用
    线程间通信
  • 原文地址:https://www.cnblogs.com/fx2008/p/5960153.html
Copyright © 2020-2023  润新知