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

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

<view class="ponet_warp">
    <view class="titel_name">环形百分比</view>
    <annulus heights="{{heights}}" bfbNumber="{{bfb}}"></annulus>
</view>

<view class="ponet_warp">
    <view class="titel_name">折线图</view>
    <brokens heights="{{heights}}"  nameArr="{{nameArr}}" valueArr="{{valueArr}}" ></brokens>
</view>

页面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 - 56
                    that.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 = 7
            ctx.arc(canvasWidth / 2 ,canvasHeight / 2 , rNumber, 0, Math.PI*2)
            ctx.stroke()
            ctx.beginPath()
            ctx.strokeStyle= annuColor
            ctx.arc(canvasWidth / 2 , canvasHeight / 2 , rNumber,Math.PI *1.5,(Math.PI * 2 / 100) * num + (Math.PI*1.5))
            ctx.stroke()
            ctx.draw()
        },
    }
})

环形图wxml

<canvas canvas-id="arc" style='width:{{canvasWidth}}px; height:{{heights}}px'></canvas>

折线图组件代码

// component/brokens/brokens.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        /**
        * 高度
        */
        heights: {
            type: Number,
            value: 300,
        },
        valueArr: {
            type: Array,
            value: [],
        },
        nameArr: {
            type: Array,
            value: [],
        },
        colorArr:{
            type:Array,
            value:['#e54d42','#f37b1d','#fbbd08','#8dc63f','#39b54a','#1cbbb4','#0081ff']
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        canvasWidth:0,
    },
    attached() {
        this.createLineCanvas();
    },
    /**
     * 组件的方法列表
     */
    methods: {

        createLineCanvas() {
            let that =this;
            let canvasWidth = 0;
            wx.getSystemInfo({
                success: function (res) {
                    canvasWidth = res.windowWidth - 56
                    that.setData({
                        canvasWidth
                    })
                },
            })
            let nameArr = that.properties.nameArr;
            let valueArr = that.properties.valueArr;
            let colorArr = that.properties.colorArr;
            let heights = that.properties.heights - 20;
            let maxData = this.getGroupAndMax(valueArr);

            let hme = heights / maxData.max
            let wme = canvasWidth / nameArr.length

            const ctx = wx.createCanvasContext('linecanvas', this)
            //底部字段
            ctx.beginPath()
            ctx.setTextAlign('center')
            nameArr.forEach((item,index) =>{
                ctx.fillText(item,wme * index +20,heights)
            })
            ctx.stroke();

            //线段
            valueArr.forEach((item,ind) =>{
                ctx.lineWidth = 1;
                ctx.beginPath()
                ctx.strokeStyle= colorArr[ind]
                item.data.forEach( (items,index) =>{
                    if(index == 0){
                        ctx.moveTo(wme*index+20 ,items * hme -20)
                    }else{
                        ctx.lineTo(wme*index+20 ,items * hme)
                    }
                   
                })
                ctx.stroke();
                ctx.closePath();
            })

            //右边标尺
            ctx.beginPath()
            ctx.strokeStyle = "#c0c0c0"
            ctx.moveTo(20,0)
            ctx.lineTo(20,heights-20)
            ctx.stroke()
            ctx.closePath();
            
           
            ctx.beginPath()
            let hdata = maxData.max / 10 
            let heid = heights / 10 
            for (let index = 0; index < 10; index++) {
                if(index == 0){
                    ctx.fillText(hdata * (index+1),10,heights-20 - (heid *index))
                }else{
                    ctx.fillText(hdata * (index+1),10,heights-20 - (heid *index))
                }
            }

           

            //底部标尺
            ctx.beginPath()
            ctx.strokeStyle = "#c0c0c0"
            ctx.moveTo(10,heights -20)
            ctx.lineTo(canvasWidth,heights-20)
            ctx.stroke()
            ctx.closePath();




            ctx.draw()
        },
        getGroupAndMax(arr){
            let max = 0;
            let group = [];
            arr.forEach(element => {
                group.push(element.name)
                element.data.forEach(item =>{
                    if(item > max){
                        max = item;
                    }
                })
            });

            let data = {
                max,
                group
            }
            return data;
        },
    }
})

折线图wxml

<canvas canvas-id="linecanvas" style='width:{{canvasWidth}}px; height:{{heights}}px'></canvas>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值