I would like to plot one serie per chart, and in the end all the series together in one chart, but did not succeed. I'm asking for help. The code is simple and straight forward. Here is my code:
The main class;
public class TestChart extends Application { GenPlots genPlots =new GenPlots(); @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(event -> { genPlots.GenPlots("Hello"); }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 200, 250); primaryStage.setTitle("TestCharts"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
And the class aimed to generate the series and the charts:
public class GenPlots { public GenPlots() {}; Axis xAxis = new NumberAxis(); Axis yAxis = new NumberAxis(); LineChart<Number, Number> lineChart = new LineChart<Number, Number> (xAxis, yAxis); LineChart<Number, Number> lineChartMulti = new LineChart<Number, Number>(xAxis, yAxis); String serName="*"; // generate the linecharts public void GenPlots (String hello) { lineChart.getData().clear(); lineChartMulti.getData().clear(); for (int j=1; j<4;j++) { XYChart.Series serSIF = new XYChart.Series(); serSIF=getSeries(); serName=String.valueOf(j); serSIF.setName("Only one "+serName); lineChart.getData().add(serSIF); displayChart(lineChart,serName); lineChartMulti.getData().add(serSIF); } displayChart(lineChartMulti,serName+"All Series"); } // end method // get the series with values - sample public XYChart.Series getSeries() { double x=0.0; double fx=0.0; XYChart.Series serL = new XYChart.Series(); for (int k=1; k<5;k++) { x=x+2; fx=x*x*j; serL.getData().add(new XYChart.Data(x,fx)); } return serL; } // plot the lineCharts public void displayChart( LineChart<Number, Number>lineChart, String chartTitle ) { Stage window = new Stage(); window.initModality(Modality.NONE); StackPane vb = new StackPane(); vb.setPadding(new Insets(20, 20, 20, 20)); lineChart.setTitle(chartTitle); vb.getChildren().add(lineChart); Scene scene = new Scene(vb,500,600); window.setScene(scene); window.show(); } }
Also, the last plots with all series are showing correctly, but the other ones , - one serie per chart - are distorted , or not plotted at all. It seems that the series are resetted to null each time a linechart is generated. I thinks is due to the that series are observable, but I can not figure out how to resolve this problem. Ask kindly for your contribution