import pyecharts
from pyecharts.charts import Bar
from pyecharts.charts import Pie
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.charts import Gauge
from pyecharts.charts import Gauge
# bar=Bar()
# bar.add_xaxis(["衬衫","毛衣","裙子","T学",])
# bar.add_yaxis("A销售",[120,110,30,40,])
# bar.add_yaxis("B销售",[120,110,30,70,])
# bar.add_yaxis("C销售",[120,110,30,60,])
# bar.set_global_opts(title_opts=opts.TitleOpts(title="商场销售情况"))
# bar.render("销售柱状图.html")
# pie=Pie()
# cate=["衬衫","毛衣","裙子","T学",]
# data=[120,110,30,40,]
# datalist=[]
# for i in range(0,len(cate)):
# d=[cate[i],data[i]]
# datalist.append(d)
# print(datalist)
# pie.set_global_opts(title_opts=opts.TitleOpts(title="商场销售情况"))
# pie.add("单位:万台",datalist)
# pie.render("销售柱状图.html")
# line=Line()
# x=["1月","2月","3月","4月","5月","6月"]
# y1=[100,200,300,400,500,600]
# y2=[50,100,200,300,400,100]
#
# line.add_xaxis(xaxis_data=x)
# line.add_yaxis(y_axis=y1,series_name="深圳")
# line.add_yaxis(y_axis=y2,series_name="长沙")
# line.set_global_opts(title_opts=opts.TitleOpts(title="降水量折线图"))
# line.render("销售柱状图.html")
#中国地图
# data = [('湖北', 9074),('浙江', 661),('广东', 632),('河南', 493),('湖南', 463),
# ('安徽', 340),('江西', 333),('重庆', 275),('江苏', 236),('四川', 231),
# ('山东', 230),('北京', 191),('上海', 182),('福建', 159),('陕西', 116),
# ('广西', 111),('云南', 105),('河北', 104),('黑龙江', 95),('辽宁', 69),
# ('海南', 64),('新疆', 21),('内蒙古', 21),('宁夏', 28),('青海', 11),('甘肃', 40),('西藏', 1),
# ('贵州', 38),('山西', 56),('吉林', 23),('台湾', 10),('天津', 48),('香港', 14),('澳门', 8)]
#
# def map_china() -> Map:
# c = (
# Map()
# .add(series_name="确诊病例", data_pair=data, maptype="china",zoom = 1,center=[10,38])
# .set_global_opts(
# title_opts=opts.TitleOpts(title="疫情地图"),
# visualmap_opts=opts.VisualMapOpts(max_=9999,is_piecewise=True,
# pieces=[{"max": 9, "min": 0, "label": "0-9","color":"#FFE4E1"},
# {"max": 99, "min": 10, "label": "10-99","color":"#FF7F50"},
# {"max": 499, "min": 100, "label": "100-499","color":"#F08080"},
# {"max": 999, "min": 500, "label": "500-999","color":"#CD5C5C"},
# {"max": 9999, "min": 1000, "label": ">=1000", "color":"#8B0000"}]
# )
# )
# )
# return c
#
# d_map = map_china()
# # d_map.render_notebook()
# d_map.render('地图.html')
#世界地图代码
# data = [['China', 14489], ['Japan', 20], ['Thailand', 19], ['Singapore', 18], ['Korea', 15],
# ['Australia', 12], ['Germany', 10], ['Malaysia', 8], ['United States', 8], ['Vietnam', 7], ['France', 6],
# ['United Arab Emirates', 5], ['Canada', 4], ['Italy', 2], ['India', 2],
# ['United Kingdom', 2], ['Philippines', 2], ['Russia', 2], ['Sri Lanka', 1], ['Cambodia', 1],
# ['Nepal', 1], ['Sweden', 1], ['Finland', 1], ['Spain', 1]]
#
#
# def map_world() -> Map:
# c = (
# Map()
# .add("确诊病例", data, maptype="world", zoom=1)
# .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
# .set_global_opts(
# title_opts=opts.TitleOpts(title="疫情地图"),
# visualmap_opts=opts.VisualMapOpts(max_=100, is_piecewise=False),
# )
# )
# return c
#
#
# d_map = map_world()
# # d_map.render_notebook()
# d_map.render('地图.html')
# 省级地?图代码
# data = [['昆明市', 31], ['玉溪市', 11], ['楚雄彝族自治州', 2], ['西双版纳傣族自治州', 12], ['普洱市', 4],
# ['昭通市', 8], ['曲靖市', 5], ['迪庆藏族自治州', 0], ['丽江市', 7], ['临沧市', 1], ['保山市', 8],
# ['怒江傈僳族自治州', 0], ['大理白族自治州', 7], ['德宏傣族景颇族自治州', 4], ['红河哈尼族彝族自治州', 5],
# ['文山壮族苗族自治州', 0]]
#
#
# def map_yunnan() -> Map:
# c = (
# Map()
# .add("确诊病例", data, "云南", zoom=1)
# .set_global_opts(
# title_opts=opts.TitleOpts(title="云南疫情地图"),
# visualmap_opts=opts.VisualMapOpts(max_=31, is_piecewise=True,
# pieces=[{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
# {"max": 9, "min": 1, "label": "0-9", "color": "#FFE4E1"},
# {"max": 99, "min": 10, "label": "10-99", "color": "#FF7F50"},
# {"max": 499, "min": 100, "label": "100-499", "color": "#F08080"},
# {"max": 999, "min": 500, "label": "500-999", "color": "#CD5C5C"},
# {"max": 9999, "min": 1000, "label": ">=1000", "color": "#8B0000"}]
# ),
# )
# )
# return c
#
#
# d_map = map_yunnan()
# # d_map.render_notebook()
# d_map.render('地图.html')
#地市级地图
# data = [['楚雄市', 31], ['玉溪市', 11], ['楚雄彝族自治州', 2], ['西双版纳傣族自治州', 12], ['普洱市', 4],
# ['昭通市', 8], ['曲靖市', 5], ['迪庆藏族自治州', 0], ['丽江市', 7], ['临沧市', 1], ['保山市', 8],
# ['怒江傈僳族自治州', 0], ['大理白族自治州', 7], ['德宏傣族景颇族自治州', 4], ['红河哈尼族彝族自治州', 5],
# ['文山壮族苗族自治州', 0]]
#
#
# def map_yunnan() -> Map:
# c = (
# Map()
# .add("确诊病例", data_pair=data, maptype="楚雄彝族自治州", zoom=1)
# .set_global_opts(
# title_opts=opts.TitleOpts(title="楚雄地图"),
# visualmap_opts=opts.VisualMapOpts(max_=31, is_piecewise=False),
# )
# )
# return c
#
#
# d_map = map_yunnan()
# # d_map.render_notebook()
# d_map.render('地图.html')
#仪表盘
# from pyecharts import options as opts
# from pyecharts.charts import Gauge
#
# c = (
# Gauge(init_opts = opts.InitOpts(width = '800px',height = '400px')) #设置GAUGE的大小
# .add(
# "这是仪表盘上方中间的标题",[("仪表里标题-完成率",88.88)],radius = "80%", #radius 设置半径大小
# split_number = 20, #设置仪表盘的刻度有多少,这里设置分成20份,每份是5%
# title_label_opts = opts.LabelOpts(
# font_size = 15, color = 'red',font_family = "Microsoft YaHei" ), #给仪表盘里的标题设置颜色大小
# axisline_opts = opts.AxisLineOpts(
# linestyle_opts= opts.LineStyleOpts(
# color = [(0.3, "#ffb6b9"), (0.7, "#fae3d9"), (1, "#bbded6")], width=30)
# #设置仪表盘的颜色,分别是30%,70%,和100%的,分成三段!圆环的宽度!
# ),)
# .set_global_opts(
# title_opts = opts.TitleOpts(title = "这是左上方的大标题"),
# toolbox_opts = opts.ToolboxOpts(), #显示右上角的工具栏
# legend_opts = opts.LegendOpts(is_show = True), #如果这里选False的话,那么"这是仪表盘上方中间的标题"就不显示了
# tooltip_opts = opts.TooltipOpts(is_show = True, formatter ="{a} <br/>{b} : {c}%" ),
# )
# #这里可以调整仪表盘的颜色和粗细!!
# # .set_series_opts(
# # axisline_opts=opts.AxisLineOpts(
# # linestyle_opts =opts.LineStyleOpts(
# # color = [[0.2,'#00dffc'],[0.8,'#008c9e'],[1,'#005f6b']],width = 35)))
# .render(r'2.html')
# )
# list1=[['合肥市', 526996], ['六安市', 458368]]
# # ['河北', 283797], ['浙江', 278097],['上海市', 251175], ['天津市', 93933], ['陕西', 84043], ['河南', 79902]]
# ggeo=(
# Map()
# .add('2021年',list1,maptype='广东')#china,zoom = 1,center=[105,38]
# # .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
# .set_global_opts(
# # visualmap_opts=opts.VisualMapOpts(max_=600000),
# title_opts=opts.TitleOpts(title='各省销售情况'))
# )
# print(list1)
# ggeo.render('map图.html')
# from pyecharts.faker import Faker
# from pyecharts import options as opts
# from pyecharts.charts import Funnel, Page
# # 导入输出图片工具
# from pyecharts.render import make_snapshot
# # 使用snapshot-selenium 渲染图片
# from snapshot_selenium import snapshot
# def funnel_base() -> Funnel:
# c = (
# Funnel()
# .add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())])
# .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))
# )
# return c
#
# funnel = funnel_base()
# make_snapshot(snapshot, funnel.render(), "Funnel.png")
from pyecharts import options as opts
from pyecharts.charts import Bar,Boxplot,Gauge,Map,Pie,HeatMap,Page
from pyecharts.faker import Faker
import random
def bar(): #柱状图
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values(), stack="stack1")
.add_yaxis("商家B", Faker.values(), stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar"))
#.render("bar_stack0.html")
)
return c
def box():#箱状图
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
c = Boxplot()
c.add_xaxis(["expr1", "expr2"])
c.add_yaxis("A", c.prepare_data(v1))
c.add_yaxis("B", c.prepare_data(v2))
c.set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例"))
#c.render("boxplot_base.html")
return c
def gau():#仪表图
c = (
Gauge(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add(series_name="业务指标", data_pair=[["完成率", 55.5]])
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}%"),
)
#.render("gauge.html")
)
return c
def maps():#地图
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))
#.render("map_base.html")
)
return c
def pie():#饼图
c = (
Pie()
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
.set_global_opts(title_opts=opts.TitleOpts(title=""))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
#.render("pie_set_color.html")
)
return c
def heat():#热力图
value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
c = (
HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("series0", Faker.week, value)
.set_global_opts(
title_opts=opts.TitleOpts(title="HeatMap-基本示例"),
visualmap_opts=opts.VisualMapOpts(),
)
#.render("heatmap_base.html")
)
return c
def tab1(name,color): #作为标题吧
c = (Pie().
set_global_opts(
title_opts=opts.TitleOpts(title=name,pos_left='center',pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(color=color,font_size=35))))
return c
page = Page()
page.add(tab1("pyecharts可视化大屏","#FF00FF"),
gau(),
heat(),
maps(),
bar(),
box(),
pie())
page.render("mytest.html")
from bs4 import BeautifulSoup
with open("mytest.html", "r+", encoding='utf-8') as html:
html_bf = BeautifulSoup(html, 'lxml')
divs = html_bf.select('.chart-container')
divs[0]["style"] = "width:40%;height:10%;position:absolute;top:0;left:30%;"
divs[1]["style"] = "width:40%;height:40%;position:absolute;top:10%;left:30%;"
divs[2]["style"] = "width:40%;height:50%;position:absolute;top:50%;left:30%;"
divs[3]["style"] = "width:30%;height:50%;position:absolute;top:0;left:0;"
divs[4][ "style"] = "width:30%;height:50%;position:absolute;top:50%;left:0;"
divs[5]["style"] = "width:30%;height:50%;position:absolute;top:0;left:70%;"
divs[6]['style'] = "width:30%;height:50%;position:absolute;top:50%;left:70%;"
body = html_bf.find("body")
body["style"] = "background-color:#FFFFFF;" # 背景颜色
html_new = str(html_bf)
html.seek(0, 0)
html.truncate()
html.write(html_new)
html.close()
# divs[0]["style"] = "width:40%;height:10%;position:absolute;top:0;left:30%;"
|