echarts 被封装后多次复用,图表被覆盖,解决方法

场景:为了方便样式统一,封装了一个盒子,其中包含echarts,option是从父组件传来的

问题: 多个父级页面使用这个盒子后,发现只有第一个盒子展示图表,但展示的是最后一个图片的样式,其他都空白

解决方法: 将原本id或class选择器改为ref方式,代码如下

// 原来
var myChart = echarts.getInstanceByDom(document.getElementById('myChart'));
// 改成
var myChart = echarts.getInstanceByDom(this.$refs.myChart);

 整体代码如下: 

<!--
 * @Description: 图表
-->
<template>
  <div class="Chart">
    <div class="titleBox">
      <div class="text">
        {{ formData.label }}
        <span>{{ formData.value }}</span>
        <svg-icon icon-class="lock" />
        <svg-icon
          icon-class="download"
          @click="downloadImg"
        />
      </div>
    </div>
    <div
      ref="myChart"
      id="myChart"
    />
    <slot />
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { downloadEchartsImg } from "@/utils/tools";

export default {
  name: 'StackedLine',
  props: {
    formData: {
      type: Object,
      required: true
    },
    option: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      open: false,
    }
  },
  mounted() {
    var myChart = echarts.init(this.$refs.myChart);
    window.addEventListener('resize', myChart.resize)
    setTimeout(() => {
      myChart.resize()
      myChart.setOption(this.option, true)
    });
  },
  methods: {
    /**
     * 下载图片
     */
    downloadImg() {
      var myChart = echarts.getInstanceByDom(this.$refs.myChart);
      downloadEchartsImg(myChart)
    }
  }
}
</script>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 ECharts 的 X 轴轴线上添加小色块,可以通过在 X 轴上添加自定义组件的方式来实现。具体的步骤如下: 1. 在 ECharts 的 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 2. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 3. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 4. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 5. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 6. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 7. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X 轴,并设置 boundaryGap 为 false,使得坐标轴从头开始。 8. 使用 ECharts 提供的 graphic 组件,根据条件来绘制小色块,可以使用 rect、circle 等基础形状。 9. 将绘制好的小色块添加到 X 轴的 axisLine 上,使用 offset 属性来控制小色块的位置。 示例如下: ``` option = { xAxis: { type: 'category', boundaryGap: false, axisLine: { onZero: false, lineStyle: { color: '#333' }, // 添加自定义组件 // 绘制小色块 // 将小色块添加到 axisLine 上 // 控制小色块的位置 // 添加多个小色块,根据条件设置不同的颜色和位置 // 示例代码如下: graphic: [{ type: 'rect', position: [100, 0], shape: { width: 10, height: 10 }, style: { fill: '#ff0000' }, // 控制小色块的位置 // 可以使用 offset 属性 // offset: [0, 10] // 在 X 轴上方,向下偏移 10 像素 // offset: [0, -10] // 在 X 轴下方,向上偏移 10 像素 // offset: [10, 0] // 在 X 轴左侧,向右偏移 10 像素 offset: [-10, 0] // 在 X 轴右侧,向左偏移 10 像素 }] }, // X 轴的数据 data: ['A', 'B', 'C', 'D', 'E', 'F', 'G'] }, yAxis: { type: 'value' }, series: [{ data: [10, 20, 30, 40, 50, 60, 70], type: 'line' }] }; ``` 这样就可以在 X 轴轴线上添加小色块了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值