使用pyecharts如同使用前端echarts,这里主要介绍pyecharts的源码内的图表属性不满足使用,应该怎么办?
使用pyecharts生成柱状图
pyecharts开源链接:http://pyecharts.herokuapp.com/bar 根据官方样例:
# encoding: utf-8
from pyecharts import Bar, Page
page = Page()
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
chart = Bar("柱状图-数据堆叠", **style.init_style)
chart.add("商家A", attr, v1, is_stack=True)
chart.add("商家B", attr, v2, is_stack=True, is_more_utils=True)
page.add(chart)
在这里主要是对属性的介绍
比如说:
字段 | 用处 |
title | 标题 |
title_pos | 标题距离左侧位置(百分比/数字) |
title_top | 标题距离顶部位置(百分比/数字) |
以上等等都是可以根据源码可以查看,一步步添加 | |
源码bar.py: |
# coding=utf-8
from pyecharts.chart import Chart
class Bar(Chart):
"""
<<< 柱状图/条形图 >>>
柱状/条形图,通过柱形的高度/条形的宽度来表现数据的大小。
"""
def __init__(self, title="", subtitle="", **kwargs):
super(Bar, self).__init__(title, subtitle, **kwargs)
def add(self, *args, **kwargs):
self.__add(*args, **kwargs)
return self
def __add(
self,
name,
x_axis,
y_axis,
is_stack=False,
bar_category_gap="20%",
**kwargs
):
"""
:param name:
系列名称,用于 tooltip 的显示,legend 的图例筛选。
:param x_axis:
x 坐标轴数据。
:param y_axis:
y 坐标轴数据。
:param is_stack:
数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置。默认为 False。
:param kwargs:
"""
assert len(x_axis) == len(y_axis)
kwargs.update(x_axis=x_axis)
chart = self._get_all_options(**kwargs)
if is_stack:
is_stack = "stack_" + str(self._option["series_id"])
else:
is_stack = ""
xaxis, yaxis = chart["xy_axis"]
self._option.update(xAxis=xaxis, yAxis=yaxis)
self._option.get("legend")[0].get("data").append(name)
self._option.get("series").append(
{
"type": "bar",
"name": name,
"data": y_axis,
"stack": is_stack,
"barCategoryGap": bar_category_gap,
"label": chart["label"],
"markPoint": chart["mark_point"],
"markLine": chart["mark_line"],
"seriesId": self._option.get("series_id"),
}
)
self._config_components(**kwargs)
更多属性添加源码chart.py:
# coding=utf-8
import random
import pyecharts.constants as constants
from pyecharts.base import Base
from pyecharts.echarts.option import get_base_options
class Chart(Base):
"""
`Chart`类是所有非自定义类的基类,继承自 `Base` 类
"""
def __init__(
self,
title,
subtitle,
width=800,
height=400,
title_pos="auto",
title_top="auto",
title_color=None,
subtitle_color=None,
title_text_size=18,
subtitle_text_size=12,
background_color=None,
page_title=constants.PAGE_TITLE,
renderer=constants.CANVAS_RENDERER,
extra_html_text_label=None,
is_animation=True,
):
"""
:param title:
主标题文本,支持 \n 换行,默认为 ""
:param subtitle:
副标题文本,支持 \n 换行,默认为 ""
:param width:
画布宽度,默认为 800(px)
:param height:
画布高度,默认为 400(px)
:param title_pos:
标题距离左侧距离,默认为'left',有'auto', 'left', 'right',
'center'可选,也可为百分比或整数
:param title_top:
标题距离顶部距离,默认为'top',有'top', 'middle', 'bottom'可选,
也可为百分比或整数
:param title_color:
主标题文本颜色,默认为 '#000'
:param subtitle_color:
副标题文本颜色,默认为 '#aaa'
:param title_text_size:
主标题文本字体大小,默认为 18
:param subtitle_text_size:
副标题文本字体大小,默认为 12
:param background_color:
画布背景颜色,默认为 '#fff'
:param page_title:
指定生成的 html 文件中 <title> 标签的值。默认为 'Echarts'
:param renderer:
指定使用渲染方式,有 'svg' 和 'canvas' 可选,默认为 'canvas'。
3D 图仅能使用 'canvas'。
:param extra_html_text_label:
额外的 HTML 文本标签,(<p> 标签)。类型为 list,list[0] 为文本内容,
list[1] 为字体风格样式(选填)。如 ["this is a p label", "color:red"]
:param is_animation:
是否开启动画,默认为 True。V0.5.9+
"""
super(Chart, self).__init__(
width=width,
height=height,
renderer=renderer,
page_title=page_title,
extra_html_text_label=extra_html_text_label,
is_animation=is_animation,
)
def add(
self,
angle_data=None,
angle_range=None,
angleaxis_label_interval=None,
area_color=None,
area_opacity=None,
axis_range=None,
bar_category_gap=None,
border_color=None,
boundary_gap=None,
center=None,
calendar_date_range=None,
calendar_cell_size=None,
coordinate_region=None,
datazoom_type=None,
datazoom_range=None,
datazoom_orient=None,
datazoom_xaxis_index=None,
datazoom_yaxis_index=None,
datazoom_extra_type=None,
datazoom_extra_range=None,
datazoom_extra_orient=None,
datazoom_extra_xaxis_index=None,
datazoom_extra_yaxis_index=None,
effect_brushtype=None,
effect_period=None,
effect_scale=None,
extra_data=None,
extra_name=None,
funnel_gap=None,
funnel_sort=None,
geo_emphasis_color=None,
geo_normal_color=None,
geo_cities_coords=None,
geo_effect_period=None,
geo_effect_traillength=None,
geo_effect_color=None,
geo_effect_symbol=None,
geo_effect_symbolsize=None,
graph_layout=None,
graph_gravity=None,
graph_edge_length=None,
graph_repulsion=None,
graph_edge_symbol=None,
graph_edge_symbolsize=None,
grid_width=None,
grid_height=None,
grid_top=None,
grid_bottom=None,
grid_left=None,
grid_right=None,
grid3d_width=None,
grid3d_height=None,
grid3d_depth=None,
grid3d_opacity=None,
grid3d_shading=None,
grid3d_rotate_speed=None,
grid3d_rotate_sensitivity=None,
is_angleaxis_show=None,
is_area_show=None,
is_axisline_show=None,
is_calculable=None,
is_calendar_heatmap=None,
is_clockwise=None,
is_convert=None,
is_datazoom_show=None,
is_datazoom_extra_show=None,
is_fill=None,
is_focusnode=None,
is_geo_effect_show=None,
is_grid3d_rotate=None,
is_label_show=None,
is_label_emphasis=None,
is_legend_show=None,
is_liquid_animation=None,
is_liquid_outline_show=None,
is_more_utils=None,
is_map_symbol_show=None,
is_piecewise=None,
is_radiusaxis_show=None,
is_random=None,
is_roam=None,
is_rotatelabel=None,
is_smooth=None,
is_splitline_show=None,
is_stack=None,
is_step=None,
is_symbol_show=None,
is_toolbox_show=None,
is_visualmap=None,
is_xaxislabel_align=None,
is_yaxislabel_align=None,
is_xaxis_inverse=None,
is_yaxis_inverse=None,
is_xaxis_boundarygap=None,
is_yaxis_boundarygap=None,
is_xaxis_show=None,
is_yaxis_show=None,
item_color=None,
label_color=None,
label_pos=None,
label_text_color=None,
label_text_size=None,
label_formatter=None,
label_emphasis_textcolor=None,
label_emphasis_textsize=None,
label_emphasis_pos=None,
legend_orient=None,
legend_pos=None,
legend_top=None,
legend_selectedmode=None,
legend_text_size=None,
legend_text_color=None,
line_curve=None,
line_opacity=None,
line_type=None,
line_width=None,
line_color=None,
liquid_color=None,
maptype=None,
mark_line=None,
mark_line_raw=None,
mark_line_symbolsize=None,
mark_line_valuedim=None,
mark_line_coords=None,
mark_point=None,
mark_point_raw=None,
mark_point_symbol=None,
mark_point_symbolsize=None,
mark_point_textcolor=None,
mark_point_valuedim=None,
pieces=None,
radius_data=None,
radius=None,
rosetype=None,
rotate_step=None,
scale_range=None,
shape=None,
start_angle=None,
symbol_size=None,
symbol=None,
sankey_node_width=None,
sankey_node_gap=None,
type=None,
tooltip_trigger=None,
tooltip_trigger_on=None,
tooltip_axispointer_type=None,
tooltip_formatter=None,
tooltip_text_color=None,
tooltip_font_size=None,
tooltip_background_color=None,
tooltip_border_color=None,
tooltip_border_width=None,
tree_layout=None,
tree_symbol=None,
tree_symbol_size=None,
tree_orient=None,
tree_top=None,
tree_left=None,
tree_bottom=None,
tree_right=None,
tree_collapse_interval=None,
tree_label_position=None,
tree_label_vertical_align=None,
tree_label_align=None,
tree_label_text_size=None,
tree_label_rotate=None,
tree_leaves_position=None,
tree_leaves_vertical_align=None,
tree_leaves_align=None,
tree_leaves_text_size=None,
tree_leaves_rotate=None,
treemap_left_depth=None,
treemap_drilldown_icon=None,
treemap_visible_min=None,
visual_orient=None,
visual_range_color=None,
visual_range_size=None,
visual_range_text=None,
visual_range=None,
visual_text_color=None,
visual_pos=None,
visual_top=None,
visual_type=None,
visual_split_number=None,
visual_dimension=None,
word_gap=None,
word_size_range=None,
x_axis=None,
xaxis_margin=None,
xaxis_interval=None,
xaxis_force_interval=None,
xaxis_pos=None,
xaxis_name_gap=None,
xaxis_name_size=None,
xaxis_name_pos=None,
xaxis_name=None,
xaxis_rotate=None,
xaxis_min=None,
xaxis_max=None,
xaxis_type=None,
xaxis_label_textsize=None,
xaxis_label_textcolor=None,
xaxis_line_color=None,
xaxis_line_width=None,
xaxis3d_name=None,
xaxis3d_name_size=None,
xaxis3d_name_gap=None,
xaxis3d_min=None,
xaxis3d_max=None,
xaxis3d_interval=None,
xaxis3d_margin=None,
yaxis_margin=None,
yaxis_interval=None,
yaxis_force_interval=None,
yaxis_pos=None,
yaxis_formatter=None,
yaxis_rotate=None,
yaxis_min=None,
yaxis_max=None,
yaxis_name_gap=None,
yaxis_name_size=None,
yaxis_name_pos=None,
yaxis_type=None,
yaxis_name=None,
yaxis_label_textsize=None,
yaxis_label_textcolor=None,
yaxis_line_color=None,
yaxis_line_width=None,
yaxis3d_name=None,
yaxis3d_name_size=None,
yaxis3d_name_gap=None,
yaxis3d_min=None,
yaxis3d_max=None,
yaxis3d_interval=None,
yaxis3d_margin=None,
zaxis3d_name=None,
zaxis3d_name_size=None,
zaxis3d_name_gap=None,
zaxis3d_min=None,
zaxis3d_max=None,
zaxis3d_margin=None,
**kwargs
):
"""
`add()` 方法只是用于提供自动参数补全
"""
return self
...等等
那么重点来了,我自己写一个样例,以上的属性还是没有办法满足我的需求,比如我要生成一个这样的柱状图:
根据属性我可以把y轴的线宽属性调成0,yaxis_line_width=0, 代码如下,效果如图:
bar.add("当前堆使用率", attr, head_used_percent, is_stack=True, legend_pos="80%",
legend_top="5%", legend_orient="vertical", yaxis_name=u"当前堆百分比",
yaxis_name_pos="end", xaxis_line_width=1, yaxis_line_width=0,
xaxis_line_color="#9a9090")
很明显这个刻度印记去不掉,这个时候怎么办?
首先我们思考一个问题,pyecharts和前端echarts的关系,一个是后端的图表库,一个是前端的图表插件,那么有什么联系?
根据pyecharts的方法,可以将这个柱状图生成HTML文件,打开这个HTML文件你就会发现它其实就是前端echarts插件代码的应用,这就意味着前端echarts能够使用的属性,pyecharts基本都能使用。图里的是echarts的option属性,而pyecharts的bar._option就相当于echarts的option。
echarts的官方网址:https://echarts.baidu.com/examples/editor.html?c=bar-gradient
以上加入后两行代码就能达到想要的效果。
bar = Bar("", title_pos="10%")
bar.add("堆空闲百分比", attr, head_free_percent, is_stack=True,
label_color=["#3aa1ff", "#4ecb73"])
bar.add("当前堆使用率", attr, head_used_percent, is_stack=True, legend_pos="80%", legend_top="5%",
legend_orient="vertical", yaxis_name=u"当前堆百分比", yaxis_name_pos="end",
xaxis_line_width=1, yaxis_line_width=0, xaxis_line_color="#9a9090")
bar._option.get("yAxis")[0]["axisTick"] = False
bar._option.get("yAxis")[0]["axisLine"] = {"show": False}
如果想要达到其他的echarts效果,都可以采用相同的方法。
例如:
bar._option.get("series")[0]["barGap"] = "10%"
bar._option.get("yAxis")[0]["axisTick"] = False
bar._option.get("xAxis")[0]["axisTick"] = False
在此就不一一举例查看效果了。