


商品スタッフII
JFreeChart1.0.11以降ではグラフ中の日本語が文字化けになる(□~いわゆるトーフ表示)。
1.0.11以降のJFreeChartでは「テーマ」設定ができるようになっており、デフォルトのテーマのフォントが「tahoma」に指定されているため、日本語が表示できない。
以下のようにレガシーテーマを設定することで解消する。
import java.awt.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.data.general.*;
public class Test extends JFrame{
public static void main(String[] args) {
Test frame = new Test();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 400, 400);
frame.setTitle("グラフサンプル");
frame.setVisible(true);
}
Test(){
// レガシーテーマを設定
ChartFactory.setChartTheme(
StandardChartTheme.createLegacyTheme()
);
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("佐藤", 25);
data.setValue("鈴木", 55);
data.setValue("小林", 15);
data.setValue("その他", 5);
JFreeChart chart =
ChartFactory.createPieChart("タイトル",
data,
true,
false,
false);
ChartPanel cpanel = new ChartPanel(chart);
getContentPane().add(cpanel, BorderLayout.CENTER);
}
}