gpt4 book ai didi

java - 如何创建带有数字轴或字符串轴的通用 JavaFX XYChart?

转载 作者:行者123 更新时间:2023-12-01 11:41:51 28 4
gpt4 key购买 nike

现在我正在使用 JavaFX 图表,但我需要为每种图表类型创建一个通用类。例如,我需要创建一个 ScatterChart,我可以从此类创建一个带有 X = 数字和 Y= 数字轴、X = 字符串和 Y = 数字轴、X = 数字和 Y = 字符串轴或 X = 字符串和Y = String,但我发现在使用图表之前必须定义图表的类型,并且不可能更改同一图表中的轴类型。

我试图做到这一点,但操作数据的方法不起作用:

    public class JFXPanelScatterChartNS extends JFXPanel....
.....
private ScatterChart chart;
.......
public JFXPanelScatterChartNS(String[] xAxisCategories, String[] yAxisCategories ){

if(xAxisCategories != null && yAxisCategories != null){
xAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(xAxisCategories)));
yAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(yAxisCategories)));
chart = new ScatterChart<String, String>(xAxisCategory, yAxisCategory);
}else if(xAxisCategories != null){
xAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(xAxisCategories)));
yAxis = new NumberAxis(0.0,10.0,1.0);
chart = new ScatterChart<String, Number>(xAxisCategory, yAxis);
}else if(yAxisCategories != null){
xAxis = new NumberAxis(0.0,10.0,1.0);
yAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(yAxisCategories)));
chart = new ScatterChart<Number, String>(xAxis, yAxisCategory);
}else{
xAxis = new NumberAxis(0.0,10.0,1.0);
yAxis = new NumberAxis(0.0,10.0,1.0);
chart = new ScatterChart<Number, Number>(xAxis, yAxisCategory);
}

Platform.runLater(() -> {
createScene(withDragAndDrop);
});
}

感谢您的帮助!!

现在我如何使用操作同一类中的数据的方法?在下面的示例中,ScatterChar 仅适用于 X 轴 = Number 和 Y 轴 = String。

我目前的方法是:

public void addSeries(String idSeries, String nameSeries, final Number xIni, final String yIni){        
final XYChart.Series<Number,String> newSeries;
//Valido si el Id ya existe
final boolean seriesExist = seriesMap.containsKey(idSeries);
//si no existe el ID entonces creo la nueva serie
if(!seriesExist){
newSeries = new XYChart.Series<>();
ObservableList<XYChart.Data<Number, String>> listaSerie;
listaSerie = FXCollections.synchronizedObservableList(FXCollections.observableList(new ArrayList<XYChart.Data<Number, String>>()));
newSeries.setData(listaSerie);
newSeries.setName(nameSeries);
newSeries.getData().add(new XYChart.Data<>(xIni,yIni));

Platform.runLater(() -> {
//Genero el index para la nueva serie
Integer newIndex = new Integer(chart.getData().size());
chart.getData().add(newIndex.intValue(), newSeries);
seriesMap.put(idSeries, newIndex);
});
}
}

public void setData(final String idSeries, final int index, final Number x, final String y){
//valido que exista la serie
boolean seriesExist = seriesMap.containsKey(idSeries);
//obtengo el index de la serie, como fue almacenada en la grafica
Integer indexSerie = seriesMap.get(idSeries);
if(seriesExist){
Platform.runLater(() -> {
XYChart.Series<Number,String> currentSeries = null;
currentSeries = chart.getData().get(indexSerie.intValue());
currentSeries.getData().set(index,new XYChart.Data<>(x,y));
});
}
}

最佳答案

考虑使用工厂方法而不是 public 构造函数,并泛化您拥有的方法:

import java.util.Arrays;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.embed.swing.JFXPanel;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;


public class JFXPanelScatterChartNS<X,Y> extends JFXPanel {
private ScatterChart<X,Y> chart ;

private boolean withDragAndDrop = false ;

private JFXPanelScatterChartNS(Axis<X> xAxis, Axis<Y> yAxis) {
chart = new ScatterChart<>(xAxis, yAxis);
Platform.runLater(() -> createScene(withDragAndDrop));
}

private void createScene(boolean withDragAndDrop) {
// ...
}

public static JFXPanelScatterChartNS<String, String> createStringInstance(String[] xAxisCategories, String[] yAxisCategories) {
Axis<String> xAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(xAxisCategories)));
Axis<String> yAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(yAxisCategories)));
return new JFXPanelScatterChartNS<>(xAxisCategory, yAxisCategory);
}

public static JFXPanelScatterChartNS<String, Number> createStringNumberInstance(String[] xAxisCategories) {
Axis<String> xAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(xAxisCategories)));
return new JFXPanelScatterChartNS<String, Number>(xAxisCategory, new NumberAxis(0.0,10.0,1.0));
}

public static JFXPanelScatterChartNS<Number, String> createNumberStringInstance(String[] yAxisCategories) {
Axis<String> yAxisCategory = new CategoryAxis(FXCollections.observableList(Arrays.asList(yAxisCategories)));
return new JFXPanelScatterChartNS<Number, String>(new NumberAxis(0.0,10.0,1.0), yAxisCategory);
}

public static JFXPanelScatterChartNS<Number, Number> createNumberInstance() {
return new JFXPanelScatterChartNS<Number, Number>(new NumberAxis(0.0,10.0,1.0), new NumberAxis(0.0,10.0,1.0));
}

public void addSeries(String idSeries, String nameSeries, final X xIni, final Y yIni){
final XYChart.Series<X,Y> newSeries;
//Valido si el Id ya existe
final boolean seriesExist = seriesMap.containsKey(idSeries);
//si no existe el ID entonces creo la nueva serie
if(!seriesExist){
newSeries = new XYChart.Series<>();
ObservableList<XYChart.Data<X, Y>> listaSerie;
listaSerie = FXCollections.synchronizedObservableList(FXCollections.observableList(new ArrayList<XYChart.Data<X, Y>>()));
newSeries.setData(listaSerie);
newSeries.setName(nameSeries);
newSeries.getData().add(new XYChart.Data<>(xIni,yIni));

Platform.runLater(() -> {
//Genero el index para la nueva serie
Integer newIndex = new Integer(chart.getData().size());
chart.getData().add(newIndex.intValue(), newSeries);

seriesMap.put(idSeries, newIndex);
});
}
}

public void setData(final String idSeries, final int index, final X x, final Y y){
//valido que exista la serie
boolean seriesExist = seriesMap.containsKey(idSeries);
//obtengo el index de la serie, como fue almacenada en la grafica
Integer indexSerie = seriesMap.get(idSeries);
if(seriesExist){
Platform.runLater(() -> {
XYChart.Series<X,Y> currentSeries = null;
currentSeries = chart.getData().get(indexSerie.intValue());
currentSeries.getData().set(index,new XYChart.Data<>(x,y));
});
}
}
}

现在您可以编写如下代码:

JFXPanelScatterChartNS<Number, String> chart = 
JFXPanelScatterChartNS.createNumberStringInstance(new String[] {"A", "B", "C"});
chart.addSeries("Series1", "Series 1", 1, "A");

JFXPanelScatterChartNS<Number, Number> numberChart =
JFXPanelScatterChartNS.createNumberInstance();
numberChart.addSeries("Series2", "Series 2", 5, 3);

关于java - 如何创建带有数字轴或字符串轴的通用 JavaFX XYChart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462718/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com