gpt4 book ai didi

java - 更改 JavaFX StackedBarChart 中的数据不断插入起始位置

转载 作者:行者123 更新时间:2023-11-30 10:51:37 26 4
gpt4 key购买 nike

我正在研究来自 https://docs.oracle.com/javafx/2/charts/bar-chart.htm 的 StackedBarCharts 的 Oracle 示例

当我启动我的程序时,一切看起来都很好 Start ,但每当我删除值并替换它们时,新条形图从零开始,就像 this .

出于对这种行为的好奇,我在网上寻找解决方案,并从 Java-Buddy 中找到了另一个示例代码.他的示例代码有 TextEdits,您可以在其中更改图表的值,更改这些值会给我同样的“错误”(例如,条形图从 120 变为 200 而不是 0 到 80,尽管在他的视频中展示了它是如何工作的,但他没有运行进入同一问题)。

如果有人能够向我解释我做错了什么,我们将不胜感激。

这是我现在正在处理的代码(它还有一些其他问题)。

import java.util.Arrays;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TablesAndStuff extends Application {
Scene scene;
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
StackedBarChart<String, Number> sbc = new StackedBarChart<String, Number>(xAxis, yAxis);
final XYChart.Series<String, Number> series1 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series2 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series3 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series4 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series5 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series6 = new XYChart.Series<String, Number>();
final XYChart.Series<String, Number> series7 = new XYChart.Series<String, Number>();

int[] leagues = new int[7];
int[] A = new int[] { 207, 1958, 7293, 11484, 17234, 20851, 13508 };
int[] B = new int[] { 283, 2189, 9392, 14959, 20821, 21125, 10148 };
int[] C = new int[] { 222, 1484, 5524, 9658, 17552, 25750, 16375 };

@Override
public void start(Stage stage) {
setUpLeagues();
stage.setTitle("Stacked Bars");
sbc.setTitle("Country Summary");
xAxis.setLabel("Country");
xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList("A", "B", "C")));

yAxis.setLabel("Value");
series1.setName("1");

series1.getData().add(new XYChart.Data<String, Number>("A", A[0]));
series1.getData().add(new XYChart.Data<String, Number>("B", B[0]));
series1.getData().add(new XYChart.Data<String, Number>("C", C[0]));

series2.setName("2");
series2.getData().add(new XYChart.Data<String, Number>("A", 1985));
series2.getData().add(new XYChart.Data<String, Number>("B", 2189));
series2.getData().add(new XYChart.Data<String, Number>("C", 1484));

series3.setName("3");
series3.getData().add(new XYChart.Data<String, Number>("A", 7293));
series3.getData().add(new XYChart.Data<String, Number>("B", 9392));
series3.getData().add(new XYChart.Data<String, Number>("C", 5524));

series4.setName("4");
series4.getData().add(new XYChart.Data<String, Number>("A", 11484));
series4.getData().add(new XYChart.Data<String, Number>("B", 14959));
series4.getData().add(new XYChart.Data<String, Number>("C", 9658));

series5.setName("5");
series5.getData().add(new XYChart.Data<String, Number>("A", 17234));
series5.getData().add(new XYChart.Data<String, Number>("B", 20821));
series5.getData().add(new XYChart.Data<String, Number>("C", 17552));

series6.setName("6");
series6.getData().add(new XYChart.Data<String, Number>("A", 20851));
series6.getData().add(new XYChart.Data<String, Number>("B", 21125));
series6.getData().add(new XYChart.Data<String, Number>("C", 25750));

series7.setName("7");
series7.getData().add(new XYChart.Data<String, Number>("A", 13508));
series7.getData().add(new XYChart.Data<String, Number>("B", 10148));
series7.getData().add(new XYChart.Data<String, Number>("C", 16375));

StackPane root = new StackPane();
root.getChildren().add(sbc);
// root.getChildren().add(btn);
// StackPane.setAlignment(btn, Pos.BOTTOM_CENTER);
scene = new Scene(root, 800, 600);

sbc.getData().addAll(series1, series2, series3, series4, series5, series6, series7);
sbc.setOnMousePressed(new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent arg0) {
/*
* Todo: Figure out a better way of detecting mouseclicks.
* (Adding handler directly to series does not seem to work,
* name of series clicked shows up in the targets toString but have not
* yet found a direct property for it
*
*/
if (arg0.getTarget().getClass().equals(Region.class)) {
Region r = (Region) arg0.getTarget();
if (r.toString().contains("series1")) {
changeScene(stage, 1);
}
if (r.toString().contains("series2")) {
changeScene(stage, 2);
}
}
}
});
stage.setScene(scene);
stage.show();
}
//Tried clearing and readding data in several different ways, still need
private void changeScene(Stage stage, int i) {
// stage.setScene(new Scene(new StackedBarChart<String, Number>(xAxis,
// yAxis)));
series1.getData().clear();
series2.getData().clear();
series3.getData().clear();
series4.getData().clear();
series5.getData().clear();
series6.getData().clear();
series7.getData().clear();
// sbc.getChildrenUnmodifiable();
series1.getData().add(new XYChart.Data<String, Number>("A", 200));
series1.getData().add(new XYChart.Data<String, Number>("B", 250));
series1.getData().add(new XYChart.Data<String, Number>("C", 225));

// Clearing SBC then re-adding any series results in
// java.lang.IllegalArgumentException: Duplicate series added
// sbc.getData().clear();
// sbc.getData().add(series1);
// sbc.getData().add(series2);

}

private void setUpLeagues() {
leagues[0] = 736;
leagues[1] = 6056;
leagues[2] = 24370;
leagues[3] = 40666;
leagues[4] = 63922;
leagues[5] = 77155;
leagues[6] = 44946;
}

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

最佳答案

再次使用 sbc.setData() 将其设置为仅您要显示的系列并重新同步系列集合。

例如。

private void changeScene(Stage stage, int i) {
series1.setData(FXCollections.observableArrayList(
new XYChart.Data<String, Number>("A", 200),
new XYChart.Data<String, Number>("B", 250),
new XYChart.Data<String, Number>("C", 225)));
sbc.setData(FXCollections.singletonObservableList(series1));
}

关于java - 更改 JavaFX StackedBarChart 中的数据不断插入起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34640396/

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