JavaFX

[JavaFX]AreaChart, BarChart, PieChart, XYChart

쟈근꿈틀이 2022. 4. 1. 12:39
728x90

AreaChart는 영역 차트 또는 영역 그래프이다.

BarChart는 막대그래프로 표현 값과 비례하는 높이와 길이를 지닌 직사각형 막대로 데이터를 표현하는 차트이다.

PieChart는 하나의 원을 여러 영역 또는 조각으로 나눈 그래프로, 각 조각은 해당 변수에 대한 백분율을 나타낸다.

 

XYChart와 PieChart는 javafx.scene.chart에 존재한다.

 

FXCollections.observableArrayList로 각 차트를 하나의 배열 객체로 묶어준다.

(x, y)값을 갖는 여러 데이터를 담기 위해서는 XYChart.Series클래스를, (x, y)값을 갖는 하나의 데이터를 저장하기 위해서는 XYChart.Data클래스를 사용하여 객체를 생성한다.

 

pie차트의 각 요소는 PieChart.Data를 사용하고, 하나의 pie차트 객체는 PieChart라는 클래스를 통해 생성한다.

 


 

public class MainController implements Initializable {
                                    @FXML private AreaChart areaChart;
                                    @FXML private BarChart barChart;
                                    @FXML private PieChart pieChart;

                                    @Override
                                    public void initialize(URL location, ResourceBundle resources) {
                                                                        XYChart.Series series1 = new XYChart.Series();

                                                                        series1.setName("평균 온도");
                                                                        series1.setData(FXCollections.observableArrayList(
                                                                                       new XYChart.Data<String, Integer>("2022-01", 5),
                                                                                       new XYChart.Data<String, Integer>("2022-02", 10),
                                                                                       new XYChart.Data<String, Integer>("2022-03", 13),
                                                                                       new XYChart.Data<String, Integer>("2022-04", 19)
                                                                        ));
                                                                        areaChart.getData().add(series1);

                                                                        XYChart.Series series2 = new XYChart.Series();
                                                                        series2.setName("확진자");
                                                                        series2.setData(FXCollections.observableArrayList(
                                                                                 new XYChart.Data<String, Integer>("2022-01", 12345),
                                                                                 new XYChart.Data<String, Integer>("2022-02", 23456),
                                                                                 new XYChart.Data<String, Integer>("2022-03", 34567),
                                                                                 new XYChart.Data<String, Integer>("2022-04", 1234)
                                                                        ));

                                                                        XYChart.Series series3 = new XYChart.Series();

                                                                        series3.setName("중증환자");
                                                                        series3.setData(FXCollections.observableArrayList(
                                                                                 new XYChart.Data<String, Integer>("2022-01", 1233),
                                                                                 new XYChart.Data<String, Integer>("2022-02", 2344),
                                                                                 new XYChart.Data<String, Integer>("2022-03", 3455),
                                                                                 new XYChart.Data<String, Integer>("2022-04", 5666)
                                                                        ));

                                                                        barChart.getData().add(series3);
                                                                        barChart.getData().add(series2);

                                                                        pieChart.setData(FXCollections.observableArrayList(
                                                                                 new PieChart.Data("AWT", 10),
                                                                                 new PieChart.Data("SWT", 25),
                                                                                 new PieChart.Data("Swing", 30),
                                                                                 new PieChart.Data("JavaFX", 35)
                                                                        ));
                                    }
}

 

기본 Area차트(좌), XYChart Series에 이름과 (x,y)값을 설정하여 Area차트에 추가한 모습(우)

 

BarChart에 자식들을 추가하는 순서대로 XYChart.Series객체가 화면에 나타난다

728x90