用canvas绘制折线图

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>用canvas绘制折线图</title>
 6 </head>
 7 <body>
 8     <canvas id="cv"></canvas>
 9 </body>
10 </html>
11 <script>
12     var cv = document.getElementById("cv");
13     cv.width = 600;
14     cv.height = 400;
15     cv.style.border = "1px solid red";
16     var ctx = cv.getContext("2d");
17     var data2 = [.3, .1, .2, .4, .2, .7, .3, .9];
18     var data3 = [3, 12, 14, 17, 29, 33, 40, 52];
19 
20     getBrokenLine(data2, "#f00");
21     getBrokenLine(data3, "#0f0");
22 
23     //封装一个折线图的函数
24     function getBrokenLine(data, color) {
25         var maxNum = Math.max.apply(null, data);    //求数组中的最大值
26         var padding = 20,  //边距
27                 x0 = padding,  //原点x轴坐标
28                 y0 = cv.height - padding,  //原点y轴坐标
29                 xArrow_x = padding, //x轴箭头处坐标x
30                 xArrow_y = padding, //x轴箭头处坐标y
31                 yArrow_x = cv.width - padding,  //y轴箭头处坐标x
32                 yArrow_y = cv.height - padding, //y轴箭头处坐标y
33                 arrowWidth = 10,    //箭头的宽度
34                 xLength = cv.width - 2*padding - arrowWidth,    //x轴的长度
35                 yLength = cv.height - 2*padding - arrowWidth,  //y轴的长度
36                 pointsWidth = xLength/(data.length + 1);    //折线上每个点之间的距离
37 
38         ctx.beginPath();//控制绘制的折线不受坐标轴样式属性的影响
39         //绘制x轴
40         ctx.moveTo(x0, y0);
41         ctx.lineTo(xArrow_x, xArrow_y);
42         ctx.moveTo(xArrow_x, xArrow_y);
43         ctx.lineTo(xArrow_x - arrowWidth, xArrow_y + arrowWidth);
44         ctx.moveTo(xArrow_x, xArrow_y);
45         ctx.lineTo(xArrow_x + arrowWidth, xArrow_y + arrowWidth);
46 
47         //绘制y轴
48         ctx.moveTo(x0, y0);
49         ctx.lineTo(yArrow_x, yArrow_y);
50         ctx.moveTo(yArrow_x, yArrow_y);
51         ctx.lineTo(yArrow_x - arrowWidth, yArrow_y - arrowWidth);
52         ctx.moveTo(yArrow_x, yArrow_y);
53         ctx.lineTo(yArrow_x - arrowWidth, yArrow_y + arrowWidth);
54         ctx.strokeStyle = "#000";
55 
56         //中断(坐标轴和折线的)连接
57         ctx.stroke();
58         ctx.beginPath();
59 
60         //绘制折线
61         for (var i = 0; i < data.length; i++) {
62             var pointX = padding + (i + 1) * pointsWidth;
63             var pointY = padding + arrowWidth + (1 - data[i]/maxNum) * yLength;
64             ctx.lineTo(pointX, pointY);
65         }
66         ctx.strokeStyle = color;
67         ctx.stroke();
68     }
69 </script>

效果图如下:

转载于:https://www.cnblogs.com/2010master/p/5936950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用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、付费专栏及课程。

余额充值