利用canvas实现折线图的绘制

    我们知道现在的很多H5的小游戏的开发都是基于canvas这个画布的,其实canvas之所以这么强大考的是他强大的API库的结果,知道一些canvas的基本API并且可以利用这些API是一个前端开发人员的基本素质,那么我们今天就来跟着小编来一起利用面向对象的思想来封装一个折线图的绘制

   html代码

   

<canvas style="border:1px solid red" width="500" height="500"></canvas>
    </body>
JavaScript代码

   

<script>
	//获取canvas的dom元素
	var cvs=document.querySelector("canvas");
	//创建绘制环境
	var cxt=cvs.getContext("2d");
	  /*
        * constructor { LineChart } 折线图构造函数
        * param { ctx: Context } 绘图上下文
        * param { paddingArr: Array } 折线图到画布四边的距离,存储顺序为上右下左
        * param { arrowArr: Array } 折线图中箭头的宽和高
        * param { data: Array } 存储了折线图中所需的数据
        * */
        function LineChart(ctx,paddingArr,arrowArr,data){
        	this.ctx=ctx;
        	this.paddingArr=paddingArr;
        	this.arrowArr=arrowArr;
        	this.data=data;
        	this.arrowHeight=arrowArr[0];
        	this.arrowWidth=arrowArr[1];

        	//计算上顶点的距离
        	this.vertexTop={
        		x:this.paddingArr[3],
        		y:this.paddingArr[0]
        	};
        	//计算原点的距离
        	this.origin={
        		x:this.paddingArr[3],
        		y:this.ctx.canvas.height-this.paddingArr[2]
        	};
        	//计算右边点的距离
        	this.vertexRight={
        		x:this.ctx.canvas.width-this.paddingArr[1],
        		y:this.ctx.canvas.height-this.paddingArr[2]
        	};
        	this.process();
        }

        LineChart.prototype={
        	//设置构造函数
        	constructor:LineChart,
        	//绘制坐标轴中的两条线
        	drawZuobiao:function(){
        		this.ctx.beginPath();
        		this.ctx.moveTo(this.vertexTop.x, this.vertexTop.y);
        		this.ctx.lineTo(this.origin.x, this.origin.y);
        		this.ctx.lineTo(this.vertexRight.x, this.vertexRight.y);
        		this.ctx.stroke();
        	},
        	drawArrow:function(){
        		this.ctx.beginPath();
        		this.ctx.moveTo(this.vertexTop.x, this.vertexTop.y);
        		this.ctx.lineTo(this.vertexTop.x-this.arrowWidth/2, this.vertexTop.y+this.arrowHeight);
        		this.ctx.lineTo(this.vertexTop.x, this.vertexTop.y+this.arrowHeight/2);
        		this.ctx.lineTo(this.vertexTop.x+this.arrowWidth/2, this.vertexTop.y+this.arrowHeight);
        		this.ctx.closePath();
        		this.ctx.fill();
        		this.ctx.stroke();

        		this.ctx.beginPath();
        		this.ctx.moveTo(this.vertexRight.x, this.vertexRight.y);
        		this.ctx.lineTo(this.vertexRight.x-this.arrowHeight, this.vertexRight.y-this.arrowWidth/2);
        		this.ctx.lineTo(this.vertexRight.x-this.arrowHeight/2, this.vertexRight.y);
        		this.ctx.lineTo(this.vertexRight.x-this.arrowHeight, this.vertexRight.y+this.arrowWidth/2);
        		this.ctx.closePath();
        		this.ctx.fill();
        		this.ctx.stroke();

        	},

        	process:function(){
        		this.processData=[];
        		//在这一部分将画布认识的坐标转化为本坐标系的坐标
        		for (var i = 0; i < this.data.length; i+=2) {
        			this.processData.push(this.origin.x+this.data[i]);
        			this.processData.push(this.origin.y-this.data[i+1]);
        		};
        		
        	},

        	drawOrcle:function(){
        		this.ctx.beginPath();
        		for (var i = 0; i < this.processData.length; i+=2) {
        			this.ctx.arc(this.processData[i],this.processData[i+1], 5, 0, Math.PI*2);
        			this.ctx.fill();
        			
        		};
        	},

        	drawLine:function(){
        		this.ctx.beginPath();
        		for (var i = 0; i < this.processData.length; i+=2) {
        			this.ctx.lineTo(this.processData[i], this.processData[i+1]);
        		};
        		this.ctx.stroke();

        	},
        	draw:function(){
        		this.drawZuobiao();
        		this.drawArrow();
        		this.drawOrcle();
        		this.drawLine();
        	}
        };

        var linechat=new LineChart(cxt,[20,20,20,20],[10,10],[10,10,20,20,60,60,100,100]);
        linechat.draw();

        
</script>
    具体的详细步骤已经在代码里有所体现了,放到H5标准下的页面就好了!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
要在 Android 上绘制折线图,可以使用 Canvas 和 Paint 类来实现。 首先,你需要创建一个自定义 View 组件,并在其 onDraw() 方法中实现绘图逻辑。在绘制折线图时,你需要先计算出每个数据点的坐标,然后使用 Path 类绘制连接这些点的线条。 下面是一个简单的示例代码,实现了一个基本的折线图绘制: ```java public class LineChartView extends View { private List<Float> mData = new ArrayList<>(); public LineChartView(Context context) { super(context); } public LineChartView(Context context, AttributeSet attrs) { super(context, attrs); } public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setData(List<Float> data) { mData = data; invalidate(); // 通知 View 进行重新绘制 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 设置画笔颜色和样式 Paint paint = new Paint(); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); // 计算每个数据点的坐标 int width = getWidth(); int height = getHeight(); float xInterval = width / (mData.size() - 1); float yInterval = height / Collections.max(mData); Path path = new Path(); for (int i = 0; i < mData.size(); i++) { float x = i * xInterval; float y = height - mData.get(i) * yInterval; if (i == 0) { path.moveTo(x, y); } else { path.lineTo(x, y); } } // 绘制折线图 canvas.drawPath(path, paint); } } ``` 在 Activity 中,你可以通过调用 setData() 方法来设置折线图的数据,并将自定义 View 组件添加到布局中: ```java List<Float> data = new ArrayList<>(); data.add(10f); data.add(20f); data.add(50f); data.add(30f); data.add(40f); LineChartView lineChartView = new LineChartView(this); lineChartView.setData(data); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); linearLayout.addView(lineChartView, layoutParams); ``` 上述代码会在 LinearLayout 中添加一个折线图,其中数据为 [10, 20, 50, 30, 40]。你可以根据自己的需求来修改上述代码,实现更加复杂的折线图绘制
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值