- Home ›
- JFreeChartを使ったグラフ作成 ›
- 時系列グラフ(TimeSeriesChart) ›
- HERE
時系列グラフの描画領域(XYPlotクラス)
グラフの描画領域を取得し、描画領域に対する設定を行っていきます。ファクトリメソッドを使ってJFreeChartクラスのオブジェクトを作成した場合は、JFreeChartクラスで用意されている「getPlot」メソッドを使ってPlotクラスのオブジェクトを取得します。
getPlot public Plot getPlot()
Returns the plot for the chart. The plot is a class responsible for coordinating the visual representation of the data, including the axes (if any). Returns: The plot.
時系列グラフの場合には、取得したPlotクラスのオブジェクトを、PlotクラスのサブクラスであるXYPlotクラスにキャストして取得して使います。例えば次のように記述します。
JFreeChart chart = ChartFactory.createTimeSeriesChart(...); /* 引数は省略 */ XYPlot plot = (XYPlot)chart.getPlot();
ではXYPlotクラスについて確認します。
XYPlotクラス
XYPlotクラスのクラス図は次のようになっています。
- java.lang.Object
- org.jfree.chart.plot.Plot
- org.jfree.chart.plot.XYPlot
- public class XYPlot extends Plot implements ValueAxisPlot, Zoomable, RendererChangeListener, java.lang.Cloneable, org.jfree.util.PublicCloneable, java.io.Serializable
用意されているコンストラクタは次の2つです。
| コンストラクタ |
|---|
| XYPlot() Creates a new XYPlot instance with no dataset, no axes and no renderer. |
| XYPlot(XYDataset dataset, ValueAxis domainAxis, ValueAxis rangeAxis, XYItemRenderer renderer) Creates a new plot with the specified dataset, axes and renderer. |
今回はコンストラクタでオブジェクトを直接生成するのではなく、JFreeChartクラスの「getPlot」メソッドでオブジェクトを取得します。
取得したXYPlotクラスのオブジェクトは時系列グラフの描画領域を表すオブジェクトです。XYPlotクラスで定義されているメソッドや、親クラスのPlotクラスで用意されているメソッドを使用して描画領域に対する設定を行うことが可能です。
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.Month;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.XYPlot;
import java.awt.Color;
public class Test4_1 extends JFrame{
public static void main(String[] args) {
Test4_1 frame = new Test4_1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(10, 10, 500, 500);
frame.setTitle("グラフサンプル");
frame.setVisible(true);
}
Test4_1(){
JFreeChart chart =
ChartFactory.createTimeSeriesChart("PV推移",
"月",
"PV",
createData(),
true,
false,
false);
XYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(Color.ORANGE);
ChartPanel cpanel = new ChartPanel(chart);
getContentPane().add(cpanel, BorderLayout.CENTER);
}
private TimeSeriesCollection createData(){
TimeSeriesCollection data = new TimeSeriesCollection();
Month[] month = new Month[6];
for (int i = 0 ; i < 6 ; i++){
month[i] = new Month(i + 7, 2007);
}
int graphdata[][] = {{120, 140, 150, 190, 230, 280}, /* Aサイト */
{400, 380, 410, 200, 150, 80}, /* Bサイト */
{150, 170, 160, 180, 150, 170}}; /* Cサイト */
TimeSeries[] series = new TimeSeries[3];
series[0] = new TimeSeries("Aサイト", Month.class);
series[1] = new TimeSeries("Bサイト", Month.class);
series[2] = new TimeSeries("Cサイト", Month.class);
for (int i = 0 ; i < 3 ; i++){
for (int j = 0 ; j < 6 ; j++){
series[i].add(month[j], graphdata[i][j]);
}
data.addSeries(series[i]);
}
return data;
}
}
上記をコンパイルした後で実行すると次のようにJavaアプリケーションが起動します。
今回は例として描画領域の背景色をオレンジに変更しました。また上記のオレンジの箇所がグラフの描画領域となります。
( Written by Tatsuo Ikura )
JavaDrive