旅游网站之数据可视化
第1关:词云的绘制
package com.showdata;
import java.awt.Dimension;
import java.io.IOException;
import java.util.List;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
public class WordCloud_img {
public WordCloud get() throws IOException {
FrequencyAnalyzer fr = new FrequencyAnalyzer();
fr.setWordFrequenciesToReturn(200);
List<WordFrequency> wordFrequencies = fr.load("wordcloud.txt");
Dimension dimension = new Dimension(500, 312);
WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("imgs/wordcloud_img.png");
return wordCloud;
}
}
第2关:词云的渲染
package com.showdata;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.util.List;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.palette.ColorPalette;
import com.kennycason.kumo.wordstart.CenterWordStart;
import java.awt.Font;
public class WordCloud_render {
public WordCloud get() throws IOException {
FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
frequencyAnalyzer.setWordFrequenciesToReturn(200);
List<WordFrequency> wordFrequencies = frequencyAnalyzer.load("wordcloud.txt");
Dimension dimension = new Dimension(500, 312);
WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
java.awt.Font font = new java.awt.Font("宋体", 3, 24);
wordCloud.setKumoFont(new KumoFont(font));
wordCloud.setPadding(2);
wordCloud.setBackgroundColor(Color.WHITE);
wordCloud.setBackground(new PixelBoundryBackground("myImgs/whale_small.png"));
wordCloud.setColorPalette(new ColorPalette(Color.RED, Color.BLUE, Color.GREEN));
wordCloud.setWordStartStrategy(new CenterWordStart());
wordCloud.setAngleGenerator(new AngleGenerator(0));
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("imgs/wordcloud_render.png");
return wordCloud;
}
}
第3关:获取酒店评论数据生成词云
package com.showdata;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.palette.LinearGradientColorPalette;
import com.kennycason.kumo.wordstart.CenterWordStart;
import com.util.HBaseUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CommentWordCloud {
public WordCloud get() throws IOException {
Connection conn = HBaseUtil.getConnection();
TableName tableName = TableName.valueOf(Bytes.toBytes("comment_word_count"));
Table table = conn.getTable(tableName);
ResultScanner scanner = table.getScanner(new Scan());
List<WordFrequency> words = new ArrayList<>();
for (Result result : scanner) {
String word = new String(result.getRow(), "utf-8");
int count = Bytes.toInt(result.getValue("word_info".getBytes(), "count".getBytes()));
WordFrequency wordFrequency = new WordFrequency(word, count);
if (count > 10) {
words.add(wordFrequency);
}
}
Dimension dimension = new Dimension(500, 312);
WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
wordCloud.setPadding(2);
Font font = new Font("宋体", 2, 24);
wordCloud.setKumoFont(new KumoFont(font));
wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
wordCloud.setBackgroundColor(Color.WHITE);
wordCloud.setBackground(new PixelBoundryBackground("myImgs/whale_small.png"));
wordCloud.setWordStartStrategy(new CenterWordStart());
wordCloud.setAngleGenerator(new AngleGenerator(0));
wordCloud.build(words);
wordCloud.writeToFile("imgs/wordcloud_comment.png");
return wordCloud;
}
}