━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 26. Pie Chart

 


Pie Chart 는 주어진 데이터를 원형 그래프로 그려주는 기능 입니다.


1. Main.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <PieChart fx:id="pieChart" title="PIE CHART" />
      <Button layoutX="200.0" layoutY="433.0" mnemonicParsing="false" onAction="#btn" text="Load Chart" />
   </children>
</AnchorPane>
 
cs


3. MainController.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package application;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
 
public class MainController {
 
    @FXML PieChart pieChart;
    
    public void btn(ActionEvent event){
        ObservableList<Data> list = FXCollections.observableArrayList(
            new PieChart.Data("Java"50),    
            new PieChart.Data("c++"20),
            new PieChart.Data("python"30),
            new PieChart.Data("c#"10),
            new PieChart.Data("c"15)
            );
        pieChart.setData(list);
    }
    
}
 
cs



'Java > JavaFx' 카테고리의 다른 글

JavaFX 28. Line Chart  (0) 2016.08.29
JavaFX 27. Event handler for a Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
COMMENT