I use the extended BarChart class to add vertical lines and text on top of bars. This works fine, but I want to display a little text in the upper right corner of the horizontal line.
I tried this without success:
public void addHorizontalValueMarker(Data<X, Y> marker) {
Objects.requireNonNull(marker, "the marker must not be null");
if (horizontalMarkers.contains(marker)) return;
Line line = new Line();
line.setStroke(Color.RED);
marker.setNode(line);
getPlotChildren().add(line);
Node text = new Text("average");
nodeMap.put(marker.getNode(), text);
getPlotChildren().add(text);
horizontalMarkers.add(marker);
}
@Override
protected void layoutPlotChildren() {
super.layoutPlotChildren();
for (Node bar : nodeMap.keySet()) {
Node text = nodeMap.get(bar);
text.relocate(bar.getBoundsInParent().getMinX() + bar.getBoundsInParent().getWidth()/2 - text.prefWidth(-1) / 2, bar.getBoundsInParent().getMinY() - 30);
}
for (Data<X, Y> horizontalMarker : horizontalMarkers) {
Line line = (Line) horizontalMarker.getNode();
line.setStartX(0);
line.setEndX(getBoundsInLocal().getWidth());
line.setStartY(getYAxis().getDisplayPosition(horizontalMarker.getYValue()) + 0.5);
line.setEndY(line.getStartY());
line.toFront();
}
}
What am I doing wrong?
source
share