跳转到内容

Java 生成「项目交付量与按时交付率趋势(项目管理)」PDF 示例(LineBar)

JQuick-PdfBiz Demo

LineBar1 页 A4 · 332 KB

1-12 月双轴混合图:柱体为月度交付项目数(左轴),橙色折线为按时交付率百分比(右轴),数据点逐月标注,图下总结交付量与交付率双双走高。

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

项目交付量与按时交付率趋势(项目管理) — LineBar
渲染效果

输出规格

PDF 页数
1
图表类型
LineBar
模板行数
36
源码行数
94

Java 源码 · JQuickBiz62LineBarTest.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 62:项目交付量与按时交付率趋势(项目管理)
 *
 * <p>业务场景:项目管理办公室(PMO)按月统计交付项目数与按时交付率,
 * 柱表示月度交付项目数(左轴),线表示按时交付率(右轴),用于过程管控。</p>
 * <p>实际图表类型:{@link JChartType#LineBar} 折线柱状混合图(双 Y 轴)。</p>
 *
 * <p>模板:{@code report/biz/62_linebar.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\62_linebar.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt 宽的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz62LineBarTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz62LineBar() throws IOException {
        List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月",
                "7月", "8月", "9月", "10月", "11月", "12月");
        List<Double> bars = Arrays.asList(18.0, 15.0, 22.0, 20.0, 26.0, 24.0, 30.0, 28.0, 33.0, 36.0, 35.0, 42.0);
        List<Double> lines = Arrays.asList(82.0, 80.0, 84.0, 83.0, 86.0, 85.0, 88.0, 87.0, 90.0, 91.0, 92.0, 95.0);

        JComboLineBarChartData chartData = JComboLineBarChartData.builder()
                .title("项目交付量与按时交付率趋势", "月度交付项目数与按时交付率")
                .barData(bars)
                .lineData(lines)
                .xAxisLabels(months)
                .barColor(new Color(13, 71, 161))
                .lineColor(new Color(239, 108, 0))
                .leftAxisTitle("交付项目数(个)")
                .rightAxisTitle("按时交付率(%)")
                .barLegendText("月度交付项目数")
                .lineLegendText("按时交付率")
                .footerText("数据来源:PMO 项目管理系统")
                .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/62_linebar.pdf");
        JPdfConfig config = new JPdfConfig();
        config.getLayoutConfig().setFlexLayout(true);
        JQuickPdfFactory factory = new JQuickPdfFactory(config);
        factory.bind("svg", svg);
        byte[] bytes = factory.executeResource("report/biz/62_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/62_linebar.txt");

模板文件 · 62_linebar.txt

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

<pdf>
<body>
  // 62 折线柱状混合图 Demo:柱表示绝对值、线表示百分比或速率(双 Y 轴)
  <div style="background:#0D47A1; 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:#BBDEFB; fontSize:9">'柱:月度交付项目数(左轴) 线:按时交付率(右轴)'</p>
      </div>
      <div style="width:220px; textAlignment:right; borderLeft:1px solid #64B5F6; paddingLeft:10px">
        <p style="color:white; fontSize:9">'统计周期:2024 年 1-12 月'</p>
        <p style="color:#BBDEFB; fontSize:9">'业务范围:全部项目'</p>
        <p style="color:#BBDEFB; fontSize:9">'编制:项目管理办公室'</p>
      </div>
    </div>
  </div>

  <div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #0D47A1">
    <h2 style="color:#0D47A1; 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:#0D47A1">'交付量逐季上升:'</span>'下半年项目交付量明显高于上半年,全年呈上升态势。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#0D47A1">'交付率稳中有升:'</span>'按时交付率由 82% 提升至 95%,过程管控成效明显。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#0D47A1">'建议:'</span>'Q4 交付量峰值下交付率仍维持高位,可总结经验形成标准化交付流程。'</li>
  </list>

  <lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
  <p style="color:#6c757d; fontSize:8; textAlignment:center; marginTop:6px">'生成时间:2025年1月6日 | 批准人:PMO 总监'</p>
</body>
</pdf>

同分组示例

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