Java 生成「渠道来源漏斗转化(信用平台 / 用户增长)」PDF 示例(Funnel)
Funnel1 页 A4 · 47 KB
从曝光、注册、实名到业务办理逐层统计转化,用漏斗图定位流失最严重的环节。

输出规格
- PDF 页数
- 1
- 图表类型
- Funnel
- 模板行数
- 95
- 源码行数
- 102
Java 源码 · JQuickBiz25ChannelFunnelTest.java
以下代码即生成上方 PDF 的完整测试类,可直接复制运行;Maven 依赖见快速开始章节。
/*
* 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 ([email protected])
*/
package com.github.paohaijiao.demo.biz;
import com.github.paohaijiao.JOption;
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 com.github.paohaijiao.funnel.*;
import org.junit.Test;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
/**
* 业务场景 Demo 25:渠道来源漏斗转化(信用平台 / 用户增长)
*
* <p>业务场景:信用平台统计广告投放、搜索引擎、渠道合作、老客推荐四类注册渠道的新增注册量,
* 以漏斗形态对比各渠道获客贡献,用于投放预算分配,主色采用暖调深橙 #BF360C。</p>
* <p>实际图表类型:{@link JChartType#Funnel} 漏斗图(展示各渠道新增注册量的递减分布)。</p>
*
* <p>模板:{@code report/biz/25_channel_funnel.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\25_channel_funnel.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt × 400pt(宽 × 高)的横版比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz25ChannelFunnelTest {
public static final String path = JQuickConstant.path;
@Test
public void biz25ChannelFunnel() throws IOException {
JFunnelOption funnelOption = JFunnelOption.createDefaultFunnel();
funnelOption = funnelOption
.title(new Title().text("注册渠道转化漏斗").subtext("2024年信用平台新增注册用户(单位:人)"))
.funnel(new Funnel()
.width(600)
.topY(80)
.bottomY(200)
.gap(2)
.borderColor(Color.GRAY)
).series(Collections.singletonList(
new Series()
.name("注册渠道")
.type("funnel")
.data(Arrays.asList(
new DataItem("广告投放", 12000),
new DataItem("搜索引擎", 8600),
new DataItem("渠道合作", 5400),
new DataItem("老客推荐", 3100)
))))
// 与主色 #BF360C 同色系的低饱和棕色渐变,避免高饱和跳色
.colors(
new Color(140, 62, 44),
new Color(166, 84, 58),
new Color(191, 108, 82),
new Color(214, 141, 117)
);
JOption option = new JOption();
option.setFunnelOption(funnelOption);
String svg = JChartRendererFactory.renderChart(JChartType.Funnel, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/25_channel_funnel.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/25_channel_funnel.txt");
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/25_channel_funnel.txt");模板文件 · 25_channel_funnel.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
<div style="background:#FDF0EA; border:1px solid #E8CDBF; borderLeft:6px solid #BF360C; borderRadius:4px; padding:12px; marginBottom:12px; keepTogether:true">
<h1 style="color:#BF360C; fontSize:17; bold:true; marginBottom:4px">'注册渠道转化漏斗分析'</h1>
<p style="color:#7A5040; fontSize:9">'报告编号:MKT-2024-Q4-025 | 生成日期:2025年1月10日 | 统计周期:2024年全年'</p>
</div>
<div style="marginBottom:12px">
<h2 style="color:#BF360C; fontSize:13; bold:true; underline:true; marginBottom:8px">'一、各渠道新增注册漏斗'</h2>
<div style="display:flex; justifyContent:space-between; marginBottom:10px">
<div style="width:285px">
<div style="display:flex; justifyContent:center">
<svg style="width:285px">${svg}</svg>
</div>
</div>
<div style="width:265px">
<h3 style="color:#BF360C; fontSize:12; bold:true; marginBottom:6px">'漏斗读法'</h3>
<list style="listStyleType:disc; paddingLeft:14px; fontSize:9">
<li style="marginBottom:5px">'自上而下为四类注册渠道,条越长代表新增注册量越大。'</li>
<li style="marginBottom:5px">'广告投放为第一大来源,贡献约四成新增注册。'</li>
<li style="marginBottom:5px">'老客推荐量最小,但单客获取成本最低。'</li>
<li style="marginBottom:5px">'渠道合作居中,性价比介于两者之间。'</li>
</list>
</div>
</div>
<p style="fontSize:9; color:#6c757d; italic:true; marginTop:6px">'图 1 2024年信用平台新增注册用户渠道漏斗(单位:人;数据来源:用户增长看板)'</p>
</div>
<div style="marginBottom:12px">
<h2 style="color:#BF360C; fontSize:13; bold:true; underline:true; marginBottom:8px">'二、渠道投放效果明细'</h2>
<table style="width:540px; fontSize:10">
<tr>
<th style="backgroundColor:#BF360C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'注册渠道'</th>
<th style="backgroundColor:#BF360C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'新增注册(人)'</th>
<th style="backgroundColor:#BF360C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'占比'</th>
<th style="backgroundColor:#BF360C; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'单客获取成本(元)'</th>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'广告投放'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'12,000'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'41.2%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'46'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'搜索引擎'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'8,600'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'29.6%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'32'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'渠道合作'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'5,400'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'18.6%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'28'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'老客推荐'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'3,100'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'10.6%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#FDF0EA; border:1px solid #DCE3EC">'12'</td>
</tr>
<tr>
<td style="padding:6px; backgroundColor:#BF360C; color:white; bold:true; border:1px solid #BF360C">'合计'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#BF360C; color:white; bold:true; border:1px solid #BF360C">'29,100'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#BF360C; color:white; bold:true; border:1px solid #BF360C">'100.0%'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#BF360C; color:white; bold:true; border:1px solid #BF360C">'35'</td>
</tr>
</table>
</div>
<div style="marginBottom:10px">
<h2 style="color:#BF360C; fontSize:13; bold:true; underline:true; marginBottom:8px">'三、投放策略建议'</h2>
<list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
<li style="marginBottom:6px">'结构结论:广告投放 12,000 人、搜索引擎 8,600 人,两者合计贡献 70.8% 的新增注册,是获客主渠道。'</li>
<li style="marginBottom:6px">'成本结论:老客推荐单客获取成本仅 12 元,不足广告投放(46 元)的三分之一,投入产出效率最高。'</li>
<li style="marginBottom:6px">'渠道合作 5,400 人、成本 28 元/人,处于量价均衡区间,可继续扩大合作方名单。'</li>
<li style="marginBottom:6px">'建议:将广告预算中约 10% 向老客推荐活动迁移,目标把推荐渠道占比由 10.6% 提升至 15%。'</li>
</list>
</div>
<lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
<div style="display:flex; justifyContent:space-between; marginTop:8px">
<div style="width:180px">
<p style="color:#6c757d; fontSize:8">'密级:内部'</p>
</div>
<div style="width:200px; textAlignment:center">
<p style="color:#6c757d; fontSize:8; opacity:0.8">'生成时间:2025年1月10日'</p>
</div>
<div style="width:180px; textAlignment:right">
<p style="color:#6c757d; fontSize:8">'编制:用户增长部'</p>
</div>
</div>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。