DEV Community

paohaijiao
paohaijiao

Posted on

Horizontal Bar Chart of Annual Budget Execution

Horizontal Bar Chart of Annual Budget Execution

jquick-pdf by example — demo #24 of 184. Every article in this series ships one complete, runnable demo: a template file and a Java class. No snippets, no omissions.

Budgets are easier to compare as horizontal bars because department names are long.

This document belongs to the Business chart series set.

What you'll build

  • A PDF written to D:/test/biz/24_budget_progress.pdf
  • A template file (report/biz/24_budget_progress.txt) with 1 bound placeholder
  • A detail grid of 7 rows × 5 columns, laid out with width:100% so it fills the text area
  • One self-contained Java class (JQuickBiz24BudgetProgressTest) that binds the data and renders the file

Bound placeholders

${svg}
Enter fullscreen mode Exit fullscreen mode

Prerequisites

Java 8+ and one dependency — jquick-pdf is built on Apache PDFBox, not iText:

<dependency>
    <groupId>io.github.paohaijiao</groupId>
    <artifactId>jquick-pdfx</artifactId>
    <version>4.2.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Coordinates: io.github.paohaijiao:jquick-pdfx:4.2.0.

Step 1 — the template file

JPdfX is jquick-pdf's HTML-like template language. It lives in its own file (src/test/resources/report/biz/24_budget_progress.txt) instead of a Java string, so designers can edit it without touching code.

<pdf>
<body>
  <div style="textAlignment:center; marginBottom:12px; padding:14px; background:#1F4E79; borderRadius:4px; keepTogether:true">
    <h1 style="textAlignment:center; color:white; marginBottom:6px; fontSize:18; bold:true">'2024年度各部门预算执行进度'</h1>
    <p style="textAlignment:center; color:white; fontSize:10; margin:2px">'预算下达额与已执行额对比(截至2024年9月30日,单位:万元)'</p>
    <p style="textAlignment:center; color:#CFE0EF; fontSize:10; margin:2px">'编制单位:市财政局预算管理科 | 报送对象:市政府常务会议'</p>
  </div>

  <div style="marginBottom:12px">
    <div style="backgroundColor:#E8F0F7; padding:6px 10px; borderRadius:3px; marginBottom:8px">
      <h2 style="color:#1F4E79; fontSize:13; bold:true; marginTop:0; marginBottom:0">'一、各部门预算执行总体情况'</h2>
    </div>
    <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>

  <div style="marginBottom:12px">
    <div style="backgroundColor:#E8F0F7; padding:6px 10px; borderRadius:3px; marginBottom:8px">
      <h2 style="color:#1F4E79; fontSize:13; bold:true; marginTop:0; marginBottom:0">'二、分部门预算执行明细'</h2>
    </div>
    <table style="width:540px; fontSize:10">
      <tr>
        <th style="backgroundColor:#1F4E79; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'部门'</th>
        <th style="backgroundColor:#1F4E79; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'预算下达(万元)'</th>
        <th style="backgroundColor:#1F4E79; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'已执行(万元)'</th>
        <th style="backgroundColor:#1F4E79; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'执行率'</th>
        <th style="backgroundColor:#1F4E79; 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">'12,800'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'11,260'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'87.97%'</td>
        <td style="padding:5px; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'进度达标'</td>
      </tr>
      <tr>
        <td style="padding:5px; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'卫健委'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'9,600'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'8,120'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'84.58%'</td>
        <td style="padding:5px; backgroundColor:#E8F0F7; 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">'8,400'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'6,930'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'82.50%'</td>
        <td style="padding:5px; color:#BF360C; bold:true; border:1px solid #DCE3EC">'预警关注'</td>
      </tr>
      <tr>
        <td style="padding:5px; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'农业农村局'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'6,200'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'5,480'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#E8F0F7; border:1px solid #DCE3EC">'88.39%'</td>
        <td style="padding:5px; backgroundColor:#E8F0F7; 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">'5,100'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'4,320'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'84.71%'</td>
        <td style="padding:5px; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'正常推进'</td>
      </tr>
      <tr>
        <td style="padding:6px; backgroundColor:#E8F0F7; bold:true; border:1px solid #DCE3EC">'合计'</td>
        <td style="padding:6px; textAlign:right; backgroundColor:#E8F0F7; bold:true; border:1px solid #DCE3EC">'42,100'</td>
        <td style="padding:6px; textAlign:right; backgroundColor:#E8F0F7; bold:true; border:1px solid #DCE3EC">'36,110'</td>
        <td style="padding:6px; textAlign:right; backgroundColor:#E8F0F7; bold:true; border:1px solid #DCE3EC">'85.77%'</td>
        <td style="padding:6px; backgroundColor:#E8F0F7; color:#1B5E20; bold:true; border:1px solid #DCE3EC">'总体达标'</td>
      </tr>
    </table>
  </div>

  <div style="marginBottom:10px">
    <div style="backgroundColor:#E8F0F7; padding:6px 10px; borderRadius:3px; marginBottom:8px">
      <h2 style="color:#1F4E79; fontSize:13; bold:true; marginTop:0; marginBottom:0">'三、进度分析与下半年建议'</h2>
    </div>
    <list style="list-style-type:none; padding-left:0; fontSize:10">
      <li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:6px">'总体进度:五个部门预算下达合计 42,100 万元,已执行 36,110 万元,整体执行率 85.77%,略高于序时进度。'</li>
      <li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:6px">'领先部门:农业农村局 88.39%、教育局 87.97% 执行率居前两位,项目开工早、资金支付节奏快。'</li>
      <li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:6px">'预警部门:交通局执行率 82.50% 为五部门最低,低于整体水平 3.27 个百分点,主要受工程招标进度滞后影响。'</li>
      <li style="borderTop:1px dashed #C7C7C7; paddingTop:6px; marginBottom:6px">'下半年建议:对交通局实行项目清单周调度;对执行率已超 87% 的部门,提示避免年末突击支出,确保资金使用合规。'</li>
    </list>
  </div>

  <lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
  <p style="color:#6c757d; fontSize:8; marginTop:8px">'数据来源:市财政局预算管理一体化系统 | 生成时间:2025年10月8日 | 密级:内部'</p>
</body>
</pdf>
Enter fullscreen mode Exit fullscreen mode

Step 2 — the Java class

jquick-pdfx/src/test/java/com/github/paohaijiao/biz/chart/JQuickBiz24BudgetProgressTest.java

/*
 * 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 (goudingcheng@gmail.com)
 */
package com.github.paohaijiao.biz.chart;

import com.github.paohaijiao.JOption;
import com.github.paohaijiao.combol.JHorizontalBarChartData;
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.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 业务场景 Demo 24:年度预算执行进度仪表盘(政务/企业财务)
 *
 * <p>业务场景:市财政局预算管理科跟踪各业务部门年度预算下达额与已执行额,用于市政府常务会议预算执行进度汇报。</p>
 * <p>实际图表类型:{@link JChartType#HorizontalBar} 横向条形图。jquick-pdf 暂无进度图(进度条)渲染器,
 * 因此用「预算下达 vs 已执行」两条横向条形并列的方式替代进度图,执行率由两段条形的长度对比直观呈现。</p>
 *
 * <p>模板:{@code report/biz/24_budget_progress.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\24_budget_progress.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt 宽的横版比例设计;模板中 {@code <svg>} 只声明宽度、不声明高度,
 * 宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz24BudgetProgressTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz24BudgetProgress() throws IOException {
        JHorizontalBarChartData chartData = new JHorizontalBarChartData();
        chartData.setTitleText("2024年度各部门预算执行进度");
        chartData.setSubtitleText("预算下达额与已执行额对比(截至2024年9月30日)");
        chartData.setXAxisTitle("金额(万元)");
        chartData.setYAxisTitle("部门");
        chartData.setValueWithPercent(false);
        chartData.setShowDataLabels(true);
        chartData.addYAxisLabel("教育局");
        chartData.addYAxisLabel("卫健委");
        chartData.addYAxisLabel("交通局");
        chartData.addYAxisLabel("农业农村局");
        chartData.addYAxisLabel("民政局");
        // 两条系列分别表示预算下达额与已执行额,颜色沿用政务系统低饱和主色
        List<Double> budget = Arrays.asList(12800.0, 9600.0, 8400.0, 6200.0, 5100.0);
        List<Double> executed = Arrays.asList(11260.0, 8120.0, 6930.0, 5480.0, 4320.0);
        chartData.addBarData(new JHorizontalBarChartData.BarData("预算下达", budget, JHorizontalBarChartData.COLOR_A));
        chartData.addBarData(new JHorizontalBarChartData.BarData("已执行", executed, JHorizontalBarChartData.COLOR_B));
        JOption option = new JOption();
        option.setData(chartData);

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

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/24_budget_progress.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/24_budget_progress.txt");
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3 — run it

The class is a JUnit test, so run it with Maven:

mvn -pl jquick-pdfx test -Dtest=JQuickBiz24BudgetProgressTest
Enter fullscreen mode Exit fullscreen mode

…or call the test method from a main wrapper if you want a plain entry point:

public static void main(String[] args) throws Exception {
    new JQuickBiz24BudgetProgressTest().biz24BudgetProgress();
}
Enter fullscreen mode Exit fullscreen mode

Output: D:/test/biz/24_budget_progress.pdf

How it works

Binding a chart SVG into the template

The chart is not a bitmap — JChartRendererFactory.renderChart(...) returns an SVG string, and the template embeds it with a single <svg> element bound to ${svg}. That means the chart stays vector-sharp at any zoom level, and it participates in normal flow layout instead of being stamped on top of the page.

  • Declare only the width on <svg>; the height is derived from the chart's aspect ratio, so the chart never stretches vertically.
  • Open config.getLayoutConfig().setFlexLayout(true) if the template centers the chart with display:flex.
  • Bind the SVG string with factory.bind("svg", svg) — bind() is pure data injection and never re-parses markup.
String svg = JChartRendererFactory.renderChart(JChartType.LINE, option);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource(TEMPLATE);
Enter fullscreen mode Exit fullscreen mode

Flex layout: what it does and does not do

display:flex is opt-in: without config.getLayoutConfig().setFlexLayout(true) the children stack vertically. Even with it enabled, children whose fixed widths sum beyond the text area wrap to the next line — so a two-column layout must budget its pixels.

  • Enable flex on JPdfConfig before creating the factory.
  • Use fixed px widths that sum to less than the text width (~483px on A4 with 40pt margins).
  • Nested div two-column layouts are unreliable here — a full-width single column is safer.
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
Enter fullscreen mode Exit fullscreen mode

Template syntax cheat sheet

Syntax Meaning
<table style="width:100%"> Spans the full text area; give explicit widths only to the leading columns.
<areaBreak></areaBreak> Force the next block onto a fresh page (CSS page-break is not supported).
<image src='D:/.../x.png'> Embed an image. src is a raw path — it is not evaluated as an expression.
<svg style="width:479pt">${svg}</svg> Embed a vector chart produced by JChartRendererFactory.
// comment Line comment. Keep <!-- ... --> comments short — long ones can blank the render.

Gotchas worth remembering

  • Compute money with BigDecimal, round once, and derive 大写 from the rounded value.
  • Flex children whose fixed widths exceed the text area wrap instead of sitting side by side.
  • ${var} must sit outside single quotes — 'No: ${docNo}' prints the placeholder literally.
  • <image src="..."> never evaluates ${...}; write a literal path.

Output

Wrap up

You now have a template file, a Java class and a PDF — all three reproducible from a clean clone. Swap the placeholder blocks for real data (or for real seal images) and the layout does not move.

Series navigation: ← #23 Radar Chart for Enterprise Customer Risk Assessment · #25 Funnel Chart of Channel Registration Conversion →

Full index: README.md · Source: jquick-pdf


Appendix — files in this demo

Role Path
Java class jquick-pdfx/src/test/java/com/github/paohaijiao/biz/chart/JQuickBiz24BudgetProgressTest.java
Template jquick-pdfx/src/test/resources/report/biz/24_budget_progress.txt
Output PDF D:/test/biz/24_budget_progress.pdf
Preview image images/24.png

Top comments (0)