用canvas写了一个移动端走势图

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title></title>
    <style type="text/css">
        body,p{
            padding: 0;
            margin: 0;
            font-family: "Comic Sans MS","幼圆","黑体",sans-serif;
        }
        #container{
            margin-top: 40px;
            position: relative;
            height: 100%;
            /*    background: #aaa;
            */ }
        .text{
            position: absolute;
            height: 100%;
            top:0;
        }
        div.text p{
            position: absolute;
            color: #666;
            font-size: .8em;
        }
        div.date{
            position: absolute;
            color: #666;
            left: 25;
        }
        div.date p{
            position: absolute;
            font-size: .6em;
        }
        #canvas{
            position: relative;
            z-index: 9;
            background: #fff;
            margin-left: 50px;
        }
    </style>
</head>
<body>
<div id="container">
    <canvas id="canvas"></canvas>
    <div class="text">
        <p>4.14%</p>
        <p>4.24%</p>
        <p>4.34%</p>
        <p>4.44%</p>
        <p>4.54%</p>
    </div>
    <div class="date">
        <p>03.01</p>
        <p>03.02</p>
        <p>03.03</p>
        <p>03.04</p>
        <p>03.05</p>
        <p>03.06</p>
        <p>03.07</p>
    </div>
</div>
</body>
</html>
<script>
    document.getElementById('container').style.width=window.screen.wdith+'px';
    var width=window.screen.width-50-20;
    var height=120;
    var height1=150;

    var pointX=[0,width/6,2*width/6,3*width/6,4*width/6,5*width/6,width];/*X*/
    var poin=[0,height-3*height/4,height-2*height/4,height-1*height/4,height];/*Y*/

    var pointY=[0,30,height1-3*height/4,height1-2*height/4,height1-1*height/4,height1];

    var pointZ=[height1-27,height1-30,height1-80,height1-90,height1-50,height1-70,height1-40];
    var p=document.querySelector('.text').getElementsByTagName('p');
    var date=document.querySelector('.date').getElementsByTagName('p');
    var canvas=document.getElementById('canvas').getContext('2d');
    document.getElementById('canvas').setAttribute('width',width);
    document.getElementById('canvas').setAttribute('height',height1);

        /*Y*/
    for(var i=0;i< p.length;i++){
        p[i].style.bottom=poin[i]+'px';
    }

    /*X*/
    for(var j=0;j<date.length;j++){
        if(window.screen.width<=320){
            date[j].style.left=pointX[j]+25+'px';
            // date[j].style.transform='rotate(-40deg)';
        }else{
            date[j].style.left=pointX[j]+25+'px';
        }
    }


    function drawX(){
        for(var i=1;i<pointY.length;i++){
            canvas.beginPath();
            if(i==5) {
                canvas.strokeStyle = 'red';
                canvas.moveTo(pointX[0], height1);
                canvas.lineTo(pointX[6], height1);
                canvas.moveTo(pointX[0], height1);
                canvas.lineTo(pointX[0], 0);

            }else{
                canvas.strokeStyle='yellow';
                canvas.moveTo(pointX[0],pointY[i]);
                canvas.lineTo(pointX[6],pointY[i]);
            }
            canvas.closePath();
            canvas.lineWidth=1;
            canvas.stroke();
        }

    }
    drawX();

    function drawY(){
        for(var i=1;i<pointX.length;i++){
            canvas.beginPath();
            canvas.strokeStyle='green';
            for(var i=1;i<pointX.length;i++){
                canvas.moveTo(pointX[i],height1);
                canvas.lineTo(pointX[i],0);
            }
            canvas.closePath();
            canvas.lineWidth=1;
            canvas.stroke();
        }
    }
    drawY();

    function drawZ(){
        canvas.beginPath();
        canvas.strokeStyle='#000';
        for(var i=0;i<pointZ.length;i++){
            canvas.arc(pointX[i],pointZ[i],2,360,false);
            canvas.moveTo(pointX[i],pointZ[i]);
            canvas.lineTo(pointX[i+1],pointZ[i+1]);
        }
        canvas.closePath();
        canvas.stroke()
        canvas.lineWidth=1;
        canvas.stroke();
    };
    drawZ();
</script>














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值