Tips for effectively utilizing the updateAxisPointer function in the latest version of vue-echarts (v4.1)

Version 3 allows for direct use of the echarts functions and events, with the ability to override event functions like const updateAxisPointer = function(event)... However, I am struggling to implement this in version 4. You can find more information about version 4 [here][1]. I attempted to utilize the delegateMethod function, but it resulted in an error stating that this.chart [methodName] is not a function.

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

Below is how the code appears in version 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

Answer №1

If you're looking to implement interactive charts in Vue, you can leverage the @updateAxisPointer method along with Vue's event handling capabilities. This code snippet demonstrates how you can achieve this using vue-echarts-v5.0, and it should also be compatible with version 4.1.

Check out the implementation in 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing selections in jQuery may not work properly on certain mobile and IE browsers

I am currently working on creating a dependency between two select boxes. The jQuery code I have implemented works perfectly on most common browsers such as Chrome and Firefox, but it seems to have some issues with Internet Explorer, Edge, and some mobile ...

Determine whether a value contains a minimum of two words or more using JavaScript

When users input their names into a field and submit it, I want to make sure that I receive both their first and last names. In order to do this, I need to check if the value contains at least two words. However, the current code I am using does not seem ...

Utilize JSON parsing with AngularJS

My current code processes json-formatted text within the javascript code, but I would like to read it from a json file instead. How can I modify my code to achieve this? Specifically, how can I assign the parsed data to the variable $scope.Items? app.co ...

Issue: Trouble with Rotating Tooltips in Javascript

I am facing a challenge with the tooltips on my website. I want to ensure that all tooltips have a consistent look and transition effects, but I am struggling to achieve this. The rotation and other effects applied using javascript are not functioning prop ...

What is the best way to secure a folder and its contents utilizing sessions for added protection?

Currently, I am developing a project that involves assigning each user a dedicated folder for uploading files. However, there is a security concern where any user could potentially access files by simply typing in the path and filename. I am exploring met ...

The slides in Swiperjs are having trouble moving smoothly

I am experiencing some challenges with swiperjs where the slides are not snapping to the next slide and I am unable to fetch the active index from them. Below is a snippet of my code where I am using typescript, swiperjs version 6.84, and the Ionic frame ...

Tips for ensuring that the horizontal scroll bar remains consistently positioned at the bottom of the screen

There are two div sections on a page, with one positioned on the left and the other on the right. The div on the right contains multiple dynamically generated tags which necessitate a horizontal scroll bar (overflow:auto). This causes the div's height ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

Having trouble displaying the selected button in React

Is it possible to include multiple functions within an onclick event? Check out the code snippet below: import React from 'react'; class Counter extends React.Component { state = { count: 0, inc: 'Increment', ...

VueJS encountered an error: listen EADDRNOTAVAIL, indicating that the address is not available

I am a beginner in JavaScript. Recently, I started learning about Vue.js with Vue-CLI 2. However, now I want to upgrade to the latest version of Vue-CLI, which is 4.3.0. I followed a step-by-step tutorial to install it, but when I try to run npm run serve, ...

Having trouble getting your AJAX call to function properly? Could be that you're searching for the incorrect

Whenever the button is pressed, I need an AJAX call to be sent to my events_controller#check action. Here's the code snippet: events\new.html.erb: <button id="check-button" type="button">Check</button> application.js: $(document). ...

Create a PHP file with various functions and access them using jquery.post or jquery.get in a separate JavaScript file

Is there a way to call multiple PHP functions from my JavaScript file using Jquery.post? Typically, we use Jquery.post to call a PHP file and pass various values as post data. function new_user(auth_type, tr_id, user_name, email) { $.post("bookmark.p ...

Move information from one webpage to a different page

After developing a site using NextJs, I successfully integrated Discord login functionality and was able to retrieve the user's guilds in the oauth file. Now, I am looking to send this list of guilds (in JSON format) to my dashboard page. In the oau ...

The CSS property overflow:hidden or overflow:scroll is not functioning as expected when applied to a

On my practice website, I have set up a demonstration for showcasing text data. The issue arises when the user inserts an excessive amount of characters in the text box. To address this, I would like the text to be scrollable so that all content can be d ...

What could be causing a problem with the select query in SQLite for iOS using PhoneGap

Could someone please assist me with retrieving values from a database? Here is my code: <script type="text/javascript" charset="utf-8"> // Wait for Cordova to load document.addEventListener("deviceready", onDeviceReady, false); ...

The Vue error message indicates that the specified module, "../services/Repository", could not be located within the project

[SOLVED] After moving the services/ folder into the src/ directory, following the Vue.js Style guide, the error was resolved. Thank you for the help. Upon running npm run serve, an error occurred. Despite researching similar issues on Stack Overflow and a ...

How to trigger a force reload on a VueJS route with a different query parameter?

Is there a method to refresh the page component when two pages utilize the same Component? I have encountered an issue where the router does not reload and the previous edit values persist. { path: "/products/new", component: ProductPage, meta: { ...

What is the best way to create an associative array using jQuery and then send it through AJAX to be parsed by PHP?

Is there a way to create an associative array in jQuery and send it via ajax to a php page for processing? Here is an example of what I am trying to achieve... // jQuery if($something == true) { data[alt] = $(this).attr('alt'); data[sr ...

Launching an Angular application from the local server

I have successfully deployed an Angular frontend on a server. It is functioning well, with three scripts: /runtime.0fad6a04e0afb2fa.js /polyfills.24f3ec2108a8e0ab.js /main.a9b28b9970fe807a.js My goal is to start this application in Firefox without ...

Tips for resolving an issue with an overflowing Uber React-Vis multicolor bar chart

I am trying to create a multi-colored bar chart using Uber's react-vis library. However, I am encountering an issue where the leftmost bar of the chart is overflowing below the YAxis instead of remaining contained to the right of it. You can view the ...