JavaFX Scope Diagram 100% Row

Well, all of you, FX expert, peek out there. I need help adding a dark line at the spot mark of 100 on AreaChart.

enter image description here

As you can see in the picture there, at around 100, I need a line to go through the sheet, to tell everyone if they have 100% production or not. Position 100 may change from any week. Sometimes the upper value will not be 170, but maybe 150 or so. My current code is as follows, but it just checks this (and it also does not work correctly).

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

The line of the diagram does not fit in the correct vertical position (I know well that my line does not end, this is just a test).

enter image description here

It is curious that there is a problem and what exactly

getYAxis().getValueForDisplay(100.0)

does.

+4
2

Example

- . ".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());
    }

}
+1

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

( , ) .

+1

Source: https://habr.com/ru/post/1661507/


All Articles