Echarts Legend legend limit displaying at least one Legend

encountered a requirement, that is, how to ensure that the data of at least one legend is displayed in the echarts chart (that is, the last legend cannot become the state of unselected)
the following figure is the picture when it was initially loaded

clipboard.png

clipboard.png
look up some ideas before posting:

  • use radio mode curve to save the nation, but there is no way to see the data of multiple legends displayed in the same chart
  • disable click events for all legends when the legend is the last

is not a good solution. Find a code that you can refer to

.

var option = {
    title: {
        text: ""
    },
    tooltip: {
        trigger: "axis"
    },
    legend: {
        data:["","","","",""]
    },
    grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: "category",
        boundaryGap: false,
        data: ["","","","","","",""]
    },
    yAxis: {
        type: "value"
    },
    series: [
        {
            name:"",
            type:"line",
            stack: "",
            data:[120, 132, 101, 134, 90, 230, 210]
        },
        {
            name:"",
            type:"line",
            stack: "",
            data:[220, 182, 191, 234, 290, 330, 310]
        },
        {
            name:"",
            type:"line",
            stack: "",
            data:[150, 232, 201, 154, 190, 330, 410]
        },
        {
            name:"",
            type:"line",
            stack: "",
            data:[320, 332, 301, 334, 390, 330, 320]
        },
        {
            name:"",
            type:"line",
            stack: "",
            data:[820, 932, 901, 934, 1290, 1330, 1320]
        }
    ]
};
myChart.setOption(option);
myChart.on("legendselectchanged", function (params) {
    let option = this.getOption();
    let select_key = Object.keys(params.selected);
    if (!params.selected[params.name]) {
        select_key.map(res => {
            option.legend[0].selected[res] = !params.selected[res];
        });
    } else {
        select_key.map(res => {
            option.legend[0].selected[res] = false;
        });
        option.legend[0].selected[params.name] = true;
    }
    this.setOption(option)
});

enter the code box of http://echarts.baidu.com/exam. to see the effect. Basically, multiple selections are manually switched to single selections, but multiple selections cannot be selected after single selection, so I would like to ask if there is any way to achieve the requirements, or after obtaining the object of the last legend, Disable only click events for this object

Apr.02,2021

solve it by yourself, attach the solution idea and part of the code below.
idea: when you select the last legend, just change the value of its selected to true. The previously attached reference code is changed to implement the
code as follows:

myChart.on('legendselectchanged', function (params) {
        let option = this.getOption();
        let select_key = Object.keys(params.selected);
        let select_value = Object.values(params.selected);
        console.log('select_value',select_value,'length',select_value.length)
        var n = 0;
        select_value.map(res => {
            if(!res){
                nPP;
            }
        });
        console.log('n',n)
        if( n ==select_value.length){
            option.legend[0].selected[params.name] = true;
        }
                            
        this.setOption(option)
});

perfect solution, reorganize the blog later


http://www.hangzhoufe.com/top.

Menu