how to paint peak map in javafx

2019-06-07 15:38发布

I want to paint a peak map in javafx, but there isn't a class to finish my aim. So I modified "LineChart" to be my "peak map", it looks that. But I want to move X axis to 0 scale(y axis), what can I do to achieve it.

By the way, if you have a better method to achieve "peak map", please give me some advices.

Thanks!

public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("X");
        yAxis.setLabel("Y");
        final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);

        lineChart.setTitle("Peak Map");

        XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
        series1.setName("Portfolio 1");

        series1.getData().add(new XYChart.Data<Number, Number>(1, 0));
        series1.getData().add(new XYChart.Data<Number, Number>(1, 14));

        XYChart.Series<Number, Number> series2 = new XYChart.Series<>();
        series2.setName("Portfolio 2");
        series2.getData().add(new XYChart.Data<Number, Number>(2, 0));
        series2.getData().add(new XYChart.Data<Number, Number>(2, 34));

        XYChart.Series<Number, Number> series3 = new XYChart.Series<>();
        series3.setName("Portfolio 3");
        series3.getData().add(new XYChart.Data<Number, Number>(3, 0));
        series3.getData().add(new XYChart.Data<Number, Number>(3, 35));

        XYChart.Series<Number, Number> series4 = new XYChart.Series<>();
        series4.setName("Protfolio 4");
        series4.getData().add(new XYChart.Data<>(2, 0));
        series4.getData().add(new XYChart.Data<>(2, -8));

        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().addAll(series1, series2, series3, series4);

        lineChart.setCreateSymbols(false);
        lineChart.setLegendVisible(false);

        stage.setScene(scene);
        stage.show();
    }

1条回答
劳资没心,怎么记你
2楼-- · 2019-06-07 16:35

I played a bit with your code. Are you expecting something like this result? enter image description here

Warning: it is really tricky solution.

Find a zero horizontal line object and move the xAxis to this position:

    final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);

    final ObservableList<Node> lineChildren = lineChart.getChildrenUnmodifiable();
    final Optional<Node> chartContent = lineChildren
            .stream()
            .filter(node -> node.getStyleClass().contains("chart-content"))
            .findFirst();

    if (chartContent.isPresent()) {
        final Optional<Node> xyChart = ((Parent)chartContent.get())
                .getChildrenUnmodifiable()
                .stream()
                .filter(node -> node.getStyleClass().isEmpty())
                .findFirst();

        if (xyChart.isPresent()) {
            final Optional<Node> xAxisOrigin = ((Parent) xyChart.get())
                    .getChildrenUnmodifiable()
                    .stream()
                    .filter(node -> node.getStyleClass().contains("chart-horizontal-zero-line"))
                    .findFirst();

            if (xAxisOrigin.isPresent()) {
                final Line node = (Line) xAxisOrigin.get();
                Platform.runLater(() -> {
                    xAxis.setTranslateY(-(xAxis.getLayoutY() - node.getEndY()));
                });
            }
        }
    }

As for me X and Y labels may be removed at all.

Note: we need handle window sizing event to move xAxis dynamically.

查看更多
登录 后发表回答