跳转到内容

Java 生成「人员在岗率对比(政务 / 人事管理)」PDF 示例(MutipleHorizontalBar)

JQuick-PdfBiz Demo

MutipleHorizontalBar2 页 A4 · 125 KB

按部门对比在岗率,用多重横向条形图呈现各部门的人员出勤差异。

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

人员在岗率对比(政务 / 人事管理) — MutipleHorizontalBar
渲染效果

输出规格

PDF 页数
2
图表类型
MutipleHorizontalBar
模板行数
85
源码行数
91

Java 源码 · JQuickBiz35StaffAttendanceTest.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.JHorizontalMultiBarChartData;
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 35:人员在岗率对比(政务/人事管理)
 *
 * <p>业务场景:某执法系统各下属单位上报当日应到人数与实到人数,人事部门据此统计在岗率,用于日常勤务调度与值守保障。</p>
 * <p>实际图表类型:{@link JChartType#MutipleHorizontalBar} 多重横向条形图,每个单位并列「应到人数 / 实到人数」两个系列。</p>
 *
 * <p>模板:{@code report/biz/35_staff_attendance.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\35_staff_attendance.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt 宽的横版比例设计;模板中 {@code <svg>} 只声明宽度、不声明高度,
 * 宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz35StaffAttendanceTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz35StaffAttendance() throws IOException {
        JHorizontalMultiBarChartData chartData = new JHorizontalMultiBarChartData();
        chartData.setTitleText("各下属单位人员在岗率对比");
        chartData.setSubtitleText("应到人数与实到人数(2024年9月,单位:人)");
        chartData.setXAxisTitle("在岗率(%)");
        chartData.setValueWithPercent(false);
        chartData.setShowDataLabels(true);
        chartData.setLegendAtTop(true);
        chartData.setGroupSpacingRatio(0.15);
        chartData.setBarSpacingRatio(0.2);
        chartData.addCategory("第一分局");
        chartData.addCategory("第二分局");
        chartData.addCategory("第三分局");
        chartData.addCategory("第四分局");
        chartData.addCategory("第五分局");
        // 两个系列分别表示应到人数与实到人数,颜色沿用政务系统低饱和主色
        chartData.addSeries("应到人数", Arrays.asList(186.0, 152.0, 134.0, 118.0, 96.0), new Color(0, 105, 92));
        chartData.addSeries("实到人数", Arrays.asList(178.0, 141.0, 129.0, 106.0, 91.0), new Color(77, 150, 140));
        JOption option = new JOption();
        option.setData(chartData);

        String svg = JChartRendererFactory.renderChart(JChartType.MutipleHorizontalBar, option);

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

模板文件 · 35_staff_attendance.txt

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

<pdf>
<body>
  <div style="marginBottom:12px; borderBottom:4px solid #00695C; paddingBottom:8px">
    <h1 style="color:#00695C; fontSize:19; bold:true; marginBottom:4px">'下属单位人员在岗率通报'</h1>
    <div style="display:flex; justifyContent:space-between">
      <div style="width:290px">
        <p style="color:#6c757d; fontSize:9">'副标题:各下属单位应到人数、实到人数与在岗率对比(单位:人)'</p>
      </div>
      <div style="width:250px; textAlignment:right">
        <p style="color:#6c757d; fontSize:9">'统计周期:2024年9月1日 - 2024年9月30日 | 编制单位:市局人事教育处'</p>
      </div>
    </div>
  </div>

  <div style="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
    <h2 style="color:#00695C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'一、各单位在岗情况'</h2>
  </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="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
    <h2 style="color:#00695C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'二、关键指标明细'</h2>
  </div>
  <div style="marginBottom:12px">
    <table style="width:540px; fontSize:10">
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'应到人数合计(人)'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'686'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'实到人数合计(人)'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'645'</td>
      </tr>
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'综合在岗率'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'94.0%'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'较上月变动'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'+1.2 个百分点'</td>
      </tr>
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'在岗率最高单位'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'第三分局'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'最高在岗率'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'96.3%'</td>
      </tr>
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'在岗率最低单位'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'第四分局'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'最低在岗率'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'89.8%'</td>
      </tr>
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'缺岗人数合计(人)'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'41'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'缺岗率'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'6.0%'</td>
      </tr>
      <tr>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'达标单位数(≥94%)'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'3'</td>
        <td style="width:150px; backgroundColor:#E6F2F0; padding:5px; border:1px solid #DCE3EC">'预警单位数'</td>
        <td style="width:120px; textAlign:right; padding:5px; border:1px solid #DCE3EC">'2'</td>
      </tr>
    </table>
  </div>

  <div style="borderLeft:4px solid #00695C; paddingLeft:8px; marginBottom:6px">
    <h2 style="color:#00695C; fontSize:13; bold:true; marginTop:0; marginBottom:0">'三、分析结论'</h2>
  </div>
  <list style="paddingLeft:18px; fontSize:10" listType="ordered">
    <li style="marginBottom:6px">'综合在岗率 94.0%:五个下属单位应到 686 人、实到 645 人,缺岗 41 人,整体在岗率 94.0%,较上月提升 1.2 个百分点。'</li>
    <li style="marginBottom:6px">'第三分局在岗率最高(96.3%),第一分局 95.7%、第五分局 94.8% 均高于平均水平,勤务安排总体均衡。'</li>
    <li style="marginBottom:6px">'第二分局(92.8%)与第四分局(89.8%)低于 94% 达标线,第四分局主要受 2 名业务骨干借调、1 人产假影响。'</li>
    <li style="marginBottom:6px">'建议:对第四分局实行跨分局警力调剂,并将夜间值班岗位纳入下一轮补员计划,力争下月在岗率回升至 95% 以上。'</li>
  </list>

  <div style="textAlignment:center; marginTop:10px; paddingTop:10px; borderTop:1px solid #dee2e6; color:#6c757d; fontSize:8">
    <p>'数据来源:勤务管理系统考勤模块 | 生成时间:2024年10月8日'</p>
    <p>'内部资料 · 未经许可请勿外传'</p>
  </div>
</body>
</pdf>

同分组示例

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