echarts初始化折线图(line)

本文档展示了如何使用ECharts库在HTML中初始化并绘制一个折线图,适用于进行数据可视化的网页开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

完整代码

<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开发路上的AZhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值