gpt4 book ai didi

java - 将数据动态添加到 LineChart JavaFX

转载 作者:行者123 更新时间:2023-12-02 13:03:30 26 4
gpt4 key购买 nike

我想在 LineChart 中设置一些值,但出现错误:添加了重复系列,或 NPE(我尝试清除我的系列列表,调用retainAll,但没有任何结果有帮助)在我的 Controller 类中,我存储了我的 chart 实例

 public static void loadToJSON(LocalDate startDate,LocalDate endDate,Controller controller) {



Task<Integer> task = new Task<Integer>() {
@Override protected Integer call() throws Exception {
double MIN_VAL=Double.MAX_VALUE;
double MAX_VALUE=Double.MIN_VALUE;
XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();


for (LocalDate date = startDate;
!date.isEqual(endDate.plusDays(1));
date = date.plusDays(1)) {

String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

URLConnection urlCon = null;

String baseURL="http://api.fixer.io/";
try {
URL url = new URL(baseURL+formattedDate+"?symbols=PLN,EUR");
urlCon = url.openConnection();

InputStreamReader in = new InputStreamReader(urlCon.getInputStream());
ObjectMapper mapper = new ObjectMapper();
ExchangeRate jsonObject = mapper.readValue(in, ExchangeRate.class);

if(MIN_VAL>jsonObject.getRates().get("PLN"))
MIN_VAL=jsonObject.getRates().get("PLN");
if(MAX_VALUE<jsonObject.getRates().get("PLN"))
MAX_VALUE=jsonObject.getRates().get("PLN");
in.close();

series.getData().clear();
series.getData().add(new XYChart.Data<String,Number>(formattedDate,jsonObject.getRates().get("PLN")));
controller.chart.getData().add(series);

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}

} catch (IOException e) {
e.printStackTrace();
}
}
controller.messageBox.setText("Best exchange rate : "+MIN_VAL + " \nWorst exchange rate : "+MAX_VALUE);
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();

最佳答案

您通过调用重复将相同的系列添加到图表中

controller.chart.getData().add(series);

在循环内。这将导致您描述的异常(“java.lang.IllegalArgumentException:添加了重复系列”)。

相反,您应该在循环开始之前添加一次系列,然后在循环内向其中添加数据。

您还违反了 JavaFX 的线程规则,因为您正在从后台线程修改图表。 UI 只能从 JavaFX 应用程序线程访问。因此,您应该将修改图表(以及系列,一旦添加到图表中)的调用包装在 Platform.runLater(...) 中:

public static void loadToJSON(LocalDate startDate, LocalDate endDate, Controller controller) {

Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
double MIN_VAL = Double.MAX_VALUE;
double MAX_VALUE = Double.MIN_VALUE;
XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();

Platform.runLater(() -> controller.chart.getData().add(series));

for (LocalDate date = startDate; !date.isEqual(endDate.plusDays(1)); date = date.plusDays(1)) {

String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

URLConnection urlCon = null;

String baseURL = "http://api.fixer.io/";
try {
URL url = new URL(baseURL + formattedDate + "?symbols=PLN,EUR");
urlCon = url.openConnection();

InputStreamReader in = new InputStreamReader(urlCon.getInputStream());
ObjectMapper mapper = new ObjectMapper();
ExchangeRate jsonObject = mapper.readValue(in, ExchangeRate.class);

if (MIN_VAL > jsonObject.getRates().get("PLN"))
MIN_VAL = jsonObject.getRates().get("PLN");
if (MAX_VALUE < jsonObject.getRates().get("PLN"))
MAX_VALUE = jsonObject.getRates().get("PLN");
in.close();

// Not sure why you are doing this?
// series.getData().clear();


Plaform.runLater(() ->
series.getData().add(new XYChart.Data<String, Number>(formattedDate, jsonObject.getRates().get("PLN"))));

// controller.chart.getData().add(series);

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}

} catch (IOException e) {
e.printStackTrace();
}
}
Platform.runLater(() ->
controller.messageBox.setText("Best exchange rate : " + MIN_VAL + " \nWorst exchange rate : " + MAX_VALUE));
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}

关于java - 将数据动态添加到 LineChart JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44228612/

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