当前位置: 首页 > news >正文

uniapp中用canvas绘制简单柱形图,小容量,不用插件——简单使用canvas

uniapp中用canvas绘制简单柱形图,小容量,不用插件——简单使用canvas

在这里插入图片描述

完整代码

<template><view><!-- 学习数据 --><!-- 头部选项卡 --><view class="navTab"><view :class="listIndex==i?'activite':''" @click="gopage(i)" v-for="(item,i) in navList" :key="i">{{item}}</view></view><!-- 不同的图表 --><view id="mycan"><canvas style="height: 250px;" canvas-id="DataCanvas" class='canvasII'></canvas></view></view>
</template><script>export default {data() {return {title: '学习数据',canvasList: [{date:"9-12",num:0},{date:"9-13",num:24},{date:"9-14",num:100},{date:"9-15",num:45},{date:"9-16",num:15},{date:"9-17",num:54},{date:"9-18",num:10}],width:300,height:200,canPersent: [] ,//百分比数组listIndex:0,navList:["最近七天","最近七个月"],}},onLoad() {this.showChart()},methods: {gopage(i){this.listIndex = iif(i == 0){this.canvasList = [{date:"9-12",num:0},{date:"9-13",num:24},{date:"9-14",num:100},{date:"9-15",num:45},{date:"9-16",num:15},{date:"9-17",num:54},{date:"9-18",num:10}], //测试数据this.showChart()}else if( i == 1){this.canvasList = [{date:"3月",num:0},{date:"4月",num:234},{date:"5月",num:120},{date:"6月",num:145},{date:"7月",num:15},{date:"8月",num:543},{date:"9月",num:10}], //测试数据this.showChart()}},// 坐标图showChart() {const ctx = uni.createCanvasContext("DataCanvas")ctx.lineWidth = 1;// 横坐标ctx.beginPath(); //新建一条pathctx.setStrokeStyle('#c6c6c6')ctx.moveTo(20, this.height); //把画笔移动到指定的坐标ctx.lineTo(this.width, this.height); //绘制一条从当前位置到指定坐标(200, 50)的直线.ctx.closePath(); //闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合,则什么都不做ctx.stroke(); //绘制路径。// 纵坐标ctx.beginPath();ctx.moveTo(20, 0);ctx.lineTo(20, this.height);ctx.closePath();ctx.stroke();var max = 0 //最大值// 找到最大的数for (var i = 0; i < this.canvasList.length; i++) {if(this.canvasList[i].num >= max){max = this.canvasList[i].num}}console.log("max---->",max);// 计算百分比: 以数组中最大的为100%,来计算其他的百分比for (var i = 0; i < this.canvasList.length; i++) {this.canPersent[i] = (this.canvasList[i].num / max) * (this.height-40) // -10 ,防止太高溢出头}console.log(this.canPersent);// 柱状图之间的间隔// 绘制7个粗线条 (300-20)/7 = 40,所以 i 每次要加上40  20为左右间隔var gap = (this.width - 10*2) / this.canPersent.lengthconsole.log("--gap----->",gap);for (var i = 0; i < this.canPersent.length; i++) {// 横坐标的数据ctx.fillText(this.canvasList[i].date, 30 + gap * i, 220);// 柱状图上的数据ctx.fillText(this.canvasList[i].num, 33 + gap * i, this.height - this.canPersent[i] - 15);if(this.canPersent[i] == 0){i++ctx.fillText(this.canvasList[i].date, 30 + gap * i, 220);ctx.fillText(this.canvasList[i].num, 33 + gap * i, this.height - this.canPersent[i] - 15);}ctx.beginPath();ctx.setStrokeStyle('#F36521') //设置柱状图颜色ctx.moveTo(40 + gap*i, this.height - this.canPersent[i] - 6);ctx.lineTo(40 + gap*i, this.height - 6);ctx.lineWidth = 10;ctx.lineCap = "round"; //设置线条样式ctx.stroke();}ctx.draw()},}}
</script><style>#mycan {margin: 20px;}.navTab{display: flex;justify-content: space-evenly;margin: 3px 0;}.activite{color: #fc722d;padding: 3px 0;border-bottom: 3px #fc722d solid;}
</style>

相关文章:

  • 五一作业-day04
  • 五一の自言自语 2025/5/5
  • 【瞎折腾/mi50 32G/ubuntu】mi50显卡ubuntu运行大模型开坑(二)使用llama.cpp部署Qwen3系列
  • 【深度学习-Day 6】掌握 NumPy:ndarray 创建、索引、运算与性能优化指南
  • 手机打电话时由对方DTMF响应切换多级IVR语音应答(一)
  • 三、【LLaMA-Factory实战】模型微调进阶:从LoRA到MoE的技术突破与工程实践
  • 微服务中怎么获取请求头信息
  • WSL 安装 Debian 后,apt get 如何更改到国内镜像网址?
  • Python从入门到高手8.2节-元组的常用操作符
  • R004 -计算机硬件基础
  • Matlab实现基于CNN-GRU的锂电池SOH估计
  • Vite简单介绍
  • 五一作业-day03
  • Baklib知识中台:智能服务架构新实践
  • Java求职面试:Spring Boot与微服务的幽默探讨
  • 【Hive入门】Hive安全管理与权限控制:用户认证与权限管理深度解析
  • 代码随想录算法训练营第三十二天
  • MATLAB人工大猩猩部队GTO优化CNN-LSTM多变量时间序列预测
  • 文本三剑客试题
  • Android设备运行yolov8
  • 我国科研团队发布第四代量子计算测控系统
  • 抚州一原副县长拉拢公职人员组建“吃喝圈”,长期接受打牌掼蛋等“保姆式”服务
  • 言短意长|如何看待“订不到酒店的游客住进局长家”这件事
  • 巴菲特宣布将于年底退休,“接班人”什么来头?
  • 2025年五一档电影票房破4亿,《水饺皇后》领跑
  • 美股三大指数均收涨超1%,热门中概股强势拉升