【问题标题】:Display chart in JavaFX在 JavaFX 中显示图表
【发布时间】:2015-06-08 00:45:09
【问题描述】:

我尝试重写和简化图表的 oracle 教程,以便您拥有一个控制器类并使用 fxml。 由于某种原因,它无法正常工作。

这是我的代码: 主类:

public class BarChartSample extends Application {

    Stage primaryStage;

    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        primaryStage.setTitle("Bar Chart Sample");

        showPage();
    }

    public void showPage(){
        try {
            FXMLLoader loader = new FXMLLoader();
        loader.setLocation(BarChartSample.class.getResource("ChartPage.fxml"));
            AnchorPane pane = loader.load();

            Scene scene  = new Scene(pane);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

控制器:

public class Controller {
    final static String performance = "Performance:";

    @FXML
    NumberAxis xAxis = new NumberAxis();
    @FXML
    CategoryAxis yAxis = new CategoryAxis();

    @FXML
    BarChart<Number,String> bc;

    public void initialize(){
        xAxis.setLabel("Percent");  
        xAxis.setTickLabelRotation(90);
        yAxis.setLabel("Performance");

        bc = new BarChart<Number,String>(xAxis,yAxis);

        XYChart.Series series1 = new XYChart.Series();     
        series1.getData().add(new XYChart.Data(80, performance));
        bc.getData().add(series1);
    }
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.chart.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="393.0" prefWidth="419.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
   <children>
      <BarChart fx:id="bc" layoutX="-40.0" layoutY="-3.0" prefHeight="205.0" prefWidth="409.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0">
        <xAxis>
          <CategoryAxis fx:id="yAxis" side="BOTTOM" />
        </xAxis>
        <yAxis>
          <NumberAxis side="LEFT" fx:id="xAxis" />
        </yAxis>
      </BarChart>
     <TextArea layoutX="103.0" layoutY="14.0" maxHeight="100.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="21.0"AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="5.0" />
        </children>
      </AnchorPane>

它无法正确显示图表,它只是画了几条线。为什么?谢谢!

【问题讨论】:

    标签: charts javafx


    【解决方案1】:

    initialize()中删除这一行

    bc = new BarChart<Number,String>(xAxis,yAxis);
    

    您不应重新初始化已在 FXML 中定义并已通过 @FXML 注释集成到控制器中的任何控件。

    由于上述原因,您还需要替换以下行

    @FXML
    NumberAxis xAxis = new NumberAxis();
    @FXML
    CategoryAxis yAxis = new CategoryAxis();
    

    @FXML
    NumberAxis xAxis;
    @FXML
    CategoryAxis yAxis;
    

    同样,您需要正确定义 BarChart。在 FXML 中,您已将 xAxis 声明为 CategoryAxis 并将 yAxis 声明为 NumberAxis。但是在控制器中你定义了一个BarChart&lt;Number,String&gt;

    public class Controller {
        final static String performance = "Performance:";
    
        @FXML
        NumberAxis xAxis;
        @FXML
        CategoryAxis yAxis;
    
        @FXML
        BarChart<String, Number> bc;
    
        public void initialize(){
            xAxis.setLabel("Percent");
            xAxis.setTickLabelRotation(90);
            yAxis.setLabel("Performance");
    
            XYChart.Series<String, Number> series1 = new XYChart.Series();
            series1.getData().add(new XYChart.Data( performance, 80));
            bc.getData().add(series1);
        }
    }
    

    【讨论】:

    • 我得到一个转换异常,但我不明白为什么... java.lang.Integer 不能转换为 java.lang.String
    • 是的,我明白了。这是因为在 FXML 中,您已将 xAxis 声明为 CategoryAxis 并将 yAxis 声明为数字轴。但是在控制器中你定义了一个BarChart&lt;Number,String&gt;。请检查更新的答案。
    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2016-12-22
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多