如何在javafx中绘制峰值地图 [英] how to paint peak map in javafx

查看:209
本文介绍了如何在javafx中绘制峰值地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javafx中绘制一个峰值地图,但是没有一个类来完成我的目标。所以我修改了LineChart作为我的峰值地图,它看起来是



警告:这是非常棘手的解决方案。



找到零水平线对象并将 xAxis 移动到此位置:

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

final ObservableList< Node> lineChildren = lineChart.getChildrenUnmodifiable();
final可选<节点> 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- horizo​​ntal-zero-line))
.findFirst();

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

至于我X和Y标签可能会被删除。



注意:我们需要处理窗口大小调整事件来移动 xAxis 动态。


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

解决方案

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

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.

这篇关于如何在javafx中绘制峰值地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆