-1

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:

  1. 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);
       }
    }
    
  2. 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

gabriel
  • 1
  • 3
  • don't repost a question, instead improve the previous.. – kleopatra Sep 20 '20 at 21:58
  • Does this answer your question? [javafx linechart multi series not ploting correct values](https://stackoverflow.com/questions/63937593/javafx-linechart-multi-series-not-ploting-correct-values) – kleopatra Sep 20 '20 at 21:58
  • you can't re-use series in multiple charts (as you do by adding serSIf to both lineChart and lineChartMulti). BTW: in future, stick to java naming conventions and take a bit more effort to format the code (no tabs, at least 4 leading spaces in each line) - otherwise it's barely readable .. – kleopatra Sep 21 '20 at 11:33
  • Thanks for your answer. How to resolve it, regenerate the same series under diffirent name? Is not, on some way, possible to delink the series, or somethink like that? – gabriel Sep 21 '20 at 13:02
  • don't understand your comment - anyway, just create a new series with new data (same value, but different instances) – kleopatra Sep 21 '20 at 13:04
  • and take some time to do basic learning on how to write code (f.i. why instantiate serSIF just to immediately overwrite it with getSeries() .. and more ;) – kleopatra Sep 21 '20 at 13:07
  • Thanks again. How if I have a lot of curves? – gabriel Sep 21 '20 at 15:23
  • If I have hundreds of series .. unknown number , and one chart for each serie, how to do? Please, suggestion, sample? – gabriel Sep 21 '20 at 17:21
  • please re-read my comments: it's __not__ ok to reuse xychart.data nor series .. and now start working instead of guessing .. just __do__ it, doesn't matter whether you have 2 or 20 or 200 series - if you run into performance issues, reorganize your logic. – kleopatra Sep 21 '20 at 20:21
  • But how? I need to generate a lot of charts. The first sort is one serie / chart and the second sort is all the the series together in one chart. The number of series is unknown, in forehand, but could be a lott. As you said the reuse of the series is not working, but how to do? I ask for a short code that demonstrate the solution that you have. – gabriel Sep 21 '20 at 20:32
  • do you mean that I shoudl repeat : " XYChart.Series xxxx = new XYChart.Series() " one hundred times, one hundred instances? – gabriel Sep 21 '20 at 20:34
  • Please, somebody that can contribute to resolve this issue? – gabriel Sep 21 '20 at 21:46

1 Answers1

0

I found the solution, which could be usefull for other people: save the series in a ObservableList-
ObservableList<XYChart.Series<Number,Number>> ser = FXCollections.observableArrayList(); If needed do not clear the series itself, rather the ObservableList.

gabriel
  • 1
  • 3