echartsカスタム凡例のクリックイベント

3513 ワード

凡例を変更してイベントのサンプルコードをクリックします.
  • 最初に凡例をクリックしたときは、クリックの凡例だけが表示されます.
  • 凡例が一つ残っていると選択がキャンセルされ、自動的にすべての凡例が選択されます.
  •   var triggerAction = function(action, selected) {
            legend = [];
    
            for ( name in selected) {
                if (selected.hasOwnProperty(name)) {
                    legend.push({name: name});
                }
            }
    
            myChart.dispatchAction({
                type: action,
                batch: legend
            });
        };
    
        var isFirstUnSelect = function(selected) {
    
            var unSelectedCount = 0;
            for ( name in selected) {
                if (!selected.hasOwnProperty(name)) {
                    continue;
                }
    
                if (selected[name] == false) {
                    ++unSelectedCount;
                }
            }
            return unSelectedCount==1;
        };
    
        var isAllUnSelected = function(selected) {
            var selectedCount = 0;
            for ( name in selected) {
                if (!selected.hasOwnProperty(name)) {
                    continue;
                }
    
                //    selected Object    true    selected, false    unselected
                if (selected[name] == true) {
                    ++selectedCount;
                }
            }
            return selectedCount==0;
        };
    
        myChart.on('legendselectchanged', function(obj) {
            var selected = obj.selected;
            var legend = obj.name;
    
            //    legendToggleSelect Action       legendselectchanged Event,         
            //      selected   
            if (selected != undefined) {
    
                if (isFirstUnSelect(selected)) {
                    triggerAction('legendToggleSelect', selected);
                } else if (isAllUnSelected(selected)) {
                    triggerAction('legendSelect', selected);
    
                }
            }
    
        });