gpt4 book ai didi

java - 如何以编程方式生成随机 JavaFX xyChart

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:12 27 4
gpt4 key购买 nike

所以我想生成随机的JavaFX XYcharts,例如散点图。我使用的是netbeans 7.4。

我想随机化输入的数据,并且我也想随机化系列的数量。我已经添加了随机数据,但我不知道如何随机化所使用的系列数。我计划使用系列 1、系列 2、...、系列 N 的命名约定。我只是不知道该怎么做。

package basicscatter;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import java.util.Random;

public class BasicScatter extends Application
{

@Override public void start(Stage stage)
{
stage.setTitle("Basic Scatter Chart");
final NumberAxis xAxis = new NumberAxis(0,50,5); //(start,end,step).
final NumberAxis yAxis = new NumberAxis(0,1000,100);
int count = 0;
int NumEntries;
int totalNumberOfSeries;
int x;
int y;
Random randomNumbers = new Random();

final ScatterChart<Number,Number> sc = new ScatterChart<Number,Number>(xAxis,yAxis);
xAxis.setLabel("X Axis");
yAxis.setLabel("Y Axis");
sc.setTitle("This is my Chart's Label");
totalNumberOfSeries = 1 + randomNumbers.nextInt(10);

//would like to generate random Series here instead of hardcoding the series
XYChart.Series series1 = new XYChart.Series();
XYChart.Series series2 = new XYChart.Series();

series1.setName("series 1");
series2.setName("series 2");

NumEntries = 1 + randomNumbers.nextInt(100);

while(count < NumEntries)
{
x = randomNumbers.nextInt(51);
y = randomNumbers.nextInt(1001);
series1.getData().add(new XYChart.Data(x,y));
count++;
}

NumEntries = 1 + randomNumbers.nextInt(100);
count = 0;

while(count < NumEntries)
{
x = randomNumbers.nextInt(51);
y = randomNumbers.nextInt(1001);
series2.getData().add(new XYChart.Data(x,y));
count++;
}


sc.getData().addAll(series1, series2);
Scene scene = new Scene(sc, 500, 400);
scene.getStylesheets().add(BasicScatter.class.getResource("Testcss.css").toExternalForm());
stage.setScene(scene);
stage.show();
}


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

}

最佳答案

您最好在 for 循环中生成随机数量的 Series 对象,如下所示:

List<XYChart.Series> seriesList = new ArrayList<Series>
int randomCount = randomNumbers.nextInt(100);
for(int i=0;i<randomCount;i++){
XYChart.Series series = new XYChart.Series();
series.setName("series " + randomCount);
seriesList.add(series);
}

然后您可以迭代所有系列并向它们添加数字(或者您可以在上面的 for 循环中执行此操作)。您可以使用 for-each 循环轻松迭代每个系列,如下所示:

for(XYChart.Series series:seriesList){
series.getData().add(...);
...
}

关于java - 如何以编程方式生成随机 JavaFX xyChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22079274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com