我正在使用 JavaFX 和他们的 Charts 库编写一些“可视化 + 编辑数据”应用程序。我正在使用 MVC 模型,所以图表都准备好在 FXML 文件中初始化并fx:id
在 Controller 类中引用。
所以目前我实际上可以从文件中导入一些数据并通过这段代码将其可视化
public class ExperimentalData {
private List<Double> AB_2; // AB/2, m
private List<Double> MN_2; // MN/2, m
private List<Double> amperage;
private List<Double> voltage;
private List<Double> resistanceApparent;
private List<Double> errorResistanceApparent;
private List<Double> polarizationApparent;
private List<Double> errorPolarizationApparent;
private Integer size;
public ExperimentalData() {
AB_2 = new ArrayList<>();
MN_2 = new ArrayList<>();
amperage = new ArrayList<>();
voltage = new ArrayList<>();
resistanceApparent = new ArrayList<>();
errorResistanceApparent = new ArrayList<>();
polarizationApparent = new ArrayList<>();
errorPolarizationApparent = new ArrayList<>();
size = 0;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public List<Double> getAB_2() {
return AB_2;
}
public void setAB_2(List<Double> AB_2) {
this.AB_2 = AB_2;
}
public List<Double> getMN_2() {
return MN_2;
}
public void setMN_2(List<Double> MN_2) {
this.MN_2 = MN_2;
}
public List<Double> getAmperage() {
return amperage;
}
public void setAmperage(List<Double> amperage) {
this.amperage = amperage;
}
public List<Double> getVoltage() {
return voltage;
}
public void setVoltage(List<Double> voltage) {
this.voltage = voltage;
}
public List<Double> getResistanceApparent() {
return resistanceApparent;
}
public void setResistanceApparent(List<Double> resistanceApparent) {
this.resistanceApparent = resistanceApparent;
}
public List<Double> getErrorResistanceApparent() {
return errorResistanceApparent;
}
public void setErrorResistanceApparent(List<Double> errorResistanceApparent) {
this.errorResistanceApparent = errorResistanceApparent;
}
public List<Double> getPolarizationApparent() {
return polarizationApparent;
}
public void setPolarizationApparent(List<Double> polarizationApparent) {
this.polarizationApparent = polarizationApparent;
}
public List<Double> getErrorPolarizationApparent() {
return errorPolarizationApparent;
}
public void setErrorPolarizationApparent(List<Double> errorPolarizationApparent) {
this.errorPolarizationApparent = errorPolarizationApparent;
}
}
// ExperimentalData is class with fields Lists of same size with Doubles
// and getSize() returns Integer
protected static void addData(LineChart<Double, Double> vesCurve, ExperimentalData experimentalData) {
vesCurve.getData().add(makeCurveData(experimentalData));
}
protected static void initializeWithData(LineChart<Double, Double> vesCurve, ExperimentalData experimentalData) {
vesCurve.getData().clear();
vesCurve.getXAxis().setAutoRanging(true);
addData(vesCurve, experimentalData);
vesCurve.setVisible(true);
}
private static XYChart.Series<Double, Double> makeCurveData(ExperimentalData experimentalData) {
final ArrayList<Double> AB_2 = new ArrayList<>(experimentalData.getAB_2());
final ArrayList<Double> resistanceApparent = new ArrayList<>(experimentalData.getResistanceApparent());
XYChart.Series<Double, Double> pointsSeries = new XYChart.Series<>();
for (int i = 0; i < experimentalData.getSize(); i++) {
double dotX = log10(AB_2.get(i));
double dotY = max(log10(resistanceApparent.get(i)), 0);
pointsSeries.getData().add(new XYChart.Data<>(dotX, dotY));
}
return pointsSeries;
}
但我需要能够使用表格或交互式折线图更改我的数据,因此它完全是双向绑定。目前我没有在图表中实现交互性,但我知道通过将EventListener
s 添加到图表幼稚节点(例如点或路径)的方法。
主要问题是如何在图表中绑定数据,而不是从其他对象添加数据?AFAIK 我什至可以设置在这种关系中计算数据的函数(在我的情况下需要软对数)。
我发现很少有关于绑定的文章,它们对我来说并不清楚。你能建议我一个带有注释的示例代码来了解如何在我的情况下使用它吗?而且我对哪些情况下数据将被某些分配自动解除绑定感兴趣(使用运算符=
)?如果它与分配解除绑定,那么使用它而不是通过引用传递有什么意义?
UPD 我找到了交互式曲线点的这种解决方案
XYChart.Series<Double, Double> series = vesCurve.getData().get(MOD_CURVE_SERIES_CNT - 1);
Axis<Double> xAxis = vesCurve.getXAxis();
Axis<Double> yAxis = vesCurve.getYAxis();
for (XYChart.Data<Double, Double> data : series.getData()) {
Node node = data.getNode();
node.setCursor(Cursor.HAND);
node.setOnMouseDragged(e -> {
Point2D pointInScene = new Point2D(e.getSceneX(), e.getSceneY());
double xAxisLoc = xAxis.sceneToLocal(pointInScene).getX();
double yAxisLoc = yAxis.sceneToLocal(pointInScene).getY();
Double x = xAxis.getValueForDisplay(xAxisLoc);
Double y = yAxis.getValueForDisplay(yAxisLoc);
data.setXValue(x);
data.setYValue(y);
});
在这种情况下,存储在我分配图表点的对象中的数据不会改变。
My object with Lists of data --adding-to--> Chart.Data - data in chart updates automatically
but not backwords!