跳转到内容

Java 生成「条形图(经典蓝)」PDF 示例(BAR)

JQuick-PdfCreditReport Demo

BAR2 页 A4 · 66 KB

以经典蓝条形图对比 2020—2023 年度的营业收入与净利润。

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

条形图(经典蓝) — BAR
渲染效果

输出规格

PDF 页数
2
图表类型
BAR
模板行数
156
源码行数
79

Java 源码 · JQuickCreditReport01BarTest.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.creditreport;

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;

/**
 * 统一信用报告 —— 风格 01:条形图(经典蓝)
 *
 * <p>以 {@code report/credit_report_01_bar.txt} 为模板,把条形图渲染成 SVG 后
 * 绑定到模板中的 {@code ${svg}} 占位符,输出到 {@code D:\test\creditreport\01_bar.pdf}。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickCreditReport01BarTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void report01Bar() 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());
        JBar revenue = new JBar();
        revenue.name("营业收入").data(8560, 12340, 18920, 25680);
        JBar profit = new JBar();
        profit.name("净利润").data(1240, 2150, 3890, 5420);
        option.series(revenue, profit);

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

        File dir = new File(path + "creditreport");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "creditreport/01_bar.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_01_bar.txt");
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }
}

最小调用(3 行)

不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。

// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/credit_report_01_bar.txt");

模板文件 · 01_bar.txt

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

<pdf>
<body>
  <div style="textAlignment:center; marginBottom:5px; paddings:16px; background:#3E6B9D; color:white; borderRadius:4px">
    <h1 style="textAlignment:center;color:white; marginBottom:8px; fontSize:20; bold:true">'企业信用评级报告'</h1>
    <p style="textAlignment:center;color:rgba(255,255,255,0.9); fontSize:11; margin:2px">'报告编号: CR-2023-08975 | 评估日期: 2023年11月15日'</p>
    <p style="textAlignment:center;color:rgba(255,255,255,0.9); fontSize:11; margin:2px">'评估机构: 四川省企业信用评估中心'</p>
  </div>
  <div style="marginBottom:3px;borderRadius:4px;">
    <div style="padding:12px">
      <h2 style="color:#2c3e50; marginTop:0; marginBottom:8px; fontSize:14; bold:true">'四川省泡海椒科技有限公司'</h2>
      <table style="width:600px;verticalAlignment:center; fontSize:10">
        <tr>
          <td style="backgroundColor:#f8f9fa;padding:5px; textAlign:left;width:150px;">'统一信用代码'</td>
          <td style="backgroundColor:#f8f9fa;padding:5px;width:150px;">'91110108MA01XX1234'</td>
          <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left;width:150px;">'成立日期'</td>
          <td style="padding:5px; width:150px;">'2018年5月20日'</td>
        </tr>
        <tr>
          <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left;">'注册资本'</td>
          <td style="padding:5px;">'1000万元人民币'</td>
          <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left;">'法定代表人'</td>
          <td style="padding:5px;">'泡海椒'</td>
        </tr>
        <tr>
          <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left;">'所属行业'</td>
          <td style="padding:5px;">'软件和信息技术服务业'</td>
          <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left;">'企业规模'</td>
          <td style="padding:5px;">'中型(150-200人)'</td>
        </tr>
      </table>
    </div>
  </div>
  <div style="margin-bottom:15px">
    <h2 style="color:#3498db; borderBottom:1px solid #3498db; paddingBottom:4px; fontSize:13; marginBottom:10px">'历年财务数据(单位:万元)'</h2>
    <table style="width:600px; fontSize:10; marginTop:8px">
      <tr>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'财务指标'</th>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'2020年'</th>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'2021年'</th>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'2022年'</th>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'2023年'</th>
        <th style="backgroundColor:#3498db; color:white; padding:6px; textAlign:center; border:1px solid #dee2e6">'增长率'</th>
      </tr>
      <tr>
        <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left; border:1px solid #dee2e6; bold:true">'营业收入'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'8,560'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'12,340'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'18,920'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'25,680'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6; color:#4CAF50; bold:true">'+44.2%'</td>
      </tr>
      <tr>
        <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left; border:1px solid #dee2e6; bold:true">'净利润'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'1,240'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'2,150'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'3,890'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'5,420'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6; color:#4CAF50; bold:true">'+63.8%'</td>
      </tr>
      <tr>
        <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left; border:1px solid #dee2e6; bold:true">'总资产'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'15,800'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'22,450'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'31,200'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'42,800'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6; color:#4CAF50; bold:true">'+39.5%'</td>
      </tr>
      <tr>
        <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left; border:1px solid #dee2e6; bold:true">'净资产'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'9,200'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'13,800'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'19,500'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'26,400'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6; color:#4CAF50; bold:true">'+42.1%'</td>
      </tr>
      <tr>
        <td style="backgroundColor:#f8f9fa; padding:5px; textAlign:left; border:1px solid #dee2e6; bold:true">'毛利率'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'42.5%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'45.8%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'48.2%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6">'51.6%'</td>
        <td style="padding:5px; textAlign:right; border:1px solid #dee2e6; color:#4CAF50; bold:true">'+9.1%'</td>
      </tr>
    </table>
  </div>

  <div style="margin-bottom:15px">
    <h2 style="color:#3498db; border-bottom:1px solid #3498db; padding-bottom:4px; font-size:13px; margin-bottom:10px">'关键经营指标'</h2>
    <list style="list-style-type:none; padding-left:0; font-size:11px">
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #4CAF50; border-radius:0 4px 4px 0">
        '营收增长率:连续三年保持40%以上增长,2023年达到25,680万元'
      </li>
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #2196F3; border-radius:0 4px 4px 0">
        '利润率提升:净利率从14.5%提升至21.1%,盈利能力显著增强'
      </li>
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #FF9800; border-radius:0 4px 4px 0">
        '研发投入:研发费用年均增长38.9%,占营收比例达13.3%'
      </li>
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #9C27B0; border-radius:0 4px 4px 0">
        '人才结构:技术研发人员占比42%,硕士以上学历占35%'
      </li>
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #E91E63; border-radius:0 4px 4px 0">
        '客户质量:服务15家上市公司,客户续约率92%,大客户满意度4.8/5.0'
      </li>
      <li style="margin-bottom:8px; padding:10px; background-color:#f8f9fa; border-left:4px solid #009688; border-radius:0 4px 4px 0">
        '资产质量:资产负债率维持在38%左右,流动比率2.8,偿债能力良好'
      </li>
    </list>
  </div>

  <div style="margin-bottom:15px">
    <h2 style="color:#3498db; border-bottom:1px solid #3498db; padding-bottom:4px; font-size:13px; margin-bottom:10px">'信用评级条形图'</h2>
    <div style="display:flex; justify-content:space-between; margin-top:10px">
      <svg style="width:340px;height:340px">${svg}</svg>
      <div style="width:290px">
        <div style="backgroundColor:#f8f9fa; padding:15px; borderRadius:4px">
          <h3 style="color:#2c3e50; margin-top:0; fontSize:12; marginBottom:8px">'评级指标说明'</h3>
          <list style="list-style-type:none; padding-left:0; font-size:10px">
            <li style="margin-bottom:6px; padding:5px; border-left:3px solid #3498db">
              <span style="bold:true; color:#2c3e50">'财务状况(90分):' </span>
              '营收稳定增长'
            </li>
            <li style="margin-bottom:6px; padding:5px; border-left:3px solid #2ecc71">
              <span style="bold:true; color:#2c3e50">'经营能力(95分):' </span>
              '客户留存率高'
            </li>
            <li style="margin-bottom:6px; padding:5px; border-left:3px solid #e74c3c">
              <span style="bold:true; color:#2c3e50">'偿债能力(85分):' </span>
              '负债率合理'
            </li>
            <li style="margin-bottom:6px; padding:5px; border-left:3px solid #f39c12">
              <span style="bold:true; color:#2c3e50">'发展潜力(92分):' </span>
              '研发投入增加'
            </li>
          </list>
        </div>
      </div>
    </div>
  </div>

  <div style="margin-bottom:15px; background:linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%); padding:15px; border-radius:4px; border-left:4px solid #2196F3">
    <h2 style="color:#1565c0; margin-top:0; font-size:13px; margin-bottom:8px">'综合评价'</h2>
    <p style="text-align:justify; margin-bottom:8px; font-size:11px">
      '四川省泡海椒科技有限公司作为国家级高新技术企业和专精特新"小巨人"企业,在云计算和大数据领域表现出色。'
      '公司财务状况健康,经营能力突出,技术创新能力强,具备良好的发展潜力。信用评级为AAA级,展望稳定。'
    </p>
    <p style="text-align:right; font-size:8px; color:#d32f2f; margin-top:2px; margin-bottom:0">'认证通过'</p>
  </div>

  <div style="text-align:center; margin-top:15px; padding-top:12px; border-top:1px solid #dee2e6; color:#6c757d; font-size:9px">
    <p>'本报告依据公开信息和专业评估模型生成,仅供参考 | 报告生成时间: 2025年09月06日'</p>
    <p>'© 2023 企业信用评估中心 版权所有 | 认证编号: CE-2023-001'</p>
  </div>
</body>
</pdf>

同分组示例

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