ArkUIeTS健康饮食APP自定义饼图组件帖子详细介绍了如何基于画布组件Canvas绘制饼图自定义组件。我们先了解一下Canvas组件的属性和方法。下面展示本帖demo的效果:Canvas组件仅供本自定义组件在Canvas画布上使用组件的关键属性,接口;查看更多Canvas参数接口请移步官方文档:https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-canvasrenderingcontext2d-0000001333720961。Demo介绍新建一个eTS文件来编写自定义PicChart组件,代码如下:@ComponentexportstructCustomPieChart{//饼图数据privatepicChartElements:PicChartElement[]//圆半径@Statecircle_radius:number=80//单位@Stateunit:string="gram"//获取上下文privatesettings:RenderingContextSettings=newRenderingContextSettings(true)privatecontext:CanvasRenderingContext2D=newCanvasRenderingContext2D(this.settings)aboutToAppear(){lettotal=0//总统计this.picChartElements.forEach((value)=>{total+=value.quantity})//初始化圆弧结束圆弧letlastEndAngle=-0.5*Math.PI//封装饼图数据this.picChartElements.forEach((value)=>{//占用百分比letpercent=value.quantity/total//四舍五入得到一个整数value.percent=Math.round(percent*100)//初始化结束弧为弧的起始value.beginAngle=lastEndAngle//计算圆弧的结束弧度值.endAngle=(percent*2*Math.PI)+lastEndAngle//将结束弧赋给一个变量,作为下一个起始弧lastEndAngle=value.endAngle//返回封装对象返回值})}build(){Column({space:30}){Canvas(this.context)//高度是半径的两倍。height(this.circle_radius*2)//纵横比,宽高一致。aspectRatio(1)//canvas组件回调的事件,此时可以绘制.onReady(()=>{this.picChartElements.forEach((item)=>{//创建一个新的控制路径this.context.beginPath()//路径从当前点移动到指定点this.context.moveTo(this.circle_radius,this.circle_radius)//绘制圆弧路径(圆心的x坐标值,y坐标值圆弧中心,圆弧半径,圆弧起点startarc,arcendarc)this.context.arc(this.circle_radius,this.circle_radius,this.circle_radius,item.beginAngle,item.endAngle)//指定绘制的填充颜色this.context.fillStyle=item.color//填充闭合路径this.context.fill()})})Flex({direction:FlexDirection.Row,wrap:FlexWrap.Wrap,justifyContent:FlexAlign.SpaceAround}){ForEach(this.picChartElements,(item:PicChartElement)=>{Row({space:4}){//标记圆的颜色Circle({width:8,height:8}).fill(item.color)//标签文本Text(item.element).fontSize(12)//标签数量Text(item.quantity+''+this.unit).fontSize(12)}.height(30)})}.width('100%')}.width('100%')}}exportclassPicChartElement{element:Resource|string//显示文字数量:个数//数量percent:number//百分比beginAngle:number//弧的起始弧endAngle:number//弧的结束弧color:string//Colorconstructor(element:Resource|string,quantity:number,color:string){this.element=elementthis.quantity=quantitythis.color=color}}将自定义PieChart组件引用到导入要使用的页面,创建Sample页面,定义饼图数据源、文本和数量、颜色对象、饼图半径、显示单位可选,默认半径为80,单位为克具体使用方法见示例代码://引用自定义组件import{CustomPieChart,PicChartElement}from'../components/CustomPieChart'@Entry@ComponentstructSampleCustomPicChart{//饼图1数据privatepicChartElements:PicChartElement[]=[newPicChartElement('Protein',14.9,'#ff9421'),newPicChartElement('Fat',39.8,'#ffd100'),newPicChartElement('Carbs',19.1,'#4cd041'),newPicChartElement('甜点',9.1,'#4cd0ee'),newPicChartElement('Seafood',11.1,'#999999')]//饼图2数据privatepicChartElements2:PicChartElement[]=[newPicChartElement('Protein',5.9,'#ff9421'),newPicChartElement('Fat',9.8,'#ffd100'),newPicChartElement('Carbs',7.1,'#4cd041')]build(){Column({space:10}){//饼图1(半径,单位使用默认值)CustomPieChart({picChartElements:this.picChartElements})//饼图2(指定半径为120,单位指定为kcal)CustomPieChart({picChartElements:this.picChartElements2,circle_radius:120,unit:'kcal'})}.width('100%').padding(30)}}总结通过这篇文章,你可以简单的学习自定义PieChart组件,如何使用,在健康饮食app中,我又完成了一小部分内容,然后继续一个一个完成组件,然后拼装了一个健康饮食app。想了解更多开源内容,请访问:开源基础软件社区https://ost.51cto.com。
