DEV Community

paohaijiao
paohaijiao

Posted on

Bar Chart of Accepted, Approved and Returned Applications

Bar Chart of Accepted, Approved and Returned Applications

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

A government service window tracks three outcomes per month to measure throughput quality.

This document belongs to the Business chart series set.

What you'll build

  • A PDF written to D:/test/biz/17_project_approval.pdf
  • A template file (report/biz/17_project_approval.txt) with 1 bound placeholder
  • A detail grid of 6 rows × 5 columns, laid out with width:100% so it fills the text area
  • One self-contained Java class (JQuickBiz17ProjectApprovalTest) 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/17_project_approval.txt) instead of a Java string, so designers can edit it without touching code.

<pdf>
<body>
  <div style="textAlignment:center; marginBottom:10px; padding:14px; background:#2F5597; borderRadius:4px; keepTogether:true">
    <h1 style="textAlignment:center; color:white; marginBottom:6px; fontSize:18pt; bold:true">'项目审批数量趋势月报'</h1>
    <p style="color:white; fontSize:10pt; margin:2px">'统计周期:2024年1月 - 2024年12月'</p>
    <p style="color:white; fontSize:10pt; margin:2px">'编制单位:市政务服务管理局 | 报送对象:项目监管联席会议'</p>
  </div>

  <div style="marginBottom:10px">
    <h2 style="color:#2F5597; fontSize:13pt; bold:true; marginBottom:6px">'一、月度审批数量趋势'</h2>
    <div style="display:flex; justifyContent:center">
      <svg style="width:700pt">${svg}</svg>
    </div>
    <p style="fontSize:9pt; color:#6c757d; italic:true; marginTop:6px">'图 1  2024年1-12月受理 / 审批通过 / 退回补正项目数量(数据来源:市项目审批监管系统)'</p>
  </div>

  <div style="marginBottom:10px">
    <h2 style="color:#2F5597; fontSize:13pt; bold:true; marginBottom:6px">'二、季度审批结果汇总'</h2>
    <table style="width:560px; fontSize:10pt">
      <tr>
        <th style="width:120px; backgroundColor:#2F5597; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'季度'</th>
        <th style="width:110px; backgroundColor:#2F5597; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'受理(件)'</th>
        <th style="width:110px; backgroundColor:#2F5597; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'审批通过(件)'</th>
        <th style="width:110px; backgroundColor:#2F5597; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'退回补正(件)'</th>
        <th style="width:110px; backgroundColor:#2F5597; 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">'435'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'346'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'66'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'79.5%'</td>
      </tr>
      <tr>
        <td style="padding:5px; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'第二季度'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'492'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'400'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'74'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'81.3%'</td>
      </tr>
      <tr>
        <td style="padding:5px; border:1px solid #DCE3EC">'第三季度'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'553'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'456'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'80'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'82.5%'</td>
      </tr>
      <tr>
        <td style="padding:5px; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'第四季度'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'643'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'536'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'86'</td>
        <td style="padding:5px; textAlign:right; backgroundColor:#EAF1F8; border:1px solid #DCE3EC">'83.4%'</td>
      </tr>
      <tr>
        <td style="padding:6px; bold:true; border:1px solid #DCE3EC">'全年合计'</td>
        <td style="padding:6px; textAlign:right; bold:true; border:1px solid #DCE3EC">'2,123'</td>
        <td style="padding:6px; textAlign:right; bold:true; border:1px solid #DCE3EC">'1,738'</td>
        <td style="padding:6px; textAlign:right; bold:true; border:1px solid #DCE3EC">'306'</td>
        <td style="padding:6px; textAlign:right; bold:true; border:1px solid #DCE3EC">'81.9%'</td>
      </tr>
    </table>
  </div>

  <div style="marginBottom:10px">
    <h2 style="color:#2F5597; fontSize:13pt; bold:true; marginBottom:6px">'三、审批运行结论'</h2>
    <list style="listStyleType:disc; paddingLeft:16px; fontSize:10pt">
      <li style="marginBottom:5px"><span style="bold:true; color:#2F5597">'审批总量稳步上行:'</span>'全年受理项目 2,123 件、审批通过 1,738 件,四季度受理量较一季度增长 47.8%(643 件对 435 件)。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#2F5597">'通过率逐季提升:'</span>'通过率由一季度 79.5% 升至四季度 83.4%,全年 81.9%;退回补正占比由 15.2% 降至 13.4%。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#2F5597">'退回补正高位平稳:'</span>'全年退回补正 306 件,月均 25.5 件,四季度 86 件为年内高点,材料完整性仍是主要影响因素。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#2F5597">'年末高峰特征明显:'</span>'12月受理 226 件、审批通过 189 件,均为全年单月峰值,与年度项目集中申报节奏一致。'</li>
      <li style="marginBottom:5px"><span style="bold:true; color:#2F5597">'建议:'</span>'在年末申报高峰前增设预审窗口,并完善线上材料校验规则,力争将退回补正占比压降至 12% 以内。'</li>
    </list>
  </div>

  <div style="textAlignment:center; marginTop:10px; paddingTop:10px; borderTop:1px solid #dee2e6; color:#6c757d; fontSize:8pt">
    <p>'数据来源:市项目审批监管系统 | 生成时间:2025年1月10日'</p>
    <p>'内部资料 · 未经许可请勿外传'</p>
  </div>
</body>
</pdf>
Enter fullscreen mode Exit fullscreen mode

Step 2 — the Java class

jquick-pdfx/src/test/java/com/github/paohaijiao/biz/chart/JQuickBiz17ProjectApprovalTest.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.axis.JCategoryAxis;
import com.github.paohaijiao.axis.JValueAxis;
import com.github.paohaijiao.code.JTrigger;
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.series.JBar;
import org.junit.Test;

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

/**
 * 业务场景 Demo 17:项目审批数量趋势(政务/项目监管平台)
 *
 * <p>业务场景:项目监管平台按月统计「受理 / 审批通过 / 退回补正」三类的项目审批数量,
 * 用于观察审批负荷与办理质量的变化。</p>
 * <p>实际图表类型:{@link JChartType#BAR} 柱状图(分组柱,用于对比三类审批结果)。</p>
 *
 * <p>模板:{@code report/biz/17_project_approval.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\17_project_approval.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt × 380pt(宽 × 高)的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz17ProjectApprovalTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz17ProjectApproval() throws IOException {
        JOption option = new JOption();
        option.title().text("2024年项目审批数量趋势").subtext("受理 / 审批通过 / 退回补正(单位:件)");
        option.tooltip().trigger(JTrigger.axis);
        JCategoryAxis xAxis = new JCategoryAxis();
        xAxis.data("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        option.xAxis(xAxis);
        option.yAxis(new JValueAxis());
        // 三类审批结果各一根柱,沿用政务系统低饱和主色
        JBar accepted = new JBar();
        accepted.name("受理").data(142, 138, 155, 163, 158, 171, 182, 176, 195, 203, 214, 226);
        JBar approved = new JBar();
        approved.name("审批通过").data(112, 110, 124, 132, 128, 140, 150, 145, 161, 169, 178, 189);
        JBar returned = new JBar();
        returned.name("退回补正").data(22, 20, 24, 25, 23, 26, 27, 25, 28, 27, 29, 30);
        option.series(accepted, approved, returned);

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

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/17_project_approval.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/17_project_approval.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=JQuickBiz17ProjectApprovalTest
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 JQuickBiz17ProjectApprovalTest().biz17ProjectApproval();
}
Enter fullscreen mode Exit fullscreen mode

Output: D:/test/biz/17_project_approval.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

CJK font configuration

CJK text needs a font that actually contains the glyphs. The engine ships with a Chinese font and lets you switch it through JFontConfig; if a glyph is missing the character is silently dropped or drawn as a box, and the failure looks like a data problem rather than a font problem.

  • Set the font once on JPdfConfig.getFontConfig() before rendering.
  • Chart SVG text is a separate pipeline — register the same family on JOption or chart labels fall back to Helvetica.
  • Prefer the full-width ¥ over ¥; the latter is often absent from CJK faces.
JPdfConfig config = new JPdfConfig();
config.getFontConfig().setDefaultFontName("Noto Sans CJK SC");
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

  • 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.
  • A <td> holds text only — nested <p>, <image> or <table> is dropped.

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: ← #16 Monthly Revenue and Net Profit: A Dual-Line Chart PDF · #18 Geo Chart of Application Volume by Region →

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/JQuickBiz17ProjectApprovalTest.java
Template jquick-pdfx/src/test/resources/report/biz/17_project_approval.txt
Output PDF D:/test/biz/17_project_approval.pdf
Preview image images/17.png

Top comments (0)