Java 生成「分区域季度销售四系列对比(销售运营)」PDF 示例(MultipleBar)
MultipleBar1 页 A4 · 223 KB
华东 / 华北 / 华南 / 西部四大区域 2024 年 10-12 月销售额分组柱状图,柱体标注数值,底部四色系列图例,图下给出华东领先、西部增速等月度结论。

输出规格
- PDF 页数
- 1
- 图表类型
- MultipleBar
- 模板行数
- 36
- 源码行数
- 104
Java 源码 · JQuickBiz59FourBarTest.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.JMultiBarChartData;
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;
/**
* 业务场景 Demo 59:分区域季度销售四系列对比(销售运营)
*
* <p>业务场景:销售运营部门对比华东、华北、华南、西部四大区域在 2024 年四季度
* 各月的销售额,采用四系列分组柱状图并列展示。</p>
* <p>实际图表类型:{@link JChartType#MultipleBar} 多系列分组柱状图。</p>
*
* <p>模板:{@code report/biz/59_fourBar.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\59_fourBar.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt 宽的横版比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz59FourBarTest {
public static final String path = JQuickConstant.path;
@Test
public void biz59FourBar() throws IOException {
JMultiBarChartData chartData = new JMultiBarChartData();
chartData.setTitleText("分区域季度销售四系列对比");
chartData.setSubtitleText("华东 / 华北 / 华南 / 西部 2024 年 10-12 月销售额(万元)");
chartData.setXAxisLabels(Arrays.asList("10月", "11月", "12月"));
chartData.setXAxisTitle("月份");
chartData.setYAxisTitle("销售额(万元)");
chartData.setFooterText("数据来源:销售运营系统 | 统计口径:自然月");
JMultiBarChartData.BarData east = new JMultiBarChartData.BarData();
east.setLegendText("华东");
east.setBarColor(new Color(230, 81, 0));
east.setValues(Arrays.asList(780.0, 830.0, 910.0));
JMultiBarChartData.BarData north = new JMultiBarChartData.BarData();
north.setLegendText("华北");
north.setBarColor(new Color(251, 140, 0));
north.setValues(Arrays.asList(560.0, 590.0, 570.0));
JMultiBarChartData.BarData south = new JMultiBarChartData.BarData();
south.setLegendText("华南");
south.setBarColor(new Color(255, 183, 77));
south.setValues(Arrays.asList(620.0, 660.0, 720.0));
JMultiBarChartData.BarData west = new JMultiBarChartData.BarData();
west.setLegendText("西部");
west.setBarColor(new Color(255, 213, 79));
west.setValues(Arrays.asList(310.0, 380.0, 460.0));
chartData.setBarDataList(Arrays.asList(east, north, south, west));
JOption option = new JOption();
option.setData(chartData);
String svg = JChartRendererFactory.renderChart(JChartType.MultipleBar, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/59_fourBar.pdf");
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource("report/biz/59_fourBar.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/59_fourBar.txt");模板文件 · 59_fourBar.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
// 59 四系列柱状图 Demo:同一维度下并列展示四个系列的数值对比
<div style="background:#E65100; padding:12px; borderRadius:4px; marginBottom:10px; keepTogether:true">
<div style="display:flex; justifyContent:space-between">
<div style="width:320px">
<h1 style="color:white; fontSize:17; bold:true; marginBottom:3px">'分区域季度销售四系列对比'</h1>
<p style="color:#FFCCBC; fontSize:9">'华东 / 华北 / 华南 / 西部 四大区域,2024 年四季度各月销售额'</p>
</div>
<div style="width:220px; textAlignment:right; borderLeft:1px solid #FFAB91; paddingLeft:10px">
<p style="color:white; fontSize:9">'统计周期:2024 年 10-12 月'</p>
<p style="color:#FFCCBC; fontSize:9">'单位:万元'</p>
<p style="color:#FFCCBC; fontSize:9">'编制:销售运营部'</p>
</div>
</div>
</div>
<div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #E65100">
<h2 style="color:#E65100; marginBottom:6px; fontSize:13; bold:true">'一、各区域月度销售额对比'</h2>
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
<p style="fontSize:9; color:#6c757d; italic:true; marginTop:6px">'图 1 四系列分组柱状图:每组四根柱分别代表华东、华北、华南、西部销售额'</p>
</div>
<list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
<li style="marginBottom:6px"><span style="bold:true; color:#E65100">'华东持续领先:'</span>'连续三个月销售额居首,12 月突破 900 万。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#E65100">'西部增速最快:'</span>'10 月至 12 月环比增长显著,市场拓展初见成效。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#E65100">'建议:'</span>'华北区域 12 月略有回落,需关注重点客户续约情况。'</li>
</list>
<lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
<p style="color:#6c757d; fontSize:8; textAlignment:center; marginTop:6px">'生成时间:2025年1月6日 | 批准人:销售总监'</p>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。