b3.
b3.

Reputation: 7165

How to determine pixel position of line chart origin?

A JavaFX LineChart consists of the plot area and axes. Since the axes rendering uses some display space, the position of the origin in a line chart is not (0, 0). How do I get this position relative to the position of the line chart itself?

I'd like to calculate the position of a point in the plot area relative to the position of the line chart. The getDisplayPosition method of the x- and y-axis provide this relative to the origin but I don't see an obvious way to get the origin position.

Upvotes: 1

Views: 3758

Answers (2)

Kalaschni
Kalaschni

Reputation: 2428

Much better way as observing is to get the plot-area by the chart.lookup command like this:

//gets the display region of the chart
Node chartPlotArea = chart.lookup(".chart-plot-background");
double chartZeroX = chartPlotArea.getLayoutX();
double chartZeroY = chartPlotArea.getLayoutY();

Upvotes: 3

Uluk Biy
Uluk Biy

Reputation: 49185

axis.getDisplayPosition(val) method provides pixel position of a given axis's val in a plot area relative to the top-left corner of the plot area. You can calculate the position of a any point relative to linechart's origin using getDisplayPosition() method. Keep in mind that these pixel positions will vary when the linechart is resized.

@Override
public void start(Stage stage) {
    stage.setTitle("Line Chart Sample");
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    //creating the chart
    final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");
    //populating the series with data
    series.getData().add(new XYChart.Data(-1, 4));
    series.getData().add(new XYChart.Data(0, 2));
    series.getData().add(new XYChart.Data(1, -2));
    series.getData().add(new XYChart.Data(5, 1));

    Scene scene = new Scene(lineChart, 800, 400);
    lineChart.getData().add(series);

    System.out.println("");
    stage.setScene(scene);
    stage.show();

    System.out.println("Points in linechart  ->   Pixel positions relative to the top-left corner of plot area: ");
    System.out.println("(0,0)                ->   " + getFormatted(xAxis.getDisplayPosition(0), yAxis.getDisplayPosition(0)));
    // Same as
    // System.out.println("(0,0) " + getFormatted(xAxis.getZeroPosition(), yAxis.getZeroPosition()));
    System.out.println("(-1,4)               ->   " + getFormatted(xAxis.getDisplayPosition(-1), yAxis.getDisplayPosition(4)));
    System.out.println("(1,-2)               ->   " + getFormatted(xAxis.getDisplayPosition(1), yAxis.getDisplayPosition(-2)));
    System.out.println("(-1.5,5) origin of plot area ->   " + getFormatted(xAxis.getDisplayPosition(-1.5), yAxis.getDisplayPosition(5)));

    System.out.println("Note: These pixel position values will change when the linechart's size is \n changed through window resizing for example.");
}

private String getFormatted(double x, double y) {
    return "[" + "" + x + "," + y + "]";
}

Upvotes: 3

Related Questions