今天分享如何產生PieChart和LineChart In Java FX,javafx.scene.chart底下有好幾種chart,而且都很美觀,但工作上只用到剛剛說的兩種,產生相關Chart只要有FXCollection的基本觀念以及用過Excel的同好們,這個技巧應該不難,PieChart Data來源必須是PieChart.Data(ObservableList)物件,LineChart Data來源必須是XYChart.Series<K, V>,範例如下:
Note:細部的設定還是需要查詢API相關函數
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author joombuopre
*/
public class MyFX2 extends Application {
public PieChart createPieChart(){
PieChart pc=new PieChart(); //Initiate PieChart Object
pc.setData(getPieData()); //get PieChart Data in form of PieChart.Data
pc.setLegendSide(Side.RIGHT); // set the position of legend
return pc;
}
public LineChart createLineChart(){
CategoryAxis x=new CategoryAxis(); // x-Axis
NumberAxis y=new NumberAxis(); //y-Axis
LineChart lc= new LineChart(x, y); //set x,y-Axis
lc.setData(getLineData()); //get Data in form of Series<K, V>
return lc;
}
public ObservableList<PieChart.Data> getPieData(){
ObservableList<PieChart.Data> pd = FXCollections.observableArrayList(); // create a fxcollection instance
pd.addAll(new PieChart.Data("UnderInvestigation", 5), //add data
new PieChart.Data("DocPrep", 28),
new PieChart.Data("Software", 33),
new PieChart.Data("Construction", 20),
new PieChart.Data("Completed", 14));
return pd;
}
public ObservableList<XYChart.Series<String, Double>> getLineData(){
ObservableList<XYChart.Series<String, Double>> ld = FXCollections.observableArrayList();
Series<String, Double> total= new Series<>(); //create a Series object for storing data
Series<String, Double> closed= new Series<>(); //create a Series object for storing data
//set lines name
total.setName("Total Items");
closed.setName("Closed Items");
//add line 1 data
total.getData().add(new XYChart.Data("2012/07", 3040));
total.getData().add(new XYChart.Data("2012/08", 3070));
total.getData().add(new XYChart.Data("2012/09", 3098));
total.getData().add(new XYChart.Data("2012/10/15", 3106));
// add line 2 data
closed.getData().add(new XYChart.Data("2012/07", 2752));
closed.getData().add(new XYChart.Data("2012/08", 2797));
closed.getData().add(new XYChart.Data("2012/09", 2835));
closed.getData().add(new XYChart.Data("2012/10/15", 2857));
ld.addAll(total,closed); //add
return ld;
}
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.getChildren().add(createPieChart());
Scene s1=new Scene(root, 1280, 600);
primaryStage.setScene(s1);
primaryStage.setTitle("FPR PieChart ");
StackPane root2=new StackPane();
root2.getChildren().add(createLineChart());
Stage lstage= new Stage();
Scene s2=new Scene(root2, 1280, 600);
lstage.setScene(s2);
lstage.setTitle("FPR LineChart 2");
primaryStage.show();
lstage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}