A guide on integrating a stacked bar chart from ApexCharts into a modal or v-dialog component

I'm facing a minor issue with vue-apexcharts while attempting to showcase some data in a stacked bar chart.

Here is the simple component I have created:

<template>
    <div v-if="this.loaded">
        <apexchart
            width="100%"
            :height="this.height"
            :options="this.chartOptions"
            :series="this.chartData"
        ></apexchart>
    </div>
</template>

<script>
export default {
    name: "StackedBar",
    props: ["data", "labels", "height"],

    data() {
        return {
            loaded: false,
            chartData: [],
            chartOptions: {},
        }
    },

    async mounted() {
        await this.$nextTick()
        this.chartData = this.data
        this.chartOptions = this.buildChartOptions()
        this.loaded = true
    },

    methods: {
        buildChartOptions: function () {
            let chartOptions = {
                theme: {
                    mode: this.getTheme(),
                },
                chart: {
                    fontFamily: "Roboto Mono, Roboto, Arial, sans-serif",
                    type: "bar",
                    stacked: true,
                },
                plotOptions: {
                    bar: {
                        horizontal: true,
                    },
                },
                xaxis: {
                    categories: this.labels,
                },
                fill: {
                    opacity: 1,
                },
                legend: {
                    position: "right",
                    horizontalAlign: "left",
                },
            }

            return chartOptions
        },

        getTheme: function () {
            if (this.$vuetify.theme.dark) {
                return "dark"
            } else {
                return "light"
            }
        },
    },
}
</script>

This component is embedded in a vuetify v-dialog and only visible when the user clicks to expand the parent component of the dialog. Excluding the nextTick() call in the mounted hook which seems necessary for the chart to render properly on the first visibility toggle, here's what I observe:

  1. User expands component; dialog appears with chart rendering correctly.
  2. User closes dialog; dialog disappears.
  3. User re-expands component; dialog displays, but the chart does not appear and an error is thrown:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'x')
    at t.value (apexcharts.min.js:6)
    at t.value (apexcharts.min.js:6)
    at t.value (apexcharts.min.js:6)
    at t.value (apexcharts.min.js:6)
    at s.value (apexcharts.min.js:6)
    at c (apexcharts.min.js:6)
    at s.value (apexcharts.min.js:6)
    at t.value (apexcharts.min.js:6)
    at t.value (apexcharts.min.js:14)
    at t.<anonymous> (apexcharts.min.js:6)

Interestingly, changing the stacked option to false resolves this issue, and the chart displays correctly every time the dialog is opened/closed. This problem also doesn't occur when displaying the data as a heatmap instead of a bar chart.

My question is: Is this expected behavior due to something on my end, or is it unexpected?

A stacked bar chart is the most appropriate choice for the data I need to show, and ApexCharts appears to be superior to other plotting libraries I've tested - so a solution would be greatly appreciated.

Thank you

Answer №1

If you have placed your component within a v-dialog, make sure to include the eager prop in the dialog. This prop will ensure that the content of the component is rendered as soon as it is mounted.

<v-dialog v-model="dialog" eager>
   ...
</v-dialog>

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

Every time I attempt to log in, the code keeps generating an error message net::ERR_EMPTY_RESPONSE. If successful, it should redirect to the

The code is producing a net::ERR_EMPTY_RESPONSE error when attempting to log in. The username and password are hardcoded. I simply want the admin to input their credentials on the Employee.html page, and if they match the ones in the main.js file, redirec ...

Is there a way to automatically change the full screen background on refresh?

Is there a way to have the background image of this website change to different pictures every time the page is refreshed? I'm curious about how this can be achieved. I noticed that Offliberty is able to do this. I tried looking at the source code b ...

Tips for passing an object as an argument to a function with optional object properties in TypeScript

Consider a scenario where I have a function in my TypeScript API that interacts with a database. export const getClientByEmailOrId = async (data: { email: any, id: any }) => { return knex(tableName) .first() .modify((x: any) => { if ( ...

Using an array to dynamically input latitude and longitude into a weather API call in PHP

I am currently working on a leaflet map project that showcases the top 10 cities in a selected country. The markers are added dynamically based on the coordinates provided in the $latLng array. For example, when Australia is chosen from the select field, t ...

How can Sequelize handle multiple foreign keys - whether it be for one-to-many relationships or many-to-many relationships

I'm working on a project with two tables: users and alerts. Users should have the ability to upvote and downvote on alerts. Here are the model definitions: Alert module.exports = function(sequelize, DataTypes) { var Alert = sequelize.define("al ...

Uncaught ReferenceError: jQuery is undefined" - Navigating the Angular and JavaScript Realm with

There is an Angular project that I am working on, and it utilizes the AvayaClientSDK consisting of three JavaScript files. While trying to import the AvayaClientSDK JS file into my component, an error message stating "jQuery is not defined" appeared. ...

Is there a way to ensure my Vue variable is declared once the page has fully loaded?

I have implemented a v-for loop in my code: <div v-for="(user, index) in users" :key="index" :presence="user.presence" class="person"> <span class="cardName h4">{{ user.name }}</span> ...

Unable to trigger the show_popup function by clicking the button

Why won't this piece of code function as expected? <script src="web_push.js"></script> <body> <button onclick="show_popup()"> Button </button> </body> The content of web_push.js file is shown below ...

Converting JSON data into an array of a particular type in Angular

My current challenge involves converting JSON data into an array of Recipe objects. Here is the response retrieved from the API: { "criteria": { "requirePictures": true, "q": null, "allowedIngredient": null, "excluded ...

The application of document.body.style.backgroundColor is not compatible with an external CSS style sheet

Why does document.body.style.backgroundColor not work with an external CSS style sheet? I have the following CSS: body { background-color: red; } In my css style sheet, when I use JavaScript alert alert(document.body.style.backgroundColor);, it sh ...

Implementing a delete functionality within a loop on a JavaScript object array

I have a JavaScript object with the following structure: var partner = { p_name: { value: partner_name, label: "Name" }, p_id: { value: partner_ID, label: "ID" }, p_status: { value: partner_status, label: "Status" }, p_email: { value: partner_emai ...

Certain images fail to load when the properties are altered

I am facing an issue where the images in my child components do not show up when props are changed, unless the page is manually refreshed. There are no 404 errors for the images, and even when I inspect the image element, I can see the correct image link b ...

Determine the variance between two strings using Jquery

Hello and thank you in advance for your help. I'm facing a basic query here - I have two variables: var x = 'abc'; var y = 'ac'; I am looking to compare the two variables and find the dissimilarity between them, which should be: ...

Modify the event from creating a table on click to loading it on the page onload

Hey there, friends! I've been brainstorming and came up with an idea, but now I'm stuck trying to switch the code to onload(). I've tried numerous approaches, but none seem to be working for me. Below is the code I've been working wit ...

Effective technique for connecting client-side JavaScript with server-side node.js

I am striving to create a compact website featuring field auto-completion, drawing suggestions from the server's database (utilizing Node.js + MySQL). What approaches can I take to establish client-server communication as the user inputs data into a ...

Combining Codeigniter with AJAX for an efficient system

I'm facing an issue with my like system where a user can give more than one like, but only one is being registered in the database. I suspect it's related to AJAX. Here is the button code: <a class="btn btn-xs btn-white" name="btn" onclick=" ...

Fontawesome is unable to update the class due to the presence of invalid characters in the string

const toggleDarkOrLight = document.getElementsByTagName('i')[0]; var toggled = false; const toggleFunction = () => { if (toggled === false) { toggleDarkOrLight.classList.remove('fa fa-toggle-off'); toggleDarkOrLight.classLi ...

Issue encountered while attempting to execute the command "vue-cli-service serve" using Vue 3

ISSUE ENCOUNTERED During an attempt to update packages, I executed ncu -u, followed by npm install to apply the updates. However, I encountered difficulties as it seemed to cause problems with eslint. Despite trying to reproduce the error for further inve ...

"Using the power of jQuery to efficiently bind events to elements through associative

I am attempting to link the same action to 3 checkboxes, with a different outcome for each: var checkboxes = { 'option1' : 'result1', 'option2' : 'result2', 'option3' : 'result3', }; ...

Explanation on subtracting the row value from a textbox

Click here to view the image For instance: If I enter a value in the "USED(kl)" textbox, it should subtract that value from the corresponding row where "KILOGRAM/S" is located; Upon clicking the update button, only the specific row should be affected wh ...