跳转到内容

Java 生成「项目状态分布(政府投资项目监管平台)」PDF 示例(TREEMAP)

JQuick-PdfBiz Demo

TREEMAP2 页 A4 · 127 KB

按在建、竣工、暂停等状态统计项目数量与投资额,用矩形树图呈现面积占比。

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

项目状态分布(政府投资项目监管平台) — TREEMAP
渲染效果

输出规格

PDF 页数
2
图表类型
TREEMAP
模板行数
137
源码行数
99

Java 源码 · JQuickBiz37ProjectStatusTreemapTest.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.treemap.JTreeMapNode;
import com.github.paohaijiao.treemap.TreeMapOption;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 业务场景 Demo 37:项目状态分布(政府投资项目监管平台)
 *
 * <p>业务场景:项目监管平台按"在建 / 已完工 / 前期 / 暂停"四大状态对政府投资项目分组,
 * 以矩形面积表示项目投资额,用于把握投资进度与资金沉淀情况。</p>
 * <p>实际图表类型:{@link JChartType#TREEMAP} 矩形树图(矩形面积正比于投资额,同色系为一组状态)。</p>
 *
 * <p>模板:{@code report/biz/37_project_status_treemap.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\37_project_status_treemap.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt × 420pt(宽 × 高)的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz37ProjectStatusTreemapTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz37ProjectStatusTreemap() throws IOException {
        // 根节点 value 为四大状态投资额之和(单位:亿元),子节点面积即代表各自投资额
        JTreeMapNode root = new JTreeMapNode("项目总览", 1000);

        JTreeMapNode building = new JTreeMapNode("在建", 420);
        building.addChild(new JTreeMapNode("市政工程", 240));
        building.addChild(new JTreeMapNode("交通工程", 180));
        root.addChild(building);

        JTreeMapNode completed = new JTreeMapNode("已完工", 300);
        completed.addChild(new JTreeMapNode("房建工程", 190));
        completed.addChild(new JTreeMapNode("水利工程", 110));
        root.addChild(completed);

        JTreeMapNode preparatory = new JTreeMapNode("前期", 180);
        preparatory.addChild(new JTreeMapNode("规划设计", 110));
        preparatory.addChild(new JTreeMapNode("征地拆迁", 70));
        root.addChild(preparatory);

        JTreeMapNode suspended = new JTreeMapNode("暂停", 100);
        suspended.addChild(new JTreeMapNode("资金待落实", 60));
        suspended.addChild(new JTreeMapNode("手续待补办", 40));
        root.addChild(suspended);

        TreeMapOption treemapOption = new TreeMapOption();
        treemapOption.setRoot(root);
        JOption option = new JOption();
        option.setTreemapOption(treemapOption);
        option.title("政府投资项目状态分布");
        String svg = JChartRendererFactory.renderChart(JChartType.TREEMAP, option);

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

模板文件 · 37_project_status_treemap.txt

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

<pdf>
<body>
  <div style="marginBottom:10px; borderBottom:1px solid #B9C2CC; paddingBottom:8px">
    <h1 style="color:#37474F; fontSize:17; bold:true; marginBottom:3px">'政府投资项目状态分布'</h1>
    <p style="color:#6c757d; fontSize:9">'统计周期:截至2024年9月30日 | 数据来源:政府投资项目监管平台 | 计量单位:亿元'</p>
  </div>

  <div style="backgroundColor:#ECEFF1; padding:6px 10px; borderRadius:3px; marginBottom:6px">
    <h2 style="color:#37474F; fontSize:13; bold:true; marginTop:0; marginBottom:0">'一、投资额构成总览'</h2>
  </div>
  <div style="background:#ECEFF1; borderLeft:4px solid #37474F; padding:10px; borderRadius:3px; marginBottom:10px">
    <p style="fontSize:10; color:#333333; italic:true">'一句话核心结论:全口径项目投资额 1,000 亿元中,在建项目占 42.0%,已完工项目占 30.0%,两者合计超过七成,投资总体处于实物工作量形成的关键期。'</p>
  </div>
  <div style="marginBottom:12px">
    <div style="display:flex; justifyContent:center">
      <svg style="width:700pt">${svg}</svg>
    </div>
    <p style="fontSize:9; color:#6c757d; italic:true; marginTop:4px">'图 1  各状态项目投资额矩形树图(矩形面积正比于投资额,同色系为一组状态)'</p>
  </div>

  <div style="backgroundColor:#ECEFF1; padding:6px 10px; borderRadius:3px; marginBottom:6px">
    <h2 style="color:#37474F; fontSize:13; bold:true; marginTop:0; marginBottom:0">'二、分状态投资明细'</h2>
  </div>
  <div style="marginBottom:12px">
    <table style="width:540px; fontSize:10">
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'状态'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'子项'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'投资额(亿元)'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'占总投资比'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'在建'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'市政工程'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'240'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'24.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'在建'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'交通工程'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'180'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'18.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'在建小计'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'在建合计'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'420'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'42.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'已完工'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'房建工程'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'190'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'19.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'已完工'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'水利工程'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'110'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'11.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'已完工小计'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'已完工合计'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'300'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'30.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'前期'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'规划设计'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'110'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'11.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'前期'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'征地拆迁'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'70'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'7.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'前期小计'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'前期合计'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'180'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'18.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'暂停'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'资金待落实'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'60'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'6.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; borderBottom:1px solid #E3E8EE">'暂停'</td>
        <td style="width:170px; padding:5px; borderBottom:1px solid #E3E8EE">'手续待补办'</td>
        <td style="width:140px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'40'</td>
        <td style="width:120px; padding:5px; textAlign:right; borderBottom:1px solid #E3E8EE">'4.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'暂停小计'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'暂停合计'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'100'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'10.0%'</td>
      </tr>
      <tr>
        <td style="width:110px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'合计'</td>
        <td style="width:170px; padding:5px; bold:true; borderBottom:1px solid #E3E8EE">'八大类子项合计'</td>
        <td style="width:140px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'1,000'</td>
        <td style="width:120px; padding:5px; bold:true; textAlign:right; borderBottom:1px solid #E3E8EE">'100.0%'</td>
      </tr>
    </table>
  </div>

  <div style="backgroundColor:#ECEFF1; padding:6px 10px; borderRadius:3px; marginBottom:6px">
    <h2 style="color:#37474F; fontSize:13; bold:true; marginTop:0; marginBottom:0">'三、项目监管建议'</h2>
  </div>
  <list style="list-style-type:none; padding-left:0; fontSize:10">
    <li style="marginBottom:6px; padding:8px; backgroundColor:#ECEFF1; border-left:3px solid #37474F">'【投资结构】在建 420 亿元占 42.0%,市政工程与交通工程分别 240 亿元、180 亿元,是形成实物工作量的主力。'</li>
    <li style="marginBottom:6px; padding:8px; backgroundColor:#ECEFF1; border-left:3px solid #37474F">'【竣工结转】已完工 300 亿元占 30.0%,房建工程 190 亿元为主,建议加快竣工结算与资产移交。'</li>
    <li style="marginBottom:6px; padding:8px; backgroundColor:#ECEFF1; border-left:3px solid #37474F">'【前期推进】前期 180 亿元占 18.0%,其中征地拆迁 70 亿元,需重点打通用地手续办理环节。'</li>
    <li style="marginBottom:6px; padding:8px; backgroundColor:#ECEFF1; border-left:3px solid #37474F">'【资金沉淀】暂停 100 亿元占 10.0%,资金待落实 60 亿元为主要成因,建议清单化开展资金撮合与手续补办。'</li>
  </list>

  <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">'生成时间:2024年10月12日'</p>
    </div>
    <div style="width:180px; textAlignment:right">
      <p style="color:#6c757d; fontSize:8">'批准人:项目监管处'</p>
    </div>
  </div>
</body>
</pdf>

同分组示例

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