Skip to content

Product review keyword word cloud (user research) — generate this PDF with Java (WORDCLOUD)

JQuick-PdfBiz Demo

WORDCLOUD1 页 A4 · 440 KB

A word cloud built from token statistics of 50,000 user reviews: top words such as stable, efficient and easy to use appear in very large tilted type while rare negative words scatter in small type, font size mapping directly to frequency.

Download the PDFView raw template fileBack to all demos

Product review keyword word cloud (user research) — WORDCLOUD
Rendered output

Output specs

PDF pages
1
Chart type
WORDCLOUD
Template lines
36
Source lines
111

Java source · JQuickBiz67WordCloudTest.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.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();
    }
}

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/67_wordcloud.txt");

Template file · 67_wordcloud.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>
  // 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>

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.