完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#main {
width: 600px;
height: 400px;
background-color: bisque;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="main"></div>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.1/echarts.min.js"></script>
<script>
let data = [
{
ym: '2022-01',
value: 10000
},
{
ym: '2022-02',
value: 20002
},
{
ym: '2022-03',
value: 5000
}
]
initCharts()
function initCharts() {
let myChart = echarts.init(document.getElementById('main'))
let option = {
legend: {
icon: 'roundRect',
bottom: 10,
itemWidth: 19,
itemHeight: 7,
textStyle: {
color: '#909090',
fontSize: 12
}
},
tooltip: {
trigger: 'axis',
padding: 0,
backgroundColor: 'transparent',
borderWidth: 0,
formatter: function (params) {
let html = `
<div style="width: fit-content;
background: linear-gradient(201deg, #EDF8FD 0%, #E2EFFE 32%, #ECEAFE 55%, #F5E9FB 100%);
backdrop-filter: blur(15px);
color:#B7A8FF;
font-size: 10px;padding: 5px;">
<div>
业绩:${params[0].value}
</div>
</div>
`
return html
},
axisPointer: {
lineStyle: {
color: '#fff'
}
}
},
// 修改echarts 布局
grid: {
top: 25,
left: 10,
bottom: 40,
right: 30,
containLabel: true
},
xAxis: [
{
type: 'category',
boundaryGap: false,
axisLabel: {
color: '#7B7C7D'
},
axisLine: {
lineStyle: {
color: '#f4f4f4'
}
},
axisTick: {
show: false
},
data: data.map((n) => n.ym)
}
],
yAxis: [
{
type: 'value',
axisLabel: {
color: '#7B7C7D'
},
splitLine: {
lineStyle: {
color: '#f4f4f4'
}
},
axisLine: {
show: false
},
axisTick: {
show: false
}
}
],
// 生成多 series
series: createSeriesItem(data)
}
option && myChart.setOption(option)
}
// 生成多 series
// 当前例子data只有一个,所以下面的数据都是直接写死的
function createSeriesItem(data) {
let arr = []
data.forEach((item) => {
arr.push({
name: item.name || '',
type: 'line',
smooth: true,
symbol: 'circle',
symbolSize: 5,
showSymbol: false,
lineStyle: {
width: 1,
// 设置渐变颜色
color: new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: '#FFBC8A'
},
{
offset: 0.8,
color: '#FFD8BB'
}
],
false
)
},
areaStyle: {
color: new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: 'rgba(246, 208, 180, 0.8)'
},
{
offset: 0.8,
color: 'rgba(246, 208, 180, 0.2)'
}
],
false
),
shadowColor: 'rgba(0, 0, 0, 0.1)',
shadowBlur: 10
},
itemStyle: {
color: new echarts.graphic.LinearGradient(
0,
0,
1,
0,
[
{
offset: 0,
color: '#FFBC8A'
},
{
offset: 0.8,
color: '#FFD8BB'
}
],
false
),
borderColor: new echarts.graphic.LinearGradient(
0,
0,
1,
0,
[
{
offset: 0,
color: '#FFBC8A'
},
{
offset: 0.8,
color: '#FFD8BB'
}
],
false
),
borderWidth: 1
},
// 注意:多series时这里的data数据需要改动,当前例子中,series只有一个,所以就直接写死了
data: data.map((n) => n.value)
})
})
return arr
}
</script>
</body>
</html>