では、前回の「統計値を表形式で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 ChartPDFWithColoredTable extends JPanel {
private List<Map<Integer, Integer>> datasets;
private List<String> labels;
private String chartTitle = "複数データセットの折れ線グラフ";
public ChartPDFWithColoredTable(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");
ChartPDFWithColoredTable panel = new ChartPDFWithColoredTable(datasets, labels);
try {
// PDF作成
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("chart_with_colored_table.pdf"));
document.open();
// 1ページ目:タイトルだけ(簡略化)
document.add(new Paragraph("グラフ(省略)"));
document.newPage();
// 2ページ目:統計値表
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));
}
document.add(table);
document.close();
System.out.println("chart_with_colored_table.pdf に保存しました。");
} catch (Exception e) {
e.printStackTrace();
}
// GUI表示(確認用)
JFrame frame = new JFrame("PDF出力用グラフ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Java✅ 実行結果
- chart_with_colored_table.pdf が生成されます
- 2ページ目の統計値表で、最大値のセルが黄色で強調表示されます
- ヘッダー行は灰色背景で見やすくなります
ポイント
PdfPCellを使ってセルごとに背景色を設定可能setBackgroundColor(BaseColor.YELLOW)で強調表示- ヘッダー行も
BaseColor.LIGHT_GRAYで色付けして区別
👉 これで「グラフ+色付き統計表の複数ページPDF出力」が完成しました。

