Pyecharats教程2——樹形圖表
本教程旨在介紹Pyecharts中樹形圖表的用法
更加詳細(xì)內(nèi)容可以查閱文檔:https://pyecharts.org/#/zh-cn/intro
圖來自github圖床际乘,如果圖刷不出來可能是網(wǎng)問題
樹形表圖有兩類:
-
樹圖
-
矩陣樹圖
下面一次介紹這兩種樹形圖表归苍。
1. 樹圖
1.1 快速上手
示例代碼:
from pyecharts import options as opts
from pyecharts.charts import Tree
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
)
c.render_notebook()
樹圖節(jié)點(diǎn)的數(shù)據(jù)格式是字典
格式顿肺。描述樹節(jié)點(diǎn)的類為TreeItem
:
class TreeItem(
# 樹節(jié)點(diǎn)的名稱,用來標(biāo)識(shí)每一個(gè)節(jié)點(diǎn)账阻。
name: Optional[str] = None,
# 節(jié)點(diǎn)的值,在 tooltip 中顯示。
value: Optional[Numeric] = None,
# 該節(jié)點(diǎn)的樣式者吁,參考 `series_options.LabelOpts`
label_opts: Optional[LabelOpts] = None,
# 子節(jié)點(diǎn),嵌套定義让歼。
children: Optional[Sequence] = None,
)
樹圖的類Tree
:
class Tree(
# 初始化配置項(xiàng)敞恋,參考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
該類下有一個(gè)重要的函數(shù):
def add(
# 系列名稱,用于 tooltip 的顯示谋右,legend 的圖例篩選硬猫。
series_name: str,
# 系列數(shù)據(jù)項(xiàng)
data: Sequence[Union[opts.TreeItem, dict]],
# 樹圖的布局,有 正交 和 徑向 兩種改执。這里的 正交布局 就是我們通常所說的水平 和 垂直 方向啸蜜,
# 對(duì)應(yīng)的參數(shù)取值為 'orthogonal' 。而 徑向布局 是指以根節(jié)點(diǎn)為圓心天梧,每一層節(jié)點(diǎn)為環(huán)盔性,
# 一層層向外發(fā)散繪制而成的布局,對(duì)應(yīng)的參數(shù)取值為 'radial'
layout: str = "orthogonal",
# 標(biāo)記的圖形呢岗。ECharts 提供的標(biāo)記類型包括 'emptyCircle', 'circle', 'rect', 'roundRect',
# 'triangle', 'diamond', 'pin', 'arrow', 'none'冕香。
symbol: types.JSFunc = "emptyCircle",
# 標(biāo)記的大小,可以設(shè)置成諸如 10 這樣單一的數(shù)字后豫,也可以用數(shù)組分開表示寬和高悉尾,
# 例如 [20, 10] 表示標(biāo)記寬為 20,高為 10挫酿。
symbol_size: types.Union[types.JSFunc, types.Numeric, types.Sequence] = 7,
# 樹圖中 正交布局 的方向构眯,也就是說只有在 layout = 'orthogonal' 的時(shí)候,
# 該配置項(xiàng)才生效早龟。對(duì)應(yīng)有 水平 方向的 從左到右惫霸,從右到左;以及垂直方向的從上到下葱弟,
# 從下到上壹店。取值分別為 'LR' , 'RL', 'TB', 'BT'。注意芝加,之前的配置項(xiàng)值 'horizontal'
# 等同于 'LR'硅卢, 'vertical' 等同于 'TB'。
orient: str = "LR",
# tree組件離容器上側(cè)的距離藏杖。
# top 的值可以是像 20 這樣的具體像素值将塑,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比,
# 也可以是 'top', 'middle', 'bottom'蝌麸。
# 如果 top 的值為'top', 'middle', 'bottom'点寥,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊。
pos_top: Optional[str] = None,
# tree 組件離容器左側(cè)的距離来吩。
# left 的值可以是像 20 這樣的具體像素值开财,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比汉柒,
# 也可以是 'left', 'center', 'right'。
# 如果 left 的值為'left', 'center', 'right'责鳍,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊碾褂。
pos_left: Optional[str] = None,
# tree 組件離容器下側(cè)的距離。
# bottom 的值可以是像 20 這樣的具體像素值历葛,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比正塌。
pos_bottom: Optional[str] = None,
# tree 組件離容器右側(cè)的距離。
# right 的值可以是像 20 這樣的具體像素值恤溶,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比乓诽。
pos_right: Optional[str] = None,
# 折疊節(jié)點(diǎn)間隔,當(dāng)節(jié)點(diǎn)過多時(shí)可以解決節(jié)點(diǎn)顯示過雜間隔咒程。
collapse_interval: Numeric = 0,
# 樹圖在 正交布局 下鸠天,邊的形狀。分別有曲線和折線兩種帐姻,對(duì)應(yīng)的取值是 curve 和 polyline.
# 注意:該配置項(xiàng)只在 正交布局 下有效稠集,在經(jīng)向布局下的開發(fā)環(huán)境中會(huì)報(bào)錯(cuò)。
edge_shape: str = "curve",
# 正交布局下當(dāng)邊的形狀是折線時(shí)饥瓷,子樹中折線段分叉的位置剥纷。
# 這里的位置指的是分叉點(diǎn)與子樹父節(jié)點(diǎn)的距離占整個(gè)子樹高度的百分比。
# 默認(rèn)取值是 '50%'呢铆,可以是 ['0', '100%'] 之間晦鞋。
# 注意:該配置項(xiàng)只有在 edgeShape = 'curve' 時(shí)才有效。
edge_fork_position: str = "50%",
# 是否開啟鼠標(biāo)縮放和平移漫游棺克。默認(rèn)不開啟悠垛。如果只想要開啟縮放或者平移。
# 可以設(shè)置成 'scale' 或者 'move'娜谊。設(shè)置成 true 為都開啟
is_roam: bool = False,
# 子樹折疊和展開的交互确买,默認(rèn)打開 。由于繪圖區(qū)域是有限的因俐,而通常一個(gè)樹圖的節(jié)點(diǎn)可能會(huì)比較多,
# 這樣就會(huì)出現(xiàn)節(jié)點(diǎn)之間相互遮蓋的問題周偎。為了避免這一問題抹剩,可以將暫時(shí)無關(guān)的子樹折疊收起,
# 等到需要時(shí)再將其展開蓉坎。如上面徑向布局樹圖示例澳眷,節(jié)點(diǎn)中心用藍(lán)色填充的就是折疊收起的子樹,可以點(diǎn)擊將其展開蛉艾。
# 注意:如果配置自定義的圖片作為節(jié)點(diǎn)的標(biāo)記钳踊,是無法通過填充色來區(qū)分當(dāng)前節(jié)點(diǎn)是否有折疊的子樹的衷敌。
# 而目前暫不支持,上傳兩張圖片分別表示節(jié)點(diǎn)折疊和展開兩種狀態(tài)拓瞪。所以缴罗,如果想明確地顯示節(jié)點(diǎn)的兩種狀態(tài),
# 建議使用 ECharts 常規(guī)的標(biāo)記類型祭埂,如 'emptyCircle' 等面氓。
is_expand_and_collapse: bool = True,
# 樹圖初始展開的層級(jí)(深度)。根節(jié)點(diǎn)是第 0 層蛆橡,然后是第 1 層舌界、第 2 層,... 泰演,
# 直到葉子節(jié)點(diǎn)呻拌。該配置項(xiàng)主要和 折疊展開 交互一起使用,目的還是為了防止一次展示過多節(jié)點(diǎn)睦焕,
# 節(jié)點(diǎn)之間發(fā)生遮蓋藐握。如果設(shè)置為 -1 或者 null 或者 undefined,所有節(jié)點(diǎn)都將展開复亏。
initial_tree_depth: Optional[Numeric] = None,
# 標(biāo)簽配置項(xiàng)趾娃,參考 `series_options.LabelOpts`
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
# 葉子節(jié)點(diǎn)標(biāo)簽配置項(xiàng),參考 `series_options.LabelOpts`
leaves_label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
# 提示框組件配置項(xiàng)缔御,參考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
)
1.2 配置樹圖
我們將在一個(gè)Base
版的樹圖代碼的基礎(chǔ)上一步一步配置樹圖抬闷。
Base
版
from pyecharts import options as opts
from pyecharts.charts import Tree
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版"))
)
c.render_notebook()
[圖片上傳失敗...(image-5a05e5-1646121362033)]
初始化配置
Tree
的構(gòu)造函數(shù)可以接收一個(gè)初始化配置項(xiàng),形參為init_opts
耕突,接收一個(gè)InitOpts
笤成。這個(gè)配置不懂的見上一篇教程。這里只顯示配置后的結(jié)果眷茁。
class Tree(
# 初始化配置項(xiàng)炕泳,參考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
配置背景、圖紙大猩掀怼:
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版"))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
c.render_notebook()
配置標(biāo)題顏色
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
配置add函數(shù)
add
函數(shù)的形參在上面已經(jīng)貼出來了培遵,讀者可以根據(jù)自己的需要進(jìn)行配置。這里我只配置其中幾個(gè)形參登刺。
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data=data, symbol="circle", symbol_size=[10,10], is_roam=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
調(diào)節(jié)標(biāo)簽顏色與大小
從上圖看到籽腕,我們的背景選用星空的照片,這導(dǎo)致節(jié)點(diǎn)的標(biāo)簽不太清晰纸俭。因此我們想把它調(diào)成白色皇耗。
設(shè)置add
函數(shù)中的label_opts
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B"},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data=data, symbol="circle", symbol_size=[10,10], is_roam=True,
label_opts=opts.LabelOpts(color='white', font_size=15))
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
突出某個(gè)節(jié)點(diǎn)的顏色
比如說,我們希望強(qiáng)調(diào)節(jié)點(diǎn)B揍很,并把節(jié)點(diǎn)B標(biāo)記為紅色郎楼。這該怎么做呢万伤?
注意,要標(biāo)記一個(gè)節(jié)點(diǎn)的顏色呜袁,這個(gè)節(jié)點(diǎn)的形狀必須是實(shí)心的敌买,如果節(jié)點(diǎn)的symble
設(shè)置成空心(如emptyCircle
),那么設(shè)置后的結(jié)果有可能會(huì)看不到傅寡。
在data
中的節(jié)點(diǎn)添加'itemStyle':{'color':"red"}
即可放妈。
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B", 'itemStyle':{'color':"red"}},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data=data, symbol="circle", symbol_size=[10,10], is_roam=True,
label_opts=opts.LabelOpts(color='white', font_size=15))
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
強(qiáng)調(diào)某個(gè)連接
我們不僅可以標(biāo)記出節(jié)點(diǎn)B
的顏色,還可以標(biāo)記出與之相連的連接荐操。
在data
中的節(jié)點(diǎn)添加'itemStyle':{'color':"red"}
即可芜抒。
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B", 'itemStyle':{'color':"red"}, "lineStyle":{'color':'red', 'width':3}},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data=data, symbol="circle", symbol_size=[10,10], is_roam=True,
label_opts=opts.LabelOpts(color='white', font_size=15))
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
[圖片上傳失敗...(image-4aa8ec-1646121362033)]
突出某個(gè)節(jié)點(diǎn)標(biāo)簽的顏色
from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.commons.utils import JsCode
data = [
{
"children": [
{"name": "B", 'itemStyle':{'color':"red"}, "lineStyle":{'color':'red', 'width':3}, "label":{"color":'red'}},
{
"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
]
c = (
Tree(init_opts=opts.InitOpts(width="600px",height="400px", bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat", "size":"100%"}))
.add("", data=data, symbol="circle", symbol_size=[10,10], is_roam=True,
label_opts=opts.LabelOpts(color='white', font_size=15))
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Base版",title_textstyle_opts={'color':"white"}))
.add_js_funcs(
"""
var img = new Image(); img.src = 'https://raw.githubusercontent.com/WinddyAkoky/awesome-gallery/main/20220301104818.png';
""")
)
#
c.render_notebook()
2. 矩陣樹形
矩陣樹形類TreeMap
:
class TreeMap(
# 初始化配置項(xiàng),參考 `global_options.InitOpts`
init_opts: opts.InitOpts = opts.InitOpts()
)
該函數(shù)也有一個(gè)add
函數(shù)用于添加數(shù)據(jù):
def add(
# 系列名稱托启,用于 tooltip 的顯示宅倒,legend 的圖例篩選。
series_name: str,
# 系列數(shù)據(jù)項(xiàng)
data: Sequence[Union[opts.TreeItem, dict]],
# 是否選中圖例屯耸。
is_selected: bool = True,
# leaf_depth 表示『展示幾層』拐迁,層次更深的節(jié)點(diǎn)則被隱藏起來。
# 設(shè)置了 leafDepth 后疗绣,下鉆(drill down)功能開啟线召。drill down 功能即點(diǎn)擊后才展示子層級(jí)。
# 例如多矮,leafDepth 設(shè)置為 1缓淹,表示展示一層節(jié)點(diǎn)。
leaf_depth: Optional[Numeric] = None,
# treemap 組件離容器左側(cè)的距離塔逃。
# left 的值可以是像 20 這樣的具體像素值讯壶,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比,
# 也可以是 'left', 'center', 'right'湾盗。
# 如果 left 的值為'left', 'center', 'right'伏蚊,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊。
pos_left: Optional[str] = None,
# treemap 組件離容器右側(cè)的距離格粪。
# right 的值可以是像 20 這樣的具體像素值躏吊,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比。
pos_right: Optional[str] = None,
# treemap 組件離容器上側(cè)的距離帐萎。
# top 的值可以是像 20 這樣的具體像素值比伏,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比,
# 也可以是 'top', 'middle', 'bottom'吓肋。
# 如果 top 的值為'top', 'middle', 'bottom'凳怨,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊瑰艘。
pos_top: Optional[str] = None,
# treemap 組件離容器下側(cè)的距離是鬼。
# bottom 的值可以是像 20 這樣的具體像素值肤舞,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比。
pos_bottom: Optional[str] = None,
# treemap 組件的寬度均蜜。
width: types.Union[str, types.Numeric] = "80%",
# treemap 組件的高度李剖。
height: types.Union[str, types.Numeric] = "80%",
# 期望矩形長(zhǎng)寬比率。布局計(jì)算時(shí)會(huì)盡量向這個(gè)比率靠近囤耳。
# 默認(rèn)為黃金比:0.5 * (1 + Math.sqrt(5))篙顺。
square_ratio: types.Optional[types.JSFunc] = None,
# 當(dāng)節(jié)點(diǎn)可以下鉆時(shí)的提示符。只能是字符充择。
drilldown_icon: str = "?",
# 是否開啟拖拽漫游(移動(dòng)和縮放)德玫。可取值有:
# false:關(guān)閉椎麦。
# 'scale' 或 'zoom':只能夠縮放宰僧。
# 'move' 或 'pan':只能夠平移。
# true:縮放和平移均可观挎。
roam: types.Union[bool, str] = True,
# 點(diǎn)擊節(jié)點(diǎn)后的行為琴儿。可取值為
# false:節(jié)點(diǎn)點(diǎn)擊無反應(yīng)嘁捷。
# 'zoomToNode':點(diǎn)擊節(jié)點(diǎn)后縮放到節(jié)點(diǎn)造成。
# 'link':如果節(jié)點(diǎn)數(shù)據(jù)中有 link 點(diǎn)擊節(jié)點(diǎn)后會(huì)進(jìn)行超鏈接跳轉(zhuǎn)。
node_click: types.Union[bool, str] = "zoomToNode",
# 點(diǎn)擊某個(gè)節(jié)點(diǎn)雄嚣,會(huì)自動(dòng)放大那個(gè)節(jié)點(diǎn)到合適的比例(節(jié)點(diǎn)占可視區(qū)域的面積比例)晒屎,這個(gè)配置項(xiàng)就是這個(gè)比例。
zoom_to_node_ratio: types.Numeric = 0.32 * 0.32,
# treemap 中采用『三級(jí)配置』:
#『每個(gè)節(jié)點(diǎn)』->『每個(gè)層級(jí)』->『每個(gè)系列』现诀。
# 即我們可以對(duì)每個(gè)節(jié)點(diǎn)進(jìn)行配置夷磕,也可以對(duì)樹的每個(gè)層級(jí)進(jìn)行配置,也可以 series 上設(shè)置全局配置仔沿。節(jié)點(diǎn)上的設(shè)置坐桩,優(yōu)先級(jí)最高。
# 最常用的是『每個(gè)層級(jí)進(jìn)行配置』封锉,levels 配置項(xiàng)就是每個(gè)層級(jí)的配置
levels: types.TreeMapLevel = None,
# 當(dāng)前層級(jí)的最小 value 值绵跷。如果不設(shè)置則自動(dòng)統(tǒng)計(jì)。
visual_min: Optional[Numeric] = None,
# 當(dāng)前層級(jí)的最大 value 值成福。如果不設(shè)置則自動(dòng)統(tǒng)計(jì)碾局。
visual_max: Optional[Numeric] = None,
# 本系列默認(rèn)的 顏色透明度 選取范圍。數(shù)值范圍 0 ~ 1奴艾。
color_alpha: types.Union[types.Numeric, types.Sequence] = None,
# 本系列默認(rèn)的 顏色飽和度 選取范圍净当。數(shù)值范圍 0 ~ 1。
color_saturation: types.Union[types.Numeric, types.Sequence] = None,
# 表示同一層級(jí)節(jié)點(diǎn),在顏色列表中(參見 color 屬性)選擇時(shí)像啼,按照什么來選擇俘闯。可選值:
# 'value' 將節(jié)點(diǎn)的值(即 series-treemap.data.value)映射到顏色列表中忽冻。這樣得到的顏色真朗,反應(yīng)了節(jié)點(diǎn)值的大小。
# 'index' 將節(jié)點(diǎn)的 index(序號(hào))映射到顏色列表中僧诚。即同一層級(jí)中遮婶,第一個(gè)節(jié)點(diǎn)取顏色列表中第一個(gè)顏色,第二個(gè)節(jié)點(diǎn)取第二個(gè)湖笨。
# 這樣得到的顏色旗扑,便于區(qū)分相鄰節(jié)點(diǎn)。
# 'id' 將節(jié)點(diǎn)的 id 映射到顏色列表中慈省。
# id 是用戶指定的肩豁,這樣能夠使得,在 treemap 通過 setOption 變化數(shù)值時(shí)辫呻,同一 id 映射到同一顏色清钥,保持一致性。
color_mapping_by: str = "index",
# 如果某個(gè)節(jié)點(diǎn)的矩形的面積放闺,小于這個(gè)數(shù)值(單位:px平方)祟昭,這個(gè)節(jié)點(diǎn)就不顯示。
# 如果不加這個(gè)限制怖侦,很小的節(jié)點(diǎn)會(huì)影響顯示效果篡悟。
# 關(guān)于視覺設(shè)置,詳見 series-treemap.levels匾寝。
visible_min: types.Numeric = 10,
# 如果某個(gè)節(jié)點(diǎn)的矩形面積搬葬,小于這個(gè)數(shù)值(單位:px平方),則不顯示這個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn)艳悔。
# 這能夠在矩形面積不足夠大時(shí)候急凰,隱藏節(jié)點(diǎn)的細(xì)節(jié)。當(dāng)用戶用鼠標(biāo)縮放節(jié)點(diǎn)時(shí)猜年,如果面積大于此閾值抡锈,又會(huì)顯示子節(jié)點(diǎn)。
# 關(guān)于視覺設(shè)置乔外,詳見 series-treemap.levels床三。
children_visible_min: types.Optional[types.Numeric] = None,
# 標(biāo)簽配置項(xiàng),參考 `series_options.LabelOpts`
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(position="inside"),
# 父級(jí)標(biāo)簽配置項(xiàng)杨幼,參考 `series_options.LabelOpts`
upper_label_opts: types.Label = opts.LabelOpts(position="inside"),
# 提示框組件配置項(xiàng)撇簿,參考 `series_options.TooltipOpts`
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
# 圖形樣式配置聂渊,參考 `global_options.ItemStyleOpts`
itemstyle_opts: types.ItemStyle = None,
# 面包屑控件配置,參考 `TreeMapBreadcrumbOpts`
breadcrumb_opts: types.TreeMapBreadcrumb = None,
)
面包屑控制類TreeMapBreadcrumbOpts
:
class TreeMapBreadcrumbOpts(
# 是否顯示面包屑四瘫。
is_show: bool = True,
# 組件離容器左側(cè)的距離歧沪。
# left 的值可以是像 20 這樣的具體像素值,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比莲组,也可以是 'left', 'center', 'right'。
# 如果 left 的值為'left', 'center', 'right'暖夭,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊锹杈。
pos_left: Union[str, Numeric] = "center",
# 組件離容器右側(cè)的距離。
# right 的值可以是像 20 這樣的具體像素值迈着,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比竭望。
# 默認(rèn)自適應(yīng)。
pos_right: Union[str, Numeric] = "auto",
# 組件離容器上側(cè)的距離裕菠。
# top 的值可以是像 20 這樣的具體像素值咬清,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比,也可以是 'top', 'middle', 'bottom'奴潘。
# 如果 top 的值為'top', 'middle', 'bottom'旧烧,組件會(huì)根據(jù)相應(yīng)的位置自動(dòng)對(duì)齊。
pos_top: Union[str, Numeric] = "auto",
# 組件離容器下側(cè)的距離画髓。
# bottom 的值可以是像 20 這樣的具體像素值掘剪,可以是像 '20%' 這樣相對(duì)于容器高寬的百分比。
# 默認(rèn)自適應(yīng)奈虾。
pos_bottom: Union[str, Numeric] = 0,
# 面包屑的高度夺谁。
height: Numeric = 22,
# 當(dāng)面包屑沒有內(nèi)容時(shí)候,設(shè)個(gè)最小寬度肉微。
empty_item_width: Numeric = 25,
# 圖形樣式匾鸥。參考 `global_options.ItemStyleOpts`
item_opts: ItemStyleOpts = ItemStyleOpts(),
)
圖形樣式類TreeMapItemStyleOpts
:
class TreeMapItemStyleOpts(
# 矩形的顏色。
color: Optional[str] = None,
# 矩形顏色的透明度碉纳。取值范圍是 0 ~ 1 之間的浮點(diǎn)數(shù)勿负。
color_alpha: Union[Numeric, Sequence] = None,
# 矩形顏色的飽和度。取值范圍是 0 ~ 1 之間的浮點(diǎn)數(shù)劳曹。
color_saturation: Union[Numeric, Sequence] = None,
# 矩形邊框 和 矩形間隔(gap)的顏色笆环。
border_color: Optional[str] = None,
# 矩形邊框線寬。為 0 時(shí)無邊框厚者。而矩形的內(nèi)部子矩形(子節(jié)點(diǎn))的間隔距離是由 gapWidth 指定的躁劣。
border_width: Numeric = 0,
# 矩形邊框的顏色的飽和度。取值范圍是 0 ~ 1 之間的浮點(diǎn)數(shù)库菲。
border_color_saturation: Union[Numeric, Sequence] = None,
# 矩形內(nèi)部子矩形(子節(jié)點(diǎn))的間隔距離账忘。
gap_width: Numeric = 0,
# 每個(gè)矩形的描邊顏色。
stroke_color: Optional[str] = None,
# 每個(gè)矩形的描邊寬度。
stroke_width: Optional[Numeric] = None,
)
層級(jí)配置類TreeMapLevelsOpts
:
class TreeMapLevelsOpts(
# 矩形顏色的透明度鳖擒。取值范圍是 0 ~ 1 之間的浮點(diǎn)數(shù)溉浙。
color_alpha: Union[Numeric, Sequence] = None,
# 矩形顏色的飽和度。取值范圍是 0 ~ 1 之間的浮點(diǎn)數(shù)蒋荚。
color_saturation: Union[Numeric, Sequence] = None,
# 表示同一層級(jí)節(jié)點(diǎn)戳稽,在顏色列表中(參見 color 屬性)選擇時(shí),按照什么來選擇期升【妫可選值:
# 'value' 將節(jié)點(diǎn)的值(即 series-treemap.data.value)映射到顏色列表中。這樣得到的顏色播赁,反應(yīng)了節(jié)點(diǎn)值的大小颂郎。
# 'index' 將節(jié)點(diǎn)的 index(序號(hào))映射到顏色列表中。即同一層級(jí)中容为,第一個(gè)節(jié)點(diǎn)取顏色列表中第一個(gè)顏色乓序,第二個(gè)節(jié)點(diǎn)取第二個(gè)。
# 這樣得到的顏色坎背,便于區(qū)分相鄰節(jié)點(diǎn)替劈。
# 'id' 將節(jié)點(diǎn)的 id 映射到顏色列表中。
# id 是用戶指定的得滤,這樣能夠使得抬纸,在 treemap 通過 setOption 變化數(shù)值時(shí),同一 id 映射到同一顏色耿戚,保持一致性湿故。
color_mapping_by: str = "index",
# 矩形樹圖的 Item 配置,參考 `class pyecharts.options.TreeMapItemStyleOpts`
treemap_itemstyle_opts: Union[TreeMapItemStyleOpts, dict, None] = None,
# 每個(gè)矩形中膜蛔,文本標(biāo)簽的樣式坛猪,參考 `series_options.LabelOpts`
label_opts: Union[LabelOpts, dict, None] = None,
# 用于顯示矩形的父節(jié)點(diǎn)的標(biāo)簽。參考 `series_options.LabelOpts`
upper_label_opts: Union[LabelOpts, dict, None] = None,
)
2.1 快速上手
import json
from pyecharts import options as opts
from pyecharts.charts import TreeMap
data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 16,
"name": "我是B.children.d",
"children": [
{"value":6, "name": "B-c-a"},
{"value":6, "name": "B-c-b"}
]},
],
}
],
},
]
c = (
TreeMap()
.add(
series_name="演示數(shù)據(jù)",
data=data
)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap配置"))
)
c.render_notebook()
2.1 配置矩陣樹圖
太簡(jiǎn)單的字段就不多廢話了皂股,講一個(gè)比較特殊的形參墅茉。
配置層級(jí)樣式
上圖中已經(jīng)展示了初步的矩陣樹形圖,但是塊與塊之間連在一起呜呐,沒有間隔就斤,不太好看。為此我們需要去配置level
這個(gè)形參來調(diào)節(jié)矩陣邊框的大小與間隔蘑辑。
import json
from pyecharts import options as opts
from pyecharts.charts import TreeMap
data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 12,
"name": "我是B.children.d",
"children": [
{"value":6, "name": "B-c-a"},
{"value":6, "name": "B-c-b"}
]},
],
},
{"value": 14, "name":"B-d"},
],
},
]
c = (
TreeMap()
.add(
series_name="演示數(shù)據(jù)",
data=data,
levels=[
opts.TreeMapLevelsOpts(
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color="#555", border_width=10, gap_width=40
)
),
],
)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap配置"))
)
c.render_notebook()
我們往level
參數(shù)中傳入了一個(gè)TreeMapLevelsOpts
洋机,并控制border_color="#555", border_width=10, gap_width=40
(邊框的顏色,邊框的寬度和塊之間的間隔)洋魂。但是绷旗,可以發(fā)現(xiàn)喜鼓,這個(gè)設(shè)置僅在第一層樹上發(fā)揮了作用。從第二層開始就不發(fā)生作用了衔肢。若想在樹的第二層也有類似的作用庄岖,需要在添加一個(gè)TreeMapLevelsOpts
,即:
import json
from pyecharts import options as opts
from pyecharts.charts import TreeMap
data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 12,
"name": "我是B.children.d",
"children": [
{"value":6, "name": "B-c-a"},
{"value":6, "name": "B-c-b"}
]},
],
},
{"value": 14, "name":"B-d"},
],
},
]
c = (
TreeMap()
.add(
series_name="演示數(shù)據(jù)",
data=data,
levels=[
opts.TreeMapLevelsOpts(
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color="#555", border_width=10, gap_width=40
)
),
opts.TreeMapLevelsOpts(
color_saturation=[0.3, 0.6],
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color_saturation=0.7, gap_width=10, border_width=6
),
)
],
)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap配置"))
)
c.render_notebook()
從上圖可以看到樹的第二層也有邊框與間隔了角骤。依此類推隅忿,想配置更精美的矩陣樹圖,多配置幾個(gè)TreeMapLevelsOpts
即可邦尊。下面是最終調(diào)節(jié)后的結(jié)果:
import json
from pyecharts import options as opts
from pyecharts.charts import TreeMap
data = [
{"value": 40, "name": "我是A"},
{
"value": 180,
"name": "我是B",
"children": [
{
"value": 76,
"name": "我是B.children",
"children": [
{"value": 12, "name": "我是B.children.a"},
{"value": 28, "name": "我是B.children.b"},
{"value": 20, "name": "我是B.children.c"},
{"value": 12,
"name": "我是B.children.d",
"children": [
{"value":6, "name": "B-c-a"},
{"value":6, "name": "B-c-b"}
]},
],
},
{"value": 14, "name":"B-d"},
],
},
]
c = (
TreeMap()
.add(
series_name="演示數(shù)據(jù)",
data=data,
levels=[
opts.TreeMapLevelsOpts(
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color="#555", border_width=4, gap_width=4
)
),
opts.TreeMapLevelsOpts(
color_saturation=[0.3, 0.6],
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color_saturation=0.7, gap_width=2, border_width=2
),
),
opts.TreeMapLevelsOpts(
color_saturation=[0.3, 0.5],
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color_saturation=0.6, gap_width=1
),
),
opts.TreeMapLevelsOpts(color_saturation=[0.3, 0.5]),
],
)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap配置"))
)
c.render_notebook()