【问题标题】:How to determine pixel position of line chart origin?如何确定折线图原点的像素位置?
【发布时间】:2012-09-17 06:36:50
【问题描述】:

JavaFX LineChart 由绘图区域和轴组成。由于轴渲染使用了一些显示空间,因此折线图中原点的位置不是 (0, 0)。如何获得相对于折线图本身位置的这个位置?

我想计算绘图区域中一个点相对于折线图位置的位置。 x 轴和 y 轴的 getDisplayPosition 方法提供了相对于原点的信息,但我看不到获取原点位置的明显方法。

【问题讨论】:

    标签: charts javafx-2 linechart


    【解决方案1】:

    axis.getDisplayPosition(val) 方法提供给定轴的val 在绘图区域中相对于绘图区域左上角的像素位置。您可以使用 getDisplayPosition() 方法计算任意点相对于折线图原点的位置。请记住,这些像素位置会在调整折线图大小时发生变化。

    @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 + "]";
    }
    

    【讨论】:

    • 您是如何确定 (-1.5,5) 是绘图区域的原点?
    • @b3。通过观察。我确定对于给定的窗口大小 (800x400) 和给定的数据系列值,在我的系统环境中,渲染的 x 轴最小值等于 -1.5,y 轴最大值等于 5。调整窗口大小或更改数据系列时,这些值会发生变化。
    【解决方案2】:

    更好的观察方法是通过chart.lookup 命令获取绘图区域,如下所示:

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

    【讨论】:

    • 是的!这正是我一直在寻找的。抱歉,我无法重新分配原始赏金。
    • 没问题,我将这个答案发布给其他搜索它的人,因为你问了那个 sep。去年我没想到你还在等待答案.. ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2011-10-18
    • 2014-03-19
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多