微信小程序canvas 画动态圆环百分比进度条实例 根据手机屏幕宽度自适应放大缩小

image.png

这里是wxml代码:

<component-analysis-report active="0" bindselectReport="selectReport"></component-analysis-report>
<component-no-data text="小喜正在统计中,敬请期待噢~" wx:if="{{!params.allAmount}}"></component-no-data>
<view class="wrap" hidden="{{!params.allAmount}}">
    
    <!-- <view class="top" style="z-index:1;">{{reportName}}询盘统计报告</view> -->
    <view class="circle">
        <canvas class="more" canvas-id="canvasCircles"></canvas>
        <canvas class="more" canvas-id="canvasCircle"></canvas>
        <canvas class="more" canvas-id="canvasArcCir"></canvas>
        <view class="more circles" wx:if="{{current==0}}">
            <view>
                <view class="circleone">
                    <text>{{params.gmvPercent}}</text> %
                </view>
                <view class="circletwo">成交占比</view>
            </view>
        </view>

        <!-- <view class="more circles" wx:if="{{current==1}}">
            <view>
                <view class="circleone">
                    <text>{{params.effectivePercent}}</text> %
                </view>
                <view class="circletwo">有效询盘占比</view>
            </view>
        </view> -->

        <!-- <view class="more circles bottom" wx:if="{{current==0}}">
            <view class="ONE"></view>
            <view class="TWO"></view>
        </view>
        <view class="more circles bottom" wx:if="{{current==1}}">
            <view class="TWO"></view>
            <view class="ONE"></view>
        </view> -->
    </view>
    <swiper class="circle-box" bindchange="currentChange">
        <block>
            <swiper-item>
            </swiper-item>
            <!-- <swiper-item>
            </swiper-item> -->
        </block>
    </swiper>
    <view class="mainS">
        <view class="deal" wx:if="{{current==0}}">
            <view class="block">
                <view class="pic">
                    <image src="{{images.totalValue}}" />
                </view>
                <view class="name"> {{enquireName.totalValue}} </view>
                <view class="prc">¥{{params.allAmount}}</view>
                <view class="num">{{params.allAmountCount}} 笔</view>
            </view>
            <view class="block">
                <view class="pic">
                    <image src="{{images.tranValue}}" />
                </view>
                <view class="name"> {{enquireName.tranValue}} </view>
                <view class="prc">¥{{params.gmvAmount}}</view>
                <view class="num">{{params.gmvAmountCount}} 笔</view>
            </view>
            <view class="block">
                <view class="pic">
                    <image src="{{images.enquireValue}}" />
                </view>
                <view class="name"> {{enquireName.enquireValue}} </view>
                <view class="prc">¥{{params.followAmount}}</view>
                <view class="num">{{params.followAmountCount}} 笔</view>
            </view>
            <view class="block">
                <view class="pic">
                    <image src="{{images.lossCount}}" />
                </view>
                <view class="name"> {{enquireName.lossValue}} </view>
                <view class="prc"> ¥{{params.lossAmount}} </view>
                <view class="num">{{params.lossAmountCount}} 笔</view>
            </view>
            <view class="block">
                <view class="pic">
                    <image src="{{images.gmvCount}}" />
                </view>
                <view class="name"> {{enquireName.gmvCount}} </view>
                <view class="prc"> {{params.effective}}次</view>
                <view class="num"></view>
            </view>
        </view>
        <!-- <view class="loss" wx:if="{{current==1}}">
                <view class="block">
                        <view class="pic">
                            <image src="{{images.allCount}}" />
                        </view>
                        <view class="name"> {{enquireName.allCount}} </view>
                        <view class="prc"></view>
                        <view class="num red">{{params.enquiryCount}}</view>
                    </view>
                    <view class="block">
                        <view class="pic">
                            <image src="{{images.gmvCount}}" />
                        </view>
                        <view class="name">{{enquireName.gmvCount}} </view>
                        <view class="prc"></view>
                        <view class="num red">{{params.effective}}</view>
                    </view>
                    <view class="block">
                        <view class="pic">
                            <image src="{{images.lossCount}}" />
                        </view>
                        <view class="name"> {{enquireName.lossCount}} </view>
                        <view class="prc"></view>
                        <view class="num red">{{params.garbage}}</view>
                    </view>
        </view> -->
    </view>
</view>

这里是wxss代码:

.wrap {
  /* width: 100%; */
  /* margin-top: 71rpx; */
  height: 100%;
  position: relative;
  font-size: 28rpx;
}
.circle-box {
  height: 500rpx;
  text-align: center;
  color: #fff;
  border-color: #55abf6;
  background-image: linear-gradient(165deg, #52a6db 0%, #6382d2 100%);
}
.top {
  position: absolute;
  top: 24rpx;
  width: 100%;
  height: 46rpx;
  line-height: 46rpx;
  font-size: 28rpx;
  color: #ffffff;
  letter-spacing: 0;
  text-align: center;
}
.circle {
  width: 380rpx;
  position: relative;
  margin: auto;
  top: 30rpx;
}
.more {
  width: 380rpx;
  height: 380rpx;
  position: absolute;
  right: 0;
}
.circles {
  width: 380rpx;
  height: 380rpx;
  color: #ffffff;
  
  font-size: 24rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.circleone {
  font-size: 36rpx;
}
.circleone text {
  font-size: 80rpx;
}
.circletwo{
  opacity: 0.64;
}
.bottom {
  height:430rpx;
  justify-content: center;
  align-items: flex-end;
}
.bottom .ONE {
  width: 60rpx;
  height: 8rpx;
  background: #ffffff;
  margin: 5rpx;
  border-radius: 2rpx;
}
.bottom .TWO {
  width: 60rpx;
  height: 8rpx;
  background: #ffffff;
  margin: 5rpx;
  opacity: 0.3;
  border-radius: 2rpx;
}
.deal,
.loss {
  height: auto;
  margin: -90rpx 24rpx 30rpx;
}
.block {
  height: 142rpx;
  /* box-shadow: 3px 3px 3px #eeeeee; */
  background: #ffffff;
  margin-bottom: 12rpx;
  /* border: 1rpx solid #e0e0e0; */
  display: flex;
  align-items: center;
  padding: 0 54rpx;
  box-shadow: 9rpx 9rpx 8rpx 0 rgba(168, 168, 169, 0.31);
  border-radius: 6rpx;
  text-align: center;
}
.block .name {
  flex: 1;
}
.block .prc {
  flex: 1.5;
  font-size: 32rpx;
  color: #CB2B2B;
}
.block .num {
  flex: 1;
  color: #999999;
}
.block .pic {
  width: 50rpx;
  height: 50rpx;
  margin-right: 20rpx;
}
.block .red {
  color: #CB2B2B;
}

这里是js代码:

var app = getApp();
var interval;
var varName;
var ctx = wx.createCanvasContext('canvasArcCir');
Page({
  data: {
    params: {
      allAmount: '',
      allAmountCount: '',
      effective: '',
      effectivePercent: '',
      enquiryCount: '',
      followAmount: '',
      followAmountCount: '',
      garbage: '',
      gmvAmount: '',
      gmvAmountCount: '',
      gmvPercent: '',
      lossAmount: '',
      lossAmountCount: ''
    },
    images: {
      totalValue: app.CDN + 'enquiry_1.png',
      tranValue: app.CDN + 'enquiry_2.png',
      enquireValue: app.CDN + 'enquiry_3.png',
      lossValue: app.CDN + 'enquiry_4.png',
      allCount: app.CDN + 'enquiry_count.png',
      gmvCount: app.CDN + 'enquiry_success.png',
      lossCount: app.CDN + 'enquiry_loss.png'
    },
    enquireName: {
      totalValue: '总金额',
      enquireValue: '跟单金额',
      tranValue: '成交金额',
      lossValue: '流失金额',
      allCount: '询盘总次数',
      gmvCount: '有效询盘',
      lossCount: '垃圾询盘'
    },
    enquireuse: {
      totalValue: '询盘总次数',
      enquireValue: '垃圾询盘',
      tranValue: '有效询盘'
    },
    reportId: 1,
    reportName: '2018年1月',
    current: 0,
    // 自定义canvas缩放比例
    width: 190,
    height: 190,
    r: 100,
    r1: 80,
    r2: 92
  },
  //选择传入reportId
  selectReport(e) {
    this.setData({
      reportId: e.detail.params.reportId,
      reportName: e.detail.params.reportName
    });
    wx.setNavigationBarTitle({
      title: e.detail.params.reportName + '询盘分析报告'
    });
    this.getList();

    // console.log(e.detail.params.reportId, e.detail.params.reportName);
  },
  //获取分析报告列表
  getList() {
    if (wx.showLoading) {
      wx.showLoading({ title: '加载中...' });
    }
    app
      .get('/report/detail', { reportId: this.data.reportId })
      .then(e => {
        if (wx.hideLoading) {
          wx.hideLoading();
        }
        this.setData({
          params: e.data
        });
        this.drawCircle();
      })
      .catch(res => {
        if (wx.hideLoading) {
          wx.hideLoading();
        }
        console.log(res);
      });
  },
  drawCircle: function() {
    let that = this;
    //进度条
    var cxt_arc = wx.createCanvasContext('canvasCircle');
    cxt_arc.setLineWidth(6);
    cxt_arc.setStrokeStyle('#81C6F6');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();
    cxt_arc.arc(that.data.r, that.data.r, that.data.r1, 0, 2 * Math.PI, false);
    cxt_arc.stroke();
    cxt_arc.draw();
    // 外线
    var cxt_arcs = wx.createCanvasContext('canvasCircles');
    cxt_arcs.setLineWidth(0.3);
    cxt_arcs.setStrokeStyle('rgba(255, 255, 255, 0.2)');
    cxt_arcs.setLineCap('round');
    cxt_arcs.beginPath();
    cxt_arcs.arc(that.data.r, that.data.r, that.data.r2, 0, 2 * Math.PI, false);
    cxt_arcs.stroke();
    cxt_arcs.draw();
    // 百分比
    clearInterval(varName);
    function drawArc(s, e) {
      ctx.setFillStyle('white');
      ctx.clearRect(0, 0, 2 * that.data.r, 2 * that.data.r);
      ctx.draw();
      var x = that.data.r,
        y = that.data.r,
        radius = that.data.r1;
      ctx.setLineWidth(5);
      ctx.setStrokeStyle('#76FBF6');
      ctx.setLineCap('round');
      ctx.beginPath();
      ctx.arc(x, y, radius, s, e, false);
      ctx.stroke();
      ctx.draw();
    }
    var step = 1,
      startAngle = 1.5 * Math.PI,
      endAngle = 0;
    var animation_interval = 10,
      n = 60;
    let a = 0;
    if (this.data.current == 0) {
      console.log(this.data.params);
      console.log(this.data.params.gmvPercent);
      a = this.data.params.gmvPercent / 100;
      // a = 0.02 / 100;
    } else {
      a = this.data.params.effectivePercent / 100;
    }
    var animation = function() {
      if (step <= n) {
        endAngle = step * a * 2 * Math.PI / n + 1.5 * Math.PI;
        drawArc(startAngle, endAngle);
        step++;
      } else {
        clearInterval(varName);
      }
    };
    varName = setInterval(animation, animation_interval);
  },
  // SWIPER
  currentChange: function(e) {
    this.setData({
      current: e.detail.current
    });
    this.drawCircle();
  },

  onReady: function() {
    // this.drawCircle();
  },
  onLoad: function(options) {
    let that = this;
    wx.getSystemInfo({
      //获取系统信息成功,将系统窗口的宽高赋给页面的宽高
      success: function(res) {
        var widths = res.windowWidth;
        var heights = res.windowHeight;
        that.setData({
          width: 190 / 414 * widths,
          height: 190 / 414 * heights,
          r: 100 / 414 * widths,
          r1: 80 / 414 * widths,
          r2: 92 / 414 * widths
        });
      }
    });
  }
});

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容