热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

微信小程序环形图/折线图(canvas)

实现效果图页面调用方式环形百分比

实现效果图
在这里插入图片描述
在这里插入图片描述
页面调用方式

环形百分比
折线图

页面CSS

.ponet_warp{display: flex;flex-direction: column;width: 100%;justify-content: center;align-items: center;
}
.titel_name{width: 94%;margin-left: 3%;padding: 10px 0px;font-size: 24px;font-weight: 700;
}

页面JSON

{"usingComponents": {"annulus":"/component/annulus/annulus","brokens":"/component/brokens/brokens"}
}

环形图组件代码

Component({/*** 组件的属性列表*/properties: {/*** 高度*/heights:{type:Number,value:300,},/*** 百分比*/bfbNumber:{type:Number,value:0,},/*** 圆弧半径*/rNumber:{type:Number,value:80},annuColor:{type:String,value:'#FF0000'}}, attached(){this.createArc();},/*** 组件的初始数据*/data: {canvasWidth:0,},/*** 组件的方法列表*/methods: {createArc(){let that =this;let canvasWidth = 0;wx.getSystemInfo({success: function (res) {canvasWidth = res.windowWidth - 56that.setData({canvasWidth})},})let num = that.properties.bfbNumber;let rNumber = that.properties.rNumber;let canvasHeight = that.properties.heights;let annuColor = that.properties.annuColor;if(num > 100){num = 100}const ctx = wx.createCanvasContext('arc', this)ctx.beginPath()ctx.strokeStyle='#c0c0c0'ctx.lineWidth = 7ctx.arc(canvasWidth / 2 ,canvasHeight / 2 , rNumber, 0, Math.PI*2)ctx.stroke()ctx.beginPath()ctx.strokeStyle= annuColorctx.arc(canvasWidth / 2 , canvasHeight / 2 , rNumber,Math.PI *1.5,(Math.PI * 2 / 100) * num + (Math.PI*1.5))ctx.stroke()ctx.draw()},}
})

环形图wxml


折线图组件代码

// component/brokens/brokens.js
Component({/*** 组件的属性列表*/properties: {/*** 高度*/heights: {type: Number,value: 300,},valueArr: {type: Array,value: [],},nameArr: {type: Array,value: [],},colorArr:{type:Array,value:[&#39;#e54d42&#39;,&#39;#f37b1d&#39;,&#39;#fbbd08&#39;,&#39;#8dc63f&#39;,&#39;#39b54a&#39;,&#39;#1cbbb4&#39;,&#39;#0081ff&#39;]}},/*** 组件的初始数据*/data: {canvasWidth:0,},attached() {this.createLineCanvas();},/*** 组件的方法列表*/methods: {createLineCanvas() {let that &#61;this;let canvasWidth &#61; 0;wx.getSystemInfo({success: function (res) {canvasWidth &#61; res.windowWidth - 56that.setData({canvasWidth})},})let nameArr &#61; that.properties.nameArr;let valueArr &#61; that.properties.valueArr;let colorArr &#61; that.properties.colorArr;let heights &#61; that.properties.heights - 20;let maxData &#61; this.getGroupAndMax(valueArr);let hme &#61; heights / maxData.maxlet wme &#61; canvasWidth / nameArr.lengthconst ctx &#61; wx.createCanvasContext(&#39;linecanvas&#39;, this)//底部字段ctx.beginPath()ctx.setTextAlign(&#39;center&#39;)nameArr.forEach((item,index) &#61;>{ctx.fillText(item,wme * index &#43;20,heights)})ctx.stroke();//线段valueArr.forEach((item,ind) &#61;>{ctx.lineWidth &#61; 1;ctx.beginPath()ctx.strokeStyle&#61; colorArr[ind]item.data.forEach( (items,index) &#61;>{if(index &#61;&#61; 0){ctx.moveTo(wme*index&#43;20 ,items * hme -20)}else{ctx.lineTo(wme*index&#43;20 ,items * hme)}})ctx.stroke();ctx.closePath();})//右边标尺ctx.beginPath()ctx.strokeStyle &#61; "#c0c0c0"ctx.moveTo(20,0)ctx.lineTo(20,heights-20)ctx.stroke()ctx.closePath();ctx.beginPath()let hdata &#61; maxData.max / 10 let heid &#61; heights / 10 for (let index &#61; 0; index <10; index&#43;&#43;) {if(index &#61;&#61; 0){ctx.fillText(hdata * (index&#43;1),10,heights-20 - (heid *index))}else{ctx.fillText(hdata * (index&#43;1),10,heights-20 - (heid *index))}}//底部标尺ctx.beginPath()ctx.strokeStyle &#61; "#c0c0c0"ctx.moveTo(10,heights -20)ctx.lineTo(canvasWidth,heights-20)ctx.stroke()ctx.closePath();ctx.draw()},getGroupAndMax(arr){let max &#61; 0;let group &#61; [];arr.forEach(element &#61;> {group.push(element.name)element.data.forEach(item &#61;>{if(item > max){max &#61; item;}})});let data &#61; {max,group}return data;},}
})

折线图wxml



推荐阅读
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • 探讨了如何解决Ajax请求响应时间过长的问题。本文分析了一个从服务器获取少量数据的Ajax请求,尽管服务器已经对JSON响应进行了缓存,但实际响应时间仍然不稳定。 ... [详细]
  • IneedtofocusTextCellsonebyoneviaabuttonclick.ItriedlistView.ScrollTo.我需要通过点击按钮逐个关注Tex ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了在通过 API 端点调用时,使用猫鼬(Mongoose)的 findOne 方法总是返回 null 的问题,并提供了详细的解决方案和建议。 ... [详细]
  • 作为一名 Ember.js 新手,了解如何在路由和模型中正确加载 JSON 数据是至关重要的。本文将探讨两者之间的差异,并提供实用的建议。 ... [详细]
  • 云函数与数据库API实现增删查改的对比
    本文将深入探讨使用云函数和数据库API实现数据操作(增删查改)的不同方法,通过详细的代码示例帮助读者更好地理解和掌握这些技术。文章不仅提供代码实现,还解释了每种方法的特点和适用场景。 ... [详细]
author-avatar
龙love猫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有