热门标签 | 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



推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
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社区 版权所有