canvas--折线图

canvas–折线图

功能:模拟从后台获取数据,将得到的数据在画布上连接成一个折线图,即数据可视化
效果如下:
折线图
绘制这个图主要包含以下几个部分:

  • 新建一个构造函数LineChart()
  • 传递数据data
  • 初始化函数的行为和方法(主要包含绘制网格,绘制坐标系,绘制点和连接点)
    代码如下:
<canvas width="600px" height="400px"></canvas>
	<script>
		//需求:利用面向对象的方法和canvas将一组数据制作成一幅折线图
		//模拟从后台获取数据
		var data = [
		{x:100,y:120},
		{x:200,y:160},
		{x:300,y:240},
		{x:400,y:320},
		{x:500,y:80}
		]
		//构造函数
		var LineChart = function(ctx) {
			// 获取绘图工具箱
			this.ctx = document.querySelector("canvas").getContext("2d");
			console.log(this.ctx);
			// 获取画布的长度和宽度
			this.cWidth = this.ctx.canvas.width;
			this.cHeight = this.ctx.canvas.height;
			// 设置坐标系的外边距
			this.space = 20;
			// 设置坐标系网格的大小
			this.grid = 10;
			//设置坐标系箭头的大小
			this.arrowSize = 10;
			//设置数据点的大小
			this.dottedSize = 6;
			//设置原点的坐标
			this.x0 = this.space;
			this.y0 = this.cHeight-this.space;
		};
		//传递data数据,
		LineChart.prototype.init = function(data) {
			//传递数据之前先绘制网格和坐标系
			this.drawGrid();
			this.drawAxis();
			//将数据传递给绘制点的方法进行绘制
			this.drawDotted(data);
		};
		//绘制网格 
		LineChart.prototype.drawGrid = function(){
			// 先绘制X方向的线条
			var xLineTotal = Math.floor(this.cHeight/this.grid);
			this.ctx.strokeStyle = "#eee";
			for(var i=0; i<=xLineTotal;i++){
				this.ctx.beginPath();
				this.ctx.moveTo(0,i*this.grid-0.5);
				this.ctx.lineTo(this.cWidth,i*this.grid-0.5);
				this.ctx.stroke();
			}
			// 绘制y方向的线条
			var yLineTotal = Math.floor(this.cWidth/this.grid);
			for(var j=0; j<=yLineTotal; j++){
				this.ctx.beginPath();
				this.ctx.moveTo(j*this.grid-0.5,0);
				this.ctx.lineTo(j*this.grid-0.5,this.cHeight);
				this.ctx.stroke();
			}
		};
		//绘制坐标系
		LineChart.prototype.drawAxis = function(){
			// 绘制X轴
			this.ctx.beginPath();
			this.ctx.moveTo(this.x0,this.y0);
			this.ctx.lineTo(this.cWidth-this.space,this.y0);
			this.ctx.lineTo(this.cWidth-this.space-this.arrowSize,this.y0+this.arrowSize/2);
			this.ctx.lineTo(this.cWidth-this.space-this.arrowSize,this.y0-this.arrowSize/2);
			this.ctx.lineTo(this.cWidth-this.space,this.y0);
			this.ctx.strokeStyle = "#000";
			this.ctx.stroke();
			this.ctx.fill();
			//绘制y轴
			this.ctx.beginPath();
			this.ctx.moveTo(this.x0,this.y0);
			this.ctx.lineTo(this.x0,this.space);
			this.ctx.lineTo(this.x0+this.arrowSize/2,this.space+this.arrowSize);
			this.ctx.lineTo(this.x0-this.arrowSize/2,this.space+this.arrowSize);
			this.ctx.lineTo(this.x0,this.space);
			this.ctx.strokeStyle = "#000";
			this.ctx.stroke();
			this.ctx.fill();
		}
		//绘制点和连线
		LineChart.prototype.drawDotted = function(){
			//数据的坐标不是canvas的坐标,需要将数据的坐标转换成canvas坐标
			var that = this;
			var prevCanvasX = 0;
			var prevCanvasY = 0;
			data.forEach(function(item,i){
				//计算数据点的坐标
				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.fill();
				// 开始连线
				if(i==0){
					that.ctx.beginPath();
					that.ctx.moveTo(that.x0,that.y0);
					that.ctx.lineTo(canvasX,canvasY);
					that.ctx.setLineDash([5]);
					that.ctx.stroke();
				}else{
					that.ctx.beginPath();
					that.ctx.moveTo(prevCanvasX,prevCanvasY);
					that.ctx.lineTo(canvasX,canvasY);
					that.ctx.setLineDash([5]);
					that.ctx.stroke();
				}
				prevCanvasX = canvasX;
				prevCanvasY = canvasY;
			});
		}
		// 初始化构造函数
		var lineChart = new LineChart();
		lineChart.init(data);
	</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要显示多个折线图并避免它们脱离画布,您可以使用 `theme` 属性来调整 G2Plot 的主题样式。下面是一个示例代码,演示如何创建一个堆叠柱线图表,并显示多个折线图。 ```jsx import React, { Component } from 'react'; import { StackedColumnLine } from '@antv/g2plot'; class StackedColumnLineChart extends Component { constructor(props) { super(props); this.chartRef = React.createRef(); } componentDidMount() { const data = [ { year: '1991', value: 3, type: 'sales' }, { year: '1992', value: 4, type: 'sales' }, { year: '1993', value: 3.5, type: 'sales' }, { year: '1994', value: 5, type: 'sales' }, { year: '1995', value: 4.9, type: 'sales' }, { year: '1996', value: 6, type: 'sales' }, { year: '1997', value: 7, type: 'sales' }, { year: '1998', value: 9, type: 'sales' }, { year: '1999', value: 13, type: 'sales' }, { year: '1991', value: 1, type: 'profits' }, { year: '1992', value: 3, type: 'profits' }, { year: '1993', value: 1.5, type: 'profits' }, { year: '1994', value: 2, type: 'profits' }, { year: '1995', value: 1.9, type: 'profits' }, { year: '1996', value: 4, type: 'profits' }, { year: '1997', value: 5, type: 'profits' }, { year: '1998', value: 8, type: 'profits' }, { year: '1999', value: 10, type: 'profits' }, ]; const lineTheme = { geometries: { line: { style: { lineWidth: 2, }, }, }, }; const chart = new StackedColumnLine(this.chartRef.current, { data, xField: 'year', yField: 'value', xAxis: { label: { autoRotate: false, }, }, yAxis: { label: { formatter: (value) => `${value}k`, }, }, seriesField: 'type', legend: { position: 'top', }, theme: { styleSheet: { ...lineTheme, }, }, }); chart.render(); } componentWillUnmount() { if (this.chart) { this.chart.destroy(); } } render() { return <div ref={this.chartRef}></div>; } } export default StackedColumnLineChart; ``` 在上面的代码中,我们定义了一个 `lineTheme` 对象,用于调整折线图的样式。然后,我们在创建图表实例时,将 `lineTheme` 对象添加到主题中。这样,我们就可以根据需要调整折线图的线宽和样式,从而避免它们脱离画布。 另外,我们还可以通过设置 `xAxis.label.autoRotate` 属性来禁用 X 轴标签的自动旋转,以确保标签不重叠。同时,我们通过设置 `yAxis.label.formatter` 属性来自定义 Y 轴标签的格式,以便更好地显示数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值