php 折线图canvas,JavaScript canvas绘制折线图

本文实例为大家分享了canvas绘制折线图的具体代码,供大家参考,具体内容如下

Title

canvas {

border: 1px solid #ccc;

}

/*1.构造函数*/

var LineChart = 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.x0 = this.space;

this.y0 = this.canvasHeight - this.space;

/*箭头的大小*/

this.arrowSize = 10;

/*绘制点*/

this.dottedSize = 6;

/*点的坐标 和数据有关系 数据可视化*/

}

/*2.行为方法*/

LineChart.prototype.init = function (data) {

this.drawGrid();

this.drawAxis();

this.drawDotted(data);

};

/*绘制网格*/

LineChart.prototype.drawGrid = function () {

/*x方向的线*/

var xLineTotal = Math.floor(this.canvasHeight / this.gridSize);

this.ctx.strokeStyle = '#eee';

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.stroke();

}

/*y方向的线*/

var yLineTotal = Math.floor(this.canvasWidth / this.gridSize);

for (var i = 0; i <= yLineTotal; i++) {

this.ctx.beginPath();

this.ctx.moveTo(i * this.gridSize - 0.5, 0);

this.ctx.lineTo(i * this.gridSize - 0.5, this.canvasHeight);

this.ctx.stroke();

}

};

/*绘制坐标系*/

LineChart.prototype.drawAxis = function () {

/*X轴*/

this.ctx.beginPath();

this.ctx.strokeStyle = '#000';

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.stroke();

this.ctx.fill();

/*Y轴*/

this.ctx.beginPath();

this.ctx.strokeStyle = '#000';

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.stroke();

this.ctx.fill();

};

/*绘制所有点*/

LineChart.prototype.drawDotted = function (data) {

/*1.数据的坐标 需要转换 canvas坐标*/

/*2.再进行点的绘制*/

/*3.把线连起来*/

var that = this;

/*记录当前坐标*/

var prevCanvasX = 0;

var prevCanvasY = 0;

data.forEach(function (item, i) {

/* x = 原点的坐标 + 数据的坐标 */

/* y = 原点的坐标 - 数据的坐标 */

var canvasX = that.x0 + item.x;

var canvasY = that.y0 - item.y;

/*绘制点*/

that.ctx.beginPath();

that.ctx.moveTo(canvasX - that.dottedSize / 2, canvasY - that.dottedSize / 2);

that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY - that.dottedSize / 2);

that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY + that.dottedSize / 2);

that.ctx.lineTo(canvasX - that.dottedSize / 2, canvasY + that.dottedSize / 2);

that.ctx.closePath();

that.ctx.fill();

/*点的连线*/

/*当时第一个点的时候 起点是 x0 y0*/

/*当时不是第一个点的时候 起点是 上一个点*/

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(prevCanvasX,prevCanvasY);

that.ctx.lineTo(canvasX,canvasY);

that.ctx.stroke();

}

/*记录当前的坐标,下一次要用*/

prevCanvasX = canvasX;

prevCanvasY = canvasY;

});

};

/*3.初始化*/

var data = [

{

x: 100,

y: 120

},

{

x: 200,

y: 160

},

{

x: 300,

y: 240

},

{

x: 400,

y: 120

},

{

x: 500,

y: 80

}

];

var lineChart = new LineChart();

lineChart.init(data);

运行结果如下:

e9a10c98da668b357574e225cf1d7098.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 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]。你可以根据自己的需求来修改上述代码,实现更加复杂的折线图绘制

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值