Pyecharts基本图:仪表盘
作者:互联网
Pyecharts基本图:仪表盘
文章目录
前言
本文主要是展示Pyecharts基本图的仪表盘Gauge
的基本用法和例子。
一. Gauge:仪表盘
1.1 Gauge类
# class pyecharts.charts.Gauge
class Gauge(
# 初始化配置项,参考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
1.2 add函数
def add(
# 系列名称,用于 tooltip 的显示,legend 的图例筛选。
series_name: str,
# 系列数据项,格式为 [(key1, value1), (key2, value2)]
data_pair: Sequence,
# 是否选中图例
is_selected: bool = True,
# 最小的数据值
min_: Numeric = 0,
# 最大的数据值
max_: Numeric = 100,
# 仪表盘平均分割段数
split_number: Numeric = 10,
# 仪表盘半径,可以是相对于容器高宽中较小的一项的一半的百分比,也可以是绝对的数值。
radius: types.Union[types.Numeric, str] = "75%",
# 仪表盘起始角度。圆心 正右手侧为0度,正上方为 90 度,正左手侧为 180 度。
start_angle: Numeric = 225,
# 仪表盘结束角度。
end_angle: Numeric = -45,
# 仪表盘刻度是否是顺时针增长。
is_clock_wise: bool = True,
# 轮盘内标题文本项标签配置项,参考 `chart_options.GaugeTitleOpts`
title_label_opts: types.GaugeTitle = opts.GaugeTitleOpts(),
# 轮盘内数据项标签配置项,参考 `chart_options.GaugeDetailOpts`
detail_label_opts: types.GaugeDetail = opts.GaugeDetailOpts(formatter="{value}%"),
# 仪表盘指针配置项目,参考 `chart_options.GaugePointerOpts`
pointer: types.GaugePointer = opts.GaugePointerOpts(),
# 提示框组件配置项,参考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
# 图元样式配置项,参考 `series_options.ItemStyleOpts`
itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,
)
这里主要是用到:名称,数据,最值,仪表盘平均分割段数,顺/逆时针,配置项(越前面越主要,越后面,越精细)。
1.3 仪表盘数据标题配置项
# GaugeTitleOpts
class GaugeTitleOpts(
)
1.4 仪表盘数据内容配置项
# 仪表盘数据内容的详细配置项设定
class GaugeDetailOpts(
# 是否显示详情。
is_show: bool = True,
# 文字块背景色。
# 可以是直接的颜色值,例如:'#123234', 'red', 'rgba(0,23,11,0.3)'。
background_color: str = "transparent",
# 文字块边框宽度。
border_width: Numeric = 0,
# 文字块边框颜色。
border_color: str = "transparent",
# 相对于仪表盘中心的偏移位置,数组第一项是水平方向的偏移,第二项是垂直方向的偏移。
# 可以是绝对的数值,也可以是相对于仪表盘半径的百分比。
offset_center: Sequence = [0, "-40%"],
# 格式化函数或者字符串
formatter: Optional[JSFunc] = None,
# 文字的颜色。
color: str = "auto",
# 文字字体的风格。可选:'normal','italic','oblique'
font_style: str = "normal",
# 文字字体的粗细。可选:'normal','bold','bolder', 'lighter', 100 | 200 | 300 | 400...
font_weight: str = "normal",
# 文字的字体系列。还可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...
font_family: str = "sans-serif",
# 文字的字体大小
font_size: Numeric = 15,
# 文字块的圆角。
border_radius: Numeric = 0,
# 文字块的内边距。例如:
# padding: [3, 4, 5, 6]:表示 [上, 右, 下, 左] 的边距。
# padding: 4:表示 padding: [4, 4, 4, 4]。
# padding: [3, 4]:表示 padding: [3, 4, 3, 4]。
# 注意,文字块的 width 和 height 指定的是内容高宽,不包含 padding。
padding: Numeric = 0,
# 文字块的背景阴影颜色。
shadow_color: Optional[str] = "transparent",
# 文字块的背景阴影长度。
shadow_blur: Optional[Numeric] = 0,
# 文字块的背景阴影 X 偏移。
shadow_offset_x: Numeric = 0,
# 文字块的背景阴影 Y 偏移。
shadow_offset_y: Numeric = 0,
)
1.5 仪表盘指针配置项
# GaugePointerOpts 指针配置内容
class GaugePointerOpts(
# 是否显示指针。
is_show: bool = True,
# 指针长度,可以是绝对数值,也可以是相对于半径的半分比。
length: Union[str, Numeric] = "80%",
# 指针宽度。
width: Numeric = 8,
)
二. 案例
2.1 仪表盘
import pyecharts.options as opts
from pyecharts.charts import Gauge
(
Gauge(init_opts=opts.InitOpts(width="1600px", height="800px")) # 设定大小
.add(series_name="业务指标", data_pair=[["完成率", 55.5]]) # 显示
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False), # 是否显示说明
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}%"), # 提示框设定
)
.render("gauge.html")
)
2.2 基本图
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add("", [("完成率", 66.6)])
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
.render("gauge_base.html")
)
2.3 不同颜色
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add(
"业务指标",
[("完成率", 55.5)],
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(
color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
)
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Gauge-不同颜色"),
legend_opts=opts.LegendOpts(is_show=False),
)
.render("gauge_color.html")
)
2.4 标题设定
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add(
"",
[("完成率", 66.6)],
title_label_opts=opts.LabelOpts( # 标题设定“字体大小,颜色,字体”
font_size=40, color="blue", font_family="Microsoft YaHei"
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-改变轮盘内的字体"))
.render("gauge_label_title_setting.html")
)
2.5 改变颜色
import pyecharts.options as opts
from pyecharts.charts import Gauge
(
Gauge(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add(series_name="业务指标", data_pair=[["完成率", 55.5]])
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}%"),
)
.set_series_opts(
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(
color=[[0.3, "#67e0e3"], [0.7, "#37a2da"], [1, "#fd666d"]], width=30
)
)
)
.render("gauge_change_color.html")
)
2.6 分割段数
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add(
"业务指标",
[("完成率", 55.5)],
split_number=5, # 分割的段数
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(
color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
)
),
detail_label_opts=opts.LabelOpts(formatter="{value}"),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Gauge-分割段数-Label"),
legend_opts=opts.LegendOpts(is_show=False),
)
.render("gauge_splitnum_label.html")
)
2.7 修改半径
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add("", [("完成率", 66.6)], radius="50%") # 半径修改
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-修改 Radius 为 50%"))
.render("gauge_change_radius.html")
)
总结
本文主要是展示了几个仪表盘的常见应用和案例。
标签:基本,Pyecharts,Numeric,Gauge,import,仪表盘,pyecharts,opts 来源: https://blog.csdn.net/Tomandjava/article/details/117435666