echarts version: 3.1.2
修改图例点击事件样例代码:
- 当第一次点击图例时,只显示点击的图例。
- 当还剩一个图例被
取消选中
后,自动全选中所有图例。
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);
}
}
});