【问题标题】:How to use the function updateAxisPointer in vue-echarts-v4.1?如何在 vue-echarts-v4.1 中使用 updateAxisPointer 函数?
【发布时间】:2020-04-28 09:11:05
【问题描述】:

在版本 3 中,可以直接使用 echarts 本身的函数和事件,覆盖事件的函数,写成 const updateAxisPointer = function(event)... 在版本中我不明白如何做到这一点4. 这是版本 4 的 [link][1]。我尝试使用 delegateMethod 函数,但它显示错误 this.chart [methodName] is not a function。

const updateAxisPointer = function(event) {
          console.log('event', event);
};
this.$refs.eChart.delegateGet('updateAxisPointer', updateAxisPointer);

这是代码在第 3 版中的查找方式

  <div class="echarts">
    <IEcharts :option="option" @ready="onReady" />
  </div>
</template>

<script>
import IEcharts from 'vue-echarts-v3/src/full.js';

export default {
  name: 'View',
  components: {
    IEcharts,
  },
  data() {
    // const that = this
    return {
      ins: null,
      echarts: null,
      option: {
        legend: {
          orient: 'vertical',
          top: 40,
          left: 60,
        },
        tooltip: {
          trigger: 'axis',
          showContent: false,
        },
        dataset: {
          source: [],
        },  
        yAxis: {
          type: 'value',
          name: '',
          nameLocation: 'middle',
          nameGap: 40,       
        },
        grid: {
          left: 40,
          right: 37,
          bottom: 45,
          top: '50%',
          containLabel: true,
        },
        series: [],
      }
    };
  },
  methods: {
    onReady(instance, echarts) {
      const that = this;
      that.ins = instance;
      that.echarts = echarts;

      const updateAxisPointer = function(event) {
        let xAxisInfo = event.axesInfo[0];
        if (xAxisInfo) {
          let dimension = xAxisInfo.value + 1;
          that.ins.setOption({
            series: {
              id: 'pie',
              label: {
                formatter: '{b}: {@[' + dimension + ']} ({d}%)',
              },
              encode: {
                value: dimension,
                tooltip: dimension,
              },
            },
          });
        }
      };
      that.ins.on('updateAxisPointer', updateAxisPointer);
    },
  },
};
</script>```


  [1]: https://github.com/ecomfe/vue-echarts/blob/master/src/components/ECharts.vue

【问题讨论】:

    标签: javascript vue.js echarts


    【解决方案1】:

    您可以将@updateAxisPointer 直接用于 Vue 的事件处理。 我使用 vue-echarts-v5.0 测试了以下示例,但它应该已经在 4.1 版本中运行。

    App.vue:

    <template>
        <chart 
           :options="options"
           @updateAxisPointer="updateAxisPointer"
           >
        </chart>
    </template>
    
    <script>
    import ECharts from 'vue-echarts'
    export default {
      name: 'Interactive',
      components: {
        'chart': ECharts,
      },
      data() {
        return {
          options: {
            title: {
              text: 'Interactive example chart',
              left: 'center',
              textStyle: {
                color: '#000'
              }
            },
            tooltip: {
              trigger: 'axis',
              showContent: false
            },
            dataset: {
              source: [
                ['technology', '2012', '2013', '2014', '2015', '2016', '2017'],
                ['photovoltaic', 41.1, 30.4, 65.1, 53.3, 83.8, 98.7],
                ['wind', 86.5, 92.1, 85.7, 83.1, 73.4, 55.1],
                ['natural gas', 24.1, 67.2, 79.5, 86.4, 65.2, 82.5],
                ['coal', 55.2, 67.1, 69.2, 72.4, 53.9, 39.1],
                ['oil', 50.2, 60, 60.2, 70.4, 50.9, 39.1],
                ['battery', 20, 30, 80.2, 50.4, 10.9, 29.1],
                ['hidrogen', 30, 40, 60, 50, 50, 19.1]
              ]
            },
            xAxis: {type: 'category'},
            yAxis: {gridIndex: 0},
            grid: {top: '55%'},
            series: [
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {type: 'line', smooth: true, seriesLayoutBy: 'row'},
              {
                type: 'pie',
                id: 'pie',
                radius: '30%',
                center: ['50%', '30%'],
                label: {
                  formatter: '{b}: {@2012} ({d}%)'
                },
                encode: {
                  itemName: 'technology',
                  value: '2012',
                  tooltip: '2012'
                }
              }
            ]
          }
        };
      },
      methods: {
        updateAxisPointer(params) {
          var xAxisInfo = params.axesInfo[0];
          if (xAxisInfo) {
            var dimension = xAxisInfo.value + 1;
            this.options.series = [{
              id: 'pie',
              label: {
                formatter: '{b}: {@'+dimension+'} ({d}%)'
              },
              encode: {
                value: dimension,
                tooltip: dimension
              }
            }]
          }
        },
      },
    };
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2021-04-01
      • 2020-05-20
      • 1970-01-01
      • 2021-05-15
      • 2018-07-08
      • 1970-01-01
      相关资源
      最近更新 更多