類似百度統計,有的時候legend的個數比較多,但是前端需要控制初始化顯示的個數,以及最多顯示的條數,先看效果圖:

先給代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.2.1/echarts-en.min.js"></script>
</head>
<body>
<div id="all_line" style="width:100%;height:500px;margin-top: 100px;"></div>
<script>
var obj = {};
var text_data = ['http://www.baidu.com', 'https://www.sogou.com', 'http://www.333.com',
'http://www.444.com', 'http://www.555.com', 'http://www.666.com', 'http://www.777.com',
'http://www.888.com', 'http://www.999.com', '外部鏈接總計'
];
for (var i = 0; i < text_data.length; i++) {
if (i > 2 && (i < text_data.length - 1)) {
obj[text_data[i]] = false;
} else {
obj[text_data[i]] = true;
}
}
obj[text_data[text_data.length - 1]] = true;
// console.log(obj)
var myChart = echarts.init(document.getElementById('all_line'));
myChart ? myChart.clear() : "";
myChart.setOption({
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(255,255,255,0.8)',
axisPointer: { // 坐標軸指示器,坐標軸觸發有效
type: 'shadow', // 默認為直線,可選為:'line' | 'shadow'
shadowStyle: {
color: 'rgba(150,150,150,0.2)'
}
},
textStyle: {
color: '#666',
fontSize: '14px',
},
extraCssText: 'width:220px;height:200px;padding:0;box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);"',
formatter: function (params) {
var htmlStr = "";
for (var i = 0; i < params.length; i++) {
var tem = params[i].name;
var val = params[i].value;
if (tem.indexOf('/') < 0) {
tem > 9 ? tem = tem + ":00 - " + tem + ":59" : tem = "0" + tem + ":00 - " +
"0" + tem + ":59";
}
val > 0 ? val = val : val = '--'
htmlStr +=
'<div style="height:26px;line-height:26px;overflow:hidden;padding:6px 8px;">' +
'<span style="float:left;max-width:160px;overflow:hidden;text-overflow: ellipsis;white-space: nowrap;">' +
'<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' +
params[i].color + ';"></span>' + params[i].seriesName + '</span>' +
'<span style="float:right">' + val + '</span>' + '</div>'
}
var res =
'<div><div style="height:40px;line-height:40px;padding:0 8px;background:rgba(237,233,233,0.4)">' +
tem + '<span style="float:right;">' + $('.table-echarts ul.group li.cur').text() +
'</span>' + htmlStr + '</div></div>'
return res;
},
},
textStyle: {
color: 'rgb(120, 122, 125)',
},
color: ['#4fa8f9', '#6ec71e', '#f56e6a', '#fc8b40', '#818af8', '#31c9d7', '#f35e7a', '#ab7aee',
'#14d68b', '#cde5ff'
],
grid: {
top: 30, //越大越靠下,默認60
bottom: 70
},
legend: {
bottom: 1,
itemGap: 15,
itemWidth: 9,
itemHeight: 10,
textStyle: {
padding: [0, 0, 0, 8],
},
formatter: function (name) {
return (name.length > 14 ? (name.slice(0, 14) + "...") : name);
},
selected: obj,
data: text_data
},
xAxis: [{
type: 'category',
// boundaryGap: false,//坐標軸兩邊留白策略
axisTick: {
alignWithLabel: true
},
axisLabel: {
interval: 2
},
axisLine: {
lineStyle: {
color: "#ddd"
}
},
data: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14',
'15', '16', '17', '18', '19', '20', '21', '22', '23'
],
}],
yAxis: [{
type: 'value',
axisLine: {
lineStyle: {
color: "transparent"
}
},
splitLine: {
lineStyle: {
color: '#eee',
}
}
}],
series: [{
name: 'http://www.baidu.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [8, 1, 1, 0, 0, 3, 2, 0, 6, 3, 1, 4, 7, 3, 7, 15, 11, 3, 8, 3, 2, 5, 4, 7]
},
{
name: 'https://www.sogou.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [1, 1, 0, 0, 2, 1, 0, 1, 3, 1, 0, 0, 1, 0, 1, 1, 1, 0, 2, 2, 0, 4, 2, 4]
},
{
name: 'http://www.333.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [1, 3, 0, 0, 0, 1, 1, 0, 0, 1, 2, 1, 3, 1, 1, 1, 1, 0, 2, 0, 2, 1, 1, 1]
},
{
name: 'http://www.444.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 2, 3, 0, 0, 0, 3, 1, 0, 2, 0, 1, 0, 0]
},
{
name: 'http://www.555.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 2, 0, 1, 1, 2, 0]
},
{
name: 'http://www.666.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 1, 0, 0]
},
{
name: 'http://www.777.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1]
},
{
name: 'http://www.888.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
},
{
name: 'http://www.999.com',
type: 'line',
symbol: 'circle',
symbolSize: '5',
data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
},
{
name: '外部鏈接總計',
type: 'bar',
barWidth: '60%',
data: [14, 6, 1, 0, 2, 5, 4, 1, 11, 8, 6, 7, 15, 5, 11, 21, 21, 6, 17, 7, 7, 14, 9, 14]
}
]
})
//echarts點擊事件
myChart.on('legendselectchanged', function (params) {
var legend_option = this.getOption();
//手動改變legend的selected狀態:
// legend_option.legend[0].selected[name] = false;
//切換tooltips的高度
var selectTotal = 0;
var tem = Object.values(params.selected);
for (var j = 0; j < tem.length; j++) {
if (tem[j] === true) {
selectTotal += 1;
}
}
// console.log(selectTotal)
legend_option.tooltip[0].extraCssText ="width:220px;padding:0;box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);height:" + (40 + selectTotal *40) + "px";
this.setOption(legend_option)
});
window.onresize = function () {
myChart ? myChart.resize() : "";
};
</script>
</body>
</html>
直接copy就能看到效果。上面是限制了初始化顯示legend的前三個和最后一個。因為之前的版本出了點問題刪了部分代碼,如果要改變legend的select狀態,請自行在代碼中的echarts點擊事件中根據我提供的方法進行修改,如若不明白可以打印點擊事件的params查看。
Tips:
- 以上的數據都是虛擬的,只是為了方便查看效果。
- 為了不影響變量的作用域,建議把js放在jquery的ready函數里面。
- 如果是vue項目,可自行調整以上代碼或留言獲取。
我再多說幾句:
在用數據刷新echarts圖表時,最好先用clear()函數去清空(echarts自帶的api),還有就是其實echarts完全可以由數據渲染,比如說上面的例子,它的顏色可以事先定義一個數組,把可能用到的顏色放入這個數組,然后通過你legend的個數從數組去length相同的值組成新數組,然后以變量的形式去配置。上面例子用自定義的formatter去修改了tooltip的顏色以及legend的樣式。
