从lineChart中的坐标确定像素位置

时间:2018-09-12 08:43:53

标签: javafx charts

每次使用“ getDisplayPosition”方法获取像素坐标时,每次返回0.0时我都会遇到问题。 这是我的代码:

public void start(Stage primaryStage) throws Exception
{
    final NumberAxis xAxis = new NumberAxis(0.0, 200.0, 5.0);
    final NumberAxis yAxis = new NumberAxis(0.0, 20000.0, 1000.0);
    xAxis.setLabel("Disante (Nm)");
    yAxis.setLabel("Altitude (ft)");

    AreaChart<Number, Number> areaChart = new AreaChart<>(xAxis, yAxis);
    Series<Number, Number> area = new XYChart.Series<>();
    area.getData().add(new XYChart.Data<>(60, 4000));
    area.getData().add(new XYChart.Data<>(65, 4500));
    area.getData().add(new XYChart.Data<>(70, 3800));
    area.getData().add(new XYChart.Data<>(75, 1000));
    area.getData().add(new XYChart.Data<>(80, 1500));
    areaChart.getData().add(area);
    areaChart.setLegendVisible(false);
    areaChart.setCreateSymbols(false);

    LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
    Series<Number, Number> line = new XYChart.Series<>();
    line.getData().add(new XYChart.Data<>(0, 0));
    line.getData().add(new XYChart.Data<>(15, 6000));
    line.getData().add(new XYChart.Data<>(145, 6000));
    line.getData().add(new XYChart.Data<>(155, 9000));
    line.getData().add(new XYChart.Data<>(200, 9000));
    lineChart.getData().add(line);
    lineChart.setLegendVisible(false);
    lineChart.setCreateSymbols(false);
    lineChart.setAlternativeColumnFillVisible(false);
    lineChart.setAlternativeRowFillVisible(false);
    lineChart.setHorizontalGridLinesVisible(false);
    lineChart.setVerticalGridLinesVisible(false);
    lineChart.getXAxis().setVisible(false);
    lineChart.getYAxis().setVisible(false);
    lineChart.getStylesheets().add(this.getClass().getResource("/resources/chart/chart.css").toExternalForm());

    polygon = new Polygon();
    polygon.getPoints().addAll(new Double[] {
            100.0, 3000.0,
            100.0, 19000.0,
            150.0, 19000.0,
            150.0, 3000.0,
            140.0, 5000.0,
            130.0, 4500.0,
            120.0, 5500.0,
            110.0, 3500.0
    });
    // polygon.setFill(new ImagePattern(createHatch(Color.BLUE)));
    Polygon polygonInChart = updateShape(xAxis, yAxis);

    StackPane root = new StackPane();
    root.getChildren().addAll(areaChart, polygonInChart, lineChart);
    Scene scene = new Scene(root, 800, 600);

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

private Polygon updateShape(NumberAxis xAxis, NumberAxis yAxis)
{
    List<Double> points = polygon.getPoints();
    Polygon polygonInChart = new Polygon();
    for (int i = 0; i < points.size(); i += 2)
    {
        double displayX = xAxis.getDisplayPosition(points.get(i).doubleValue());
        double displayY = yAxis.getDisplayPosition(points.get(i + 1).doubleValue());
        polygonInChart.getPoints().addAll(displayX, displayY);
    }
    return polygonInChart;
}

我想在图表中添加一个多边形,所以我尝试在多边形坐标的屏幕中找到像素位置,并将其添加到stackPane中,以使该多边形“位于”图表中。

0 个答案:

没有答案