canvas绘制表格

canvas绘制表格

最近在为公司产品做技术预研,经理让用canvas做一个表格,于是就有了这篇博客。

我们的数据是后端通过MQTT推送过来的
我在代码中也直接使用了
具体MQTT的实现代码,可见博客
在vue使用MQTT

在这里为了方便实用我直接封装成组件了,当MQTT数据来了就出发绘制方法

<template>  
  <div>  
    <!-- 画布元素,用于绘图 -->  
    <canvas ref="canvasRef" height="180" width="600"></canvas>  
  </div>  
</template>  
  
<script setup>  
import {defineExpose, onMounted, ref} from 'vue';  
import UseMqtt from "../hooks/useMqtt.js"; // 引入MQTT通信的自定义hook  
  
// 画布引用  
const canvasRef = ref(null);  
// 画布上下文  
const ctx = ref(null);  
// 存储接收到的数据  
const data = ref([]);  
// 行高  
const rowHeight = 30;  
// 当前偏移量,用于控制画布滚动  
const currentOffset = ref(30);  
  
// 数据格式示例  
/*[{  
  "hx": 56,  
  "szy": 77,  
  "xl": 74,  
  "ssy": 122,  
  "xybhd": 0.36  
}]*/  
  
/**  
 * 绘制函数,用于在画布上绘制表格  
 */  
const drawTable = () => {  
  const canvas = canvasRef.value;  
  ctx.value = canvas.getContext('2d');  
  ctx.value.clearRect(0, 0, canvas.width, canvas.height); // 清除画布  
  ctx.value.fillStyle = 'black'; // 设置填充颜色  
  ctx.value.font = '16px Arial'; // 设置字体  
  // 绘制列标题  
  const headers = ["hx", "szy", "xl", "ssy", "xybhd"];  
  headers.forEach((header, index) => {  
    ctx.value.fillText(header, index * 120, rowHeight);  
  });  
  
  // 绘制数据行  
  data.value.forEach((item, rowIndex) => {  
    const y = (rowIndex + 1) * rowHeight + currentOffset.value;  
    if (y < canvas.height) {  
      Object.values(item).forEach((value, colIndex) => {  
        ctx.value.fillText(value, colIndex * 120, y);  
      });  
    }  
  });  
};  
  
onMounted(() => {  
  drawTable(); // 组件挂载后绘制表格  
});  
  
const options = {  
  subscription: {topic: "/testtopic/yq", qos: 0} // MQTT订阅配置  
}  
const {  
  createAndDo,  
  destroyConnection  
} = UseMqtt(options, getMessage); // 使用MQTT hook,并传入消息处理函数  
  
/**  
 * 接收消息  
 * @param v 接收到的消息  
 */  
function getMessage(v) {  
  try {  
    data.value.push(JSON.parse(v)) // 解析并存储消息  
    if (data.value.length >= 5) {  
      data.value.shift() // 如果数据超过5条,移除最旧的一条  
    }  
    drawTable(); // 重新绘制表格  
  } catch (e) {  
    console.error(e) // 错误处理  
  }  
}  
  
onMounted(() => {  
  createAndDo() // 组件挂载后建立MQTT连接并开始接收消息  
})  
  
defineExpose({  
  destroyConnection, // 暴露销毁MQTT连接的方法  
  createAndDo // 暴露建立并开始MQTT连接的方法  
})  
</script>

效果图

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的uniapp canvas表格的示例代码: ```html <template> <view class="canvas-container"> <canvas canvas-id="myCanvas" class="canvas"></canvas> </view> </template> <script> export default { onReady() { const context = uni.createCanvasContext('myCanvas', this); const width = uni.upx2px(300); // 表格宽度 const height = uni.upx2px(200); // 表格高度 const padding = uni.upx2px(10); // 表格与画布边缘的距离 const rowHeight = uni.upx2px(30); // 行高 const colWidth = uni.upx2px(80); // 列宽 const rowCount = 4; // 行数 const colCount = 3; // 列数 // 绘制表格边框 context.strokeRect(padding, padding, width, height); // 绘制表格行 for(let i = 1; i < rowCount; i++) { context.beginPath(); context.moveTo(padding, padding + rowHeight * i); context.lineTo(padding + width, padding + rowHeight * i); context.stroke(); } // 绘制表格列 for(let i = 1; i < colCount; i++) { context.beginPath(); context.moveTo(padding + colWidth * i, padding); context.lineTo(padding + colWidth * i, padding + height); context.stroke(); } // 填充表格内容 const data = [ ['姓名', '年龄', '性别'], ['张三', '18', '男'], ['李四', '20', '女'], ['王五', '22', '男'] ]; context.font = '14px sans-serif'; context.textAlign = 'center'; context.textBaseline = 'middle'; for(let i = 0; i < rowCount; i++) { for(let j = 0; j < colCount; j++) { context.fillText(data[i][j], padding + colWidth * (j + 0.5), padding + rowHeight * (i + 0.5)); } } context.draw(); } } </script> <style> .canvas-container { width: 300upx; height: 200upx; } .canvas { width: 100%; height: 100%; } </style> ``` 这段代码绘制了一个4行3列的表格表格宽度为300upx,高度为200upx。你可以根据实际需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万水千山走遍TML

您的鼓励,将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值