跳转到内容

Java 生成「城市空气质量监测气泡图(生态环境监测)」PDF 示例(Bubble)

JQuick-PdfBiz Demo

Bubble1 页 A4 · 312 KB

15 个国控点 1 月逐日监测数据:X 轴日期、Y 轴 AQI、气泡大小代表 PM2.5 浓度、颜色区分空气质量等级,气泡上直接标注当日数值,图下给出污染过程与减排建议。

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

城市空气质量监测气泡图(生态环境监测) — Bubble
渲染效果

输出规格

PDF 页数
1
图表类型
Bubble
模板行数
36
源码行数
124

Java 源码 · JQuickBiz57BubbleTest.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.bubble.CategoryAxis;
import com.github.paohaijiao.bubble.ScatterSeries;
import com.github.paohaijiao.bubble.ValueAxis;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 业务场景 Demo 57:城市空气质量监测气泡图(生态环境监测)
 *
 * <p>业务场景:生态环境监测中心对城市空气质量进行逐日监测,以气泡图展示
 * 日期(X 轴)、AQI 数值(Y 轴)、PM2.5 浓度(气泡大小)以及空气质量等级(气泡颜色)。</p>
 * <p>实际图表类型:{@link JChartType#Bubble} 气泡图。</p>
 *
 * <p>模板:{@code report/biz/57_bubble.txt};图表以 SVG 文本形式绑定到模板的
 * {@code ${svg}} 占位符,输出到 {@code D:\test\biz\57_bubble.pdf}。</p>
 *
 * <p>图表尺寸说明:图表按 700pt × 380pt 的横版比例设计;模板中 {@code <svg>}
 * 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz57BubbleTest {

    public static final String path = JQuickConstant.path;

    @Test
    public void biz57Bubble() throws IOException {
        JOption option = new JOption()
                .title("城市空气质量监测气泡图", "X 轴:日期  Y 轴:AQI  气泡大小:PM2.5 浓度  颜色:等级")
                .legend("优", "良", "轻度污染", "中度污染", "重度污染")
                .xAxis(new CategoryAxis().name("日期"))
                .yAxis(new ValueAxis().name("AQI 数值"));

        ScatterSeries series = new ScatterSeries("空气质量监测");
        List<Map<String, Object>> seriesData = new ArrayList<>();
        Random random = new Random(42);
        String[] dates = {"01-01", "01-02", "01-03", "01-04", "01-05", "01-06", "01-07", "01-08",
                "01-09", "01-10", "01-11", "01-12", "01-13", "01-14", "01-15"};
        for (int i = 0; i < dates.length; i++) {
            int aqi = 20 + random.nextInt(180);
            double pm25 = 10 + random.nextDouble() * 150;
            String category;
            if (aqi <= 50) category = "优";
            else if (aqi <= 100) category = "良";
            else if (aqi <= 150) category = "轻度污染";
            else if (aqi <= 200) category = "中度污染";
            else category = "重度污染";
            Map<String, Object> point = new HashMap<>();
            point.put("x", dates[i]);
            point.put("y", aqi);
            point.put("size", pm25);
            point.put("category", category);
            point.put("name", "日期:" + dates[i] + ", AQI:" + aqi + ", PM2.5:" + String.format("%.1f", pm25));
            point.put("color", getBubbleColor(category));
            seriesData.add(point);
        }
        series.data(seriesData.toArray());
        option.series(series);

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

        File dir = new File(path + "biz");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/57_bubble.pdf");
        JPdfConfig config = new JPdfConfig();
        config.getLayoutConfig().setFlexLayout(true);
        JQuickPdfFactory factory = new JQuickPdfFactory(config);
        factory.bind("svg", svg);
        byte[] bytes = factory.executeResource("report/biz/57_bubble.txt");
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }

    private static Color getBubbleColor(String category) {
        switch (category) {
            case "优":
                return new Color(102, 194, 165, 180);
            case "良":
                return new Color(252, 194, 91, 180);
            case "轻度污染":
                return new Color(246, 138, 89, 180);
            case "中度污染":
                return new Color(232, 96, 85, 180);
            default:
                return new Color(158, 42, 95, 180);
        }
    }
}

最小调用(3 行)

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

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

模板文件 · 57_bubble.txt

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

<pdf>
<body>
  // 57 气泡图 Demo:X 轴为维度一、Y 轴为维度二、气泡大小为第三维度(权重)
  <div style="background:#1B5E20; padding:12px; borderRadius:4px; marginBottom:10px; keepTogether:true">
    <div style="display:flex; justifyContent:space-between">
      <div style="width:320px">
        <h1 style="color:white; fontSize:17; bold:true; marginBottom:3px">'城市空气质量监测气泡图'</h1>
        <p style="color:#C8E6C9; fontSize:9">'X 轴:日期 Y 轴:AQI 数值 气泡大小:PM2.5 浓度 颜色:空气质量等级'</p>
      </div>
      <div style="width:220px; textAlignment:right; borderLeft:1px solid #66BB6A; paddingLeft:10px">
        <p style="color:white; fontSize:9">'监测周期:2024年1月'</p>
        <p style="color:#C8E6C9; fontSize:9">'监测站点:15 个国控点'</p>
        <p style="color:#C8E6C9; fontSize:9">'编制:生态环境监测中心'</p>
      </div>
    </div>
  </div>

  <div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #1B5E20">
    <h2 style="color:#1B5E20; marginBottom:6px; fontSize:13; bold:true">'一、1 月逐日 AQI 与 PM2.5 关系'</h2>
    <div style="display:flex; justifyContent:center">
      <svg style="width:700pt">${svg}</svg>
    </div>
    <p style="fontSize:9; color:#6c757d; italic:true; marginTop:6px">'图 1  气泡图:气泡越大代表 PM2.5 浓度越高,颜色由绿到紫对应 AQI 等级由优到重污染'</p>
  </div>

  <list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
    <li style="marginBottom:6px"><span style="bold:true; color:#1B5E20">'上旬整体优良:'</span>'1 月上旬 AQI 多在 100 以下,PM2.5 浓度较低。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#1B5E20">'中下旬污染加重:'</span>'受静稳天气影响,AQI 上升至轻度至中度污染区间,PM2.5 同步升高。'</li>
    <li style="marginBottom:6px"><span style="bold:true; color:#1B5E20">'建议:'</span>'污染过程期间启动应急减排,加强工业源与移动源管控。'</li>
  </list>

  <lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
  <p style="color:#6c757d; fontSize:8; textAlignment:center; marginTop:6px">'生成时间:2025年1月6日 | 批准人:监测中心主任'</p>
</body>
</pdf>

同分组示例

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