"""
演示JSON数据和Python字典地相互转换
"""
import json
from pyecharts.options import TitleOpts, LegendOpts, TooltipOpts, VisualMapOpts
# 准备列表,列表内每一个元素都是字典,将其转换为JSON
data=[{"name":"张三","age":11},{"name":"李四","age":12},{"name":"王二","age":13}]
json_str=json.dumps(data,ensure_ascii=False)
print(json_str)
print(type(json_str))
# 准备字典,将字典转换为JSON
d={"name":"周杰伦","addr":"台北"}
json_str=json.dumps(d,ensure_ascii=False)
print(json_str)
print(type(json_str))
# 将JSON字符串转换为Python数据类型[{k:v,k:v},{k:v,k:v}]
s='[{"name":"张三","age":11},{"name":"李四","age":12},{"name":"王二","age":13}]'
l=json.loads(s)
print(l)
print(type(l))
# 将JSON字符串转换为Python数据类型{k:v,k:v}
s='{"name":"周杰伦","addr":"台北"}'
d=json.loads(s)
print(d)
print(type(d))
"""
演示pyecharts的基础入门
"""
# 导包
from pyecharts.charts import Line
# 创建一个折线图对象
Line=Line()
# 给折线图对象添加X轴的数据
Line.add_xaxis(["中国","美国","英国"])
# 给折线图对象添加Y轴数据
Line.add_yaxis("GDP",[30,20,10])
# 设置全局配置
Line.set_global_opts(
title_opts=TitleOpts(title="GDP展示",pos_left="center",pos_bottom="1%"),
legend_opts=LegendOpts(is_show=True),
tooltip_opts=TooltipOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True)
)
# 通过render方法,将代码生成图像
Line.render()
"""
演示可视化需求1:折线图开发
"""
import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, LabelOpts
# 处理数据
f_us=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\折线图数据\美国.txt","r",encoding="utf-8")
us_data=f_us.read() # 得到了美国的全部内容
f_jp=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\折线图数据\日本.txt","r",encoding="utf-8")
jp_data=f_jp.read() # 得到了日本的全部内容
f_in=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\折线图数据\印度.txt","r",encoding="utf-8")
in_data=f_in.read() # 得到了印度的全部内容
# 去掉不合json规范的开头
us_data=us_data.replace("jsonp_1629344292311_69436(","")
jp_data=jp_data.replace("jsonp_1629350871167_29498(","")
in_data=in_data.replace("jsonp_1629350745930_63180(","")
# 去掉不合json规范的结尾
us_data=us_data[:-2]
jp_data=jp_data[:-2]
in_data=in_data[:-2]
# JSON转python字典
us_dict=json.loads(us_data)
jp_dict=json.loads(jp_data)
in_dict=json.loads(in_data)
# 获取trend key
us_trend_data=us_dict["data"][0]["trend"]
jp_trend_data=jp_dict["data"][0]["trend"]
in_trend_data=in_dict["data"][0]["trend"]
# 获取日期数据,用于X轴,取2020年(到314下标结束)
us_x_data=us_trend_data["updateDate"][:314]
jp_x_data=jp_trend_data["updateDate"][:314]
in_x_data=in_trend_data["updateDate"][:314]
# 获取确认数据,用于Y轴 取2020年(到314下标结束)
us_y_data=us_trend_data["list"][0]["data"][:314]
jp_y_data=jp_trend_data["list"][0]["data"][:314]
in_y_data=in_trend_data["list"][0]["data"][:314]
# 生成图表
Line=Line() # 构建折线图对象
# 添加X轴数据
Line.add_xaxis(us_x_data) # X轴是公用的,添加一份数据即可
# 添加Y轴数据
Line.add_yaxis("美国确诊人数",us_y_data,label_opts=LabelOpts(is_show=False))
Line.add_yaxis("日本确诊人数",jp_y_data,label_opts=LabelOpts(is_show=False))
Line.add_yaxis("印度确诊人数",in_y_data,label_opts=LabelOpts(is_show=False))
# 设置全局选项
Line.set_global_opts(
# 设置标题
title_opts=TitleOpts(title="2020年美日印三国确诊人数对比折线图",pos_left="center",pos_bottom="1%")
)
# 调用render方法生成图表
Line.render()
# 关闭文件对象
f_us.close()
f_jp.close()
f_in.close()
"""
演示地图可视化的基本使用
"""
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
# 准备地图对象
map=Map()
# 准备数据
data=[
("北京",99),
("上海",199),
("湖南",299),
("台湾",399),
("广东",499)
]
# 添加数据
map.add("测试地图",data,"china")
# 设置全局选项
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"mini":1,"max":9,"label":"1-9","color":"#CCFFFF"},
{"mini":10,"max":99,"label":"1-9","color":"#FF6666"},
{"mini":100,"max":500,"label":"1-9","color":"#990033"}
]
)
)
# 绘图
map.render()
"""
演示全国疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取数据文件
f=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\地图数据\疫情.txt","r",encoding="utf-8")
data=f.read() # 全部数据
# 关闭文件
f.close()
# 取到各省数据
# 将字符串json转化为python的字典
data_dict=json.loads(data) # 基础数据字典
# 从字典中取出省份的数据
province_data_list=data_dict["areaTree"][0]["children"]
# 组装每个省份和确诊人数为元组,并各个省的数据都封装入列表内
data_list=[]
for province_data in province_data_list:
province_name=province_data["name"] # 省份名称
province_confirm=province_data["total"]["confirm"] # 确诊人数
(province_name,province_confirm)
data_list.append((province_name,province_confirm))
# 创建地图对象
map=Map()
# 添加数据
map.add("跟省份确诊人数",data_list,"china")
# 设置全局配置,定制分段的视觉映射
map.set_global_opts(
title_opts=TitleOpts(title="全国疫情地图"),
visualmap_opts=VisualMapOpts(
is_show=True, # 是否显示
is_piecewise=True, # 是否分段
pieces=[
{"min":1, "max":99, "label":"1~99","color":"#CCFFFF"},
{"min":100, "max":999, "label":"100~9999","color":"#FFFF99"},
{"min":1000, "max":4999, "label":"1000~4999","color":"#FF9966"},
{"min":5000, "max":9999, "label":"5000~9999","color":"#FF6666"},
{"min":10000, "max":99999, "label":"10000~99999","color":"#CC3333"},
{"min":100000, "label":"100000+","color":"#990033"},
]
)
)
# 绘图
map.render("全国疫情地图.html")
"""
演示河南省疫情地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取文件
f=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\地图数据\疫情.txt","r",encoding="utf-8")
data=f.read()
# 关闭文件
f.close()
# 获取河南省数据
# json数据转换为python字典
data_dict=json.loads(data)
# 取到河南省数据
cities_data=data_dict["areaTree"][0]["children"][3]["children"]
# 准备数据为元组并放入list
data_list=[]
for city_data in cities_data:
city_name=city_data["name"]+"市"
city_confirm=city_data["total"]["confirm"]
data_list.append((city_name,city_confirm))
# 手动添加济源市
data_list.append(("济源市",5))
# 构建地图
map=Map()
map.add("河南省疫情分布",data_list,"河南")
# 设置全局选项
map.set_global_opts(
title_opts=TitleOpts(title="河南省疫情地图"),
visualmap_opts=VisualMapOpts(
is_show=True, # 是否显示
is_piecewise=True, # 是否分段
pieces=[
{"min":1, "max":99, "label":"1~99","color":"#CCFFFF"},
{"min":100, "max":999, "label":"100~9999","color":"#FFFF99"},
{"min":1000, "max":4999, "label":"1000~4999","color":"#FF9966"},
{"min":5000, "max":9999, "label":"5000~9999","color":"#FF6666"},
{"min":10000, "max":99999, "label":"10000~99999","color":"#CC3333"},
{"min":100000, "label":"100000+","color":"#990033"},
]
)
)
# 绘图
map.render("河南省疫情地图.html")
"""
基础柱状图开发
"""
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
# 使用Bar构建基础柱状图
bar=Bar()
# 添加X轴数据
bar.add_xaxis(["中国","美国","英国"])
# 添加Y轴数据
# 设置数值标签在右侧
bar.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
# 反转X轴和Y轴
bar.reversal_axis()
# 绘图
bar.render("基础柱状图.html")
"""
演示带有时间线的柱状图开发
"""
from pyecharts.charts import Bar,Timeline
from pyecharts.options import LabelOpts
from pyecharts.globals import ThemeType
bar1=Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis("GDP",[30,30,20],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2=Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,50,50],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
bar3=Bar()
bar3.add_xaxis(["中国","美国","英国"])
bar3.add_yaxis("GDP",[70,60,60],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()
# 构建时间线对象
timeline=Timeline({"theme": ThemeType.LIGHT})
# 在时间线内添加柱状图对象
timeline.add(bar1,"点11")
timeline.add(bar2,"点2")
timeline.add(bar3,"点3")
# 自动播放设置
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True
)
# 主题设置
# 绘图使用时间线对象绘图,而不是bar对象
timeline.render("基础时间线柱状图.html")
"""
扩展列表sort方法
在学习了将函数作为参数传递后,我们可以学习列表的sort方法来对列表进行自定义排序
"""
# 准备列表
my_list=[["a",33],["b",55],["c",11]]
# 排序,基于带命函数
def choose_sort_key(element):
return element[1]
my_list.sort(key=choose_sort_key,reverse=True)
print(my_list)
# 排序,基于lambda匿名函数
my_list.sort(key=lambda element: element[1],reverse=True)
print(my_list)
"""
演示GDP动态柱状图开发
"""
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 读取数据
f=open(r"C:\Users\qpx\Desktop\资料\可视化案例数据\动态柱状图数据\1960-2019全球GDP数据.csv","r",encoding="GB2312")
data_lines=f.readlines()
# 关闭文件
f.close()
# 删除第一条数据
data_lines.pop(0)
# 将数据转化为字典存储,格式为:
# {年份:[[国家,gdp],[国家,gdp],......],年份:[[国家,gdp],[国家,gdp],........]}
data_dict={}
for line in data_lines:
year=int(line.split(",")[0]) # 年份
country=line.split(",")[1] # 国家
gdp=float(line.split(",")[2]) # gdp数据
# 如何判断字典里有没有指定的key呢?
try:
data_dict[year].append([country,gdp])
except KeyError:
data_dict[year]=[]
data_dict[year].append([country,gdp])
# 创建时间线对象
timeline=Timeline({"theme": ThemeType.LIGHT})
# 排序年份
sorted_year_list=sorted(data_dict.keys())
for year in sorted_year_list:
data_dict[year].sort(key=lambda element:element[1],reverse=True)
# 取出本年份前8名的国家
year_data=data_dict[year][0:8]
x_data=[]
y_data=[]
for country_gdp in year_data:
x_data.append(country_gdp[0]) # x轴添加国家
y_data.append(country_gdp[1]/100000000) # y轴添加gdp数据
# 构建柱状图对象
bar=Bar()
x_data.reverse()
y_data.reverse()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(亿)",y_data,label_opts=LabelOpts(position="right"))
# 反转x轴和y轴
bar.reversal_axis()
# 设置每一年的图表的标题
bar.set_global_opts(title_opts=TitleOpts(title=f"{year}年全球前8GDP数据"))
timeline.add(bar,str(year))
# for循环每一年的数据,基于每一年的数据,创建每一年的bar对象
# 在for循环中,将每一年的bar对象添加到时间线中
# 设置时间线自动播放
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
# 绘图
timeline.render("1960-2019全球GDP前8国家.html")
jsonschema生成java
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
在Java中使用二维数组生成迷宫
在Java中使用二维数组生成迷宫
深度优先搜索 Java 二维数组 -
接了个变态需求:生成 Excel + PDF 导出,用 Java 怎么实现?
接了个变态需求:生成 Excel + PDF 导出,用 Java 怎么实现?
List 表单 实体类 pdf springboot