我想知道为什么我的图表在我的设置后没有获得新的设置,而不是更新轴。
主要课程:
project <- structure(list(ROLL.NO. = c(3138L, 3138L, 3138L, 3138L, 3138L,
3138L, 3138L, 3138L, 3138L, 3138L, 3138L, 3138L, 3138L, 3138L,
3138L, 3138L, 3138L, 3138L, 3138L, 3138L, 3138L, 3129L, 3129L,
3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L,
3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L, 3129L,
3129L, 3129L, 3129L, 3121L, 3121L, 3121L, 3121L, 3121L, 3121L
), DC31 = c(2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L,
1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 2L), D14 = c(2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 0L, 1L, 2L, 2L, 0L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L), HOUSE.NO = c("14/274",
"14/259", "14/217", "14/258", "14/306", "14/300", "14/96", "14/166",
"14/69", "14/68", "14/16", "14/93", "14/130", "14/321", "14/324",
"14/139", "14/314", "14/323", "14/208", "14/78", "14/150", "14/155",
"14/102", "14/132", "14/159", "14/163", "14/165", "14/146", "14/148",
"14/104", "14/56", "14/53", "14/99", "14/48", "15/164", "15/148",
"15/158", "15/107", "15/160", "15/162", "15/243", "15/66", "15/249",
"15/86", "14/388", "14/396", "14/431", "14/401", "14/103", "15/36"
)), .Names = c("ROLL.NO.", "DC31", "D14", "HOUSE.NO"), row.names = c(NA,
50L), class = "data.frame")
控制器类;单击“应用”按钮后,您可以在轴X和Y上设置您想要的变量,也可以使用一些验证,
package sample;
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) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 1200, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Axes class用于设置我想要在Axes上拥有的每个变量。
package sample;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Side;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.TextField;
public class Controller {
@FXML
private TextField xMin, xMax, yMin, yMax, xTick, yTick, width, height;
@FXML
private LineChart<Number, Number> axesTest;
@FXML
private NumberAxis xAxis, yAxis;
private Axes axes;
public void applyButtonClicked(ActionEvent event) {
System.out.println("Applyed...");
axes = new Axes();
axes.setxMin(Double.parseDouble(xMin.getText()));
axes.setxMax(Double.parseDouble(xMax.getText()));
axes.setyMin(Double.parseDouble(yMin.getText()));
axes.setyMax(Double.parseDouble(yMax.getText()));
axes.setxTickUnit(Double.parseDouble(xTick.getText()));
axes.setyTickUnit(Double.parseDouble(yTick.getText()));
axes.setAxesWidth(Integer.parseInt(width.getText()));
axes.setAxesHeight(Integer.parseInt(height.getText()));
xAxis = new NumberAxis(axes.getxMin(), axes.getxMax(), axes.getxTickUnit());
xAxis.setSide(Side.BOTTOM);
xAxis.setMinorTickVisible(false);
xAxis.setPrefWidth(axes.getAxesWidth());
xAxis.setLayoutY(axes.getAxesHeight()/2);
xAxis.autosize();
xAxis.setLowerBound(axes.getxMin());
xAxis.setUpperBound(axes.getxMax());
xAxis.setTickUnit(axes.getxTickUnit());
yAxis = new NumberAxis(axes.getyMin(), axes.getyMax(), axes.getyTickUnit());
yAxis.setSide(Side.LEFT);
yAxis.setMinorTickVisible(false);
yAxis.setPrefHeight(axes.getAxesHeight());
yAxis.layoutXProperty().bind(Bindings.subtract((axes.getAxesWidth() / 2) + 1, yAxis.widthProperty()));
yAxis.setLowerBound(axes.getyMin());
yAxis.setUpperBound(axes.getyMax());
yAxis.setTickUnit(axes.getyTickUnit());
axesTest = new LineChart<Number, Number>(xAxis, yAxis);
// System.out.println("xMin : " + axes.getxMin() + "xMax : " + axes.getxMax() + "yMin :" + axes.getyMin() + "yMax : " + axes.getyMax()
// + "xTick : " + axes.getxTickUnit() + "yTick : " + axes.getyTickUnit());
}
public void startButtonClicked() {
}
}
sample.fxml文件,我在场景构建器中创建的每一件事。
package sample;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.Pane;
public class Axes extends Pane {
private NumberAxis xAxis;
private NumberAxis yAxis;
private double xMin, xMax, yMin, yMax, xTickUnit, yTickUnit;
private int axesWidth, axesHeight;
public Axes() {
this.xMin = 0;
this.xMax = 0;
this.yMin = 0;
this.yMax = 0;
this.xTickUnit = 0;
this.yTickUnit = 0;
this.axesWidth = 0;
this.axesHeight = 0;
}
public NumberAxis getXAxis() {
return xAxis;
}
public NumberAxis getYAxis() {
return yAxis;
}
public int getAxesWidth() {
return axesWidth;
}
public void setAxesWidth(int axesWidth) {
this.axesWidth = axesWidth;
}
public int getAxesHeight() {
return axesHeight;
}
public void setAxesHeight(int axesHeight) {
this.axesHeight = axesHeight;
}
public double getxMin() {
return xMin;
}
public void setxMin(double xMin) {
this.xMin = xMin;
}
public double getxMax() {
return xMax;
}
public void setxMax(double xMax) {
this.xMax = xMax;
}
public double getyMin() {
return yMin;
}
public void setyMin(double yMin) {
this.yMin = yMin;
}
public double getyMax() {
return yMax;
}
public void setyMax(double yMax) {
this.yMax = yMax;
}
public double getxTickUnit() {
return xTickUnit;
}
public void setxTickUnit(double xTickUnit) {
this.xTickUnit = xTickUnit;
}
public double getyTickUnit() {
return yTickUnit;
}
public void setyTickUnit(double yTickUnit) {
this.yTickUnit = yTickUnit;
}
}
边框窗格的中心应该是启动应用程序默认轴,单击“应用”按钮后应显示我想要的轴。它没有更新它,所以我的问题是:我做错了什么?或者我在代码中缺少什么?感谢。
答案 0 :(得分:2)
您遇到的第一个问题是,每次单击“应用”按钮时,都会添加NumberAxis
和LineChart
的新实例。这些实例不会添加到场景图中,而您已经拥有的那些(使用@FXML注释并由FXMLLoader
创建的实例)不会收到任何通知。
所以你必须删除它:
public void applyButtonClicked(ActionEvent event) {
xAxis = new NumberAxis(axes.getxMin(), axes.getxMax(), axes.getxTickUnit());
yAxis = new NumberAxis(axes.getyMin(), axes.getyMax(), axes.getyTickUnit());
axesTest = new LineChart<Number, Number>(xAxis, yAxis);
}
然后设置:
public void applyButtonClicked(ActionEvent event) {
//xAxis = new NumberAxis(axes.getxMin(), axes.getxMax(), axes.getxTickUnit());
xAxis.setLowerBound(axes.getxMin());
xAxis.setUpperBound(axes.getxMax());
xAxis.setTickUnit(axes.getxTickUnit());
//yAxis = new NumberAxis(axes.getyMin(), axes.getyMax(), axes.getyTickUnit());
yAxis.setLowerBound(axes.getyMin());
yAxis.setUpperBound(axes.getyMax());
yAxis.setTickUnit(axes.getyTickUnit());
//axesTest = new LineChart<Number, Number>(xAxis, yAxis);
}
第二个问题:默认情况下,轴启用了自动范围,因此所有应用的更改都将被覆盖。
所以只需禁用它:
public void initialize() {
axes = new Axes();
xAxis.setAutoRanging(false);
yAxis.setAutoRanging(false);
}
最后,你无法绑定layoutX属性,因为它已经绑定了。而是使用translateX属性:
yAxis.translateXProperty().bind(Bindings.subtract((axes.getAxesWidth() / 2) + 1, yAxis.widthProperty()));