跳转到内容

Java 生成「多指标复合折线柱状混合图(工业经济运行监测)」PDF 示例(LineBar)

JQuick-PdfBiz Demo

LineBar2 页 A4 · 207 KB

柱状图表示产值、折线表示增速,在同一张图中同时呈现总量与增长率。

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

多指标复合折线柱状混合图(工业经济运行监测) — LineBar
渲染效果

输出规格

PDF 页数
2
图表类型
LineBar
模板行数
105
源码行数
94

Java 源码 · JQuickBiz41CompositeLineBarTest.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.JComboLineBarChartData;
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;
import java.util.List;

/**
 * 业务场景 Demo 41:多指标复合折线柱状混合图(工业经济运行监测)
 *
 * <p>业务场景:工业经济运行监测平台按月复合展示工业产值与单位产值综合能耗,柱表示月度产值、
 * 线表示能耗强度,用于研判「增产」与「降耗」是否脱钩,主色采用深紫 #6A1B9A。</p>
 * <p>实际图表类型:{@link JChartType#LineBar} 折线柱状混合图(双 Y 轴:左轴产值亿元、右轴能耗强度)。</p>
 *
 * <p>模板:{@code report/biz/41_composite_linebar.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\41_composite_linebar.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt × 380pt(宽 × 高)的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz41CompositeLineBarTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz41CompositeLineBar() throws IOException {
        List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        // 柱:月度工业产值(亿元)
        List<Double> bars = Arrays.asList(42.6, 38.9, 45.2, 47.8, 49.5, 52.1, 54.6, 53.2, 56.4, 58.9, 61.3, 64.8);
        // 线:单位产值综合能耗(千克标煤/万元),逐月下降体现能效提升
        List<Double> lines = Arrays.asList(392.0, 384.0, 376.0, 365.0, 356.0, 347.0, 338.0, 326.0, 315.0, 302.0, 289.0, 275.0);
        JComboLineBarChartData chartData = JComboLineBarChartData.builder()
                .title("2024年工业产值与能耗复合分析", "月度工业产值与单位产值综合能耗")
                .barData(bars)
                .lineData(lines)
                .xAxisLabels(months)
                .barColor(new Color(106, 27, 154))
                .lineColor(new Color(0, 105, 92))
                .leftAxisTitle("工业产值(亿元)")
                .rightAxisTitle("单位产值能耗(千克标煤/万元)")
                .barLegendText("月度工业产值")
                .lineLegendText("单位产值综合能耗")
                .footerText("数据来源:工业经济运行监测平台 | 统计口径:自然月")
                .build();
        JOption option = new JOption();
        option.setData(chartData);

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

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/41_composite_linebar.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/41_composite_linebar.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/41_composite_linebar.txt");

模板文件 · 41_composite_linebar.txt

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

<pdf>
<body>
  <div style="borderBottom:2px solid #6A1B9A; paddingBottom:8px; marginBottom:10px">
    <div style="display:flex; justifyContent:space-between">
      <div style="width:150px">
        <p style="color:#6A1B9A; fontSize:10; bold:true">'工业经济运行监测中心'</p>
      </div>
      <div style="width:250px; textAlignment:center">
        <h1 style="color:#6A1B9A; fontSize:16; bold:true; textAlignment:center">'工业产值与能耗复合趋势分析'</h1>
      </div>
      <div style="width:150px; textAlignment:right">
        <p style="color:#6c757d; fontSize:9">'2025年1月10日'</p>
      </div>
    </div>
  </div>

  <div style="backgroundColor:#F3EAF7; padding:6px 10px; borderRadius:3px; marginBottom:6px">
    <h2 style="color:#6A1B9A; 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  2024年月度工业产值(柱,单位:亿元)与单位产值综合能耗(线,单位:千克标煤/万元),数据来源:工业经济运行监测平台'</p>

  <areaBreak></areaBreak>

  <div style="backgroundColor:#F3EAF7; padding:6px 10px; borderRadius:3px; marginBottom:6px">
    <h2 style="color:#6A1B9A; fontSize:13; bold:true; marginTop:0; marginBottom:0">'二、月度产值与能耗明细'</h2>
  </div>
  <table style="width:560px; fontSize:10">
    <tr>
      <th style="backgroundColor:#6A1B9A; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'月份'</th>
      <th style="backgroundColor:#6A1B9A; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'工业产值(亿元)'</th>
      <th style="backgroundColor:#6A1B9A; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'单位产值能耗(千克标煤/万元)'</th>
      <th style="backgroundColor:#6A1B9A; color:white; padding:6px; textAlign:center; border:1px solid #DCE3EC">'能耗环比'</th>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'1月'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'42.6'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'392.0'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'—'</td>
    </tr>
    <tr>
      <td style="padding:5px; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'2月'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'38.9'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'384.0'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'-2.0%'</td>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'3月'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'45.2'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'376.0'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'-2.1%'</td>
    </tr>
    <tr>
      <td style="padding:5px; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'4月'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'47.8'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'365.0'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'-2.9%'</td>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'6月'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'52.1'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'347.0'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'-2.6%'</td>
    </tr>
    <tr>
      <td style="padding:5px; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'9月'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'56.4'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'315.0'</td>
      <td style="padding:5px; textAlign:right; backgroundColor:#F3EAF7; border:1px solid #DCE3EC">'-2.9%'</td>
    </tr>
    <tr>
      <td style="padding:5px; border:1px solid #DCE3EC">'12月'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'64.8'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'275.0'</td>
      <td style="padding:5px; textAlign:right; border:1px solid #DCE3EC">'-4.5%'</td>
    </tr>
  </table>

  <div style="backgroundColor:#F3EAF7; padding:6px 10px; borderRadius:3px; marginBottom:6px; marginTop:10px">
    <h2 style="color:#6A1B9A; fontSize:13; bold:true; marginTop:0; marginBottom:0">'三、增产降耗研判'</h2>
  </div>
  <list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
    <li style="marginBottom:6px"><span style="bold:true; color:#6A1B9A">'产值:'</span>'全年累计 625.3 亿元,同比增长 11.4%;12月单月 64.8 亿元为年内最高点,较年内低点(2月 38.9 亿元)高出 66.6%。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#6A1B9A">'能耗:'</span>'单位产值综合能耗由 1月 392.0 降至 12月 275.0 千克标煤/万元,累计降幅 29.8%,且下半年月度环比降幅持续扩大。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#6A1B9A">'脱钩:'</span>'产值增长 11.4% 而单位能耗下降 29.8%,综合能耗总量仅增约 1.1%,说明增长动能已由规模扩张转为效率提升。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#6A1B9A">'建议:'</span>'2月受春节停工影响为年内产值低点、能耗环比仅降 2.0%,建议对低负荷运行时段单独制定能效管控方案。'</li>
  </list>

  <lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
  <div style="display:flex; justifyContent:space-between; marginTop:8px">
    <div style="width:180px">
      <p style="color:#6c757d; fontSize:8">'密级:内部'</p>
    </div>
    <div style="width:180px; textAlignment:center">
      <p style="color:#6c757d; fontSize:8; opacity:0.8">'生成时间:2025年1月10日'</p>
    </div>
    <div style="width:180px; textAlignment:right">
      <p style="color:#6c757d; fontSize:8">'批准人:监测中心主任'</p>
    </div>
  </div>
</body>
</pdf>

同分组示例

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