前言
Tablesaw是一個用來進行數據分析和可視化顯示的java庫,這里是官方文檔,類似python中的Pandas庫。
引入maven依賴
<dependency>
<groupId>tech.tablesaw</groupId>
<artifactId>tablesaw-jsplot</artifactId>
<version>0.38.1</version>
</dependency>
簡單使用
自己創建行列數據
import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;
public class TestTable {
public static void main(String[] args) {
String[] students = {"小明", "小華", "小紅"};
//語文分數
int[] chineseScores = {76, 63, 87};
//數學分數
int[] mathScores = {90, 84, 99};
//英語分數
int[] englishScores = {76, 63, 87};
Table table = Table.create("學生分數統計表").addColumns(
StringColumn.create("姓名", students),
IntColumn.create("語文分數", chineseScores),
IntColumn.create("數學分數", mathScores),
IntColumn.create("英語分數", englishScores)
);
System.out.println(table.print());
}
}
制作數據表格,輸出為
學生分數統計表
姓名 | 語文分數 | 數學分數 | 英語分數 |
---------------------------------
小明 | 76 | 90 | 76 |
小華 | 63 | 84 | 63 |
小紅 | 87 | 99 | 87 |
從CSV文件中讀取數據
關於csv文件格式的介紹,student_csv.csv文件內容如下
學生,語文分數,數學分數,英語分數
小明,86,90,76
小華,93,84,63
小紅,72,99,87
import java.io.IOException;
import tech.tablesaw.api.Table;
public class TestCsvTable {
public static void main(String[] args) throws IOException {
Table table = Table.read().file("D:/Temp/student_csv.csv");
System.out.println(table.print());
}
}
從excel文件中讀取數據
需要引入操作excel的maven依賴
<dependency>
<groupId>tech.tablesaw</groupId>
<artifactId>tablesaw-excel</artifactId>
<version>0.38.1</version>
</dependency>
excel文件內容如下
import java.io.IOException;
import tech.tablesaw.api.Table;
public class TestExcelTable {
public static void main(String[] args) throws IOException {
Table table = Table.read().file("D:/Temp/students.xlsx");
System.out.println(table.print());
}
}
內部使用Apache的POI庫來操作excel。
數據過濾和匯總
import java.io.IOException;
import tech.tablesaw.aggregate.AggregateFunctions;
import tech.tablesaw.api.QuerySupport;
import tech.tablesaw.api.Table;
public class TestTableStatistics {
public static void main(String[] args) throws IOException {
Table table = Table.read().csv("D:/Temp/student_csv.csv");
//過濾語文分數大於等於80的學生
Table filterResult = table
.where(QuerySupport.all(t -> t.intColumn("語文分數").isGreaterThanOrEqualTo(80)));
System.out.println(filterResult);
//統計語文分數的平均值,最大值和最小值
Table summarizeResult = table
.summarize("語文分數", AggregateFunctions.mean, AggregateFunctions.max, AggregateFunctions.min)
.apply();
System.out.println(summarizeResult);
}
}
輸出為
student_csv.csv
學生 | 語文分數 | 數學分數 | 英語分數 |
---------------------------------
小明 | 86 | 90 | 76 |
小華 | 93 | 84 | 63 |
student_csv.csv summary
Mean [語文分數] | Max [語文分數] | Min [語文分數] |
---------------------------------------------------
83.66666666666667 | 93 | 72 |
數據可視化
import java.io.IOException;
import tech.tablesaw.api.Table;
import tech.tablesaw.plotly.Plot;
import tech.tablesaw.plotly.components.Figure;
import tech.tablesaw.plotly.components.Layout;
import tech.tablesaw.plotly.traces.BarTrace;
import tech.tablesaw.plotly.traces.BarTrace.Orientation;
public class TestTableVisualzation {
public static void main(String[] args) throws IOException {
dataVisualization();
}
private static void dataVisualization() throws IOException {
Table table = createTable();
Layout layout = Layout.builder()
.title("學生分數統計表")
.height(700)
.width(900)
.build();
Figure figure = new Figure(layout,
createBarTrace(table, "語文分數"),
createBarTrace(table, "數學分數"),
createBarTrace(table, "英語分數"));
Plot.show(figure);
}
private static Table createTable() throws IOException {
return Table.read().csv("D:/Temp/student_csv.csv");
}
private static BarTrace createBarTrace(Table table, String numberColumnName) {
return BarTrace.builder(table.categoricalColumn("學生"), table.numberColumn(numberColumnName))
.orientation(Orientation.VERTICAL)
.name(numberColumnName)
.build();
}
}
根據數據創建條形圖

內部使用 pebble模板引擎 來生成HTML文件,關於創建更多類型的圖表,請查看官方文檔。