如何使用数组列表中的javafx折线图绘制多个系列 [英] How to plot multiple series with javafx line chart which is in array list

查看:251
本文介绍了如何使用数组列表中的javafx折线图绘制多个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有错误,但是图表有问题我没有得到正确的折线图,我想要x轴的参数和y轴的值,但我有3个引擎作为系列,但系列是没有添加,但最后一个系列只是加起来。请帮助我解决这个问题。

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.

我想要做的是:假设我有4个引擎和30个参数,我有n个引擎和m个引擎参数然后总值将是nxm,即4x30 = 120,所以我想在一个轴上绘制引擎,在另一个轴上绘制参数。目前我正在将引擎作为系列,但无法根据参数绘制它。应该有多个引擎系列和带有值的常量参数

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

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

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);
}
}


推荐答案

更改 generateChart 表现

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);
}

并使用中的类开始方法 NewFXMain 类如下所示并删除for循环

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();

注意:始终保持班级名称CamelCase

Note : Always keep the class name CamelCase

这篇关于如何使用数组列表中的javafx折线图绘制多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆