• 【uni-app】uni项目打包微信小程序中使用 ECharts (mpvue-echarts)


    先看成品图
    在这里插入图片描述

    原生小程序使用的是echarts-for-weixin,具体地址如下:
    https://github.com/ecomfe/echarts-for-weixin

    想在uni项目开发的小程序中使用Echarts太艰辛了,我选择使用mpvue-echarts,踩了一路坑!!!

    网上写这个的还不少,常见的两种方法:

    • .执行命令(npm install echarts mpvue-echarts),把 node_modules 下生成mpvue-echarts的src文件放到components下,成为自己的组件 (类似文章传送门)
    • .mpvue-echarts的官方介绍:https://github.com/F-loat/mpvue-echarts

    很巧,我是一个也没用上,各种报错

    • 编译错误:Module build failed (from ./node_modules/@dcloudio/webpack-uni-mp-loader/lib/script.js)
    • this.echarts.setCanvasCreator is not a function的错误
    • e.addEventListener is not a function 报错
    • 文件体积超过 500KB,已跳过压缩以及 ES6 转 ES5 的处理,手机端使用过大的js库影响性能

    很多教程都说的不是很明白,下面直接上我改进后的步骤:

    1、npm install & 定制echarts

    • 在package.js里的写echarts的版本号后执行该命令
    "dependencies": {
        "echarts": "^5.0.0",
      },
    
    • 1
    • 2
    • 3

    注意版本号
    在这里插入图片描述

    2. 下载组件

    下载 mpvue-echarts文件夹,放到主包的components或者分包下的components都可以

    值得注意的是,如果你遇到this.echarts.setCanvasCreator is not a function的错误
    检查一下需要修改引入方法路径是否正确:下图是通过npm的路径,如果是在线定制的话写你的文件相对路径
    在这里插入图片描述

    1. 在vue文件中使用
    <template>
      <view class="container">
        <view class="wrap">
          <mpvue-echarts :echarts="echarts" :onInit="onInit" canvasId="gauge" />
        </view>
        <view class="card-result">
          <view class="card-result-h">结果评估 <span>(满分为100分)</span></view>
          <view class="card-result-r">
            <view
              ><span class="car-title">测验表名</span
              ><span class="car-content" style="margin-right: -0.55rem">{{
                type_name
              }}</span></view
            >
            <view
              ><span class="car-title">测验分数</span
              ><span class="car-content">{{ score }}</span></view
            >
            <view
              ><span class="car-title">测验结果</span
              ><span class="car-content">{{ judge }}</span></view
            >
          </view>
        </view>
      </view>
    </template>
    
    <script>
    import * as echarts from "../components/mpvue-echarts/src/echarts.min.js";
    import mpvueEcharts from "../components/mpvue-echarts";
    
    let chart = null;
    let EScore = null;
    
    function initChart(canvas, width, height) {
      chart = echarts.init(canvas, null, {
        width: width,
        height: height,
      });
      canvas.setChart(chart);
    
      var option = {
            series: [
              {
                type: 'gauge',
                center: ['50%', '65%'],
                radius: '115%',
                axisLine: {
                  lineStyle: {
                    width: 30,
                    color: [
                      [0.25, (function() {
                        return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                          offset: 0,
                          color: '#52B5FF'
                        }, {
                          offset: 0.5,
                          color: '#4EA4FF'
                        }, {
                          offset: 1,
                          color: '#498CFF'
                        }])
                      })()], [0.5, (function() {
                        return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                          offset: 0,
                          color: '#2BEFD1'
                        }, {
                          offset: 0.5,
                          color: '#16E1C9'
                        }, {
                          offset: 1,
                          color: '#04D5C2'
                        }])
                      })()],
                      [0.75, (function() {
                        return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                          offset: 0,
                          color: '#FDD75C'
                        }, {
                          offset: 0.5,
                          color: '#FDC249'
                        }, {
                          offset: 1,
                          color: '#FE9A27'
                        }])
                      })()],
                      [1, (function() {
                        return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                          offset: 0,
                          color: '#A4FBEC'
                        }, {
                          offset: 0.5,
                          color: '#73DEF5'
                        }, {
                          offset: 1,
                          color: '#3FBFFE'
                        }])
                      })()]
                    ]
                  }
                },
                pointer: {
                  itemStyle: {
                    color: 'auto'
                  }
                },
                axisTick: {
                  distance: -30,
                  length: 8,
                  show: false,
                  lineStyle: {
                    color: '#fff',
                    width: 2
                  }
                },
                splitLine: {
                  show: false,
                  distance: -30,
                  length: 30,
                  lineStyle: {
                    color: '#fff',
                    width: 4
                  }
                },
                axisLabel: {
                  color: '#999',
                  distance: 40,
                  fontSize: 20
                },
                title: {
                  show: true,
                  color: '#444B5F',
                  fontSize: 25
                },
                startAngle: 180,
                endAngle: 0,
                splitNumber: 4,
                detail: {
                  valueAnimation: true,
                  formatter: '{value}',
                  color: '#333333',
                  fontSize: 35
                },
                data: [
                  {
                    value: EScore,
                    name: '最终评分结果'
                  }
                ]
              }
            ]
    
          }
      chart.setOption(option);
      return chart;
    }
    
    export default {
      data() {
        return {
          echarts,
          onInit: initChart,
          size: "medium",
          score: "54",
          judge: "轻中度抑郁症倾向",
          type_name: "Hamilton汉密尔顿抑郁量表(HAMD)",
        };
      },
    
      components: {
        mpvueEcharts,
      },
      onLoad(options) {
        console.log('99',options) // 路由传参
        this.score = options.score,
        EScore = Number(options.score)
      },
      methods: {
      },
    };
    </script>
    
    <style lang="scss" scoped>
    .container {
      background: #ffff;
      min-height: 100vh;
    }
    .wrap {
      width: 100%;
      height: 250px;
    }
    .card-result {
      margin: 0 10px;
      padding: 15px;
      background: #f2fbfd;
      border-radius: 1rem;
      text-align: left;
      font-size: 20px;
      &-h {
        span {
          font-weight: 400;
          color: #00aac6;
        }
        font-weight: 600;
      }
      &-r {
        display: flex;
        flex-direction: column;
        view {
          margin-top: 15px;
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
        }
        .car-title {
          font-size: 17px;
          &::before {
            content: "";
            border-left: 0.1rem solid #00aac6;
            border-radius: 1rem;
            margin-right: 5px;
            border-left-width: 5px;
          }
        }
        .car-content {
          font-size: 14px;
          color: #00aac6;
        }
      }
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232

    到这里就可以正常展示了
    在这里插入图片描述

    放在最后

    改进的地方主要是拿到组件mpvue-echarts无法接收到prop的echarts,所以改用install后的echarts,当然你也可以去在线定制echarts.simple.min文件,我目前只定制了仪表盘
    在这里插入图片描述

    .完结~欢迎留言

  • 相关阅读:
    这个IfxCpu_Trap_busError函数是干嘛的
    JAVA基础—面向对象
    算法训练营day17
    如何设置接口调用失败后自动重连
    Vue学习(一)之Vue2前端工程化和WenBack
    前端进击笔记第十七节 提升开发效率:数据抽象与配置化
    【业务功能118】微服务-springcloud-springboot-Kubernetes集群-k8s集群-KubeSphere-OpenELB部署及应用
    Oracle中sequence(序列)详解
    使用stream流合并多个List(根据实体类特定属性合并)
    职业教育数字化成行业发展新方向
  • 原文地址:https://blog.csdn.net/weixin_44070058/article/details/126015295