简体   繁体   中英

JavaFX Polygon in LineChart glitching when resizing the window

I am currently writing a program that has LineChart, and I have colored its background conditionally with the help of this question. When I resize the window where my JavaFX program is, the color warps all over the place.

这是调整大小之前的程序图片

这是调整大小后的程序图片

As you can see, the color is never "cleaned out". Here's the code where I draw the polygon and the linechart:

    private void createTabWithChart(String searchedValue) throws Exception {
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    ObservableList<Data<String, Number>> chartData = createChart(searchedValue);

    if (chartData != null) {

        ObservableList<Data<String, Number>> list = FXCollections.observableArrayList();
        Data<String, Number> data;
        for (int i=0; i < dataArray.size(); ++i) {
            data = new Data<>(date.get(i), 0);
            list.add(data);
        }
        Series limit = new XYChart.Series(list);
        limit.setName("Limit");
        LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis, FXCollections.observableArrayList() )
        {
            protected void layoutPlotChildren() {
                super.layoutPlotChildren();
                double y1;
                double y2;
                for (int i = 0; i < list.size() - 1; i++) {
                    double x1 = 0;
                    double x2 = 100000; // an absurdly large number!
                        y1 = getYAxis().getDisplayPosition(yAxis.getUpperBound());
                        y2 = getYAxis().getDisplayPosition(yAxis.getUpperBound());

                    Polygon polygon = new Polygon();

                    polygon.getPoints().addAll(new Double[] { 
                                    x1, y1, 
                                    x1, getYAxis().getDisplayPosition(list.get(i).getYValue()), 
                                    x2, getYAxis().getDisplayPosition(list.get((i + 1)).getYValue()), 
                                    x2, y2 });
                    getPlotChildren().add(polygon);
                    polygon.toBack();
                    polygon.getStyleClass().add("polygon");
                }
            }
        };
        Series series = new XYChart.Series(chartData);
        series.setName(searchedValue);
        limit.setName("Limit");
        lineChart.getData().addAll(series, limit);
        lineChart.setLegendVisible(false);
        lineChart.setTitle(searchedValue);
        Tab tab = new Tab();
        tab.setText(searchedValue);
        tabPane.getTabs().add(tab);
        tabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
        tab.setContent(lineChart);
    }
}

(The code is cleared a bit and I have excluded some things that I don't find relevant)

I solved this problem by moving the Polygon polygon = new Polygon(); outside of lineChart and called getPlotChildren().remove(polygon) in the beginning of the layoutplotchildren.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM