简单设计:
1.简单折线图显示变化趋势
import random
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
line = (
Line()
.add_xaxis(['1月','2月','3月','4月','5月']) # 1-5月
.add_yaxis('产品1', [20,22,21,18,19])
.add_yaxis('产品2', [21,25,27,25,22])
.add_yaxis('产品3', [26,28,28,27,30])
.set_series_opts(title_opts=opts.TitleOpts(title='近两季度三个产品的销售'))
)
2.横向条形图进行数据对比
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["产品1", "产品2", "产品3"])
.add_yaxis("一季度",[63,73,82])
.add_yaxis("二季度",[37,47,57])
.reversal_axis()
.set_global_opts(title_opts=opts.TitleOpts("rever")
,toolbox_opts = opts.ToolboxOpts(is_show = True))
.set_series_opts(title_opts=opts.TitleOpts(title='近两季度三个产品的销售'),label_opts=opts.LabelOpts(position = "right"))
)
3.环形图进行查看占比
from pyecharts import options as opts
from pyecharts.charts import Pie
pie = (
Pie()
.add(
'销售:',
[list(z) for z in zip(["产品1", "产品2", "产品3"],[100,120,139])],
radius=['50%', '75%'], #设置内径外径
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(title_opts=opts.TitleOpts(title='近两季度三个产品的销售'),
legend_opts=opts.LegendOpts(is_show=False))
)
4.地理图显示各个区域的门店的销售情况
from pyecharts import options as opts
from pyecharts.charts import Map
import random
province = ['广东', '湖北', '湖南', '四川', '重庆', '黑龙江', '浙江', '山西', '河北', '安徽', '河南', '山东', '*']
data = [(i, random.randint(50, 150)) for i in province]
map = (
Map()
.add("销售额", data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="近两个季度各地门店销售情况"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
)
)
5.雷达图显示多维度信息的影响度
import random
from pyecharts import options as opts
from pyecharts.charts import Page, Radar
def radar_simple() -> Radar:
c = (
Radar()
.add_schema(
# 各项的max_值可以不同
schema=[
opts.RadarIndicatorItem(name='款式', max_=100),
opts.RadarIndicatorItem(name='颜色', max_=100),
opts.RadarIndicatorItem(name='码数', max_=100),
opts.RadarIndicatorItem(name='材质', max_=100),
opts.RadarIndicatorItem(name='质量', max_=100),
opts.RadarIndicatorItem(name='价格', max_=100),
]
)
.add('产品1', [[random.randint(10, 101) for _ in range(6)]],
color='red',
areastyle_opts = opts.AreaStyleOpts( #设置填充的属性
opacity = 0.5,
color='red'
),)
.add('产品2', [[random.randint(10, 101) for _ in range(6)]],
color='blue',
areastyle_opts = opts.AreaStyleOpts(
opacity = 0.5,#透明度
color='blue'
),)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(title_opts=opts.TitleOpts(title='款色码质价对产品影响度'))
)
return c
#radar_simple()
from pyecharts.charts import Funnel
from pyecharts import options as opts
funnel = (Funnel()
.add("用户数", [list(z) for z in zip(['访问', '注册', '加入购物车', '提交订单', '付款成功'], [30398, 15230, 10045, 8109, 5698])],
sort_='ascending',
label_opts=opts.LabelOpts(position="inside"))
.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例", subtitle="我是副标题"))
)
最后得到一个比较丑的数据大屏
全篇代码
import random
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Bar,Pie,Map,Page, Radar,Funnel,Line,Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType
line1 = (
Line()
.add_xaxis(['1月','2月','3月','4月','5月']) # 1-5月
.add_yaxis('产品1', [20,22,21,18,19])
.add_yaxis('产品2', [21,25,27,25,22])
.add_yaxis('产品3', [26,28,28,27,30])
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30)), # 设置x轴标签旋转角度
yaxis_opts=opts.AxisOpts(name='销售额', min_=3),
title_opts=opts.TitleOpts(title='近两季度销售'))
)
line1.theme = ThemeType.PURPLE_PASSION
bar1 = (
Bar()
.add_xaxis(["产品1", "产品2", "产品3"])
.add_yaxis("一季度",[63,73,82])
.add_yaxis("二季度",[37,47,57])
.reversal_axis()
# .set_global_opts(title_opts=opts.TitleOpts("rever")
# ,toolbox_opts = opts.ToolboxOpts(is_show = True))
.set_series_opts(title_opts=opts.TitleOpts(title='近两季度三个产品的销售'),label_opts=opts.LabelOpts(position = "right"))
)
bar1.theme = ThemeType.PURPLE_PASSION
pie = (
Pie()
.add(
'销售:',
[list(z) for z in zip(["产品1", "产品2", "产品3"],[100,120,139])],
radius=['50%', '75%'], #设置内径外径
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(title_opts=opts.TitleOpts(title='近两季度三个产品的销售\n'),
legend_opts=opts.LegendOpts(is_show=False))
)
pie.theme = ThemeType.PURPLE_PASSION
province = ['广东', '湖北', '湖南', '四川', '重庆', '黑龙江', '浙江', '山西', '河北', '安徽', '河南', '山东', '*']
data = [(i, random.randint(50, 150)) for i in province]
map1 = (
Map()
.add("销售额", data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="近两个季度各地门店销售情况"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
)
)
map1.theme = ThemeType.PURPLE_PASSION
radar1 = (
Radar()
.add_schema(
# 各项的max_值可以不同
schema=[
opts.RadarIndicatorItem(name='款式', max_=100),
opts.RadarIndicatorItem(name='颜色', max_=100),
opts.RadarIndicatorItem(name='码数', max_=100),
opts.RadarIndicatorItem(name='材质', max_=100),
opts.RadarIndicatorItem(name='质量', max_=100),
opts.RadarIndicatorItem(name='价格', max_=100),
]
)
.add('产品1', [[random.randint(10, 101) for _ in range(6)]],
color='red',
areastyle_opts = opts.AreaStyleOpts( #设置填充的属性
opacity = 0.5,
color='red'
),)
.add('产品2', [[random.randint(10, 101) for _ in range(6)]],
color='blue',
areastyle_opts = opts.AreaStyleOpts(
opacity = 0.5,#透明度
color='blue'
),)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(title_opts=opts.TitleOpts(title='款色码质价影响'))
)
radar1.theme = ThemeType.PURPLE_PASSION
x_data = ["{}月".format(i) for i in range(1, 13)]
zengfaliang = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
jiangshuiliang = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
average_wendu = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
grid = Grid()
bar = Bar()
grid.theme = ThemeType.PURPLE_PASSION
line = Line()
bar.add_xaxis(x_data)
bar.add_yaxis("产品A销售量",zengfaliang)
bar.add_yaxis("产品B销售量",jiangshuiliang)
bar.set_global_opts(title_opts=opts.TitleOpts("天气影响")
,tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") # 交叉指向工具
)
bar.extend_axis(yaxis=opts.AxisOpts(type_="value",
name="温度",
min_=0,
max_=25,
position="right",
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
))
# 在bar上增加Y轴,在line图上选择对应的轴向
line.add_xaxis(x_data)
line.add_yaxis("平均温度",average_wendu,yaxis_index = 1)
# 把line添加到bar上
bar.overlap(line)
# 这里如果不需要grid也可以,直接设置bar的格式,然后显示bar即可
#bar.render_notebook()
grid.add(chart = bar,grid_opts = opts.GridOpts(),is_control_axis_index = True)
funnel = (Funnel()
.add("用户数", [list(z) for z in zip(['访问', '注册', '加入购物车', '提交订单', '付款成功'], [30398, 15230, 10045, 8109, 5698])],
sort_='ascending',
label_opts=opts.LabelOpts(position="inside"))
.set_global_opts(title_opts=opts.TitleOpts(title=" ", subtitle="线上交易用户数据分析"))
)
funnel.theme = ThemeType.PURPLE_PASSION
#拼合所有图表
page = (Page().add(line1).add(bar1).add(pie).add(map1).add(radar1).add(grid).add(funnel))
page.render('产品销售情况大屏幕数据显示.html')
#调整图表位置
from bs4 import BeautifulSoup
with open("产品销售情况大屏幕数据显示.html", "r+", encoding='utf-8') as html:
html_bf = BeautifulSoup(html, 'lxml')
divs = html_bf.select('.chart-container')
#宽度:500px;高度:250像素;位置:绝对;顶部:50像素;左:0px;边框样式:纯色;边框颜色:#444444;边框-宽度:3px;
divs[0]["style"] = "width:500px;height:300px;position:absolute;top:50px;left:50px;border-style:solid;border-color:#444444;border-width:0px;"
divs[1]['style'] = "width:500px;height:300px;position:absolute;top:350px;left:50px;border-style:solid;border-color:#444444;border-width:0px;"
divs[2]["style"] = "width:500px;height:300px;position:absolute;top:650px;left:50px;border-style:solid;border-color:#444444;border-width:0px;"
divs[3]["style"] = "width:600px;height:600px;position:absolute;top:150px;left:650px;border-style:solid;border-color:#444444;border-width:0px;"
divs[4]["style"] = "width:500px;height:300px;position:absolute;top:50px;left:1350px;border-style:solid;border-color:#444444;border-width:0px;"
divs[5]["style"] = "width:500px;height:300px;position:absolute;top:350px;left:1350px;border-style:solid;border-color:#444444;border-width:0px;"
divs[6]["style"] = "width:500px;height:300px;position:absolute;top:650px;left:1350px;border-style:solid;border-color:#444444;border-width:0px;"
body = html_bf.find("body")
body["style"] = "background-color:rgb(91 92 110);"
div_title="<div align=\"center\" style=\"width:1800px;\">\n<span style=\"font-size:32px;font face=\'黑体\';color:#FFFFFF\"><b>产品销售情况大屏幕数据显示</b></div>"
#修改页面背景色、追加标题
body.insert(0,BeautifulSoup(div_title,"lxml").div)
html_new = str(html_bf)
html.seek(0, 0)
html.truncate()
html.write(html_new)
html.close()