Home  >  Article  >  Web Front-end  >  canvas draws rectangular coordinate system

canvas draws rectangular coordinate system

迷茫
迷茫Original
2017-03-25 13:58:063209browse

canvasDrawing a rectangular coordinate system

March 17, 2017

Using canvas to draw a rectangular coordinate system is actually quite simple, as long as the origin (0, 0) point, it can also be other points, as long as you know that it is the origin! Once you know the origin, just draw two straight lines in the direction of the X-axis and the Y-axis (the X-axis and the Y-axis are also relative, this purely depends on personal preference and actual needs).

Without further ado, let’s just look at the code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>canvas画直角坐标系和柱状图</title>
    </head>
    <body>
        <h1>canvas画直角坐标系及柱状图</h1>
        <canvas id="canvas" width="600" height="400">如果您只看到了这句话,那么您的浏览器该升级换代了!</canvas>
             <!--canvas的宽高即可以设置成行内样式,也可以通过Js设置,不过建议设置成行内样式-->
        <script src="js/drawChart.js" type="text/javascript" charset="utf-8"></script>
        <!--为了表现与样式分离同时为了页面简洁,引入一个外部js文件-->
        <script type="text/javascript">
            var canvas=document.getElementById("canvas");
            var cxt = canvas.getContext("2d");
            //通过id获取canvas画布,并获得cxt画笔,canvas目前只支持2d效果,所以“2d”不能省;
              
            var arr = [16,15,20,21]//利用数组模拟柱状图的数据
            Coordinate(50,350);//自己写的一个直角坐标系
            ColumnChart1(50,350,arr);    //一个简单的柱状图函数     
            /*为了有个简单的动画效果,canvas绘制完成以后先将它隐藏,然后用jQuery的slideDown()方法淡入显示*/
            $("#canvas").hide();
            $("#canvas").slideDown(1000);    
        </script>
    </body>
</html>

Then there is the drawing method of the rectangular coordinate system (if the master sees that the functions are more complete and the code size can be more streamlined, please feel free to enlighten me. , thank you):

function Coordinate(x,y){
        //x为横坐标起点,Y为纵坐标起点
              var originX = x;
               var originY = y;
             //设置原点的那个文字的样式,并绘制出来
               cxt.font = "2rem 微软雅黑";
               cxt.fillText("0",originX-10,originY+15);//此处-10和+15是为了调整字的位置

              cxt.strokeStyle = "black";//设置坐标系X轴Y轴的颜色,绘制线条用strockeStyle属性,绘制填充色块用fillStyle属性;
             cxt.lineWidth = 3;//设置线条粗细,这里为了方便看设置了3个像素,可以根据情况自行调整
           //开始绘制Y轴
     cxt.beginPath();//开启路径
             cxt.moveTo(originX,originY);//x轴与y轴的起点位置
             cxt.lineTo(originX,originY-320);//轴的终点位置,即X大小不变,只是改变了Y点位置(根据实际情况做调整);
             cxt.stroke();//将这条线绘制出来
          //画小箭头
           cxt.moveTo(originX,originY-320);//小箭头起点位置即为Y轴终点位置
            cxt.lineTo(originX+3,originY-310);//originX+3和originY-310是设置小箭头的终点位置,小箭头的大小和尖锐程度请自行摸索
            cxt.stroke();
            cxt.moveTo(originX,originY-320);
            cxt.lineTo(originX-3,originY-310);
           cxt.stroke();
          //画横坐标
     //绘制X轴和Y轴相似
          cxt.moveTo(originX,originY);
          cxt.lineTo(originX+450,originY);
          cxt.stroke();
         //画小箭头
         cxt.moveTo(originX+450,originY);
         cxt.lineTo(originX+440,originY-3);
        cxt.stroke();
         cxt.moveTo(originX+450,originY);
         cxt.lineTo(originX+440,originY+3);
         cxt.stroke();
         cxt.fillText("Y轴",originX-5,originY-325)
     }

Then there is the method of drawing the histogram:

function ColumnChart1(x,y,arr){
        //绘制之前先清空原有的柱状图所占区域
        cxt.clearRect(x,y,500,0);
        var arrColor = ["red","yellow","blue","purple","green","mauve"];//为了使每个柱子的颜色不一样,如果可以尽量用#******或rgb()方法设置颜色,因为我这样直接用单词有些浏览器对个别颜色不识别(头疼的IE)
        //请务必保持x,y值与坐标系的x,y值相同
        this.arr= arr;
        for (var i=0;i<arr.length;i++ ) {//for循环用来遍历数组内数据
                if(i==0){
                    var    originX = x+40;
                    var originY = y-1;
                }else{
                    var originX = i*70+80;
                    var originY = y-1;
                }
                cxt.beginPath();
                cxt.strokeStyle = arrColor[i];//设置线条颜色
                cxt.lineWidth = 20;//这里为了方便直接将线条的宽度设置为20,这样就可以模拟柱子
                cxt.moveTo(originX+(i+1)*20,originY);//柱子的顶点位置,这里因为数组内数字小,所以都乘十了,这样有利于小数字的表现
                cxt.lineTo(originX+(i+1)*20,originY-(arr[i]*10));//调整每根柱子的间距;
                cxt.stroke();    
                cxt.font = "20px 宋体"
                cxt.fillText(arr[i],originX+(i+1)*20-10,originY-(arr[i]*10+3));
                //文字的绘制
                cxt.font = "13px 宋体"
                cxt.fillText("第"+(i+1)+"季度业绩",originX+(i+1)*20-35,originY+20)
            }
        }

If you still want the chart to look more beautiful, you can use the shadowColor, shadowOffsetX, shadowOffsetY, etc. provided by canvas. Related properties set the shadow.

At the same time, draw an ellipse with a slightly different color than the column with the vertex of the column as the center.

The above is the detailed content of canvas draws rectangular coordinate system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn