Java 生成「合同签订趋势(企业法务与合同管理)」PDF 示例(LineBar)
LineBar2 页 A4 · 202 KB
同时呈现合同签订数量与金额,用折线柱状混合图对比数量与金额的走势差异。

输出规格
- PDF 页数
- 2
- 图表类型
- LineBar
- 模板行数
- 131
- 源码行数
- 94
Java 源码 · JQuickBiz32ContractLineBarTest.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.combol.JComboLineBarChartData;
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.Arrays;
import java.util.List;
/**
* 业务场景 Demo 32:合同签订趋势(企业法务与合同管理)
*
* <p>业务场景:企业合同管理系统按月统计签订合同数与平均履约率,柱表示月度签订量、
* 线表示履约质量,用于法务风险管控与经营例会汇报,主色采用墨绿 #1B5E20。</p>
* <p>实际图表类型:{@link JChartType#LineBar} 折线柱状混合图(双 Y 轴:左轴合同份数、右轴履约率百分比)。</p>
*
* <p>模板:{@code report/biz/32_contract_linebar.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\32_contract_linebar.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt × 380pt(宽 × 高)的横版比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz32ContractLineBarTest {
public static final String path = JQuickConstant.path;
@Test
public void biz32ContractLineBar() throws IOException {
List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
// 柱:月度签订合同数(份)
List<Double> bars = Arrays.asList(186.0, 152.0, 205.0, 228.0, 216.0, 241.0, 258.0, 236.0, 265.0, 289.0, 302.0, 328.0);
// 线:平均履约率(%)
List<Double> lines = Arrays.asList(82.6, 79.8, 83.1, 84.7, 85.9, 87.4, 88.6, 87.8, 90.2, 91.5, 93.3, 95.2);
JComboLineBarChartData chartData = JComboLineBarChartData.builder()
.title("2024年合同签订趋势分析", "月度签订合同数与平均履约率")
.barData(bars)
.lineData(lines)
.xAxisLabels(months)
.barColor(new Color(27, 94, 32))
.lineColor(new Color(191, 54, 12))
.leftAxisTitle("签订合同数(份)")
.rightAxisTitle("平均履约率(%)")
.barLegendText("月度签订合同数")
.lineLegendText("平均履约率")
.footerText("数据来源:合同管理系统 | 统计口径:自然月")
.build();
JOption option = new JOption();
option.setData(chartData);
String svg = JChartRendererFactory.renderChart(JChartType.LineBar, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/32_contract_linebar.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/32_contract_linebar.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/32_contract_linebar.txt");模板文件 · 32_contract_linebar.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
<div style="background:#EDF3ED; border:1px solid #D6DEE8; borderLeft:6px solid #1B5E20; borderRadius:4px; padding:12px; marginBottom:10px">
<h1 style="color:#1B5E20; fontSize:17pt; bold:true; marginBottom:4pt">'2024年合同签订趋势分析'</h1>
<p style="color:#5A6B7C; fontSize:9pt">'报告编号:FW-2024-012 | 统计周期:2024年1月 - 12月 | 生成日期:2025年1月10日'</p>
</div>
<div style="marginBottom:10px">
<div style="backgroundColor:#EDF3ED; padding:6px 10px; borderRadius:3px; marginBottom:6px">
<h2 style="color:#1B5E20; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'一、月度签订与履约走势'</h2>
</div>
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
<p style="fontSize:9pt; color:#6c757d; italic:true; marginTop:6pt">'图 1 月度签订合同数(柱)与平均履约率(线)双轴走势(数据来源:合同管理系统)'</p>
</div>
<div style="marginBottom:10px">
<div style="backgroundColor:#EDF3ED; padding:6px 10px; borderRadius:3px; marginBottom:6px">
<h2 style="color:#1B5E20; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'二、月度明细数据'</h2>
</div>
<table style="width:540px; fontSize:10pt">
<tr>
<th style="backgroundColor:#1B5E20; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'月份'</th>
<th style="backgroundColor:#1B5E20; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'签订合同数(份)'</th>
<th style="backgroundColor:#1B5E20; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'环比变动'</th>
<th style="backgroundColor:#1B5E20; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'平均履约率'</th>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'1月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'186'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'—'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'82.6%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'2月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'152'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'-18.3%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'79.8%'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'3月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'205'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+34.9%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'83.1%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'4月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'228'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'+11.2%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'84.7%'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'5月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'216'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'-5.3%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'85.9%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'6月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'241'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'+11.6%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'87.4%'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'7月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'258'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+7.1%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'88.6%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'8月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'236'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'-8.5%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'87.8%'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'9月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'265'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+12.3%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'90.2%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'10月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'289'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'+9.1%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'91.5%'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #DCE3EC">'11月'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'302'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+4.5%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'93.3%'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'12月'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'328'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'+8.6%'</td>
<td style="padding:5px; textAlign:right; backgroundColor:#EDF3ED; border:1px solid #DCE3EC">'95.2%'</td>
</tr>
</table>
<p style="fontSize:8pt; color:#8a939b; marginTop:4pt">'注:全年累计签订 2,906 份,平均履约率 87.5%;12月单月 328 份创年内新高。'</p>
</div>
<div style="marginBottom:10px">
<div style="backgroundColor:#EDF3ED; padding:6px 10px; borderRadius:3px; marginBottom:6px">
<h2 style="color:#1B5E20; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'三、履约质量结论'</h2>
</div>
<list style="listStyleType:none; paddingLeft:0; fontSize:10pt">
<li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:4px">'签订量前低后高:上半年 1,228 份、下半年 1,678 份,四季度合计 919 份、占全年 31.6%。'</li>
<li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:4px">'履约率稳步提升:由 1月 82.6% 升至 12月 95.2%,全年均值 87.5%,合同全生命周期管理成效显现。'</li>
<li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:4px">'2月为全年低点:签订 152 份、履约率 79.8%,受春节假期与合同评审周期叠加影响,3月起同步回升。'</li>
<li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:4px">'建议:四季度密集签订的合同将在未来 3-6 个月集中进入履约节点,需提前排布履约跟踪与法务资源。'</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:8pt">'密级:内部'</p>
</div>
<div style="width:180px; textAlignment:center">
<p style="color:#6c757d; fontSize:8pt">'生成时间:2025年1月10日'</p>
</div>
<div style="width:180px; textAlignment:right">
<p style="color:#6c757d; fontSize:8pt">'批准人:法务总监'</p>
</div>
</div>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。