利用pyecharts实现地图下钻

参考地址:利用pyecharts实现中国省与市之间的跳转_pyecharts点击地图跳转-CSDN博客

代码:

import csv
import jionlp as jio
from pyecharts import options as opts
from pyecharts.charts import Map
from collections import Counter
import random

from pyecharts.globals import ThemeType

place = []
province = []
city = []
county = []
filename = "E:\\数据\\allnew.csv"
with open(filename, 'r', encoding='ansi') as file:
    df = csv.reader(file)
    data = list(df)
for row in data:
    if row[4] != '地址':
        place.append(row[4])
print(place)
area = ''
for position in place:
    area = position
    df_str = jio.parse_location(area)  # jionlp分词 河北省石家庄市分为河北省 石家庄市
    province.append(df_str['province'])
    city.append(df_str['city'])
    county.append(df_str['county'])

province_num = Counter(province)  # 计算各个省出现的次数
city_num = Counter(city)
county_num = Counter(county)
pro = province_num.items()  # 例如河北省出现两次,转化为dict_items([('河北省',2)])
cis = city_num.items()
cos = county_num.items()
print(pro)
plist = []
cilist = []
colist = []
for data in pro:
    plist.append(data)  # 将其转化为list的形式    [('河北省',2)]
for data in cis:
    cilist.append(data)
for data in cos:
    colist.append(data)
print(plist)
print(cilist)
map_chart = Map()
map_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="各省项目数"),
    visualmap_opts=opts.VisualMapOpts(max_=100),
)

map_chart.add("各省项目数", plist)
map_chart.set_series_opts(
    label_opts=opts.LabelOpts(is_show=False),
    emphasis_label_opts=opts.LabelOpts(is_show=False),
)

map_chart.render(path='各省项目数.html')
# 对应的省市地图的生成
for i in city:
    province_city = (
        Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE))
        .add("",
             cilist,
             i)
        .set_global_opts(
            title_opts=opts.TitleOpts(title=i + "地图"),
            visualmap_opts=opts.VisualMapOpts(
                min_=1,
                max_=17,
                is_piecewise=True
            )
        )
        .render(path=i + "地图.html")
    )
'''
for i in county:
    province_county = (
        Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE))
        .add("",
             colist,  # 可以自己写一个字典写各个省份下的市所对应的数据,在这里只是一个range()
             i)
        .set_global_opts(
            title_opts=opts.TitleOpts(title=i + "地图"),
            visualmap_opts=opts.VisualMapOpts(
                min_=1,
                max_=17,
                is_piecewise=True
            )
        )
        .render(path=i + "地图.html")
    )
'''

在生成的各省项目数.html文件<script></script>的最后一句上加上跳转连接  chart_66e2afdd6d494dde8b4a2b09d1720668  要换成上一句

chart_66e2afdd6d494dde8b4a2b09d1720668.on('click', function (param){
            var selected = param.name;
                if (selected) {
                    switch(selected){
                        case '北京市':
                            location.href = "./北京市地图.html";
                            break;
                        case '天津市':
                            location.href = "./天津市地图.html";
                            break;
                        case '四川省':
                            location.href = "./四川省地图.html";
                            break;
                        case '河北省':
                            location.href = "./河北省地图.html";
                            break;
                        case '浙江省':
                            location.href = "./浙江省地图.html";
                            break;
                        case '湖北省':
                            location.href = "./湖北省地图.html";
                            break;
                        case '湖南省':
                            location.href = "./湖南省地图.html";
                            break;
                        case '广东省':
                            location.href = "./广东省地图.html";
                            break;
                        case '吉林省':
                            location.href = "./吉林省地图.html";
                            break;
                        case '陕西省':
                            location.href = "./陕西省地图.html";
                            break;
                        case '青海省':
                            location.href = "./青海省地图.html";
                            break;
                        default:
                            break;
                    }

            }
      });

 问题:上面的python代码由于是使用jionlp进行省市县的分词的,因此分出来会是河北省,但是在建立省份地图的时候是要找河北,因此会出现生成的HTML文件运行出来的是一片空白

 

如果是自治州、自治区就不用修改,只用修改省市区县的

posted @ 2024-04-06 19:17  辞楠  阅读(21)  评论(0编辑  收藏  举报