当前位置:网站首页>Echart折线图:当多条折线存在相同name,图例仍全部显示
Echart折线图:当多条折线存在相同name,图例仍全部显示
2022-06-13 06:04:00 【HaanLen】
默认情况
option = {
title: {
text: 'Stacked Line'
},
tooltip: {
trigger: 'axis'
},
legend: {
show: true
// data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {
}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Email',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Union Ads',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ads',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Direct',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
当折线存在相同名称时,图例相同的只会显示一个
option = {
title: {
text: 'Stacked Line'
},
tooltip: {
trigger: 'axis'
},
legend: {
show: true
// data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {
}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Email',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Email',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ads',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Direct',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
当折线图存在相同名称时,也展示全部图例。
解决逻辑:
series系列中的name设置唯一值;legend通过formatter,利用唯一id获取相应的名称来展示;浮窗通过data中的value数组的设置的name来显示。
主要利用legend的formatter方法
浮窗的内容也需要修改
option = {
title: {
text: 'Stacked Line'
},
tooltip: {
trigger: 'axis',
formatter: (params) => {
let arr = [
{
name: '3333',
type: 'line',
text: 'Videot',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Videot',
type: 'line',
text: 'Videot',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Videot1',
type: 'line',
text: 'Videot',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Search Engine',
type: 'line',
text: 'Search Engine',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine1',
type: 'line',
text: 'Search Engine',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
];
let middle = ``;
for (let i = 0; i < params.length; i++) {
let name = arr.find((v) => v.name === params[i].seriesName).text;
middle += `<div> <span> ${
name}</span>: <span>${
params[i].value}</span> </div>`;
}
let content = ` <div> ${
middle} </div>`;
return content;
}
},
legend: {
show: true,
// data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
formatter: (name) => {
let arr = [
{
name: '3333',
type: 'line',
text: 'Videot',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Videot',
type: 'line',
text: 'Videot',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Videot1',
type: 'line',
text: 'Videot',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Search Engine',
type: 'line',
text: 'Search Engine',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine1',
type: 'line',
text: 'Search Engine',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
];
let showText = arr.find((v) => v.name === name).text;
return showText;
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {
}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: '3333',
type: 'line',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Videot',
type: 'line',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Videot1',
type: 'line',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Search Engine',
type: 'line',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine1',
type: 'line',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
边栏推荐
- Tongweb adapts to openrasp
- 2021.9.29 learning log MIME type
- Leetcode- distribute cookies - simple
- Leetcode- find a difference - simple
- Pod libwebp error reporting solution
- Shardingsphere JDBC exception: no table route info
- Leetcode- find all missing numbers in the array - simple
- = = relation between int and integer
- Add attributes in storyboard and Xib (fillet, foreground...) Ibinspectable and ibdesignable
- Complete USB debugging assistant
猜你喜欢
php 分布式事务 原理详解
軟件測試——接口常見問題匯總
Sentinel series hot spot current limiting
The SQL file of mysql8.0 was imported into version 5.5. There was a pit
AUTOSAR实战教程pdf版
Software testing - Summary of common interface problems
自我总结ing
Service fusing and degradation of Note Series
Self summarizing
Function and application scenario of field setaccessible() method
随机推荐
Swift property property
智能数字资产管理助力企业决胜后疫情时代
The SQL file of mysql8.0 was imported into version 5.5. There was a pit
Timeout thread log for tongweb
Leetcode- number of maximum consecutive ones - simple
Leetcode fizz buzz simple
【var const let区别】
Status management --provider
Concurrent programming -- source code analysis of thread pool
Feel the power of shardingsphere JDBC through the demo
Power of leetcode-4 - simple
Audio stereo to mono (Audio Dual Channel to mono channel)
Conf/tongweb Functions of properties
2021.9.29学习日志-MIME类型
Leetcode- minimum number of operations to make array elements equal - simple
OpenGL Mosaic (8)
arrayList && linkedList
OpenGL mosaic (VIII)
SPI primary key generation strategy for shardingsphere JDBC
1+1>2,Share Creators可以帮助您实现