City air-quality monitoring bubble chart (environmental monitoring) — generate this PDF with Java (Bubble)
Bubble1 页 A4 · 312 KB
Daily January readings from 15 national monitoring stations: date on the X axis, AQI on the Y axis, bubble size for PM2.5 concentration and color for the air-quality grade, with values printed on the bubbles and pollution-event analysis below.

Output specs
- PDF pages
- 1
- Chart type
- Bubble
- Template lines
- 36
- Source lines
- 124
Java source · JQuickBiz57BubbleTest.java
This is the complete test class that produced the PDF above — copy it and run as-is; see the quick-start section for the Maven dependency.
/*
* 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);
}
}
}Minimal invocation (3 lines)
Skip the full test class — the core is these three lines: bind the chart SVG, execute the template, get the PDF bytes. Copy them straight into your project.
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/57_bubble.txt");Template file · 57_bubble.txt
An HTML-like template with inline CSS that renders to PDF once bound to data. The full template is below — copy it or download the raw file.
<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>More demos in this group
The PDF, preview image, template and source code on this page were all produced by a real JQuick-Pdf run and are free to copy into your own project.