Java 生成「产品用户评价关键词词云(用户研究)」PDF 示例(WORDCLOUD)
WORDCLOUD1 页 A4 · 440 KB
基于 50,000 条用户评价分词统计的词云:稳定、高效、易用等头部词以超大字号倾斜排列,长尾负面词小字散布,字体大小直接对应出现频次。

输出规格
- PDF 页数
- 1
- 图表类型
- WORDCLOUD
- 模板行数
- 36
- 源码行数
- 111
Java 源码 · JQuickBiz67WordCloudTest.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.config.JPdfConfig;
import com.github.paohaijiao.data.JData;
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.words.JWordCloudSeries;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 业务场景 Demo 67:产品用户评价关键词词云(用户研究)
*
* <p>业务场景:用户研究部门对 50,000 条用户评价进行 NLP 分词统计,
* 以词云图展示高频关键词,字体越大代表出现频次越高。</p>
* <p>实际图表类型:{@link JChartType#WORDCLOUD} 词云图。</p>
*
* <p>模板:{@code report/biz/67_wordcloud.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\67_wordcloud.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt × 380pt 的横版比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz67WordCloudTest {
public static final String path = JQuickConstant.path;
@Test
public void biz67WordCloud() throws IOException {
List<JData> wordList = new ArrayList<>();
// 词云视觉要点:头部词权重与尾部词拉开数量级差距,字体大小才有明显层次;
// 若权重过于接近(如都在 3000-10000),渲染出来大小趋同、布局像个矩形。
// 头部超高频词
wordList.add(new JData("稳定", 20000));
wordList.add(new JData("高效", 15000));
wordList.add(new JData("易用", 12000));
// 腰部中频词
wordList.add(new JData("界面清晰", 5000));
wordList.add(new JData("响应快", 4200));
wordList.add(new JData("功能丰富", 3600));
wordList.add(new JData("体验好", 3000));
wordList.add(new JData("性价比高", 2600));
wordList.add(new JData("服务周到", 2200));
wordList.add(new JData("更新及时", 1800));
// 尾部长尾低频词
wordList.add(new JData("一般", 800));
wordList.add(new JData("还可以", 600));
wordList.add(new JData("有待提升", 500));
wordList.add(new JData("卡顿", 400));
wordList.add(new JData("闪退", 300));
wordList.add(new JData("加载慢", 240));
wordList.add(new JData("偶尔崩溃", 200));
wordList.add(new JData("耗电", 160));
wordList.add(new JData("广告多", 120));
JWordCloudSeries wordCloudSeries = new JWordCloudSeries("用户评价关键词");
wordCloudSeries.data(wordList);
wordCloudSeries.minFontSize(12);
wordCloudSeries.maxFontSize(96);
// 多种旋转角度(-45 / -15 / +15),避免全部水平排列显得像矩形列表
wordCloudSeries.rotationStep(30);
wordCloudSeries.rotationRange(90);
wordCloudSeries.gridSize(4);
JOption option = new JOption();
option.title("年度产品用户评价关键词词云", "样本量:50,000 条评价 | 字体大小代表出现频次");
option.series(wordCloudSeries);
String svg = JChartRendererFactory.renderChart(JChartType.WORDCLOUD, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/67_wordcloud.pdf");
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource("report/biz/67_wordcloud.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/67_wordcloud.txt");模板文件 · 67_wordcloud.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
// 67 词云图 Demo:以词语出现的频次决定字体大小,直观展示高频关键词
<div style="background:#212121; 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:#BDBDBD; fontSize:9">'字体越大代表出现频次越高,颜色区分不同情感倾向'</p>
</div>
<div style="width:220px; textAlignment:right; borderLeft:1px solid #616161; paddingLeft:10px">
<p style="color:white; fontSize:9">'数据来源:用户评价系统'</p>
<p style="color:#BDBDBD; fontSize:9">'样本量:50,000 条评价'</p>
<p style="color:#BDBDBD; fontSize:9">'编制:用户研究部'</p>
</div>
</div>
</div>
<div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #212121">
<h2 style="color:#212121; marginBottom:6px; fontSize:13; bold:true">'一、用户评价高频关键词'</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 词云图:字体大小对应关键词出现频次,颜色随机区分(数据来源:NLP 分词统计)'</p>
</div>
<list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
<li style="marginBottom:6px"><span style="bold:true; color:#212121">'正面高频词:'</span>'"稳定"、"高效"、"易用" 等词出现频次最高,用户对核心体验认可度高。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#212121">'需关注词:'</span>'"卡顿"、"闪退" 等词仍有一定出现量,需优化性能稳定性。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#212121">'建议:'</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 真实运行产出,可自由复制到你的项目中使用。