[英]JavaFX Polygon in LineChart glitching when resizing the window
我目前正在編寫一個帶有LineChart的程序,並借助此問題有條件地為其着色了背景。 當我調整JavaFX程序所在的窗口的大小時,顏色到處都是扭曲的。
如您所見,顏色永遠不會被“清除”。 這是我繪制多邊形和折線圖的代碼:
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);
}
}
(代碼已經清除了一些,我排除了一些我認為不相關的東西)
我通過移動Polygon polygon = new Polygon();
解決了這個問題Polygon polygon = new Polygon();
在lineChart外部,並在layoutplotchildren的開頭調用了getPlotChildren().remove(polygon)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.