- Home ›
- JFreeChartを使ったグラフ作成 ›
- 凡例の設定 ›
- HERE
パディングの設定(setPadding)
広告
凡例のアイテムなどが表示される部分と枠線との間のパディングを設定することが可能です。パディングを設定するにはLegendTitleクラスの親クラスである「org.jfree.chart.block.AbstractBlock」で用意されている「setPadding」メソッドを使います。
setPadding public void setPadding(double top, double left, double bottom, double right)
Sets the padding. Parameters: top - the top padding. left - the left padding. bottom - the bottom padding. right - the right padding.
引数には上左下右のパディングのサイズをdouble型の値でそれぞれ指定します。
例えば次のように記述します。
JFreeChart chart = ChartFactory.createPieChart(...); /* 引数は省略 */ LegendTitle legend = chart.getLegend(); title.setPadding(2d, 2d, 2d, 2d);
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import org.jfree.chart.JFreeChart; import org.jfree.chart.ChartFactory; import org.jfree.data.general.DefaultPieDataset; import javax.swing.JFrame; import java.awt.BorderLayout; import org.jfree.chart.ChartPanel; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.plot.Plot; import java.awt.Font; public class Test12_1 extends JFrame{ public static void main(String[] args) { Test12_1 frame = new Test12_1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 500, 500); frame.setTitle("グラフサンプル"); frame.setVisible(true); } Test12_1(){ JFreeChart chart = ChartFactory.createPieChart("夏の旅行", createData(), true, false, false); LegendTitle legend = chart.getLegend(); legend.setItemFont(new Font("MS 明朝", Font.PLAIN, 16)); legend.setPadding(5d, 5d, 5d, 5d); ChartPanel cpanel = new ChartPanel(chart); getContentPane().add(cpanel, BorderLayout.CENTER); } private DefaultPieDataset createData(){ DefaultPieDataset data = new DefaultPieDataset(); data.setValue("海外", 30); data.setValue("国内", 60); data.setValue("行かない", 8); data.setValue("未定", 2); return data; } }
上記をコンパイルした後で実行すると次のようにJavaアプリケーションが起動します。
今回はパディングを5ピクセルに設定してあります。なお他の例として15ピクセルに設定した場合は次のようになります。
( Written by Tatsuo Ikura )