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

JavaFX 28. Line 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
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="528.0" prefWidth="614.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <LineChart fx:id="lineChart" layoutY="-1.0" prefHeight="408.0" prefWidth="614.0" title="LINE CHART">
        <xAxis>
          <CategoryAxis label = "My X Label" side="BOTTOM" />
        </xAxis>
        <yAxis>
          <NumberAxis label = "My Y Label" side="LEFT" />
        </yAxis>
      </LineChart>
      <Button layoutX="250.0" layoutY="453.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
package application;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
 
public class MainController {
    @FXML LineChart<String, Number> lineChart;
    
    public void btn(ActionEvent event) {
        lineChart.getData().clear();
        XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
        series.getData().add(new XYChart.Data<String, Number>("Jan"200));
        series.getData().add(new XYChart.Data<String, Number>("Feb"500));
        series.getData().add(new XYChart.Data<String, Number>("Mar"300));
        series.getData().add(new XYChart.Data<String, Number>("Apr"600));
        series.setName("Month Pay");
        lineChart.getData().add(series);
    }
}
 
cs



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

JavaFX 30. Event Handler for a Line Chart  (0) 2016.08.29
JavaFX 29. Multiple Line Chart  (0) 2016.08.29
JavaFX 27. Event handler for a Pie Chart  (0) 2016.08.29
JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
COMMENT