では、前回の「統計値を表形式でPDFに出力」に 条件付き書式(平均値以上を緑、未満を赤で表示) を加えるサンプルを紹介します。
サンプルコード:条件付き書式付き PdfPTable
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class ChartPDFWithConditionalFormatting extends JPanel {
private List<Map<Integer, Integer>> datasets;
private List<String> labels;
public ChartPDFWithConditionalFormatting(List<Map<Integer, Integer>> datasets, List<String> labels) {
this.datasets = datasets;
this.labels = labels;
setPreferredSize(new Dimension(600, 400));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// グラフ描画は省略(前回と同じ)
}
public static void main(String[] args) {
// ダミーデータ
Map<Integer, Integer> dataset1 = new LinkedHashMap<>();
dataset1.put(1, 3);
dataset1.put(2, 5);
dataset1.put(3, 2);
dataset1.put(4, 7);
Map<Integer, Integer> dataset2 = new LinkedHashMap<>();
dataset2.put(1, 2);
dataset2.put(2, 4);
dataset2.put(3, 6);
dataset2.put(4, 5);
List<Map<Integer, Integer>> datasets = Arrays.asList(dataset1, dataset2);
List<String> labels = Arrays.asList("データセット1", "データセット2");
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("chart_with_conditional_table.pdf"));
document.open();
document.add(new Paragraph("=== 統計値表(条件付き書式) ===\n\n"));
PdfPTable table = new PdfPTable(5); // 列数: ラベル, 合計, 平均, 最大, 最小
table.setWidthPercentage(100);
// ヘッダー行
Stream.of("データセット", "合計", "平均", "最大値", "最小値")
.forEach(h -> {
PdfPCell cell = new PdfPCell(new Phrase(h));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
});
// 各データセットの統計値
for (int i = 0; i < datasets.size(); i++) {
Map<Integer, Integer> data = datasets.get(i);
int sum = data.values().stream().mapToInt(Integer::intValue).sum();
double avg = sum / (double) data.size();
int max = Collections.max(data.values());
int min = Collections.min(data.values());
table.addCell(labels.get(i));
table.addCell(String.valueOf(sum));
table.addCell(String.format("%.2f", avg));
// 最大値セル
PdfPCell maxCell = new PdfPCell(new Phrase(String.valueOf(max)));
maxCell.setBackgroundColor(BaseColor.YELLOW);
table.addCell(maxCell);
// 最小値セル
table.addCell(String.valueOf(min));
// --- 条件付き書式(各値を平均と比較して色付け) ---
// ここではデータセット内の各値を別途出力してみる
PdfPTable subTable = new PdfPTable(data.size());
for (int val : data.values()) {
PdfPCell valCell = new PdfPCell(new Phrase(String.valueOf(val)));
if (val >= avg) {
valCell.setBackgroundColor(BaseColor.GREEN);
} else {
valCell.setBackgroundColor(BaseColor.PINK);
}
subTable.addCell(valCell);
}
// サブテーブルを1行として追加
PdfPCell subTableCell = new PdfPCell(subTable);
subTableCell.setColspan(5);
table.addCell(subTableCell);
}
document.add(table);
document.close();
System.out.println("chart_with_conditional_table.pdf に保存しました。");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java✅ 実行結果
- chart_with_conditional_table.pdf が生成されます
- 統計値表の下に「各データの値」が並び、平均値以上は緑、未満は赤(ピンク)で色付けされます
- 最大値セルは引き続き黄色で強調表示
ポイント
PdfPCellにsetBackgroundColor()を使って条件付き書式を実現- 平均値との比較でセルの色を変えることで、データの傾向が直感的に分かる
- サブテーブルを使うと「統計値表+詳細データ」を1つのまとまりで出力できる
👉 これで「条件付き書式付き統計表のPDF出力」が完成しました。

