Now I am work with JavaFX charts, but I need to create a generic Class to each chart type. for example, I need to create a ScatterChart that I can create from this Class an ScatterChart with X = Number and Y= Number axes, X = String and Y = Number axes, X = Number and Y = String axes or X = String and Y = String, but I see that is mandatory to define the type of Chart before to work with it, and is not possible change the types of axes at the same chart.
I am tried to make this, but methods to manipulate data not work:
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);
});
}
Thank you for your help!!
Now how do I work with methods that manipulate the data in the same Class? In the example below, the ScatterChar only works for X axis = Number and Y axis = String.
My current methods are:
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));
});
}
}