之前写过一篇文章,但基本都是官方的例子,算是自己的学习笔记 官方文档笔记
这次基于实际工作中的需求写一个记录文档,加深自己的理解,因为具体数据不方便,就只给出画图的代码段。
直接上代码,需要解释的地方直接在代码中添加。
def drawing(file):
"""
构建画图数据
param file:包含有画图的字典数据,是一个包含三个元素的元组
return:生成的html文件
"""
bar_list = []
# 构建多页画布
page = Page()
# 画整个部门的图
"""
提取轴数据和销售额数据,x和y轴数据必须是列表数据,为了显示问题,直接改成一位小数了
""""
all_x_data = file[2].index.to_list()
y_data_all_fba = file[2]["FBA"].map(lambda x:round(x,1)).to_list()
y_data_all_fbm = file[2]["FBM"].map(lambda x:round(x,1)).to_list()
"""
想在图上加一些文字说明等信息,所以先把需要的变量处理好
"""
fba_all = round(sum(y_data_all_fba),1)
fbm_all = round(sum(y_data_all_fbm),1)
all_sale_all = round(fba_all + fbm_all,1)
fba_rate_all = "%s%%"%(round(((fba_all+0.01)/all_sale_all)*100,2))
fbm_rate_all = "%s%%"%(round(((fbm_all+0.01)/all_sale_all)*100,2))
# 画条形图
bar_all = (Bar(init_opts=opts.InitOpts( width='1600px',height='500px',
bg_color='rgba(123, 200, 88, 0.4)')
)
# 添加轴和数据
.add_xaxis(all_x_data)
.add_yaxis("FBA", y_data_all_fba,stack=True,
# 显示数据标签及格式(两位小数)
label_opts=opts.LabelOpts(is_show=True,
# 这个是原生的Javascript,后续仔细学学
formatter=JsCode(
"""
function(params) {
return (params.value).toFixed(1)
}
"""
))
)
.add_yaxis("FBM", y_data_all_fbm,stack=True,
# 显示数据标签及格式(两位小数)
label_opts=opts.LabelOpts(
formatter=JsCode(
"""
function(params) {
return (params.value).toFixed(1)
}
"""
))
)
# 设置全局变量
.set_global_opts(# 增加图表标题,位置居中
title_opts=opts.TitleOpts(title=team, pos_left="center"),
# 图例设置
legend_opts=opts.LegendOpts(pos_left="75%",pos_top="5%"),
# 设置提示框
tooltip_opts=opts.TooltipOpts(is_show=True,trigger_on='mousemove',
trigger='axis',axis_pointer_type="cross",
background_color="gray"
),
# 设置工具箱
toolbox_opts=opts.ToolboxOpts(pos_left='85%',pos_top="5%",
feature=opts.ToolBoxFeatureOpts(brush=opts.ToolBoxFeatureBrushOpts(type_="clear"),
magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=True),
data_view=opts.ToolBoxFeatureDataViewOpts(is_show=True, is_read_only=False),
data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False)
)
),
# 设置原生图形元素,也就是标签框
graphic_opts = [
# 标签框标题
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="35%",
z=100 # 图层
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="小组销售额总览",
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="orange"
)
)
),
]
),
# 标签框
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91%",
top="41%",
z=100,
width="250px",
height="40px"
),
children=[
# 背景框
opts.GraphicRect(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_shape_opts=opts.GraphicShapeOpts(
width=135, # 这里是背景框的宽度
height=240,
r=5 # 这个是背景框角平滑度
),
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="rgba(0, 0, 0, 0.2)", # 这个是标签框的背景色和透明度
stroke="rgba(0, 0, 0, 0.2)",# 边框线的颜色和透明度
line_width=2,
shadow_blur=5,
shadow_color="#000",
shadow_offset_x=1,
shadow_offset_y=3
)
)
]
),
# 标签框的内容,第一行
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="45%",
z=101 # 这里需要注意的是它比框体的图层高一层。不然看不到
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=101
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="FBA:%s$" % fba_all,
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="pink"
)
)
),
]
),
# 标签框的内容,第二行
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="53%",
z=101
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="FBM:%s$" %fbm_all,
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="pink"
)
)
),
]
),
# 标签框的内容,第三行
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="61%",
z=101
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="总计:%s$" %all_sale_all,
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="pink"
)
)
),
]
),
# 标签框的内容,第四行
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="69%",
z=101
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="FBM占比:%s" %fbm_rate_all,
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="pink"
)
)
),
]
),
# 标签框的内容,第五行
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
left="91.5%",
top="77%",
z=101
),
children=[
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center",
top="middle",
z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="FBA占比:%s" %fba_rate_all,
font="bold 14px sans-serif",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="pink"
)
)
),
]
),
]# 原生
)#全局变量
)
最终的结果如下: