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
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