Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

Jquery, chart.js onClick not working properly with many datas

im trying to change the default click on the legend. this is my code:

legend: {
                    onClick: function (e, legendItem) {
                        var index = legendItem.datasetIndex;
                        var ci = this.chart;
                        var alreadyHidden = (ci.getDatasetMeta(index).hidden === null) ? false : ci.getDatasetMeta(index).hidden;
                        var selectedMac;
                        var selectedLabel = ci.data.datasets[index].label;
                        if (selectedLabel.length > 3) {
                            var selectedSplit = selectedLabel.split(' ');
                            selectedMac = selectedSplit[1];
                        } else {
                            selectedMac = selectedLabel;
                        }
                        
                        ci.data.datasets.forEach(function (e, i) {

                            var currentMac;
                            var currentLabel = ci.data.datasets[i].label;
                            var meta = ci.getDatasetMeta(i);

                            if (currentLabel.length > 3) {
                                var currentSplit = currentLabel.split(' ');
                                currentMac = currentSplit[1];
                            } else {
                                currentMac = currentLabel;
                            }

                            if (currentMac == selectedMac) {
                                // -- non esiste la macchina all interno di questo oggetto
                            } else {
                                

                                if (i !== index) {
                                    if (!alreadyHidden) {
                                        meta.hidden = meta.hidden === null ? !meta.hidden : null;
                                    } else if (meta.hidden === null) {
                                        meta.hidden = true;
                                    }
                                } else if (i === index) {
                                    meta.hidden = null;
                                }
                            }


                            
                        });

                        ci.update();
                    },
                    labels: {
                        fontColor: 'black'
                    }
                }

This is my chart: chart

for exemple, i want filter only CDL, so i click it and it works: chart_after_click

If i press CDL again it back at "original state" and it's ok. If i press another while CDL is already clicked, the chart not works and i cant click the legend like this: enter image description here

Can someone help me?

question from:https://stackoverflow.com/questions/65903681/jquery-chart-js-onclick-not-working-properly-with-many-datas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...