Prevent legend strike-through on click in Vue Chart.js

Recently, I made the transition from vue 2 to vue 3 on my website and part of that process involved updating vue-chartjs and chartjs too.

However, after modifying the legend text of my pie chart using the generateLabels option (as seen below), the strikethrough feature stopped working when clicking on a legend to hide a segment.

plugins: {
  legend: {
    labels: {
      generateLabels: chart => {
        const data = chart.data;

        if (data.labels.length && data.datasets.length) {
          return data.labels.map((label, i) => {
            const meta = chart.getDatasetMeta(0);
            const style = meta.controller.getStyle(i);

            return {
              text: `${label}: ${this.isMoney ? StringHelper.FormatNumber(data.datasets[0].data[i], true) : data.datasets[0].data[i]}`,
              fillStyle: style.backgroundColor,
              strokeStyle: style.borderColor,
              lineWidth: style.borderWidth,
              hidden: isNaN(data.datasets[0].data[i]) || !meta.hidden,
              index: i,
            };
          });
        }

        return [];
      },
      padding: 30,
      usePointStyle: true,
    },
    position: 'left',
  },
}

View Stackblitz Example

How can I restore the strikethrough functionality? I attempted to implement the onclick solution mentioned in this answer, but it ended up breaking the pie chart entirely.

It seems that the issue lies with the hidden property now being undefined in meta.data[i].hidden instead of available as before.

Click Here for Updated Stackblitz

Answer â„–1

To determine if a segment is hidden, you can now look at the chart object and check the _hiddenIndices property:

hidden: chart._hiddenIndices[i] === true,

See it in action here

An alternative method suggested by yoduh involves using getDataVisibility:

hidden: !chart.getDataVisibility(i),

Try this approach instead

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

AngularJS is not properly clearing the datepicker

Upon submitting the form, I have managed to reset all input fields to blank except for the datepicker which still retains the previously selected date. How can I clear that as well? Snippet of HTML: <div> <form name="profileform" role="fo ...

"Delivering dynamic HTML content using React/Redux variables in the production environment

I am currently in the process of developing a React web application, and I have encountered an issue where a variable intended to store a schedule is being filled with the HTML content of the page when I build and serve the application. Interestingly, the ...

Steps to keep only one submenu open at a time and close others

Struggling to figure out how to open one submenu at a time, and I can't seem to locate the issue! I've searched around for solutions but haven't found anything that works for me. The submenu also needs to remain closed when the page loads. - ...

Making HTTP requests with axios in Node.js based on multiple conditions

I'm facing an issue with making get calls using axios to both activeURl and inactiveURl. The goal is to handle error messages from the activeUrl call by checking data from the inactiveUrl. However, I keep receiving error messages for the inactiveURL e ...

Tips for calculating the total of a virtual column through child associations in Sequelize

I have a network of 3 interconnected objects (A, B, and C). There can be multiple Bs associated with A, and multiple Cs associated with each B. For instance: A └───B │ └───C │ └───C └───B └───C Object ...

Navigating through nested objects in JSON when working with D3: a guide

Currently, I am in the process of learning D3 using various tutorials. Here is the code snippet I have been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title ...

Is there a way I can turn off the dragging functionality in my situation?

Is there a method to disable the dragging functionality within a draggable element? $('#dragitem').draggable({ scroll : false, drag: function(event, ui){ //check if condition is met if(†...

Setting up multiple environments in CypressJs can be easily achieved by following these steps

I've been struggling to correctly set up my CypressJS environments for testing. Any assistance would be greatly appreciated. Within my index.html file, I have a CONFIG object in the <script> section. In production, this object is added by the M ...

How to use jQuery to retrieve the style of an element based on a specific data attribute

I've been using bxSlider and I decided to create a unique custom pager using the pagerCustom option. My goal was to make the pager resemble a thumbnail pager, so I attempted to copy the style of each slide and attach it to the corresponding pager. For ...

Removing duplicate elements from an array using lodash

What is the best way to remove duplicate values from an array? var numbers = [1, 1, 5, 5, 4, 9]; I want my result to be: var numbers = [4, 9]; Is there a method in lodash that can help me achieve this? ...

Is there a way for me to automatically generate and send a random collection each time a GET request is made on my Node.js

I have been developing a typing application that utilizes an API to fetch random quotes from . I successfully created an API using MongoDB, Node.js, and Express which functions well. However, I encountered an issue when trying to send a random collection ...

The 'property' is not found within the type '{ my_function(): void; }'

I am just starting out with TypeScript and VueJS. Currently, I am pondering the best approach for setting the type of a JSON key that should start off as null. <script lang="ts"> import Vue from 'vue'; export default Vue. ...

What is the reason behind the varying display of values?

When trying to set a value using the input tag, I encountered an issue. For example, if I type 1000.0009 into the input text, the valid value should be 1000.0001. However, the value displayed in the input tag is incorrect, while the value outside the tag i ...

invoking a JavaScript function with onClick

Every time I try deploying my code, an error is thrown saying: saveRows is not a function. Can anyone help me figure out what's going on? dataGrid.prototype = { display: function() { var self = this; var html = []; va ...

TinyMCE - Utilizing selection.setContent along with getContent for the Warp Button

I am looking to implement a button that will wrap content with all tags. Snippet of Code: editor.addButton('MobileToggleArea', { text: '<M>', icon: false, onclick: function (){ editor.selection. ...

What is the best way to send data from a header component to a separate container component?

Utilizing React-router-dom, I am able to seamlessly switch between components in the following setup: <Router> <div> <Header /> <NavigationBar /> <Switch> <Route exact path ...

Menu interface in webpack cli for configuring settings

When setting up with vue-cli, I noticed something similar to this in the meta.js file. user@ny-laptop ~/Documents/ > $ vue init webpack my-project ? Project name ...

What are the steps to troubleshoot an unexpected 302 redirect caused by an Ajax request?

I'm attempting to retrieve data from a database using this ajax request: axios.get('/about-info') web.php: Route::get('/about-info', [CMSController::class, 'aboutInfo']); CMSController.php: public function aboutInfo() { ...

Ways to disable .animate() specifically for a particular element

I have implemented a countdown using which smoothly animates the numbers down and fades to opacity 0. Update. I have also created a demo on jsFiddle here: http://jsfiddle.net/Mw4j2/ // The .static class is added when the animation // complet ...

Leveraging Vue Js directives within a component using vue-loader

I am currently facing an issue with vue-loader. I have created a directive in the main app.js to use in a component, but I am unable to implement that directive within the component. The error message I receive is: vue.common.js?e881:1014 [Vue warn]: Fai ...