vue封装echars图表

该博客详细介绍了如何在 Vue 中使用 Echarts 实现柱状图和环形图。通过定义组件模板、脚本及样式,展示了如何设置图表的配置项,包括 x 轴和 y 轴的显示、颜色渐变、边框圆角等,并提供了数据绑定和渲染的方法。

使用于柱状,环形

<template>
  <div>
    <!-- echarts -->
    <div ref="box" id="box"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  props: ["option"],
  methods: {
    renderCharts() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(this.$refs.box);
      // 指定图表的配置项和数据
      var option = {
        grid: {
          left: "3%",
          right: "6%",
          bottom: "5%",
          containLabel: true,
        },

        xAxis: {
          name:this.option.name,
          type: "category",
          boundaryGap : true,//柱子间距
          data: this.option.xData, // x轴数据
          show: this.option.type == "bar" ? true : false,
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisTick:{
            show:false
          },
          axisLabel: {
                show: true,
                textStyle: {
                  color:"rgba(255,255,255,0.5)",
                },
                margin:10
          },
          nameTextStyle:{
            color:"rgba(255,255,255,0.5)",
            align:'lift',
            padding:[15,10,10,0],
            fontSize:15
          }
          
        },
        yAxis: {
          name:'数量',
          type: "value",
          show: this.option.type == "bar" ? true : false,
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: "dashed",
              color: "rgba(255,255,255,0.1)",

            },
          },
          axisLabel: {
                margin:15,
                show: true,
                textStyle: {
                  color:"rgba(255,255,255,0.5)",

                }
          },
          nameTextStyle:{
            color:"rgba(255,255,255,0.5)",
            align:'right',
            padding:[15,10,0,0],
            fontSize:14
          }
        },
        series: [
          {
            type: this.option.type, // y轴数据要显示柱状图或者折线图 改参数
            data: this.option.amountData, // y轴的核心数据
            radius: ["60%", "70%"], //半径
            label: { show: false },
            barWidth: 20, //柱图宽度
            itemStyle: {
              normal: {
                color: this.option.color?new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: this.option.color.top,
                  },
                  {
                    offset: 1,
                    color: this.option.color.bottom,
                  },
                ]):'',
                barBorderRadius: this.option.barBorderRadius
                  ? this.option.barBorderRadius
                  : [0, 0, 0, 0],
              },
            },
             color: [{
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0.4,
                y2: 1,
                colorStops: [{
                    offset: 0,
                    color: '#ae34ff' // 0% 处的颜色
                }, {
                    offset: 1,
                    color: '#4844fa' // 100% 处的颜色
                }],
                globalCoord: false // 缺省为 false
            },{
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0.4,
                y2: 1,
                colorStops: [{
                    offset: 0,
                    color: '#14e3ea' // 0% 处的颜色
                }, {
                    offset: 1,
                    color: '#09de8c' // 100% 处的颜色
                }],
                globalCoord: false // 缺省为 false
            }],
             
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
  mounted() {
    // 渲染视图
    this.renderCharts();
    //   setTimeout(()=>{
    //   },2000)
  },
};
</script>

<style lang="less" scoped>
#box {
  width: 94%;
  height: 345px;
  padding: 15px 25px 30px 30px;
  box-sizing: border-box;
  background: transparent;
  border-radius: 7px;
  // box-shadow: 1px 1px 10px 1px #ccc;
}
</style>

Vue 封装 Echarts 的方法: 1. 安装 Echarts 使用 npm 安装 Echarts: ``` npm install echarts --save ``` 2. 创建 Vue 组件 创建一个 Vue 组件,例如 `Echarts.vue`。 3. 引入 Echarts 在 `Echarts.vue` 中引入 Echarts: ``` import echarts from 'echarts' ``` 4. 初始化 Echarts 在 `mounted` 钩子函数中初始化 Echarts: ``` mounted() { this.initChart() }, methods: { initChart() { const container = this.$refs.container this.chart = echarts.init(container) } } ``` 5. 配置 Echarts 在 `initChart` 方法中配置 Echarts: ``` initChart() { const container = this.$refs.container this.chart = echarts.init(container) const option = { // 配置项 } this.chart.setOption(option) } ``` 6. 监听组件大小变化 为了保证 Echarts 图表在组件大小变化时能够自适应,需要监听组件大小变化,并重新渲染 Echarts 图表。 ``` mounted() { this.initChart() window.addEventListener('resize', this.resizeHandler) }, beforeDestroy() { window.removeEventListener('resize', this.resizeHandler) }, methods: { resizeHandler() { this.chart.resize() } } ``` 7. 封装成可复用的组件 将以上步骤封装成一个可复用的 Vue 组件,供其他组件使用。例如: ``` <template> <div ref="container" style="width: 100%; height: 300px;"></div> </template> <script> import echarts from 'echarts' export default { name: 'Echarts', props: { option: { type: Object, required: true } }, mounted() { this.initChart() window.addEventListener('resize', this.resizeHandler) }, beforeDestroy() { window.removeEventListener('resize', this.resizeHandler) }, methods: { initChart() { const container = this.$refs.container this.chart = echarts.init(container) this.chart.setOption(this.option) }, resizeHandler() { this.chart.resize() } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值