echarts实现湖南省地图并且定时轮询

1、在HTML页面引入echarts.min.js

javascript 复制代码
<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>

2、实现代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>湖南省地图</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
    
    <style>
        #main {
            width: 100%;
            height: 600px;
			background-color: aqua;
        }
    </style>
</head>
<body>
    <div id="main"></div>
    <script>
        const xhr = new XMLHttpRequest();
        xhr.open('GET', 'hunan.json', true);
        xhr.onload = function () {
            if (xhr.status === 200) {
                echarts.registerMap('hunan', JSON.parse(xhr.responseText));
                
                const data = [
                    { name: '长沙市', value: 120 },
                    { name: '株洲市', value: 98 },
                    { name: '湘潭市', value: 76 },
                    { name: '衡阳市', value: 54 },
                    { name: '岳阳市', value: 45 },
                    { name: '常德市', value: 43 },
                    { name: '张家界市', value: 38 },
                    { name: '益阳市', value: 35 },
                    { name: '娄底市', value: 29 },
                    { name: '郴州市', value: 25 },
                    { name: '永州市', value: 20 },
                    { name: '怀化市', value: 18 },
                    { name: '邵阳市', value: 15 },
                    { name: '湘西土家族苗族自治州', value: 6 },
                ];

                const myChart = echarts.init(document.getElementById('main'));

                const option = {
                    title: {
                        text: '湖南省各市数据情况',
                        subtext: '数据来源于XXX',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{b}: {c}',
						 axisPointer: {
						            type: 'line' // 可选:使用线状指示器
						        }
                    },
                    visualMap: {
                        min: 0,
                        max: 150,
                        left: 'left',
                        top: 'bottom',
                        text: ['高', '低'],
                        calculable: true,
                        inRange: {
                            color: ['#d94e5d', '#eac736', '#50a3ba']
                        }
                    },
                    series: [
                        {
                            name: '数据量',
                            type: 'map',
                            roam: false,
                            map: 'hunan',
                            label: {
                                show: true,
                                fontSize: 10,
                                position: 'inside',
                                offset: [0, 5],
                                color: '#fff',
                                emphasis: {
                                    show: true
                                }
                            },
                            itemStyle: {
                                areaColor: '#323c48',
                                borderColor: '#fff'
                            },
                            data: data
                        }
                    ]
                };

                myChart.setOption(option);

                // 封装的轮询函数
         function startCityPolling() {
    let currentIndex = 0;

    setInterval(() => {
        // 清除之前高亮并重置所有市的颜色
        option.series[0].data.forEach((item) => {
            item.itemStyle = {
                areaColor: '#323c48', // 默认颜色
                borderColor: '#fff'
            };
        });

        // 高亮当前市并修改颜色
        const currentItem = option.series[0].data[currentIndex];
        currentItem.itemStyle = {
            areaColor: '#ff6347', // 高亮颜色
            borderColor: '#fff'
        };

        // 更新图表配置并刷新显示
        myChart.setOption(option);

        // 确保调用 dispatchAction 时,正确的 seriesIndex 和 dataIndex
        myChart.dispatchAction({
            type: 'showTip',
            seriesIndex: 0, // 确保这是你的系列的索引
            dataIndex: currentIndex // 当前高亮城市的索引
        });

        // 更新当前索引
        currentIndex = (currentIndex + 1) % option.series[0].data.length; // 使用 option 中的长度保证正常循环
    }, 2000); // 每2秒切换一次
}
                // 启动轮询
                startCityPolling();
            } else {
                console.error('无法加载湖南地图数据');
            }
        };
        xhr.send();
    </script>
</body>
</html>

3、实现效果

其它省份修改对应的json文件即可

相关推荐
半点寒12W1 小时前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端2 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~2 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程2 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏2 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083163 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头4 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
wen's4 小时前
React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
javascript·react native·react.js
一只叫煤球的猫4 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
vvilkim4 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron