改变JavaFX中的数据StackedBarChart不断推动启动位置

时间:2016-01-06 18:42:13

标签: java javafx

我正在使用来自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);
}
}

1 个答案:

答案 0 :(得分:1)

再次使用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));
}