Pyecharts基本图:水球图
文章目录
前言
本文主要是展示Pyecharts基本图的水球图的基本用法和例子。
一. Liquid:水球图
1.1 Liquid 类
# class pyecharts.charts.Liquid
class Liquid(
# 初始化配置项,参考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
1.2 add 函数
# func pyecharts.charts.Liquid.add
def add(
# 系列名称,用于 tooltip 的显示,legend 的图例筛选。
series_name: str,
# 系列数据,格式为 [value1, value2, ....]
data: Sequence,
# 水球外形,有' circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow' 可选。
# 默认 'circle'。也可以为自定义的 SVG 路径。
shape: str = "circle",
# 波浪颜色。
color: Optional[Sequence[str]] = None,
# 背景颜色
background_color: types.Union[str, dict, None] = None,
# 是否显示波浪动画。
is_animation: bool = True,
# 是否显示边框。
is_outline_show: bool = True,
# 外沿边框宽度
outline_border_distance: types.Numeric = 8,
# 外沿样式
outline_itemstyle_opts: types.ItemStyle = None,
# 标签配置项,参考 `series_options.LabelOpts`
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(font_size=50, position="inside"),
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
)
主要是:名称,数据,外形,颜色,标签配置项。
二. 例子
2.1 菱形
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid() # 下面确定名称,比例,是否显示轮廓,形状
.add("lq", [0.3, 0.7], is_outline_show=False, shape=SymbolType.DIAMOND)
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-Diamond"))
.render("liquid_shape_diamond")
)
2.2 多水球图
from pyecharts import options as opts
from pyecharts.charts import Grid, Liquid
from pyecharts.commons.utils import JsCode
l1 = (
Liquid()
.add("lq", [0.6, 0.7], center=["60%", "50%"])
.set_global_opts(title_opts=opts.TitleOpts(title="多个 Liquid 显示"))
)
l2 = Liquid().add(
"lq",
[0.3254],
center=["25%", "50%"],
label_opts=opts.LabelOpts(
font_size=50,
formatter=JsCode(
"""function (param) {
return (Math.floor(param.value * 10000) / 100) + '%';
}"""
),
position="inside",
),
)
# 如何展示多图结构
grid = Grid().add(l1, grid_opts=opts.GridOpts()).add(l2, grid_opts=opts.GridOpts())
grid.render("multiple_liquid.html")
2.3 矩形水球
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("lq", [0.3, 0.7], is_outline_show=False, shape=SymbolType.RECT)
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-rect"))
.render("liquid_shape_rect.html")
)
其实跟菱形水球差不多,就是形状处shpe 改改就行。
2.4 基本图
from pyecharts import options as opts
from pyecharts.charts import Liquid
c = (
Liquid()
.add("lq", [0.6, 0.7])
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-基本示例"))
.render("liquid_base.html")
)
2.5 精确数据
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.commons.utils import JsCode
c = (
Liquid()
.add(
"lq",
[0.3254], # 这里显示,看你保留几位小数
label_opts=opts.LabelOpts(
font_size=50,
formatter=JsCode(
"""function (param) {
return (Math.floor(param.value * 10000) / 100) + '%';
}"""
),
position="inside",
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-数据精度"))
.render("liquid_data_precision.html")
)
2.6 无轮廓
from pyecharts import options as opts
from pyecharts.charts import Liquid
c = (
Liquid() # 这里不显示水球的轮廓
.add("lq", [0.6, 0.7, 0.8], is_outline_show=False)
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-无边框"))
.render("liquid_without_outline.html")
)
2.7 箭头型
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid() # 也是类似,改改shape
.add("lq", [0.3, 0.7], is_outline_show=False, shape=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title="Liquid-Shape-arrow"))
.render("liquid_shape_arrow.html")
)
总结
本文主要是展示了几个水球图的简单应用和案例。