跳转到内容

Java 生成「各产品线年度预算与实际支出对比(财务管理)」PDF 示例(MutipleHorizontalBar)

JQuick-PdfBiz Demo

MutipleHorizontalBar1 页 A4 · 223 KB

五条产品线的横向三系列条形图:预算、实际支出、偏差率(×10)三行并列,条上标注金额,超支产品线一目了然,图下给出预算复核与基数调整建议。

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

各产品线年度预算与实际支出对比(财务管理) — MutipleHorizontalBar
渲染效果

输出规格

PDF 页数
1
图表类型
MutipleHorizontalBar
模板行数
36
源码行数
102

Java 源码 · JQuickBiz64MhBarChartTest.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 64:各产品线年度预算与实际支出对比(财务管理)
 *
 * <p>业务场景:财务管理部门对各产品线的预算、实际支出、偏差率进行横向分组对比,
 * 便于快速识别预算执行偏差。</p>
 * <p>实际图表类型:{@link JChartType#MutipleHorizontalBar} 多重横向柱状图。</p>
 *
 * <p>模板:{@code report/biz/64_mhBarChart.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\64_mhBarChart.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt 宽的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz64MhBarChartTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz64MhBarChart() throws IOException {
        JHorizontalMultiBarChartData chartData = new JHorizontalMultiBarChartData();
        chartData.setTitleText("各产品线年度预算与实际支出对比");
        chartData.setSubtitleText("单位:万元 | 2024 年度");
        chartData.setXAxisTitle("金额(万元)");
        chartData.setValueWithPercent(false);
        chartData.setShowDataLabels(true);
        chartData.setLegendAtTop(true);
        chartData.setGroupSpacingRatio(0.15);
        chartData.setBarSpacingRatio(0.2);
        chartData.addCategory("产品线 A");
        chartData.addCategory("产品线 B");
        chartData.addCategory("产品线 C");
        chartData.addCategory("产品线 D");
        chartData.addCategory("产品线 E");

        // 预算
        chartData.addSeries("预算",
                Arrays.asList(1200.0, 980.0, 750.0, 520.0, 300.0),
                new Color(49, 27, 146));
        // 实际
        chartData.addSeries("实际",
                Arrays.asList(1180.0, 960.0, 885.0, 510.0, 290.0),
                new Color(103, 80, 174));
        // 偏差率(放大 10 倍以便与金额同量级展示,仅作视觉对比)
        chartData.addSeries("偏差率(×10)",
                Arrays.asList(-16.0, -20.0, 135.0, -10.0, -10.0),
                new Color(149, 117, 205));

        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/64_mhBarChart.pdf");
        JPdfConfig config = new JPdfConfig();
        config.getLayoutConfig().setFlexLayout(true);
        JQuickPdfFactory factory = new JQuickPdfFactory(config);
        factory.bind("svg", svg);
        byte[] bytes = factory.executeResource("report/biz/64_mhBarChart.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/64_mhBarChart.txt");

模板文件 · 64_mhBarChart.txt

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

<pdf>
<body>
  // 64 多重横向柱状图 Demo:多组系列以横向条形并列展示,适合类别较多的对比
  <div style="background:#311B92; 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:#D1C4E9; fontSize:9">'横向分组条形图:预算 / 实际 / 偏差 三组数据并列'</p>
      </div>
      <div style="width:220px; textAlignment:right; borderLeft:1px solid #9575CD; paddingLeft:10px">
        <p style="color:white; fontSize:9">'统计年度:2024 年'</p>
        <p style="color:#D1C4E9; fontSize:9">'单位:万元'</p>
        <p style="color:#D1C4E9; fontSize:9">'编制:财务管理部'</p>
      </div>
    </div>
  </div>

  <div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #311B92">
    <h2 style="color:#311B92; 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:#311B92">'产品线 A 执行良好:'</span>'实际支出与预算基本持平,偏差率控制在 5% 以内。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#311B92">'产品线 C 超支明显:'</span>'实际支出超出预算 18%,需专项分析超支原因。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#311B92">'建议:'</span>'对超支产品线启动预算复核,下年度适度调整预算基数。'</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 真实运行产出,可自由复制到你的项目中使用。