调整窗口大小时LineChart中的JavaFX Polygon出现故障

时间:2018-07-10 11:26:08

标签: java javafx

我当前正在编写一个带有LineChart的程序,并且在this question.的帮助下,我有条件地为其背景涂色。当我调整JavaFX程序所在的窗口的大小时,整个地方的颜色都会弯曲。 >

Here is a picture of the program before resizing

Here is a picture of the program after resizing

如您所见,颜色永远不会被“清除”。这是我绘制多边形和折线图的代码:

    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);
    }
}

(清除了一些代码,我排除了一些我认为不相关的东西)

1 个答案:

答案 0 :(得分:0)

我通过将Polygon polygon = new Polygon();移动到lineChart之外并在layoutplotchildren的开头调用了getPlotChildren().remove(polygon)解决了这个问题。