为什么xAxis tickUnit在fxml文件中不起作用?

时间:2017-12-19 08:37:29

标签: javafx charts fxml scatter

我想通过fxml文件创建一个散点图。我把下面的代码放在我的fxml文件中但是当它完成渲染时,它不像我预期的那样。有人知道发生了什么吗?

<BorderPane xmlns="http://javafx.com/javafx/8.0.121"
      xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ChartController">
<left>
    <ScatterChart fx:id="memoryDistribution" prefWidth="360" title="Memory Distribution">
        <xAxis>
            <NumberAxis fx:id="xAxis" label="Memory Address/k" lowerBound="0" upperBound="7" tickUnit="1.00"/>
        </xAxis>
        <yAxis>
            <NumberAxis fx:id="yAxis" label="Memory Address/k" lowerBound="1" upperBound="257" tickUnit="8.00"/>
        </yAxis>
    </ScatterChart>
</left>

上面的代码是这样的:

wrong expectation

但我的期望是这样的:

expectation 第二个图像代码:

    public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

    primaryStage.setTitle("Scatter Chart Sample");
    final NumberAxis xAxis = new NumberAxis(0, 8, 1);
    final NumberAxis yAxis = new NumberAxis(1, 257, 8);
    final ScatterChart<Number,Number> sc = new
            ScatterChart<>(xAxis,yAxis);
    xAxis.setLabel("Memory Address/k");
    yAxis.setLabel("Memory Address/k");
    sc.setTitle("Memory Distribution");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("job-1");
    series1.getData().add(new XYChart.Data(0.5, 5));
    series1.getData().add(new XYChart.Data(1.5, 5));
    series1.getData().add(new XYChart.Data(2.5, 5));
    series1.getData().add(new XYChart.Data(3.5, 5));
    series1.getData().add(new XYChart.Data(4.5, 5));
    series1.getData().add(new XYChart.Data(5.5, 5));
    series1.getData().add(new XYChart.Data(6.5, 5));
    series1.getData().add(new XYChart.Data(7.5, 5));

    series1.getData().add(new XYChart.Data(0.5, 13));
    series1.getData().add(new XYChart.Data(1.5, 13));
    series1.getData().add(new XYChart.Data(2.5, 13));
    series1.getData().add(new XYChart.Data(3.5, 13));
    series1.getData().add(new XYChart.Data(4.5, 13));
    series1.getData().add(new XYChart.Data(5.5, 13));

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("job-2");
    series2.getData().add(new XYChart.Data(0.5, 29));
    series2.getData().add(new XYChart.Data(1.5, 29));
    series2.getData().add(new XYChart.Data(2.5, 29));
    series2.getData().add(new XYChart.Data(3.5, 29));
    series2.getData().add(new XYChart.Data(4.5, 29));
    series2.getData().add(new XYChart.Data(5.5, 29));


    sc.getData().addAll(series1, series2);

    Scene chartScene = new Scene(sc,360,736);//best lookup

    primaryStage.setScene(chartScene);
    primaryStage.setMaximized(true);
    primaryStage.show();
}


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

}

1 个答案:

答案 0 :(得分:0)

我已经完成了我的要求:网格打开,单元格大小为256。

  1. 我在fxml中添加了任何一种Pane,当init启动它时应该渲染并在其中放置一个空白窗格。

    
        <center>
            <ScrollPane fx:id="chartPane" prefWidth="380" prefHeight="760">
            </ScrollPane>
        </center>
    
  2. 然后我编写代码来初始化散点图并将其放到上面的窗格中。

  3. 在类sample.ChartController中配置fxml,将init代码放在此类中,命名方法初始化。

    
        private void initialize(){
                final NumberAxis xAxis = new NumberAxis(0,8,1);
                final NumberAxis yAxis = new NumberAxis(0,256,8);
                xAxis.setLabel("Memory Address/k");
                yAxis.setLabel("Memory Address/k");
                this.memoryDistributionChart = new ScatterChart(xAxis,yAxis);
                memoryDistributionChart.setTitle("Memory Distribution");
                memoryDistributionChart.setPrefHeight(736);
                memoryDistributionChart.setPrefWidth(360);
                chartPane.setContent(memoryDistributionChart);
        }
    
  4. memoryDistributionChart是一个ScatterChart,一个成员属性 chartPane是另一个成员财产。

    最后,the result like this

    因为当主舞台渲染时,应首先调用控制器初始化方法。