JFreeChartクラス

広告

グラフ全体を表すオブジェクトはJFreeChartクラスのオブジェクトになります。JFreeChartクラスのオブジェクトに対して、グラフ領域を表すオブジェクト、軸を表すオブジェクト、凡例を表すオブジェクトなどを追加していくことになります。

JFreeChartクラスのクラス図は次のようになっています。

  • java.lang.Object
  • org.jfree.chart.JFreeChart
  • public class JFreeChart extends java.lang.Object implements org.jfree.ui.Drawable, TitleChangeListener, PlotChangeListener, java.io.Serializable, java.lang.Cloneable

用意されているコンストラクタは次の3つです。

コンストラクタ
JFreeChart(Plot plot)
Creates a new chart based on the supplied plot.
JFreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend)
Creates a new chart with the given title and plot.
JFreeChart(java.lang.String title, Plot plot)
Creates a new chart with the given title and plot.

1番目のコンストラクタでは引数にグラフ領域を扱うPlotクラスのオブジェクトを指定します。ここで指定されたPlotクラスのオブジェクトを元にグラフを作成することになります。2番目と3番目ではグラフのタイトルや軸に関する追加の引数を指定します。

それでは1番目のコンストラクタを確認してみます。

Creates a new chart based on the supplied plot. The chart will have a legend 
added automatically, but no title (although you can easily add one later).

Note that the ChartFactory class contains a range of static methods that 
will return ready-made charts, and often this is a more convenient way to 
create charts than using this constructor.

Parameters:
  plot - the plot (null not permitted).

引数に指定したPlotクラスのオブジェクト元にJFreeChartクラスのオブジェクトを作成します。実際にはコンストラクタからオブジェクトを作成するのではなく、別途グラフ毎に用意されたファクトリメソッドを使ってオブジェクトを作成することになります。ファクトリメソッドについては別のページで確認します。

実際の使い方は次のようになります。

JFreeChart chart = new JFreeChart(new Plot());

PlotクラスはAbstractクラスなので、実際にはオブジェクトを直接作成することが出来ませんのでエラーとなります。その為、引数にはPlotクラスを実装したクラスか、用意されているPlotクラスのサブクラスを使用することになります。詳細は次のページで確認します。

( Written by Tatsuo Ikura )