Gauge Chart for Cluster Resource Utilisation
jquick-pdf by example — demo #22 of 184. Every article in this series ships one complete, runnable demo: a template file and a Java class. No snippets, no omissions.
CPU, memory and disk expressed as dials on a single ops dashboard page.
This document belongs to the Business chart series set.
What you'll build
- A PDF written to
D:/test/biz/22_resource_usage.pdf - A template file (
report/biz/22_resource_usage.txt) with 1 bound placeholder - A detail grid of 8 rows × 5 columns, laid out with
width:100%so it fills the text area - One self-contained Java class (
JQuickBiz22ResourceUsageTest) that binds the data and renders the file
Bound placeholders
${svg}
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>
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/biz/22_resource_usage.txt) instead of a Java string, so designers can edit it without touching code.
<pdf>
<body>
<div style="marginBottom:10px; padding:10px; backgroundColor:#ECEFF1; borderTop:3px solid #37474F; borderRadius:4px; keepTogether:true">
<h2 style="color:#37474F; marginTop:0px; marginBottom:6px; fontSize:15pt; bold:true; textAlignment:center">'资源使用率监控'</h2>
<p style="fontSize:9pt; color:#6c757d; textAlignment:center; marginBottom:6px">'统计周期:2025年9月 | 数据来源:政务云运维中心 | 指标:CPU / 内存 / 磁盘综合使用率'</p>
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
<p style="fontSize:9pt; color:#6c757d; italic:true; textAlignment:center; marginTop:6px">'图 1 集群综合资源使用率仪表盘(当前值 78.0%)'</p>
</div>
<div style="marginBottom:10px">
<div style="borderLeft:4px solid #37474F; paddingLeft:8px; marginBottom:6px">
<h2 style="color:#37474F; fontSize:13pt; bold:true; marginTop:0px; marginBottom:0px">'二、节点资源使用明细'</h2>
</div>
<table style="width:560px; fontSize:9pt">
<tr>
<th style="width:160px; backgroundColor:#37474F; color:white; padding:6px; textAlign:center; border:1px solid #CFD8DC">'计算节点'</th>
<th style="width:105px; backgroundColor:#37474F; color:white; padding:6px; textAlign:center; border:1px solid #CFD8DC">'CPU 使用率'</th>
<th style="width:105px; backgroundColor:#37474F; color:white; padding:6px; textAlign:center; border:1px solid #CFD8DC">'内存使用率'</th>
<th style="width:105px; backgroundColor:#37474F; color:white; padding:6px; textAlign:center; border:1px solid #CFD8DC">'磁盘使用率'</th>
<th style="width:85px; backgroundColor:#37474F; color:white; padding:6px; textAlign:center; border:1px solid #CFD8DC">'状态'</th>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N01'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'64.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'72.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'60.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'正常'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N02'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'86.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'89.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'66.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'预警'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N03'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'93.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'87.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'74.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'高水位'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N04'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'58.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'65.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'53.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'正常'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N05'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'80.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'83.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'71.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'关注'</td>
</tr>
<tr>
<td style="padding:5px; border:1px solid #CFD8DC">'计算节点 N06'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'97.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'93.0%'</td>
<td style="padding:5px; textAlign:right; border:1px solid #CFD8DC">'83.0%'</td>
<td style="padding:5px; textAlign:center; border:1px solid #CFD8DC">'高水位'</td>
</tr>
<tr>
<td style="padding:6px; backgroundColor:#37474F; color:white; bold:true; border:1px solid #37474F">'集群加权均值'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#37474F; color:white; bold:true; border:1px solid #37474F">'79.7%'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#37474F; color:white; bold:true; border:1px solid #37474F">'81.5%'</td>
<td style="padding:6px; textAlign:right; backgroundColor:#37474F; color:white; bold:true; border:1px solid #37474F">'67.8%'</td>
<td style="padding:6px; textAlign:center; backgroundColor:#37474F; color:white; bold:true; border:1px solid #37474F">'综合 78.0%'</td>
</tr>
</table>
</div>
<div style="marginBottom:10px">
<div style="borderLeft:4px solid #37474F; paddingLeft:8px; marginBottom:6px">
<h2 style="color:#37474F; fontSize:13pt; bold:true; marginTop:0px; marginBottom:0px">'三、容量管理与扩容建议'</h2>
</div>
<list style="listStyleType:disc; paddingLeft:16px; fontSize:10pt">
<li style="marginBottom:6px"><span style="bold:true; color:#37474F">'整体水位:'</span>'集群综合使用率 78.0%,CPU / 内存 / 磁盘加权均值分别为 79.7% / 81.5% / 67.8%,处于中等偏上水位。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#37474F">'高水位节点:'</span>'计算节点 N06 三项指标达 97.0% / 93.0% / 83.0%,N03 达 93.0% / 87.0% / 74.0%,均需优先扩容。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#37474F">'低效节点:'</span>'计算节点 N04 CPU 仅 58.0%、内存 65.0%,建议通过调度策略迁移部分负载以提升整体利用率。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#37474F">'内存是主要瓶颈:'</span>'内存加权均值 81.5% 高于 CPU 的 79.7%,扩容时应优先增加内存配额。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#37474F">'建议:'</span>'本周内为 N03、N06 各扩容 16 核 / 64GB,并将综合使用率预警阈值由 85% 下调至 80%。'</li>
</list>
</div>
<div style="textAlignment:center; marginTop:10px; paddingTop:10px; borderTop:1px solid #dee2e6; color:#6c757d; fontSize:8pt">
<p>'数据来源:政务云运维监控平台 | 生成时间:2025年9月30日'</p>
<p>'内部资料 · 未经许可请勿外传'</p>
</div>
</body>
</pdf>
Step 2 — the Java class
jquick-pdfx/src/test/java/com/github/paohaijiao/biz/chart/JQuickBiz22ResourceUsageTest.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.chart;
import com.github.paohaijiao.JOption;
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.guage.GuageConfig;
import com.github.paohaijiao.guage.JGuageOption;
import org.junit.Test;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 业务场景 Demo 22:资源使用率监控(运维 / 政务云)
*
* <p>业务场景:政务云运维中心对计算节点集群的 CPU / 内存综合使用率进行监控,仪表盘分值
* 表示当前综合使用率(0~100),用于容量水位预警与扩容决策,主色采用运维深灰蓝 #37474F。</p>
* <p>实际图表类型:jquick-pdf 无进度图渲染器,<b>实际使用仪表盘 {@link JChartType#Guage} 替代进度图</b>。</p>
*
* <p>模板:{@code report/biz/22_resource_usage.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\22_resource_usage.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt × 400pt(宽 × 高)的方形比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,因此宽度被页面自动收窄时高度会按原比例同步缩放,不会纵向拉伸变形。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz22ResourceUsageTest {
public static final String path = JQuickConstant.path;
@Test
@SuppressWarnings("deprecation")
public void biz22ResourceUsage() throws IOException {
GuageConfig scoreConfig = GuageConfig.builder()
.score(78)
.pointerColor(new Color(55, 71, 79))
.backgroundColor(new Color(236, 239, 241))
.title("RESOURCE USAGE")
.build();
JGuageOption guageOption = JGuageOption.builder().scoreMeter(scoreConfig).build();
JOption option = new JOption();
option.setGuageOption(guageOption);
String svg = JChartRendererFactory.renderChart(JChartType.Guage, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/22_resource_usage.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/22_resource_usage.txt");
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}
Step 3 — run it
The class is a JUnit test, so run it with Maven:
mvn -pl jquick-pdfx test -Dtest=JQuickBiz22ResourceUsageTest
…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 JQuickBiz22ResourceUsageTest().biz22ResourceUsage();
}
Output: D:/test/biz/22_resource_usage.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 withdisplay: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);
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 IDdo 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>
Template syntax cheat sheet
| Syntax | Meaning |
|---|---|
<areaBreak></areaBreak> |
Force the next block onto a fresh page (CSS page-break is not supported). |
<image src='D:/.../x.png'> |
Embed an image. src is a raw path — it is not evaluated as an expression. |
<svg style="width:479pt">${svg}</svg> |
Embed a vector chart produced by JChartRendererFactory. |
// comment |
Line comment. Keep <!-- ... --> comments short — long ones can blank the render. |
<p style="fontSize:9pt; bold:true"> |
Inline styles use camelCase keys, CSS-like values. |
Gotchas worth remembering
-
${var}must sit outside single quotes —'No: ${docNo}'prints the placeholder literally. -
<image src="...">never evaluates${...}; write a literal path. - A
<td>holds text only — nested<p>,<image>or<table>is dropped. - There is no
colspan; emit one<td>per column and use'—'as filler.
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: ← #21 Multi-Line Chart of Ticket Resolution SLA Trends · #23 Radar Chart for Enterprise Customer Risk Assessment →
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/chart/JQuickBiz22ResourceUsageTest.java |
| Template | jquick-pdfx/src/test/resources/report/biz/22_resource_usage.txt |
| Output PDF | D:/test/biz/22_resource_usage.pdf |
| Preview image | images/22.png |

Top comments (0)