跳转到内容

Java 生成「商户行业分布(收单 / 商户运营平台)」PDF 示例(SUNBURST)

JQuick-PdfBiz Demo

SUNBURST1 页 A4 · 131 KB

统计各类行业商户的数量占比,用旭日图呈现行业与子行业的两级结构。

下载 PDF 文件查看模板原始文件返回全部示例

商户行业分布(收单 / 商户运营平台) — SUNBURST
渲染效果

输出规格

PDF 页数
1
图表类型
SUNBURST
模板行数
96
源码行数
103

Java 源码 · JQuickBiz31MerchantIndustrySunburstTest.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.data.JSunburstData;
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.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 业务场景 Demo 31:商户行业分布(收单/商户运营平台)
 *
 * <p>业务场景:收单业务按行业大类汇总存量商户,并下钻至细分子行业,用于商户经营结构分析与
 * 行业拓展策略制定。</p>
 * <p>实际图表类型:{@link JChartType#SUNBURST} 旭日图。选题原拟用环形图,但 Demo 20 已使用
 * {@link JChartType#Circle} 环形图,为避免图表类型重复,本 Demo 改用旭日图——旭日图本质上是
 * 分层展开的同心环,既能呈现一级行业的占比,又能通过第二层环直观展示子行业构成。</p>
 *
 * <p>模板:{@code report/biz/31_merchant_industry_sunburst.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\31_merchant_industry_sunburst.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 640pt × 460pt(宽 × 高)的方形比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz31MerchantIndustrySunburstTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz31MerchantIndustrySunburst() throws IOException {
        // 根节点:商户总览;value 为该层相对父节点的比例(根节点固定为 1.0)
        JSunburstData root = new JSunburstData("商户总览", 1.0);
        // 一级行业 5 个,比例之和为 1.0;每个一级行业再下钻 2 个细分子行业(比例之和为 1.0)
        JSunburstData retail = new JSunburstData("批发零售", 0.34);
        retail.addChild(new JSunburstData("食品饮料", 0.55));
        retail.addChild(new JSunburstData("日用百货", 0.45));
        root.addChild(retail);

        JSunburstData catering = new JSunburstData("住宿餐饮", 0.22);
        catering.addChild(new JSunburstData("正餐服务", 0.62));
        catering.addChild(new JSunburstData("快餐小吃", 0.38));
        root.addChild(catering);

        JSunburstData infoTech = new JSunburstData("信息技术", 0.18);
        infoTech.addChild(new JSunburstData("软件开发", 0.58));
        infoTech.addChild(new JSunburstData("数据服务", 0.42));
        root.addChild(infoTech);

        JSunburstData manufacturing = new JSunburstData("制造业", 0.15);
        manufacturing.addChild(new JSunburstData("装备制造", 0.66));
        manufacturing.addChild(new JSunburstData("消费品制造", 0.34));
        root.addChild(manufacturing);

        JSunburstData others = new JSunburstData("其他", 0.11);
        others.addChild(new JSunburstData("居民服务", 0.52));
        others.addChild(new JSunburstData("文化体育", 0.48));
        root.addChild(others);

        JOption option = new JOption();
        option.setSunburstData(root);
        option.title("商户行业分布");
        String svg = JChartRendererFactory.renderChart(JChartType.SUNBURST, option);

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/31_merchant_industry_sunburst.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/31_merchant_industry_sunburst.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/31_merchant_industry_sunburst.txt");

模板文件 · 31_merchant_industry_sunburst.txt

类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。

<pdf>
<body>
  <div style="marginBottom:10px; borderBottom:1px solid #B9C2CC; paddingBottom:8px">
    <h1 style="color:#243B53; fontSize:17pt; bold:true; marginBottom:3pt">'商户行业分布(旭日图)'</h1>
    <p style="color:#6c757d; fontSize:9pt">'统计周期:2025年上半年 | 数据来源:收单商户管理系统 | 样本:存量商户 120,000 户'</p>
  </div>

  <div style="marginBottom:10px">
    <div style="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
      <h2 style="color:#00695C; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'一、行业结构总览'</h2>
    </div>
    <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:#00695C; fontSize:12pt; bold:true; marginBottom:6pt">'指标说明'</h3>
        <list style="listStyleType:disc; paddingLeft:14px; fontSize:9pt">
          <li style="marginBottom:5px">'内环为 5 个一级行业,外环为 10 个细分子行业。'</li>
          <li style="marginBottom:5px">'批发零售占比 34.0%,为第一大类商户行业。'</li>
          <li style="marginBottom:5px">'信息技术与制造业合计占比 33.0%。'</li>
          <li style="marginBottom:5px">'子行业构成与环比数据见下方明细表。'</li>
        </list>
      </div>
    </div>
    <p style="fontSize:9pt; color:#6c757d; italic:true">'图 1  商户行业分布旭日图(旭日图即分层同心环:内环为一级行业、外环为细分子行业)'</p>
  </div>

  <div style="marginBottom:10px">
    <div style="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
      <h2 style="color:#00695C; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'二、一级行业与环比明细'</h2>
    </div>
    <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>
        <th style="backgroundColor:#00695C; 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">'40,800'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'34.0%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+2.6%'</td>
        <td style="padding:5px; textAlign:center; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'达标'</td>
      </tr>
      <tr>
        <td style="padding:5px; border:1px solid #DCE3EC">'住宿餐饮'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'26,400'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'22.0%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+4.1%'</td>
        <td style="padding:5px; textAlign:center; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'达标'</td>
      </tr>
      <tr>
        <td style="padding:5px; border:1px solid #DCE3EC">'信息技术'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'21,600'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'18.0%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+6.8%'</td>
        <td style="padding:5px; textAlign:center; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'达标'</td>
      </tr>
      <tr>
        <td style="padding:5px; border:1px solid #DCE3EC">'制造业'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'18,000'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'15.0%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+1.2%'</td>
        <td style="padding:5px; textAlign:center; color:#BF360C; bold:true; border:1px solid #DCE3EC">'预警'</td>
      </tr>
      <tr>
        <td style="padding:5px; border:1px solid #DCE3EC">'其他'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'13,200'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'11.0%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'+0.5%'</td>
        <td style="padding:5px; textAlign:center; color:#BF360C; bold:true; border:1px solid #DCE3EC">'预警'</td>
      </tr>
    </table>
    <p style="fontSize:8pt; color:#8a939b; marginTop:4pt">'注:占比合计 100.0%;结构评价以环比增速 2.0% 为达标线。'</p>
  </div>

  <div style="marginBottom:10px">
    <div style="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
      <h2 style="color:#00695C; fontSize:13pt; bold:true; marginTop:0; marginBottom:0">'三、结构特征与拓展建议'</h2>
    </div>
    <list style="listStyleType:disc; paddingLeft:16px; fontSize:10pt">
      <li style="marginBottom:5px"><span style="bold:true; color:#00695C">'行业集中度:'</span>'批发零售与住宿餐饮合计占比 56.0%,构成存量商户的基本盘。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#00695C">'增速领跑:'</span>'信息技术占比 18.0%、环比 +6.8%,为五大行业中增速最快的一类。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#00695C">'子行业构成:'</span>'批发零售中食品饮料占 55%,住宿餐饮中正餐服务占 62%,均为各自主导子行业。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#00695C">'拓展建议:'</span>'制造业与其他行业环比增速低于 1.5%,建议将拓展资源向信息技术类商户倾斜。'</li>
    </list>
  </div>

  <lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
  <p style="color:#6c757d; fontSize:8pt; marginTop:8px">'数据来源:收单核心系统 / 商户运营平台 | 统计截止:2025年6月30日 | 生成时间:2025年7月5日'</p>
</body>
</pdf>

同分组示例

本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。