canvas实现折点图,并有鼠标hover样式

本文展示了如何使用HTML5的canvas元素来创建一个折点图,并实现鼠标hover时的高亮样式。提供了最终的图表样式示例以及相关HTML代码,感兴趣的读者可以查看GitHub上的完整资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、直接上图,看最终样式

2、html代码

<div class="myCanvas-cont">
		<canvas id="myCanvas" width="1100" height="500">
		您的浏览器暂不支持canvas
		</canvas>
	</div>

3、js代码

$(function(){

	var c=document.getElementById("myCanvas");
	var cxt=c.getContext("2d");

	/*开始绘制外边框*/
	cxt.beginPath();
	cxt.strokeStyle   = '#cdc9c4';
	cxt.lineWidth = 4;
	cxt.moveTo(100,400);
	cxt.lineTo(1020,400);
	cxt.stroke();

	cxt.beginPath();
	cxt.strokeStyle   = '#cdc9c4';
	cxt.lineWidth = 4;
	cxt.moveTo(100,400);
	cxt.lineTo(100,0);
	cxt.stroke();

	/*开始绘制横向内边框*/
	cxt.beginPath();
	cxt.strokeStyle='#e7e5e2';
	cxt.lineWidth = 1;
	for(let i=1;i<=6;i++){
		cxt.moveTo(100,400-60*i);
		cxt.lineTo(980,400-60*i);
		cxt.stroke();
	}
	/*开始绘制竖向内边框*/
	for(let i=1;i<=11;i++){
		cxt.moveTo(100+80*i,400);
		cxt.lineTo(100+80*i,40);
		cxt.stroke();
	}
	
	/*开始绘制竖轴文字*/
	cxt.font = "16px SimHei";
    cxt.fillStyle = "#00c5de";
    //从坐标点(50,345)开始绘制文字
    for(let i=0;i<=6;i++){
    	cxt.fillText(10*i+"万", 50, 405-60*i);
    }

    /*开始绘制横轴文字*/
    for(let i=1;i<=12;i++){
    	cxt.fillText("2017-"+i, 80*i, 440);
    }


    /*开始绘制圆点(每月数据)*/
    ////模拟的每月数据
    var arr=[22,40,50,60,32,11,0,45,50,15,20,55];
   
    //开始绘制(圆点Y轴坐标)
    var yArr=[];
    for(let i=0,len=arr.length;i<len;i++){
    	yArr.push(400-arr[i]*6);
    }
    for(let j=0,len=yArr.length;j<len;j++){
    	//画实心圆
    	cxt.beginPath();
    	cxt.fillStyle="#00c5de";
		cxt.arc(100+80*j,yArr[j],9,0,Math.PI*2,true);
		cxt.fill();
		cxt.closePath();
		//绘制连接圆的折线
		cxt.beginPath();
		cxt.strokeStyle='#00c5de';
		cxt.lineWidth = 1;
		cxt.moveTo(100+80*j,yArr[j]);
		cxt.lineTo(100+80*(j+1),yArr[j+1]);
		cxt.stroke();
    }
	
	c.addEventListener("mousemove", function (evt) { 
	  var mousePos = getMousePos(c, evt); 
	  var pagex=mousePos.x,
	  	  pagey=mousePos.y;
	  for(let j=0,len=yArr.length;j<len;j++){
	 	//鼠标移入圆的范围,给圆添加新样式
	 	if(pagex>(100+80*j-9) && pagex<(100+80*j+9) && pagey>(yArr[j]-9) && pagey<(yArr[j]+9)){
	 		//画实心圆
	    	cxt.beginPath();
	    	cxt.fillStyle="#c9302c";
			cxt.arc(100+80*j,yArr[j],9,0,Math.PI*2,true);
			cxt.fill();
			cxt.closePath();
	 	}else{
	 		//清除鼠标移入时的圆
	 		
	 		//再从新绘制圆
 		 	cxt.beginPath();
	    	cxt.fillStyle="#00c5de";
			cxt.arc(100+80*j,yArr[j],9,0,Math.PI*2,true);
			cxt.fill();
			cxt.closePath();
	 	}
    	
      }
 	}, false); 


	 //获取鼠标在canvas画布上的位置(**不是浏览器窗口的鼠标位置)
	 function getMousePos(canvas, evt) { 
	   var rect = canvas.getBoundingClientRect(); 
	   return { 
	     x: evt.clientX - rect.left * (canvas.width / rect.width),
	     y: evt.clientY - rect.top * (canvas.height / rect.height)
	   }
	 }

	//定义清除圆形区域函数
	function clearCircle(oc,x,y,r){
	    for(var i=0; i< Math.round(Math.PI * r); i++){
	        var angle = (i / Math.round(Math.PI * r)) * 360;
	        oc.clearRect(x, y, Math.sin(angle * (Math.PI / 180)) * r , Math.cos(angle * (Math.PI / 180)) * r);
	    }

	}

   

})

更多可参考:https://github.com/chuanzaizai/line-charts

下面是用Canvas实现线且有鼠标hover显示值的示例代码: HTML代码: ```html <canvas id="canvas" width="400" height="300"></canvas> ``` JavaScript代码: ```javascript // 获取Canvas元素 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 数据 const data = [10, 20, 30, 40, 50, 60, 70]; // 绘制线 ctx.beginPath(); ctx.moveTo(0, canvas.height - data[0]); for (let i = 1; i < data.length; i++) { const x = canvas.width * i / data.length; const y = canvas.height - data[i]; ctx.lineTo(x, y); } ctx.stroke(); // 鼠标hover事件 canvas.addEventListener('mousemove', function (event) { // 获取Canvas元素的尺寸 const rect = canvas.getBoundingClientRect(); const scaleX = canvas.width / rect.width; const scaleY = canvas.height / rect.height; // 获取鼠标位置相对于Canvas元素的坐标 const x = (event.clientX - rect.left) * scaleX; const y = (event.clientY - rect.top) * scaleY; // 判断鼠标位置是否在线上 const index = Math.floor(x / (canvas.width / data.length)); if (index >= 0 && index < data.length) { // 显示值 const value = data[index]; ctx.font = '14px Arial'; ctx.fillStyle = '#000'; ctx.fillText(value, x, canvas.height - value - 10); } }); ``` 在上述代码中,我们先通过`moveTo()`和`lineTo()`方法绘制了线,然后在鼠标移动事件的回调函数中判断鼠标位置是否在线上,鼠标位置上显示对应的数据值。 需要注意的是,在显示数据值时,我们可以使用`fillText()`方法将数据值绘制在Canvas中,但是需要注意数据值的位置和字体、颜色等属性的设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值