[英]JavaFX Area Chart 100% line
好的,所有專家FX都在那里偷看。 我需要一些幫助,在AreaChart的100刻度點上添加一條暗線。
正如你所看到的那樣,在100張標記處,我需要一條線來表示每個人是否處於100%生產狀態。 100的位置可以從任何給定的一周改變。 有時最高值不會是170但可能是150左右。 我目前的代碼如下,但它只是測試它(並且它也無法正常工作)。
@Override
protected void layoutPlotChildren() {
super.layoutPlotChildren();
ObservableList<Node> removeable = FXCollections.observableArrayList();
removeable.addAll(getPlotChildren().stream().filter(node -> node instanceof Line).collect(Collectors.toList()));
getPlotChildren().removeAll(removeable);
Double y = Double.valueOf(getYAxis().getValueForDisplay(100.0).toString());
System.out.println(y);
Line line = new Line();
line.setStartX(0.0);
line.setStartY(y);
line.setEndX(600.0);
line.setEndY(y);
getPlotChildren().add(line);
}
圖表線沒有被放置在正確的垂直位置(我很清楚我的線路沒有結束,這只是一個測試)。
好奇這里的問題是什么,究竟是什么
getYAxis().getValueForDisplay(100.0)
是在做。
我過去以不同的方式解決了這個問題。 我使用未經修改的圖表,而是將組件添加到“.chart-content”窗格中。 然后使用坐標變換將它們與“.plot-area”組件對齊。 這是這種方法的一個完全可行的例子。
public class Horse extends Application {
public static void main(String args[]) {
launch(args);
}
private Line line;
private NumberAxis yAxis;
private Region plotArea;
private Pane chartContent;
@Override
public void start(Stage primaryStage) throws Exception {
final CategoryAxis xAxis = new CategoryAxis();
yAxis = new NumberAxis();
final AreaChart<String, Number> chart = new AreaChart<String, Number>(xAxis, yAxis);
Series<String, Number> series = new Series<>();
series.getData().add(new Data<>("foo", 50));
series.getData().add(new Data<>("bar", 25));
series.getData().add(new Data<>("baz", 125));
chart.getData().add(series);
plotArea = (Region) chart.lookup(".chart-plot-background");
chartContent = (Pane) chart.lookup(".chart-content");
line = new Line();
chartContent.getChildren().add(line);
primaryStage.setScene(new Scene(chart));
primaryStage.show();
chart.boundsInParentProperty().addListener((obs, oldValue, newValue) -> {
update();
});
plotArea.boundsInLocalProperty().addListener((obs, oldValue, newValue) -> {
update();
});
update();
}
private void update() {
double location = yAxis.getDisplayPosition(100);
Point2D a = plotArea.localToScene(new Point2D(0, location));
Point2D b = plotArea.localToScene(new Point2D(plotArea.getWidth(), location));
Point2D aTrans = chartContent.sceneToLocal(a);
Point2D bTrans = chartContent.sceneToLocal(b);
line.setStartX(aTrans.getX());
line.setStartY(aTrans.getY());
line.setEndX(bTrans.getX());
line.setEndY(bTrans.getY());
}
}
您正在使用getValueForDisplay()
將像素值轉換為軸值。 您想使用getDisplayPosition()
將軸值轉換為像素值。
更換
Double y = Double.valueOf(getYAxis().getValueForDisplay(100.0).toString());
同
double y = getYAxis().getDisplayPosition(100.0);
SSCCE:
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class LineChartWithHorizontal extends Application {
@Override
public void start(Stage primaryStage) {
LineChart<String, Number> chart = new LineChart<String, Number>(new CategoryAxis(), new NumberAxis()) {
// Line line = new Line();
//
// @Override
// protected void layoutPlotChildren() {
// super.layoutPlotChildren();
// getPlotChildren().remove(line);
// line.setStartX(0);
// line.setEndX(600);
// double y = getYAxis().getDisplayPosition(100.0);
// line.setStartY(y);
// line.setEndY(y);
// getPlotChildren().add(line);
// }
@Override
protected void layoutPlotChildren() {
super.layoutPlotChildren();
ObservableList<Node> removeable = FXCollections.observableArrayList();
removeable.addAll(getPlotChildren().stream().filter(node -> node instanceof Line).collect(Collectors.toList()));
getPlotChildren().removeAll(removeable);
double y = getYAxis().getDisplayPosition(100.0);
System.out.println(y);
Line line = new Line();
line.setStartX(0.0);
line.setStartY(y);
line.setEndX(600.0);
line.setEndY(y);
getPlotChildren().add(line);
}
};
Random rng = new Random();
for (int i = 1 ; i <= 4; i++) {
Series<String, Number> s = new Series<>();
s.setName("Data "+i);
chart.getData().add(s);
}
for (int i = 1 ; i <= 6; i++) {
for (int s = 0 ; s < 4 ; s++) {
chart.getData().get(s).getData().add(new Data<>("Item "+i, rng.nextDouble()*200));
}
}
primaryStage.setScene(new Scene(chart, 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
評論代碼中顯示了更自然(對我而言)的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.