Vue throwing 'RangeError: Maximum call stack size exceeded' error when trying to update Chart.js chart

I've initialized a chart object within the mounted() lifecycle hook and stored it in the 'myChart' variable that is declared in the data() method. This allows me to access the chart object from various parts of the code since I have heard that it is not possible to pass variables defined in mounted to other methods.

However, when I try to update the chart using this.myChart.update(), I encounter the error "Uncaught (in promise) RangeError: Maximum call stack size exceeded".

Could someone please explain why this error is occurring and suggest ways to fix it? Alternatively, are there alternative methods for accessing variables defined in mounted within watch or methods?

<script>
    import Chart from 'chart.js/auto';
    export default {
        name: 'ChartTest',
        props: {
            data: Object,
            title: String,
    },
    data() {
        return {
            myChart:'' //declaration of myChart variable
        }
    },
    watch: {
        data:function() {
            this.myChart.update()  //issue occurs here
        }
    },       
    mounted() {
        const progressChart = new Chart(document.getElementById("progress-chart"), {
            type: 'line',
            data: this.data,
            options: {
                plugins: {
                    title: {
                        display: true,
                        text: this.title
                    }
                },
                scales: {
                    y: {
                        display: true,
                        stacked: true,
                        max: 0,
                        min: 100,
                        title: {
                            display: true,
                            text: 'Your Score (%)'
                        }
                    }
                }
            }
        });
        
        this.myChart = progressChart; //assigning chart object to myChart variable
    }     
}
</script>

Answer №1

When using chart.js with Vue, there may be compatibility issues due to the direct manipulation of the DOM by chart.js. This interference with Vue's tracking and managing of the DOM can lead to errors related to exceeding the maximum call stack size. To address this issue, there are two potential solutions:

  1. To prevent Vue from tracking changes to the chart, make the chart non-reactive by declaring the myChart variable outside the return statement of the data function:
data() {
    this.myChart = null;
    return {
      // myChart: '' //variable declared
    };
  },

Additionally, it is important to ensure that the data is set before calling the chart's update() function:

watch: {
    data: function () {
      this.myChart.data = this.data;
      this.myChart.update();
    }
  },

Following these steps should allow your chart to work and update, though it will not be reactive. If reactivity is crucial for you, consider using vue-chartjs, which is a Vue wrapper for chart.js designed to be compatible with Vue and provide reactive charts.

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

Implementing Dynamic Styling Using Parent Component in React Native

I am facing an issue with a <Text> component that is receiving a style through props... TextFile.js: <Text style={styles.text}> This is a line of text and this might be a second line </Text> screenFile.js: <View style={styles.v ...

Validation for an empty string in the value of an input element

Why is my empty string validation not functioning correctly for the value property of an HTMLInputElement after trimming? The issue lies in the code within the storeOnEnter function. My intention is to update the state only when the input field is empty. ...

The ASP.NET platform is experiencing issues in displaying the Bootstrap 5 modal

Whenever I try to click the button that triggers the bootstrap modal, I encounter an error. I expected to be able to open and close the modal without needing any extra JavaScript code. https://i.sstatic.net/OMg6b.png Index.cshtml <button class="r ...

Creating a monorepo project in JavaScript that mimics the structure of Visual Studio projects

When working on a typical .NET Core project (with Visual Studio 2019), the structure typically looks like this: Example/ |-- Example.Common/ | |-- FileExample1.cs | |-- FileExample2.cs | `-- Example.Common.csproj |-- Example.WebAPI/ | |-- Control ...

The focus is lost when v-if is triggered during input

Within my code, there is an input field: <input type="text" maxlength="2" :value="valueHours" @input="calculateTimes($event)" @blur="updateValueHours($event)"> Accompanied by a list section: <div ...

Comparing Watch and $watch: Understanding the Distinctions

Let's ponder a question. What sets apart options from instance methods? In the context of a watch example, we can utilize a watcher as an option (https://v3.vuejs.org/api/options-data.html#watch) or as a method of an instance (https://v3.vuejs.org/a ...

"Extracting JSON data from a URL and loading it into a

I am currently working on a project where I am retrieving data from a URL and storing it in an array. Here is the code snippet: $url = 'https://www.datastro.eu/api/explore/v2.1/catalog/datasets/orbits-for-current-comets-in-the-mpc-database/records?ord ...

Script for collapsing or expanding is non-functional

Although I am a beginner in Javascript coding, I am eager to learn more about it. I stumbled upon some tutorials on creating collapse/expand <div> blocks. After testing the code on jsfiddle, I found that it works fine there. You can find the code he ...

methods for extracting json data from the dom with the help of vue js

Can anyone help me with accessing JSON data in the DOM using Vue.js? Here is my script tag: <script> import axios from "axios"; export default { components: {}, data() { return { responseObject: "" }; }, asy ...

How to customize nouislider appearance using Bootstrap styles

I've been attempting to style the noUi tooltips to match the bootstrap design, but I've been unsuccessful so far. I'm wondering if there's an easy way to achieve this. I tried to structure the markup based on the bootstrap template ava ...

Conditionally displaying ng-options in AngularJSI'm looking for a

After spending hours searching, I'm unable to find a solution to my problem. I was able to implement it before but lost the code and can't remember how I did it. I need to display only certain array values in a select box using ng-options. The d ...

A guide on dividing date strings within a JavaScript array

When dealing with an array of Month/Day/Year datestrings like the one below... const array1 = ["05/31/2022", "06/01/2022", "06/02/2022"] ...my goal is to modify the array to exclude any datestrings (starting with 01 as the Day) that come after datestrings ...

Determine the presence of a value within a specific column of an HTML table using jquery

When I input an ID number into a textbox, it shows me the corresponding location on a scale in another textbox. You can see an example of this functionality in action on this jsFiddle: http://jsfiddle.net/JoaoFelipePego/SdBBy/310/ If I enter a User ID num ...

Is there a way to skip the current page in an Ajax GET request?

As an example, I am currently viewing the /users page and when I send a request from that location, it directs me to /users/conversations/id instead of just conversations/id. How can this be adjusted? users.js $('.start-conversation').click(fun ...

Creating a Vue.js webpack build optimized for production platform

This is my first experience using a frontend Build Tool. Up until now, I've only worked with frameworks that were as simple to install as adding a <script> tag to my HTML page. Now you have an idea of my skill level. I am working on a Vue.js/Vu ...

issue with retrieving data from PHP script via ajax

My attempts to use AJAX to call a PHP script have been unsuccessful. I added an echo alert statement in my deleteitem.php script to ensure it was being called, but no matter what I tried, it never executed. Both the PHP script and the JS script calling it ...

Peculiar redirection encountered while handling a form with checkbox option

When I try to submit the form in Chrome, the PHP file does not get called and I am redirected to another HTML page. However, in Firefox, when I select three checkboxes, it redirects me to that same HTML page as in Chrome. I even tried using radio buttons i ...

Having trouble with importing a TypeScript class: encountering a "cannot resolve" error message

Could you lend me your expertise? I'm puzzled by this issue that seems to be quite simple and straightforward: export class Rectangle { height: number = 0 width: number = 0 constructor(height: number, width: number) { this. ...

The variable fails to receive the AJAX response

Trying to retrieve the content of data.dat, I have utilized the following code. Below are the relevant files: main.js function getData() { var result; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState ...

Issue encountered: The 'secp256k1-browserify' module could not be located when using browserify in conjunction with ethereumjs-lib

Trying to execute a basic script in the browser using the ethereumjs-lib library that has been installed. Utilizing browserify to produce the necessary javascript file for embedding into the html page. However, encountering an error related to missing modu ...