Displaying historical data on hover in a chart using Vue3 and Chart.js

Displaying previous data on hover in Vue3 Chart JS

After updating my chart data with API information, I encountered an issue where the chart would display the previous API data when hovering over it. I attempted using distory() but it did not resolve the problem.

Below are the relevant code snippets:

 methods: {
    barChart() {
      var Chart = require("chart.js");
      let options = new Chart(document.getElementById("bar-chart-grouped"), {
        type: "bar",
        data: {
          labels: this.llabels,
          datasets: [
            {
              label: "Male Population",
              backgroundColor: "rgba(0, 143, 251, 0.8)",
              data: this.mdata,
            },
            {
              label: "Female Population",
              backgroundColor: "rgba(0, 227, 150, 0.8)",
             data: this.Fdata,
             },
            {
              label: "Other Population",
              backgroundColor: "rgb(254, 176, 25,0.8)",
              data: this.Odata,
            },
          ],
        },
      });
      if (!this.chart) {
        this.chart = options;
      } else {
        this.chart.options = options;
        this.chart.update();
      }
    },
}

Answer №1

Try utilizing

this.chart.options = options.options;
in place of this.chart.options = options;

Remember, the variable options contains your chart data as well, not just the configuration settings.

Consider improving your code by only initializing the chart on the initial execution of that method (put the new Chart within the if statement)

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

Ways to reset input fields following form submission

I've been trying to figure out how to clear the input fields once the form is submitted, but for some reason, the input data remains there even after each submission. I'm using ajax jquery form. Any ideas on how to resolve this issue? Thank you ...

Route Handler 13 is encountering difficulties in retrieving data from the body in the (app/api/auth) endpoint

Whenever I attempt to retrieve the body from the new export async function POST( req: Request), it seems to come through as a stream instead of the expected content type. The route handler can be found in api/auth/signup See folder layout image export asyn ...

Having difficulty running lint on Vue 3 TypeScript application, but building process is successful

We are encountering an issue at the moment. We can successfully build our app, but we are facing challenges with linting using the vue tools (vue-cli-service ...). The hot-reloading feature works initially, but upon saving a file, we receive an error mess ...

What is the best method for importing components with identical names in Vue Router?

As I delve into my VueJs project and organize my router records, an issue has arisen regarding components with the same name. How can I import and utilize them within the route records? Is there a way to ensure their names are unique when setting up the ro ...

What is the best way to retrieve the value of a dynamically created button in code?

I am dealing with a situation where I have a button that is supposed to fetch some data based on the classroom ID. button(type='button' id='getButton', onclick='get()', value=classroom.id ) Class Students Additionally, I ha ...

Problem with overlapping numbers in the Vis network

I am currently working on a project using Angular 8 and Visnetwork. Everything is going well, but I am facing an issue with overlapping numbers on lines. Is there a way to adjust the position of the numbers on one line without separating the lines? Can s ...

Establishing a standard flatiron-director route (within the element) using the polymer core-pages component

This particular query is closely linked with issues surrounding the usage of flatiron-director/core-pages SPA in conjunction with route-specific JavaScript functions and default routes. While the solution proposed may be effective, my limited expertise in ...

Jquery ajax is failing to achieve success, but it is important not to trigger an error

My jQuery ajax request seems to be stuck in limbo - it's not throwing an error, but it also never reaches the success function. This is how my code looks: function delete() { $("input[name='delete[]']:checked").each(function() { ...

What is the best method for setting a session cookie securely while also using CSRF tokens?

In my express application, I am working on setting the session cookie to be secure. Here is the code snippet I have tried so far: app.use(express.cookieParser()); sessionOptions = definitions.REDIS; sessionOptions.ttl = definitions.session.expiration; app ...

Creating a row of aligned card components in React

Recently, I began learning React and successfully created a card component using Material UI. However, this time around, I'm attempting to create it using axios and map() methods. I expected the cards to be displayed in the same row horizontally, not ...

How to change the background color of a slider using jQuery

As a newcomer to web programming, I am currently working on incorporating a slider into my website using jQuery. My goal is to change the background color of the slider to red when the value is less than 5 and green when it exceeds 5. Can this be achieved ...

Having trouble with my inline CSS not working with the NextJS Image component

Why am I unable to style the Image component in NextJS to manipulate my image? Am I overlooking something? <Image className={styles.bg} style={{ transform: `translateY(${offset * 0.5}px)` }} src="/../public/bg.jpg" // ...

Can anyone suggest an effective method for displaying images using JavaScript by specifying a route and filename?

Currently, I have the following code snippet: const route = '/Images/Banner/'; const slides = [ { name: "banner-01", url: `${route}banner-01.png` }, { name: "banner-02", url: `${route}banner-02.pn ...

Angular 2: Emptying input field value on click event

I am experiencing an issue with resetting my input values. I have a search bar with filter functions. When I enter a value, it displays a list below and I want to add a function to these links. When I click on one of them, it takes me to another component ...

Retrieving and submitting text in a NodeJS environment

I'm attempting to retrieve text from an API that only provides a string of text ((here)), but I am encountering difficulties in displaying it accurately. When I try to post it, the output shows as [object Response], and the console.log is not showing ...

Ways to retrieve the current tab title that I have designated in the Jquery Tabs UI

I found this helpful resource at http://jqueryui.com/demos/tabs/#manipulation and I am currently using it. My goal is to retrieve the title of the tab that is currently selected and which I had previously named (for example, in the href attribute). How can ...

Is Node.js truly a single-threaded and non-blocking platform?

As I delve into the world of Node.js, one thing that has caught my attention is the fact that it operates on a single thread and is non-blocking in nature. While I have a solid understanding of JavaScript and how callbacks work, the concept of Node.js bei ...

Finding common elements between two arrays through partial matching

Imagine I have two arrays: var array_full = ['table', 'sleeping', 'data']; var array_part = ['sleep', 'able']; My goal is to extract items from the full string array (array_full) that do not contain any e ...

Utilize the search component multiple times within a single template in Vue.js

I have a regular search input that searches and displays results effectively. Now, I need to duplicate this search input and reuse it within the same component. <input class="search ml-2" type="search" placeholder="Search" v-model="search"> Here is ...

How can I use JavaScript (specifically Reactjs/NextJs) to add a new contact to my phone from a web

I'm currently working on creating a new contact using JS. I've come across documentation for retrieving all contacts from the phone here. Are there any libraries available to assist with this task, or is it only achievable with React Native? ...