vue echart写法组件案例

这篇博客介绍了如何在 Vue2 中使用 ECharts 库初始化图表,特别是在 `mounted` 生命周期钩子中通过 `this.$el` 获取元素并创建 ECharts 实例。同时,当组件接收到新的图表数据时,通过监听器自动更新图表。博客还展示了详细的 ECharts 图表配置,包括颜色、线条样式和响应式设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只适用于vue2,vue3中不支持this.$el了

<template>
  <div class="chartPart" style="width: 100%; height: 100%"></div>
</template>

<script>
import echarts from "echarts";

export default {
  props: ["chartData"],
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.autoChart(val);
      },
    },
  },
  data() {
    return {
        legendColor: '#5589E6',
        lineColor: '#0A2860',
        fontColor: '#497BD5'
    };
  },
  mounted() {
    this.autoChart(this.chartData);
    window.addEventListener("resize", () => {
      this.autoChart(this.chartData);
    });
  },
  methods: {
    autoChart(chartData) {
      const thisChart = this.$echarts.init(this.$el);
      let thisOption = this.optionFn(chartData);
      thisChart.setOption(thisOption);
      thisChart.resize();
    },
    optionFn(chartData) {
      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          right: "5%",
          top: "15%",
          itemWidth: 14,
          itemHeight:14,
          textStyle: {
            //图例文字的样式
            fontSize: 14,
            color: this.legendColor,
          },
          data: chartData.legend,
        },
        grid: {
          top: "25%",
          right: "5%",
          left: "8%",
          bottom: "15%",
        },
        xAxis: [
          {
            type: "category",
            data: chartData.xAxisData,
            axisLine: {
              lineStyle: {
                color:  this.lineColor,
              },
            },
            axisLabel: {
              margin: 10,
              color: this.fontColor,
            //   textStyle: {
            //     fontSize: 14,
            //   },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            axisLabel: {
              formatter: "{value}",
              color: this.fontColor,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: this.lineColor,
              },
            },
            splitLine: {
              lineStyle: {
                color: this.lineColor,
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            name: chartData.legend[0],
            data: chartData.seriesData,
            barWidth: "14px",
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: chartData.color[0], // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: chartData.color[1], // 100% 处的颜色
                    },
                  ],
                  false
                ),
                barBorderRadius: [8, 8, 1, 1],
                // shadowColor: 'rgba(0,160,221,1)', // 阴影
                // shadowBlur: 4,
              },
            },
            // label: { // 柱子上面显示数字
            //     normal: {
            //         show: true,
            //         lineHeight: 30,
            //         formatter: '{c}',
            //         position: 'top',
            //         textStyle: {
            //             color: '#00D6F9',
            //             fontSize: 20
            //         }

            //     }
            // }
          },
        ],
      };
    },
  },
};
</script>

<style scoped></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值