点击跳转

(Click Mario)

说明

第三步,建立显示的图表模型。
我使用的是pyecharts库,通过它的chart模型,生成需要的图表。

PyEcharts & ECharts

ECharts是一个由百度开发的纯 Javascript 的图表库,pyecharts是某三位大佬将ECharts移植到Python项目中的产物。

因为我自己在学python,所以基本实现就是用pyecharts。pyecharts模块,chartst还很全的第三方库。
但是其中每个chart的使用,还是要学习下的。官网提供了比较详细可靠的代码示例。按照例子自己做一些测试验证,还是最后能搞出来比较good-looking的图表的。

官网参考:
Apache ECharts (incubating)
pyecharts-galley

使用模型

Sankey桑基图

展示关系流向,目前比较流行的一个图表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def sankey_custom(nodes, links) -> Sankey:
now = datetime.datetime.now()
s_date = str(now.strftime('%Y-%m-%d'))
chart_title = '原因分析' + ' ' + s_date
# 绘制Sankey。如果想要垂直显示,只需要在add函数里面加一个orient="vertical"就好。
s = (
Sankey()
.add('原因', # 图例名称
nodes, # 传入节点数据
links, # 传入边和流量数据
# 设置透明度、弯曲度、颜色
linestyle_opt = opts.LineStyleOpts(opacity=0.3, curve=0.5, color="source"),
# 标签显示位置
label_opts = opts.LabelOpts(position="right"),
# 节点之前的距离
node_gap = 10,
)
.set_global_opts(title_opts=opts.TitleOpts(title=chart_title))
)
return s

Map地图

显示地图区域数据的数据密度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def chinamap_custom(provinces, value, max_value) -> Map:
now = datetime.datetime.now()
s_date = str(now.strftime('%Y-%m-%d'))
chart_title = '人数分布' + ' ' + s_date
# 绘制China的Map图。
m = (
Map()
.add("人数", [list(z) for z in zip(provinces, value)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title=chart_title),
# visualmap_opts=opts.VisualMapOpts(max_ = int(max_value + 1), is_piecewise=True),
visualmap_opts=opts.VisualMapOpts(max_ = int(max_value + 1)),
)
#.render("map_visualmap_piecewise001.html")
)
return m

Bar条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def bar_custom(list_provinces, list_value) -> Bar:
now = datetime.datetime.now()
s_date = str(now.strftime('%Y-%m-%d'))
chart_title = '离人数分布' + ' ' + s_date
# 绘制bar图,自定义柱状图颜色。
b = (
Bar()
.add_xaxis(list_provinces)
.add_yaxis(
"离职人员",
list_value,
itemstyle_opts=opts.ItemStyleOpts(color='green'),
)
.set_global_opts(
title_opts=opts.TitleOpts(title=chart_title),
yaxis_opts=opts.AxisOpts(name="人数"),
xaxis_opts=opts.AxisOpts(name="省份", axislabel_opts=opts.LabelOpts(rotate=-50)),
)
)
return b

Pie饼图

我使用了好几个样式的饼图。下面放一段富文本格式的饼图,效果比较美观。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def pie_custom_richtext(list_reason, list_value) -> Pie:
now = datetime.datetime.now()
s_date = str(now.strftime('%Y-%m-%d'))
chart_title = '原因比例' + ' ' + s_date
# 绘制pie图。富文本自定义样式。
p = (
Pie()
.add(
"",
[list(z) for z in zip(list_reason, list_value)],
radius=["40%", "60%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 11, "lineHeight": 33}, # 标签
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title=chart_title),
legend_opts=opts.LegendOpts(is_show=True, type_="scroll", pos_left="85%", orient="vertical"),
)
)
return p

以上。