Javafx-area-chart
JavaFX-面グラフ
エリアチャートは、エリアベースのチャートを描画するために使用されます。 与えられた一連の点と軸の間の面積をプロットします。 一般に、このチャートは2つの量を比較するために使用されます。
以下は、1週間に2人が消費した果物の数を示す面グラフです。
JavaFXでは、面グラフは AreaChart という名前のクラスで表されます。 このクラスは、パッケージ javafx.scene.chart に属します。 このクラスをインスタンス化することにより、JavaFXでAreaChartノードを作成できます。
面グラフを生成する手順
JavaFXで面グラフを生成するには、以下の手順に従います。
ステップ1:クラスを作成する
Javaクラスを作成し、パッケージ javafx.application の Application クラスを継承し、このクラスの* start()*メソッドを次のように実装します。
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
}
}
ステップ2:軸の定義
面グラフのX軸とY軸を定義し、ラベルを設定します。 この例では、X軸は1週間の日数を表し、y軸は消費された果物の単位を表します。
//Defining the X axis
CategoryAxis xAxis = new CategoryAxis();
//Defining the y Axis
NumberAxis yAxis = new NumberAxis(0, 15, 2.5);
yAxis.setLabel("Fruit units");
ステップ3:面グラフの作成
パッケージ javafx.scene.chart の AreaChart という名前のクラスをインスタンス化して、折れ線グラフを作成します。 このクラスのコンストラクターに、前の手順で作成したX軸とY軸を表すオブジェクトを渡します。
//Creating the Area chart
AreaChart<String, Number> areaChart = new AreaChart(xAxis, yAxis);
areaChart.setTitle("Average fruit consumption during one week");
ステップ4:データの準備
*XYChart.Series* クラスをインスタンス化します。 次に、データ(一連のx、y座標)を次のようにこのクラスのObservableリストに追加します-
//Prepare XYChart.Series objects by setting data
XYChart.Series series1 = new XYChart.Series();
series1.setName("John");
series1.getData().add(new XYChart.Data("Monday", 3));
series1.getData().add(new XYChart.Data("Tuesday", 4));
series1.getData().add(new XYChart.Data("Wednesday", 3));
series1.getData().add(new XYChart.Data("Thursday", 5));
series1.getData().add(new XYChart.Data("Friday", 4));
series1.getData().add(new XYChart.Data("Saturday", 10));
series1.getData().add(new XYChart.Data("Sunday", 12));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Jane");
series2.getData().add(new XYChart.Data("Monday", 1));
series2.getData().add(new XYChart.Data("Tuesday", 3));
series2.getData().add(new XYChart.Data("Wednesday", 4));
series2.getData().add(new XYChart.Data("Thursday", 3));
series2.getData().add(new XYChart.Data("Friday", 3));
series2.getData().add(new XYChart.Data("Saturday", 5));
series2.getData().add(new XYChart.Data("Sunday", 4));
ステップ5:エリアチャートにデータを追加する
次のように、前の手順で準備したデータ系列を面グラフに追加します-
//Setting the XYChart.Series objects to area chart
areaChart.getData().addAll(series1,series2);
ステップ6:グループオブジェクトの作成
- start()メソッドで、パッケージ *javafx.scene に属する Group という名前のクラスをインスタンス化して、グループオブジェクトを作成します。
前の手順で作成したAreaChart(ノード)オブジェクトをパラメーターとしてGroupクラスのコンストラクターに渡します。 これは、次のようにグループに追加するために行う必要があります-
Group root = new Group(areaChart);
ステップ7:シーンオブジェクトの作成
パッケージ javafx.scene に属する Scene という名前のクラスをインスタンス化して、シーンを作成します。 このクラスに、前の手順で作成したGroupオブジェクト( root )を渡します。
ルートオブジェクトに加えて、画面の高さと幅を表す2つのdoubleパラメータを、Groupクラスのオブジェクトとともに次のように渡すこともできます。
Scene scene = new Scene(group ,600, 300);
ステップ8:ステージのタイトルを設定する
*Stage* クラスの* setTitle()*メソッドを使用して、ステージにタイトルを設定できます。 *primaryStage* はStageオブジェクトであり、シーンクラスのstartメソッドにパラメーターとして渡されます。
*primaryStage* オブジェクトを使用して、シーンのタイトルを次のように *Sample Application* として設定します。
primaryStage.setTitle("Sample Application");
ステップ9:ステージにシーンを追加する
*Stage* という名前のクラスの* setScene()*メソッドを使用して、Sceneオブジェクトをステージに追加できます。 次の方法を使用して、前の手順で準備したSceneオブジェクトを追加します。
primaryStage.setScene(scene);
ステップ10:ステージのコンテンツを表示する
次のように Stage クラスの* show()*という名前のメソッドを使用して、シーンのコンテンツを表示します。
primaryStage.show();
ステップ11:アプリケーションの起動
次のように、メインメソッドから Application クラスの* launch()*静的メソッドを呼び出して、JavaFXアプリケーションを起動します。
public static void main(String args[]){
launch(args);
}
例
次の表は、1週間にジョンとジェーンが消費した果物の数を示しています。
Day of the Week | Fruits consumed by John | Fruits consumed by Jane |
---|---|---|
Monday | 3 | 1 |
Tuesday | 4 | 3 |
Wednesday | 3 | 4 |
Thursday | 5 | 3 |
Friday | 4 | 3 |
Saturday | 10 | 5 |
Sunday | 12 | 4 |
以下は、JavaFXを使用して上記のデータを示す面グラフを生成するJavaプログラムです。
このコードを AreaChartExample.java という名前のファイルに保存します。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.CategoryAxis;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
public class AreaChartExample extends Application {
@Override
public void start(Stage stage) {
//Defining the X axis
CategoryAxis xAxis = new CategoryAxis();
//defining the y Axis
NumberAxis yAxis = new NumberAxis(0, 15, 2.5);
yAxis.setLabel("Fruit units");
//Creating the Area chart
AreaChart<String, Number> areaChart = new AreaChart(xAxis, yAxis);
areaChart.setTitle("Average fruit consumption during one week");
//Prepare XYChart.Series objects by setting data
XYChart.Series series1 = new XYChart.Series();
series1.setName("John");
series1.getData().add(new XYChart.Data("Monday", 3));
series1.getData().add(new XYChart.Data("Tuesday", 4));
series1.getData().add(new XYChart.Data("Wednesday", 3));
series1.getData().add(new XYChart.Data("Thursday", 5));
series1.getData().add(new XYChart.Data("Friday", 4));
series1.getData().add(new XYChart.Data("Saturday", 10));
series1.getData().add(new XYChart.Data("Sunday", 12));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Jane");
series2.getData().add(new XYChart.Data("Monday", 1));
series2.getData().add(new XYChart.Data("Tuesday", 3));
series2.getData().add(new XYChart.Data("Wednesday", 4));
series2.getData().add(new XYChart.Data("Thursday", 3));
series2.getData().add(new XYChart.Data("Friday", 3));
series2.getData().add(new XYChart.Data("Saturday", 5));
series2.getData().add(new XYChart.Data("Sunday", 4));
//Setting the XYChart.Series objects to area chart
areaChart.getData().addAll(series1,series2);
//Creating a Group object
Group root = new Group(areaChart);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Setting title to the Stage
stage.setTitle("Area Chart");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args)
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac AreaChartExample.java
java AreaChartExample
実行時に、上記のプログラムは、以下に示すようなエリアチャートを表示するJavaFXウィンドウを生成します。