没有错误,但是图表有问题我没有得到正确的折线图,我想要x轴的参数和y轴的值,但我有3个引擎作为系列,但系列是没有添加,但最后一个系列只是加起来。请帮助我解决这个问题。
我想要做的是:假设我有n个引擎和引擎的m个参数,如果我有4个引擎和30个参数,那么总值将是nxm,即4x30 = 120所以我想绘制引擎一个轴和另一个参数。目前我正在将引擎作为系列,但无法根据参数绘制它。应该有多个引擎系列和带有值的常量参数
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);
}
}
答案 0 :(得分:0)
更改generateChart
班级的performance
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);
}
并使用start
类的NewFXMain
方法中的类,如下所示,删除for循环
performance performvalues = new performance(lineChart, parameters, finalValue, engines);
performvalues.generateChart();
注意:始终保留类名CamelCase