How to plot multiple series with javafx line chart which is in array list

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 18:02:13

问题


There is no error but there is an issue with the plot am not getting the correct line chart plot, I want parameters in the x-axis and values in the y-axis but I have 3 engines as series but the series is not adding but the last series is only adding up. kindly help me with this issue.

What I am trying to do is: Suppose I have n engines and m parameters of the engines, if I have 4 engines and 30 parameters then the total value will be n x m which is 4x30=120 so I want to plot engines in one axis and parameters in another. For now am having engines as series but unable to plot it with respect to parameters. There should be multiples engines series and constant parameters with values plotted

Click this url to see the plot. Am getting something like this, as am not able to add multiple series.

1: performance.java

import java.util.ArrayList;
import java.util.List;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;

public class performance {

protected LineChart<String,Number> lineChart;
protected ArrayList parameters;
protected ArrayList<List> param;
protected ArrayList engines;
public XYChart.Series series = new XYChart.Series();



public performance(LineChart<String,Number> lineChart, ArrayList parameters, ArrayList<List> paramValues, ArrayList engines) {
    this.lineChart = lineChart;
    this.parameters = parameters;
    this.param = paramValues;
    this.engines = engines;

}

public XYChart.Series generateChart() {

    for(int i=0;i<engines.size();i++)
    {

        series.setName(engines.get(i).toString());

        for(int j=0;j<parameters.size();j++)
        {

    series.getData().add(new XYChart.Data(parameters.get(j).toString(),param.get(i).get(j)));

    System.out.println(engines.get(i) + "with parameter: " + parameters.get(j) + "having value: "+ param.get(i).get(j));

        }
    }
    System.out.println(lineChart);



    return series;
}

}

2: mainClass

import java.util.ArrayList;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.stage.Stage;

public class NewFXMain extends Application {

@Override 
public void start(Stage stage) {

    stage.setTitle("Performance Analysis");

    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Values");
    yAxis.setLabel("Engines");

    LineChart<String, Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);

    Scene scene  = new Scene(lineChart,800,600); 

    lineChart.setTitle("Performance Analysis");

    ArrayList engines = new ArrayList();
     engines.add("engine1");
     engines.add("engine2");
     engines.add("engine3");
     ArrayList parameters = new ArrayList();
     parameters.add("parameter1");
     parameters.add("parameter2");
     parameters.add("parameter3");
     parameters.add("parameter4");
     parameters.add("parameter5");
     ArrayList paramvalue1 = new ArrayList();
     paramvalue1.add(12);
     paramvalue1.add(13);
     paramvalue1.add(15);
     paramvalue1.add(11);
     paramvalue1.add(10);


    ArrayList paramvalue2 = new ArrayList();
     paramvalue2.add(12);
     paramvalue2.add(20);
     paramvalue2.add(18);
     paramvalue2.add(17);
     paramvalue2.add(22);
     ArrayList paramValue3 = new ArrayList();
     paramValue3.add(14);
     paramValue3.add(12);
     paramValue3.add(11);
     paramValue3.add(22);
     paramValue3.add(12);
     ArrayList finalValue = new ArrayList();
     finalValue.add(paramvalue1);
     finalValue.add(paramvalue2);
     finalValue.add(paramValue3);

     for(int i=0;i<engines.size();i++)
       {
        performance performvalues = new performance(lineChart, parameters, finalValue, engines);

        lineChart.getData().add(performvalues.generateChart());
       }

    stage.setScene(scene);
    stage.show();
}    

public static void main(String[] args) {
    launch(args);
}
}

回答1:


Change the generateChart of performance class

public void generateChart() {
    for (int i = 0; i < engines.size(); i++) {
        XYChart.Series series = new XYChart.Series();
        series.setName(engines.get(i).toString());
        for (int j = 0; j < parameters.size(); j++) {
            series.getData().add(new XYChart.Data(parameters.get(j).toString(), param.get(i).get(j)));
        }
        lineChart.getData().add(series);
    }
    System.out.println(lineChart);
}

And Use the class from the start method of NewFXMain class like below and remove the for loop

performance performvalues = new performance(lineChart, parameters, finalValue, engines);
performvalues.generateChart();

Note : Always keep the class name CamelCase



来源:https://stackoverflow.com/questions/44513136/how-to-plot-multiple-series-with-javafx-line-chart-which-is-in-array-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!