Clicking on Vue ChatJS to instantly redirect

Utilizing the Vue ChartJS, I have successfully generated a Line Chart.

My Objective:

I want to redirect the user whenever they click on a data point.

Here is a screenshot for referencehttps://i.sstatic.net/06kWB.png

For instance, clicking on the first data point should redirect the user to invoices/month_of_the_pointer.

The Code:

chart_data: {
  labels: ['January', 'February', 'March', 'April'],
  datasets: [
    // GENERATED DYNAMICALLY FROM A FUNCTION
  ],
},
options: {
  scales: {
    yAxes: [{
      ticks: {
        beginAtZero: true,
        callback: function(label, index, labels) {
          return env.EGP_CURRENCY + label;
        },
      },
      gridLines: {
        display: true
      },
    }],
    xAxes: [ {
      gridLines: {
        display: false
      }
    }]
  },
  legend: {
    display: true
  },
  responsive: true,
  maintainAspectRatio: false
},

Approaches Taken:

I attempted to include an onClick callback, which does register the click event but triggers it for all areas of the chart rather than just the data points.

onClick: function(evt) {
  console.log(evt);
},

Answer №1

To start, the first step is to access the current Chart instance. I found an informative thread on a GitHub issue, where the author mentions that the current instance can be accessed by referring to this.$data._chart.

Next, you will need to invoke the getElementsAtEvent method of the instance, with the event as the parameter.

By calling getElementsAtEvent(event) on your Chart instance and providing it with an event argument or jQuery event, you can retrieve the point elements that correspond to the same position as the event. This feature can prove useful for implementing DOM-based tooltips or triggering specific actions in your application.

this.$data._chart.getElementsAtEvent(event);

Finally, tailor your custom logic based on the outcome of this method (for Vue Router users:

this.$router.push("/path/to/redirect");
).

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

"Why does the form.submit() function fail in IE9 when the form is in an iframe and the user is coming from Gmail

I have recently developed a function within my CodeIgniter framework that allows me to send emails with a backlink to my site. The link directs users to a page on my website that includes an iframe. Within this iframe, I have implemented a file input form ...

Transforming the date from JavaScript to the Swift JSON timeIntervalSinceReferenceDate structure

If I have a JavaScript date, what is the best way to convert it to match the format used in Swift JSON encoding? For example, how can I obtain a value of 620102769.132999 for a date like 2020-08-26 02:46:09? ...

Regular Expression: Identify specific characters at the start or end of a string

I am in need of a regular expression (regex) that can precisely match the char set 'AB' only if it is at the beginning or end of a string, and then replace it with an empty string. It is important to note that the regex should not match parts of ...

Executing MongoDB collection operations with array filtering

I am looking to count records based on tags and filter them before including in specific groups // data in database {tags: ['video', 'Alex'], ... }, {tags: ['video', 'John'], ... }, {tags: ['video', 'J ...

Obtain the index of the selected item from a dropdown menu

Is there a way for the selectedIndex to return -1 if no item is selected, instead of the element's text at position 0? It seems that the selectedIndex always returns 0 even when nothing is selected. <select id="abc" name="abc"> <option& ...

Clicking our way back through the loop

Looking to display a name when each item is clicked. I've assigned an ID to each element, but it seems like I'm overlooking something. In the current code, I'm thinking there should be a variable involved, but I'm uncertain: $(document ...

Updating a Div on your webpage using a JQuery UI dialog: A step-by-step guide

I am currently trying to update my webpage from a JQuery UI dialog, but the code I have so far does not seem to be working. Any assistance or guidance would be greatly appreciated. function submit_new_site() { // These are the input text IDs on the ...

What is the purpose of encasing the routes within the <Switch> element in react-router, when they seem to function perfectly fine without it

Currently utilizing react-router-dom version 5.2.0. As a practice, I typically enclose all my Route components within a Switch component. For instance: const MyRoutes = () => ( <Switch> <Route path='/route1' exact ...

The browser is opting to download the HTML file rather than displaying it

Recently, I set up a server using Node.JS express where I specified the location of an HTML file in the public folder. app.use(express.static(__dirname + '/public')); app.listen(8080); In previous projects, this setup worked seamlessly. However ...

When you make a POST request to an express API, the properties are not clearly defined

Just getting into Vue.JS and experimenting with creating a basic MEVN to-do list app for practice. I keep encountering an issue when trying to send a POST request to my express server, receiving the error message: TypeError: Cannot read properties of unde ...

Import components on the fly

In my Home.vue component, I am attempting to dynamically import components. The Vue component structure is as follows: <template> <div> <!-- NEW --> <div v-for="widget in widgets"> <component v-b ...

Efficient Data Fetching in Vue-Router: Fetch Data Once

I am currently utilizing Vue Router's beforeEnter hooks to fetch data for a specific route. { path: '/A', beforeEnter: (to, from, next) => { loadData(); next(); }, } An illustration of the process would be: Navi ...

Display images with sequential animation and a delay, combining fade-in and slide-up effects

I am attempting to create a cycling image display with specific effects: There should be a one-second delay before the first image is shown. The first image will appear with a fade-in and slide-up effect. Image #1 will remain visible for 5 seconds before ...

Stop the WebGL Three.js container from capturing scroll events

I am working on a full-screen three.js application which serves as the background and I am trying to add overlaid text that can be scrolled. However, my issue arises from the fact that the three.js WebGL container intercepts all scroll events and prevents ...

Is there a way to create a dropdown menu that transforms into a burger icon on smaller screens, while still housing all of my navigation items within it?

I'm currently working on a segment of my header that includes a navbar with 3 links, as well as a dropdown menu listing additional functionalities. Here is the current code I have: <div class="col-md-4 pt-4"> <nav class="navbar ...

The onBlur event attached to a div is triggered when transitioning from one input element to another within the same div

Here's a scenario: I have a div with two input fields nested inside, like this: <div> <input placeholder="Input1"/> <input placeholder="Input2"/> </div> I have a requirement to trigger a specific ...

Removing items in vue.js

I'm currently in the process of learning Vue.js. This is my first attempt at creating a small to-do application, and I am encountering issues with deleting each individual task upon clicking. Despite watching multiple YouTube tutorials, I have not bee ...

Bootstrap tab requires double taps to switch on iPhone when using meteor platform

One recurring issue I have encountered with my current website involves iPhone users and their difficulty in switching between two weeks on my site. <ul class="nav nav-tabs justify-content-center" id="myTab" role="tablist"> <li class="nav-item" ...

The `.append()` function includes HTML content as plain text

I am using JavaScript to dynamically add HTML elements to my webpages. I have created a loop that iterates through all the projects, each containing multiple pictures. The first step involves generating the project title and adding it within a div element ...

Retrieve an image from an external web service and transfer it to a different route in Express.js

I am facing an issue with passing an image object from an external web service through a node express route. The specific problem I am encountering involves retrieving an image from a URL and attempting to pass it as is, but it seems to be not functioning ...