DEV Community

paohaijiao
paohaijiao

Posted on

Plotting a Four-Year Credit Trend Line Chart in a PDF Report

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

Management wants revenue and net profit on one timeline to judge whether the credit portfolio is improving.

This document belongs to the Credit report — chart series set. Like every demo in the series it has its own colour scheme and layout combination, derived deterministically from the demo number — so the look is stable across runs and two neighbouring documents never look alike.

What you'll build

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


<pdf>
<body>
  <div style="marginBottom:18px; borderLeft:6px solid #00695C; paddingLeft:16px">
    <div style="display:flex; justifyContent:space-between; alignItems:center">
      <h1 style="width:360px; textAlignment:left; fontSize:20; color:#00695C; margin:0">'企业信用评级报告'</h1>
      <div style="width:220px; textAlignment:right">
        <p style="fontSize:10; color:#607d8b; margin:1px">'报告编号: CR-2023-08975'</p>
        <p style="fontSize:10; color:#607d8b; margin:1px">'评估日期: 2023年11月15日'</p>
      </div>
    </div>
    <p style="textAlignment:left; fontSize:10; color:#00695C; marginTop:6px; marginBottom:0">'评估机构: 四川省企业信用评估中心'</p>
  </div>

  <div style="marginBottom:18px">
    <h2 style="color:#00695C; fontSize:13; marginBottom:8px">'企业信息'</h2>
    <div style="display:flex; flexWrap:wrap">
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'统一信用代码'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'91110108MA01XX1234'</p>
      </div>
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'成立日期'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'2018年5月20日'</p>
      </div>
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'注册资本'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'1000万元人民币'</p>
      </div>
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'法定代表人'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'泡海椒'</p>
      </div>
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'所属行业'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'软件和信息技术服务业'</p>
      </div>
      <div style="width:290px; margin:4px; padding:8px; backgroundColor:#f5f7f7; borderRadius:3px">
        <p style="fontSize:9; color:#00897B; margin:0">'企业规模'</p>
        <p style="fontSize:11; bold:true; marginTop:3px; marginBottom:0">'中型(150-200人)'</p>
      </div>
    </div>
  </div>

  <div style="marginBottom:18px">
    <h2 style="color:#00695C; borderBottom:2px solid #00695C; paddingBottom:4px; fontSize:13; marginBottom:10px">'历年财务数据(单位:万元)'</h2>
    <table style="width:600px; fontSize:10; marginTop:8px">
      <tr>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:left">'财务指标'</th>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:right">'2020年'</th>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:right">'2021年'</th>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:right">'2022年'</th>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:right">'2023年'</th>
        <th style="backgroundColor:#ffffff; color:#00695C; borderBottom:2px solid #00695C; padding:6px; textAlign:right">'增长率'</th>
      </tr>
      <tr>
        <td style="padding:6px; textAlign:left; borderBottom:1px solid #e0e0e0; bold:true">'营业收入'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'8,560'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'12,340'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'18,920'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'25,680'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0; color:#00897B; bold:true">'+44.2%'</td>
      </tr>
      <tr>
        <td style="padding:6px; textAlign:left; borderBottom:1px solid #e0e0e0; bold:true">'净利润'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'1,240'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'2,150'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'3,890'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'5,420'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0; color:#00897B; bold:true">'+63.8%'</td>
      </tr>
      <tr>
        <td style="padding:6px; textAlign:left; borderBottom:1px solid #e0e0e0; bold:true">'总资产'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'15,800'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'22,450'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'31,200'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'42,800'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0; color:#00897B; bold:true">'+39.5%'</td>
      </tr>
      <tr>
        <td style="padding:6px; textAlign:left; borderBottom:1px solid #e0e0e0; bold:true">'净资产'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'9,200'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'13,800'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'19,500'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'26,400'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0; color:#00897B; bold:true">'+42.1%'</td>
      </tr>
      <tr>
        <td style="padding:6px; textAlign:left; borderBottom:1px solid #e0e0e0; bold:true">'毛利率'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'42.5%'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'45.8%'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'48.2%'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0">'51.6%'</td>
        <td style="padding:6px; textAlign:right; borderBottom:1px solid #e0e0e0; color:#00897B; bold:true">'+9.1%'</td>
      </tr>
    </table>
  </div>

  <div style="marginBottom:18px">
    <h2 style="color:#00695C; borderBottom:2px solid #00695C; paddingBottom:4px; fontSize:13; marginBottom:10px">'关键经营指标'</h2>
    <div style="display:flex; flexWrap:wrap">
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'营收增长率:连续三年保持40%以上增长,2023年达到25,680万元'</p>
      </div>
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'利润率提升:净利率从14.5%提升至21.1%,盈利能力显著增强'</p>
      </div>
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'研发投入:研发费用年均增长38.9%,占营收比例达13.3%'</p>
      </div>
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'人才结构:技术研发人员占比42%,硕士以上学历占35%'</p>
      </div>
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'客户质量:服务15家上市公司,客户续约率92%,大客户满意度4.8/5.0'</p>
      </div>
      <div style="width:290px; margin:4px; padding:10px; backgroundColor:#e0f2f1; borderRadius:4px">
        <p style="fontSize:11; margin:0">'资产质量:资产负债率维持在38%左右,流动比率2.8,偿债能力良好'</p>
      </div>
    </div>
  </div>

  <div style="marginBottom:18px">
    <h2 style="color:#00695C; borderBottom:2px solid #00695C; paddingBottom:4px; fontSize:13; marginBottom:10px">'信用评级趋势折线图'</h2>
    <div style="display:flex; justifyContent:center">
      <svg style="width:460px;height:400px">${svg}</svg>
    </div>
    <div style="display:flex; justifyContent:space-around; marginTop:10px">
      <p style="width:150px; fontSize:10; color:#00695C; margin:0">'财务状况(90分): 营收稳定增长'</p>
      <p style="width:150px; fontSize:10; color:#00695C; margin:0">'经营能力(95分): 客户留存率高'</p>
      <p style="width:150px; fontSize:10; color:#00695C; margin:0">'偿债能力(85分): 负债率合理'</p>
      <p style="width:150px; fontSize:10; color:#00695C; margin:0">'发展潜力(92分): 研发投入增加'</p>
    </div>
  </div>

  <div style="marginBottom:18px; borderLeft:6px solid #009688; paddingLeft:14px">
    <h2 style="color:#00695C; fontSize:13; marginBottom:8px">'综合评价'</h2>
    <p style="fontSize:11; margin:0">
      '四川省泡海椒科技有限公司作为国家级高新技术企业和专精特新"小巨人"企业,在云计算和大数据领域表现出色。'
      '公司财务状况健康,经营能力突出,技术创新能力强,具备良好的发展潜力。信用评级为AAA级,展望稳定。'
    </p>
  </div>

  <div style="display:flex; justifyContent:space-between; marginTop:18px; paddingTop:10px; borderTop:1px solid #b2dfdb">
    <div style="width:500px">
      <p style="fontSize:9; color:#607d8b; margin:0">'本报告依据公开信息和专业评估模型生成,仅供参考 | 报告生成时间: 2025年09月06日'</p>
      <p style="fontSize:9; color:#607d8b; margin:0">'© 2023 企业信用评估中心 版权所有 | 认证编号: CE-2023-001'</p>
    </div>
    <div style="width:120px; textAlignment:right">
      <p style="fontSize:9; color:#00897B; margin:0">'CE-2023-001'</p>
    </div>
  </div>
</body>
</pdf>
Enter fullscreen mode Exit fullscreen mode

Step 2 — the Java class

jquick-pdfx/src/test/java/com/github/paohaijiao/biz/report/JQuickCreditReport02LineTest.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.report;

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.JLine;
import org.junit.Test;

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

/**
 * 统一信用报告 —— 风格 02:折线图(墨绿)
 *
 * <p>以 {@code report/credit_report_02_line.txt} 为模板,把折线图渲染成 SVG 后
 * 绑定到模板中的 {@code ${svg}} 占位符,输出到 {@code D:\test\creditreport\02_line.pdf}。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickCreditReport02LineTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void report02Line() throws IOException {
        JOption option = new JOption();
        option.title().text("信用评级趋势").subtext("2020-2023年度(单位:万元)");
        option.tooltip().trigger(JTrigger.axis);
        JCategoryAxis xAxis = new JCategoryAxis();
        xAxis.data("2020年", "2021年", "2022年", "2023年");
        option.xAxis(xAxis);
        option.yAxis(new JValueAxis());
        JLine revenue = new JLine();
        revenue.name("营业收入").data(8560, 12340, 18920, 25680);
        JLine profit = new JLine();
        profit.name("净利润").data(1240, 2150, 3890, 5420);
        option.series(revenue, profit);

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

        File dir = new File(path + "creditreport");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "creditreport/02_line.pdf");
        JPdfConfig config = new JPdfConfig();
        // 开启 flex 行布局:图表与其右侧的指标说明面板在宽度允许时并排,而不是换行堆叠。
        config.getLayoutConfig().setFlexLayout(true);
        JQuickPdfFactory factory = new JQuickPdfFactory(config);
        factory.bind("svg", svg);
        byte[] bytes = factory.executeResource("report/credit_report_02_line.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=JQuickCreditReport02LineTest
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 JQuickCreditReport02LineTest().report02Line();
}
Enter fullscreen mode Exit fullscreen mode

Output: 02_line.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

Making the table fill the text area

Tables default to content width, which leaves a ragged right margin on a formal document. Setting width:100% on the <table> makes it span the full text area; percentage widths on the first row's cells then distribute the remaining space across columns.

  • Set width:100% on <table> and give explicit widths only to the leading columns.
  • Label columns need around 100px so labels like Taxpayer ID do not wrap.
  • Leave the last column without a width so it absorbs rounding remainder.
<table style="width:100%; fontSize:9pt">
  <tr>
    <td style="width:100px; padding:5px">'Taxpayer ID'</td>
    <td style="padding:5px">${taxNo}</td>
  </tr>
</table>
Enter fullscreen mode Exit fullscreen mode

Template syntax cheat sheet

Syntax Meaning
<td style="padding:4px; border:1px solid #C3CDD8"> Cell styling. A <td> holds text only — no nested blocks.
factory.bind("k", v) Injects plain text into the context; markup inside a value is dropped.
'literal text' A single-quoted string is a literal — whatever is inside is printed verbatim.
${var} An expression. It must sit outside the quotes: 'No: '${docNo}.
<table style="width:100%"> Spans the full text area; give explicit widths only to the leading columns.

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

Demo 2 rendered 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: ← #1 Rendering a Credit-Rating Distribution Bar Chart into PDF... · #3 Building an Area Chart for Credit Coverage Trends →

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/report/JQuickCreditReport02LineTest.java
Template jquick-pdfx/src/test/resources/report/credit_report_02_line.txt
Output PDF 02_line.pdf
Preview image images/02.png

Top comments (0)