jquick-pdf 树图实战:三级资金流向里,那三套节点尺寸到底各管哪一层
摘要(meta description):树图数据类上有六个尺寸方法:root、level、leaf 各一套宽高,外加水平、垂直两个间距。第一次读 API 完全分不清 setLevelNodeWidth 和 setLeafNodeWidth 各管哪一层,更不明白画布 1000×800 为什么只放 12 个节点。这篇按"先卡住、再解开"的顺序逐个回答:三套尺寸的边界、8 个叶子的高度账、图例色与根节点色不一致、节点第三个参数干什么、模板只给宽度怎么等比缩。附完整 Java 源码与模板(jquick-pdfx 4.7.0+,Apache-2.0)。
标签:jquick-pdf、Java PDF、报表开发、树图
官网示例:
真实渲染效果预览
以下为上述代码 + 模板真实运行生成的 PDF 首屏预览(由 jquick-pdf 渲染输出,非示意图):

一、先卡住我的那个方法名:setLevelNodeWidth 管的是哪一层
JTreeChartData 的 setter 列出来是这样一串:setRootNodeWidth、setRootNodeHeight、setLevelNodeWidth、setLevelNodeHeight、setLeafNodeWidth、setLeafNodeHeight、setHorizontalSpacing、setVerticalSpacing。
root 是根,leaf 是叶子,这两个词没有歧义。卡住的是 level——它指的是"除根和叶之外的所有中间层",还是"根以下的第一层"?
答案是前者。这棵树只有三级,所以 level 恰好就等于第二级,看不出区别;但如果哪天加到四级,setLevelNodeWidth 会同时作用在第二级和第三级上,只有最末端的叶子走 leaf 那套。
想通这一点,"为什么是三套而不是每层一套"就顺了:树图的视觉层级靠尺寸递减表达,根最大、中间一档、叶子最小,中间层再多也用同一档,不然四级五级每层缩一点,缩到最后字都放不下。
代码里三个档位是这么定的:
data.setRootNodeWidth(240);
data.setRootNodeHeight(60);
data.setLevelNodeWidth(190);
data.setLevelNodeHeight(54);
data.setLeafNodeWidth(110);
data.setLeafNodeHeight(48);
240 → 190 → 110,高度 60 → 54 → 48。宽度递减得比高度狠,因为根节点要写"集团总部(资金池)"七个字,二级写"基础设施建设板块"八个字,三级写"供应链金融公司"七个字。等等,三级字数没比二级少,宽度却只有 110——这就是后面要回答的第二个问题:字放不下怎么办。
二、追问一:叶子只有 110 宽,七个字怎么塞进去
塞不进去。算一下:110 宽的框减掉内边距,按 10pt 左右的字号,一行大概放五到六个汉字。"供应链金融公司"七个字要么换行,要么缩字号,要么溢出。
渲染器的处理是换行加居中——这也是 setLeafNodeHeight(48) 比二级的 54 只矮一点点的原因:给两行文字留了高度。如果叶子高度压到 36,第二行就会被切掉。
(效果预览图 alt:浅绿抬头下一张横向展开的树图,左侧一个深青绿色的大方块写着集团总部,往右伸出三条线连到三个中等方块,再往右每个方块各伸出两三根短线连到白色底的小方块,小方块里是两行字,第二行字号明显更小。)
所以叶子宽度不能再压。要让节点更紧凑,只能从间距上省:setHorizontalSpacing(70) 是层与层之间的横向间隙,setVerticalSpacing(60) 是同层兄弟节点之间的纵向间隙。70 这个数不算小,但三级节点加起来 240+70+190+70+110=680,放在 1000 宽的画布里还剩 320 的余量——余量是留给"以后某一层想加宽"的,不是浪费。
三、追问二:画布 1000×800,为什么只放 12 个节点
这是我核得最久的一处。竖直方向摊开算:8 个叶子,每个高 48,兄弟间距 60,8×48+7×60=804,比 800 的画布还多出 4pt;按 8×(48+60) 算更宽松。两种算法都指向同一件事——8 个叶子正好卡在画布高度上,再加一个就得压 setVerticalSpacing 或者换画布。
这就是类注释里"节点总数控制在 12 个(1 个一级 + 3 个二级 + 8 个三级)以避免拥挤"这个数字的来历。不是随手写的 12,是按 48 高、60 间距、800 画布倒推出来的。
12 个节点怎么分:集团总部 1 个;基础设施、产业投资、金融服务 3 个二级;底下 8 个项目公司——路桥、市政工程、轨道交通(基础设施 3 个),新能源、智能制造、新材料(产业投资 3 个),融资租赁、供应链金融(金融服务 2 个)。
三个二级节点分别挂 3、3、2 个孩子,不是平均分。平均分会好看,但业务上金融服务板块确实只有两家公司,硬凑第四家就成造假了。树图的好处是结构本身就是信息:金融服务那根枝条短,一眼就能看出这个板块实体少。
四、追问三:图例里的"一级节点"颜色,跟根节点不是同一个
legends.add(new JTreeChartData.LegendItem("一级节点(集团)", new Color(26, 82, 118)));
legends.add(new JTreeChartData.LegendItem("二级节点(板块)", new Color(44, 127, 184)));
legends.add(new JTreeChartData.LegendItem("三级节点(项目公司)", Color.WHITE));
JTreeChartData.TreeNode root = new JTreeChartData.TreeNode("root", "集团总部(资金池)", "Group Treasury Pool");
root.setColor(new Color(0, 105, 92));
对照着看会发现:图例说一级是 26, 82, 118(偏蓝),根节点实际 setColor 给的是 0, 105, 92(偏青绿)。
这处不一致我没改。原因有两个:一是模板整页主色是 #00695C,正好就是 0,105,92,根节点用这个色跟抬头、章节标题是通的;二是图例色 26,82,118 偏蓝,可能来自渲染器的默认分类色,不一定读 TreeNode 上的 setColor。到底哪个生效,得看渲染器实现,我没去翻,先记着这处差异——真有人拿着图例来对色的时候,这就是第一个要解释的点。
图例第三条 Color.WHITE 更好解释:三级节点是白底,靠边框和连线跟背景区分。8 个叶子如果也填深色,整张图右半边会糊成一片深色块,什么都看不清。
五、追问四:节点构造函数第三个参数是干什么的
new JTreeChartData.TreeNode("road", "路桥项目公司", "Road & Bridge")
三个参数:id、中文名、英文名。中文名是主标题,英文名是副标题,一行大字一行小字。源码里所有节点都给了英文名,包括根节点的 "Group Treasury Pool"。
英文名在这一页里没有信息价值——读者是集团投融资决策委员会,看中文就够了。但它在版面上有价值:给每个方块填了第二行,方块看起来更"实",不像只有一行的空框。如果哪天要把这份报表给海外子公司看,这行小字直接就是双语标题,不用改结构。
id 是 "root"、"infra"、"road" 这样的短字符串。树图靠 addChild 建父子关系,不靠 id 连线,所以 id 只用来调试时定位节点,起个有意义的名字比 "1"、"2" 好排查。
六、追问五:模板只给 700pt 宽,1000×800 的图会不会被压扁
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
不会。只写宽度不写高度,渲染器按原始比例 1000:800 算高度,700pt 宽对应 560pt 高。比例 5:4,出来的树是横向展开的,层与层之间的 70pt 间隙在缩放后按同比例变小,视觉关系不变。
要是手写 height:560pt 也没错,但哪天有人把宽度改成 600、高度还是 560,方块就变成竖条。只给宽度,改一处不会连带出错。
页面上图独占一节,上面 13pt 深绿标题"二、资金流向层级结构",下面 9pt 灰斜体图注,写明"节点 12 个:1 个一级 + 3 个二级 + 8 个三级"。图注把节点数写出来,是给核对用的——改结构的时候忘了改图注,这行字会立刻暴露。
七、追问六:树图不带金额,那 468 亿放哪
树图只表达层级,节点上没有数值位置。TreeNode 的构造参数里没有 value,这意味着"哪条枝占多少钱"这件事,图上完全看不出来。
金额放在第一节的表格里:8 行,四列——项目公司、所属板块、归集资金(亿元)、占比。96.0、82.0、58.0、62.0、48.0、38.0、48.0、36.0,加起来 468.0,跟表下注的"合计归集 468.0 亿元"一致。占比分别是 20.5%、17.5%、12.4%、13.2%、10.3%、8.1%、10.3%、7.7%,合计 100.0%。
表格第一列的单元格用了 borderLeft:3px solid #00695C,其余三边是 1px 浅灰。这条深绿粗线在视觉上把"项目公司名"这一列拎出来,读者顺着往下扫就知道有八家。这是个小技巧:不给整表加框,只给第一列加条肩线,表会轻很多。
第三节的四条结论才把图和表接起来:基础设施三家合计 236.0 亿、占 50.4%(96+82+58=236,236÷468=50.43%);产业投资 148.0 亿、占 31.6%(62+48+38=148);金融服务两家 84.0 亿;前三大节点 96+82+62=240、占 51.3%(240÷468=51.28%)。结论里的每个数都能在表里找到出处,这是我判断这页合不合格的标准。
八、最后一个追问:footerText 跟模板抬头是不是重复了
Java 里 data.setFooterText("数据来源:集团资金管理中心资金池台账 · 统计截止2025年6月30日"),模板抬头第二行也是"统计周期:2025年1月 - 6月 | 数据来源:集团资金管理中心资金池台账"。
看着重复,其实分工不同:footerText 画在 SVG 内部,图被单独切出来贴进汇报材料时,来源跟着图走;模板抬头那行是整页的元信息,管的是"这一页从哪来"。两处都留是有意的,删掉图内那行,图就成了没出处的图。
要小心的是两边日期口径一致。图内写"统计截止2025年6月30日",抬头写"统计周期:2025年1月 - 6月",一回事;抬头改了而图内忘了改,同一页就有两个截止日。
复现清单
- 依赖
io.github.paohaijiao:jquick-pdfx:4.7.0+(Apache-2.0 协议); JTreeChartData设 titleText/subtitleText/footerText,三套节点尺寸按 240/190/110 宽、60/54/48 高递减;setHorizontalSpacing(70)管层间、setVerticalSpacing(60)管同层兄弟,叶子数决定后者能压到多小;LegendItem(名称, Color)三条挂进setLegendList,三级节点用Color.WHITE;TreeNode(id, 中文名, 英文名)建三级结构,根节点单独setColor,addChild串起来后setRootNode;JOption.setData(data),renderChart(JChartType.Tree, option)取 SVG,setFlexLayout(true)后bind("svg", svg),executeResource("report/biz/33_fund_flow_tree.txt")出D:\test\biz\33_fund_flow_tree.pdf。
完整源码附录
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) [2025-2099] Martin (goudingcheng@gmail.com)
*/
package com.github.paohaijiao.biz.chart;
import com.github.paohaijiao.JOption;
import com.github.paohaijiao.combol.JTreeChartData;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.demo.constant.JQuickConstant;
import com.github.paohaijiao.enums.JChartType;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import com.github.paohaijiao.factory.JChartRendererFactory;
import org.junit.Test;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 业务场景 Demo 33:资金流向树图表(集团资金管理)
*
* <p>业务场景:集团资金管理中心展示「集团总部 → 各业务板块 → 各项目公司」三级资金流向层级结构,
* 用于投融资决策委员会分析资金沉淀与周转效率。</p>
* <p>实际图表类型:{@link JChartType#Tree} 树图(层级水平布局,展示资金归集与下拨的父子层级)。</p>
*
* <p>模板:{@code report/biz/33_fund_flow_tree.txt};树图以 SVG 文本形式绑定到模板的 {@code ${svg}} 占位符,
* 输出到 {@code D:\test\biz\33_fund_flow_tree.pdf}。</p>
*
* <p>图表尺寸说明:树图渲染器画布固定 1000pt × 800pt(宽 × 高),节点总数控制在 12 个(1 个一级 + 3 个二级 + 8 个三级)
* 以避免拥挤;模板中 {@code <svg>} 只声明宽度、不声明高度,宽度被页面自动收窄时高度按原比例同步缩放,不会纵向拉伸变形。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz33FundFlowTreeTest {
public static final String path = JQuickConstant.path;
@Test
public void biz33FundFlowTree() throws IOException {
JTreeChartData data = new JTreeChartData();
data.setTitleText("集团资金流向结构图");
data.setSubtitleText("集团总部 → 业务板块 → 项目公司 · 2025年上半年(单位:亿元)");
data.setFooterText("数据来源:集团资金管理中心资金池台账 · 统计截止2025年6月30日");
// 三级节点尺寸:一级为集团、二级为板块、三级为项目公司
data.setRootNodeWidth(240);
data.setRootNodeHeight(60);
data.setLevelNodeWidth(190);
data.setLevelNodeHeight(54);
data.setLeafNodeWidth(110);
data.setLeafNodeHeight(48);
data.setHorizontalSpacing(70);
data.setVerticalSpacing(60);
List<JTreeChartData.LegendItem> legends = new ArrayList<>();
legends.add(new JTreeChartData.LegendItem("一级节点(集团)", new Color(26, 82, 118)));
legends.add(new JTreeChartData.LegendItem("二级节点(板块)", new Color(44, 127, 184)));
legends.add(new JTreeChartData.LegendItem("三级节点(项目公司)", Color.WHITE));
data.setLegendList(legends);
JTreeChartData.TreeNode root = new JTreeChartData.TreeNode("root", "集团总部(资金池)", "Group Treasury Pool");
root.setColor(new Color(0, 105, 92));
// 二级节点一:基础设施建设板块
JTreeChartData.TreeNode infra = new JTreeChartData.TreeNode("infra", "基础设施建设板块", "Infrastructure");
infra.addChild(new JTreeChartData.TreeNode("road", "路桥项目公司", "Road & Bridge"));
infra.addChild(new JTreeChartData.TreeNode("municipal", "市政工程项目公司", "Municipal Works"));
infra.addChild(new JTreeChartData.TreeNode("rail", "轨道交通项目公司", "Rail Transit"));
// 二级节点二:产业投资板块
JTreeChartData.TreeNode industry = new JTreeChartData.TreeNode("industry", "产业投资板块", "Industry Investment");
industry.addChild(new JTreeChartData.TreeNode("energy", "新能源项目公司", "New Energy"));
industry.addChild(new JTreeChartData.TreeNode("manufacture", "智能制造项目公司", "Smart Manufacturing"));
industry.addChild(new JTreeChartData.TreeNode("material", "新材料项目公司", "New Material"));
// 二级节点三:金融服务板块
JTreeChartData.TreeNode finance = new JTreeChartData.TreeNode("finance", "金融服务板块", "Financial Services");
finance.addChild(new JTreeChartData.TreeNode("leasing", "融资租赁公司", "Financial Leasing"));
finance.addChild(new JTreeChartData.TreeNode("supply", "供应链金融公司", "Supply Chain Finance"));
root.addChild(infra);
root.addChild(industry);
root.addChild(finance);
data.setRootNode(root);
JOption option = new JOption();
option.setData(data);
String svg = JChartRendererFactory.renderChart(JChartType.Tree, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/33_fund_flow_tree.pdf");
JPdfConfig config = new JPdfConfig();
// 开启 flex 行布局,使模板中的 display:flex 生效(图表居中)
config.getLayoutConfig().setFlexLayout(true);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource("report/biz/33_fund_flow_tree.txt");
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}
<pdf>
<body>
<div style="textAlignment:center; marginBottom:10px; padding:14px; background:#00695C; borderRadius:4px">
<h1 style="textAlignment:center; color:white; marginBottom:6pt; fontSize:18pt; bold:true">'集团资金流向结构图'</h1>
<p style="color:white; fontSize:10pt; margin:2px">'统计周期:2025年1月 - 6月 | 数据来源:集团资金管理中心资金池台账'</p>
<p style="color:white; fontSize:10pt; margin:2px">'编制单位:集团资金管理中心 | 报送对象:投融资决策委员会'</p>
</div>
<div style="marginBottom:10px">
<h2 style="color:#00695C; fontSize:13pt; bold:true; marginBottom:6pt">'一、项目公司资金归集明细'</h2>
<table style="width:540px; fontSize:10pt">
<tr>
<th style="backgroundColor:#00695C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'项目公司'</th>
<th style="backgroundColor:#00695C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'所属板块'</th>
<th style="backgroundColor:#00695C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'归集资金(亿元)'</th>
<th style="backgroundColor:#00695C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'占比'</th>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'路桥项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'基础设施建设'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'96.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'20.5%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'市政工程项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'基础设施建设'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'82.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'17.5%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'轨道交通项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'基础设施建设'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'58.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'12.4%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'新能源项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'产业投资'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'62.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'13.2%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'智能制造项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'产业投资'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'48.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'10.3%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'新材料项目公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'产业投资'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'38.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'8.1%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'融资租赁公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'金融服务'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'48.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'10.3%'</td>
</tr>
<tr>
<td style="padding:5px; borderTop:1px solid #DCE3EC; borderRight:1px solid #DCE3EC; borderBottom:1px solid #DCE3EC; borderLeft:3px solid #00695C">'供应链金融公司'</td>
<td style="padding:5px; border:1px solid #DCE3EC">'金融服务'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'36.0'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'7.7%'</td>
</tr>
</table>
<p style="fontSize:8pt; color:#8a939b; marginTop:4pt">'注:8 家项目公司合计归集 468.0 亿元,占比合计 100.0%。'</p>
</div>
<div style="marginBottom:10px; padding:10px; backgroundColor:#F7FBFA; borderRadius:4px">
<h2 style="color:#00695C; fontSize:13pt; bold:true; marginTop:0; marginBottom:6pt">'二、资金流向层级结构'</h2>
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
<p style="fontSize:9pt; color:#6c757d; italic:true; marginTop:6pt">'图 1 集团总部 → 业务板块 → 项目公司三级资金流向树图(节点 12 个:1 个一级 + 3 个二级 + 8 个三级)'</p>
</div>
<div style="marginBottom:10px">
<h2 style="color:#00695C; fontSize:13pt; bold:true; marginBottom:6pt">'三、流向特征与管控建议'</h2>
<list style="list-style-type:none; padding-left:0; fontSize:10pt">
<li style="marginBottom:6px; padding:8px; backgroundColor:#E6F2F0; borderLeft:3px solid #00695C">'基础设施板块是资金沉淀主力:三家项目公司合计 236.0 亿元、占集团 50.4%,其中路桥项目公司单点占用 96.0 亿元,为全集团最大资金单元。'</li>
<li style="marginBottom:6px; padding:8px; backgroundColor:#E6F2F0; borderLeft:3px solid #00695C">'产业投资板块合计 148.0 亿元、占 31.6%,新能源项目公司 62.0 亿元为该板块最大节点,与集团战略投资方向一致。'</li>
<li style="marginBottom:6px; padding:8px; backgroundColor:#E6F2F0; borderLeft:3px solid #00695C">'金融服务板块资金效率最高:两家公司以 84.0 亿元资金支撑高周转运营,是集团资金效率的调节阀。'</li>
<li style="marginBottom:6px; padding:8px; backgroundColor:#E6F2F0; borderLeft:3px solid #00695C">'建议:对单一项目公司设置资金占用上限并纳入月度监控,重点关注前三大节点合计 240.0 亿元(占 51.3%)的集中度风险。'</li>
</list>
</div>
<p style="textAlignment:right; color:#8a939b; fontSize:8pt">'注:本表为集团内部资金管理报告附表,未经许可请勿外传。'</p>
</body>
</pdf>