当前位置:   article > 正文

微信小程序使用echarts画饼图、条形图_小程序饼状图

小程序饼状图

首先我们需要在GitHub,下载 GitHub 上的 ecomfe/echarts-for-weixin 项目。
在这里插入图片描述
在这里插入图片描述

抽出组件

  1. 创建组件
    在这里插入图片描述

  2. 写好文件结构,json区域引入我们的ec-canvas组件

<script type="application/json">
  {
    "usingComponents": {
      "ec-canvas": "../../../ec-canvas/ec-canvas"
    }
  }
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.在页面上写一个容器给我们echart画图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200911105718957.png#pic_center

4.对应的CSS如下

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200911105908590.png#pic_center

源码

<template>
  <view class="container">
    <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
  </view>
</template>
<script>
import { createComponent } from '@mpxjs/core'
import { createPage } from '@mpxjs/core'
import * as echarts from '../../../ec-canvas/echarts.js'

let chart = null

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  })
  canvas.setChart(chart)

  var option = {
    title: {
      text: '暂无数据',
      padding: [15, 100, 10, 10]
    }
  }

  chart.setOption(option)
  return chart
}
createComponent({
  data: {
    ec: {
      onInit: initChart
    }
  },
  properties: {
    option: {
      type: Object,
      value: {},
      observer: function(newVal) {
        if (newVal) {
          if (chart !== null) {
            console.log(chart)
            chart.setOption(newVal, true) // 设置数据且不merge
          } else {
            return
          }
        } else {
          return
        }
      }
    }
  },
  attached() {
    setTimeout(function() {
      // 获取 chart 实例的方式
      //   console.log(chart)
    }, 2000)
  }
})
</script>
<style lang="stylus" scoped>
.container
  width 100%
  height 100%
ec-canvas
  width 100%
  height 100%
</style>
<script type="application/json">
  {
    "usingComponents": {
      "ec-canvas": "../../../ec-canvas/ec-canvas"
    }
  }
</script>
  • 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

6.在其它页面引用本组件
在这里插入图片描述
在这里插入图片描述
页面代码

<template>
  <view class="container">
    <wxechart option="{{option}}" />
    <button bindtap="change">改直方图数据</button>
    <button bindtap="change2">改饼图数据</button>
  </view>
</template>
<script>
import { createPage } from '@mpxjs/core'
createPage({
  data: {
    option: {}
  },
  onReady() {},
  methods: {
    change() {
      this.setData({
        option: {
          title: {
            text: '直方图'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
            }
          },
          grid: {
            left: 20,
            right: 20,
            bottom: 15,
            top: 40,
            containLabel: true
          },
          xAxis: [
            {
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: '#5C89FF'
                }
              },
              axisLabel: {
                color: '#666'
              }
            }
          ],
          // y轴
          yAxis: [
            {
              type: 'category',
              axisTick: { show: false },
              data: [
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县',
                'XXX县'
              ],
              axisLine: {
                lineStyle: {
                  color: '#999'
                }
              },
              axisLabel: {
                color: '#666'
              }
            }
          ],
          series: [
            {
              name: '人口',
              type: 'bar',
              label: {
                normal: {
                  formatter: '{c}',
                  show: true,
                  textStyle: {
                    fontSize: '12',
                    color: '#fff'
                  }
                }
              },
              // data: [],
              data: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000],
              itemStyle: {
                color: '#E92F20',
                barBorderRadius: [0, 16, 16, 0]
              },
              barWidth: '25'
            }
          ]
        }
      })
    },
    change2() {
      this.setData({
        option: {
          backgroundColor: '#ffffff',
          title: {
            text: '某站点用户访问来源'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} \n{b} : {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: '5%',
            right: '5%',
            bottom: '10%',
            orient: 'horizontal',
            textStyle: {
              fontSize: 12
            },
            data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
          },
          series: [
            {
              name: '访问来源',
              type: 'pie',
              center: ['50%', '50%'],
              radius: ['40%', '60%'],
              data: [
                { value: 335, name: '直接访问' },
                { value: 310, name: '邮件营销' },
                { value: 234, name: '联盟广告' },
                { value: 135, name: '视频广告' },
                { value: 1548, name: '搜索引擎' }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
      })
    }
  }
})
</script>
<style lang="stylus" scoped>
.container
  width 100%
  height 600px
</style>
<script type="application/json">
  {
    "navigationBarTitleText": "canvas柱状图",
    "usingComponents": {
      "wxechart": "../components/wxEchart/wxEchart"
    }
  }
</script>
  • 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

效果

开始

在这里插入图片描述
改直方图数据
在这里插入图片描述
改饼图数据
在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/526505
推荐阅读
相关标签
  

闽ICP备14008679号