小程序 mpvue 使用echarts 饼状图、折线图(组件封装)

这篇博客介绍了如何在Vue.js应用中使用mpvue-echarts库创建饼状图和折线图组件。通过监听数据变化,动态更新图表配置,实现了图表与数据的实时同步。同时,提供了详细的代码示例,包括初始化图表、设置图表选项以及处理页面关闭时的资源清理。
摘要由CSDN通过智能技术生成

直接撸码!!!

饼状图组件代码

<template>
  <div class="echarts-wrap">
    <mpvue-echarts :echarts="echarts" :onInit="pieChart" :canvas-id="pieData.pieId"/>
  </div>
</template>
<script>
import echarts from 'echarts'
import mpvueEcharts from 'mpvue-echarts'

let pieChart = null;
export default {
  components: { //引入其它组件
    mpvueEcharts
  },
  data() {  // 设置数据
    return {
      echarts,
    }
  },
  props: { //接收传过来的数据
    pieData: Object,
  },
  watch: {
    pieData: {
      handler() {
        if (pieChart) {
          pieChart.setOption(this.myPieOptions())
        }
      },
      deep: true
    }
  },
  methods: {  //方法
    pieChart(canvas, width, height) {
      let that = this;
      pieChart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      canvas.setChart(pieChart);
      pieChart.setOption(that.myPieOptions());
      return pieChart; // 返回 pieChart 后可以自动绑定触摸操作
    },
    myPieOptions() {
      let that = this;
      return {
        title: {  // 标题
          text: that.pieData.text,
          subtext: that.pieData.subtext ? that.pieData.subtext : '',
          left: 'center',
        },
        tooltip: {  // 提示框组件
          trigger: 'item',  // item 数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。
        },
        legend: { //图例组件展示
          orient: 'horizontal', // vertical 垂直、horizontal 水平
          left: 'left', // 如果 left 的值为left, center, right 组件会根据相应的位置自动对齐。
          bottom: '20'
        },
        series: [
          {
            type: 'pie',  // pie 饼状图
            radius: '50%',
            label: {
              formatter: '{c} ({d}%)',
            },
            data: that.pieData.data,
            emphasis: { //高亮状态的扇区和标签样式
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    },
    pageClose() { //页面销毁
      this.pieData = []
    },
  }
}

</script>

<style lang="scss" scoped>
.echarts-wrap {
  width: 100%;
  height: 300px;
}
</style>

折线图组件代码 

<template>
  <div class="echarts-wrap">
    <mpvue-echarts :echarts="echarts" :onInit="lineChart" :canvas-id="lineData.lineId"/>
  </div>
</template>
<script>
import echarts from 'echarts'
import mpvueEcharts from 'mpvue-echarts'

let chartLine = null;
export default {
  components: { //引入其它组件
    mpvueEcharts
  },
  data() {  // 设置数据
    return {
      echarts,
    }
  },
  props: { //接收传过来的数据
    lineData: Object,
  },
  watch: {
    lineData: {
      handler() {
        if (chartLine) {
          chartLine.setOption(this.myLineOptions())
        }
      },
      deep: true
    }
  },
  methods: {  //方法
    lineChart(canvas, width, height) {
      let that = this;
      chartLine = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      canvas.setChart(chartLine);
      chartLine.setOption(that.myLineOptions());
      return chartLine; // 返回 chartLine 后可以自动绑定触摸操作
    },
    myLineOptions() {
      let that = this;
      return {
        title: {
          text: that.lineData.text,
          textAlign: 'auto'
        },
        tooltip: {
          trigger: 'axis',
          position: ['20%', '30%'],
        },
        legend: {
          padding: [30, 0, 0, 0],
          data: that.lineData.legend
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '15%',
          containLabel: true
        },
        /*toolbox: { // 保存图片
          feature: {
            saveAsImage: {}
          }
        },*/
        xAxis: {
          type: 'category',
          axisLabel: {
            interval: 0
          },
          data: that.lineData.xAxis
        },
        yAxis: {
          type: 'value'
        },
        dataZoom: [
          {
            bottom: '30%',
            type: 'inside',
            start: 0,
            end: 50
          },
          {
            height: 25,
            start: 0,
            end: 50,
          }],
        series: that.seriesInit()
      }
    },
    seriesInit() {
      let that = this;
      let l = this.lineData.legend;
      let s = that.lineData.series;
      let data = [];
      for (let i = 0; i < l.length; i++) {
        data.push({name: l[i], type: 'line', data: s[i]})
      }
      return data;
    },
    pageClose() { //页面销毁
      this.lineData = []
    },
  }
}

</script>

<style lang="scss" scoped>
.echarts-wrap {
  width: 100%;
  height: 300px;
}
</style>

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值